Author: szimano Date: 2005-11-07 16:13:21 -0500 (Mon, 07 Nov 2005) New Revision: 1526 Modified: trunk/forge/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/WikiPortlet.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/JBossWiki.properties trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/shotoku.properties trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/ShotokuDataSource.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiAttachment.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiPage.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AttachementInfoPlugin.java trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/RecentlyChangedPagesPlugin.java Log: shotoku almost found http://jira.jboss.com/jira/browse/JBWIKI-67?page=worklog Modified: trunk/forge/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/WikiPortlet.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/WikiPortlet.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/forge-wiki/src/java/org/jboss/wiki/WikiPortlet.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -22,6 +22,7 @@ package org.jboss.wiki; import java.io.File; +import java.io.FileInputStream; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -122,7 +123,10 @@ WikiPage pageWithAtt = wikiEngine.getByName(rReq .getParameter("page"), null); - pageWithAtt.addAttachement(serverFile, fileName, + + FileInputStream fis = new FileInputStream(serverFile); + + pageWithAtt.addAttachement(fis, fileName, credentials.getName()); // at the end delete the temp file Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/JBossWiki.properties =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/JBossWiki.properties 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/JBossWiki.properties 2005-11-07 21:13:21 UTC (rev 1526) @@ -12,7 +12,7 @@ credentialsClass=org.jboss.wiki.JBossPortalCredentials #mediaDataSourceClass class to use for mediaDataSource -mediaDataSourceClass=org.jboss.wiki.ShotokuDataSource +mediaDataSourceClass=org.jboss.wiki.FileDataSource #attachmentDataSourceClass class to use for AttachmentDataSource attachmentDataSourceClass=org.jboss.wiki.FileDataSource Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/shotoku.properties =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/shotoku.properties 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/etc/shotoku.properties 2005-11-07 21:13:21 UTC (rev 1526) @@ -1,4 +1,6 @@ #Shotoku properties file # folder where wiki content is placed -shotokuPrefix = wiki-content/ +pagesPrefix = wiki-content/ + +attPrefix = wiki-content/ Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/AttachmentDataSource.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -22,6 +22,7 @@ package org.jboss.wiki; import java.io.File; +import java.io.InputStream; import java.util.Set; /** @@ -39,7 +40,7 @@ * @param page Page to add atachment to. * @param user Username of user adding the attachment. */ - public void addAttachment(File attFile, String attName, WikiPage page, String user); + public void addAttachment(InputStream attFile, String attName, WikiPage page, String user); /**Gets attachment list for a given page. * @param page Page to get list for. Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileDataSource.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -26,6 +26,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Date; import java.util.HashSet; import java.util.Properties; @@ -623,7 +624,7 @@ } } - public void addAttachment(File attFile, String attName, WikiPage page, + public void addAttachment(InputStream attFile, String attName, WikiPage page, String user) { String pageName = page.getName(); Properties attProps = getAttProps(pageName, attName, true); @@ -649,12 +650,12 @@ + ((attName.indexOf('.') != -1) ? extension[extension.length - 1] : "bin"); - FileInputStream oldFile = null; + InputStream oldFile = null; FileOutputStream newFile = null; try { newFile = new FileOutputStream(new File(fileName)); - oldFile = new FileInputStream(attFile); + oldFile = attFile; } catch (FileNotFoundException e) { log.error(e); } @@ -783,9 +784,18 @@ + ((attachementName.indexOf(".") != -1) ? tokens[tokens.length - 1] : "bin")); - return new WikiAttachment(attachementName, - new Date(file.lastModified()), user, file.length(), file, - version); + FileInputStream fis; + try { + fis = new FileInputStream(file); + return new WikiAttachment(attachementName, + new Date(file.lastModified()), user, file.length(), fis, + version); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } + + } public int getLastAttachmentVersion(String pageName, String attachementName) { Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/ShotokuDataSource.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/ShotokuDataSource.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/ShotokuDataSource.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -21,10 +21,6 @@ */ package org.jboss.wiki; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -37,14 +33,12 @@ import org.jboss.logging.Logger; import org.jboss.shotoku.ContentManager; +import org.jboss.shotoku.Directory; import org.jboss.shotoku.Node; -import org.jboss.shotoku.NodeList; +import org.jboss.shotoku.exceptions.ResourceAlreadyExists; import org.jboss.shotoku.exceptions.ResourceDoesNotExist; -import org.jboss.wiki.exceptions.DataSourceException; import org.jboss.wiki.exceptions.PageRenamingException; -import sun.security.krb5.internal.p; - /** * <p> * </p> @@ -53,61 +47,450 @@ public class ShotokuDataSource implements MediaDataSource, AttachmentDataSource, WikiPageDictionary { - private WikiEngine wikiEngine; + /** + * <p> + * Represents ... + * </p> + * + */ - private ContentManager contentManager = null; + private ContentManager pagesManager; - private final String propsName = "shotoku.properties"; + private ContentManager attManager; + + private final String SAVE_COMMENT = "Saved by ShotokuDataSource of JBoss Wiki"; + + private static final String propFileName = "shotoku.properties"; + + private static final String propModFileName = "fileMod.properties"; + + private static final String dictionaryPropertiesFileName = "dictionary.properties"; - private final String pageSuffix = ".txt"; + private String pagesPrefix; - public ShotokuDataSource() throws DataSourceException { - Properties shotokuProps = new Properties(); + private String attPrefix; + + public String pathToMediaTrash; + public String pathToAttTrash; + + private boolean lock = false; + + // private Properties fileDSProps; + + private final int BUF_SIZE = 32768; + + private Properties pageModProps; + + private Properties pageDictionary; + + private Properties pageRevDictionary; + + private Node modFile; + + private Node pageDictionaryFile; + + private WikiEngine wikiEngine; + + private Logger log; + + public void setWikiEngine(WikiEngine wikiEngine) { + this.wikiEngine = wikiEngine; + } + + public ShotokuDataSource() { + + log = Logger.getLogger(ShotokuDataSource.class); + + log.info("Looking for props file !"); + + Properties shotokuDSProps = new Properties(); + try { - shotokuProps.load(ShotokuDataSource.class.getResourceAsStream("/" - + propsName)); - contentManager = ContentManager.getContentManager(shotokuProps - .getProperty("shotokuPrefix")); + shotokuDSProps.load(ShotokuDataSource.class.getResourceAsStream("/" + + propFileName)); + pagesPrefix = shotokuDSProps.getProperty("pagesPrefix"); + + attPrefix = shotokuDSProps.getProperty("attPrefix"); + + pagesManager = ContentManager.getContentManager(pagesPrefix); + + attManager = ContentManager.getContentManager(attPrefix); + + } catch (IOException ioe) { + log.error("Can't load the file " + propFileName + "\n", ioe); + } + + /* + * File mainWikiDir = new File(pathToMedia); File attWikiDir = new + * File(pathToAttachments); + * + * if (!mainWikiDir.exists()) mainWikiDir.mkdirs(); + * + * if (!attWikiDir.exists()) attWikiDir.mkdirs(); + */ + pageModProps = new Properties(); + // modFile = new File(pathToMedia + "/" + propModFileName); + + // FileInputStream fis = null; + try { + modFile = pagesManager.getNode(propModFileName); + } catch (ResourceDoesNotExist e) { + modFile = pagesManager.getDirectory("").newNode(propFileName); + } + InputStream is = null; + try { + is = modFile.getContentInputStream(); + + pageModProps.load(is); + + } catch (IOException ioe) { + log.error("Can't load the file " + propModFileName + "\n", ioe); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // create trash directory for deleted pages if it doesn't exists + try { + pagesManager.getDirectory(MediaDataSource.mediaTrashName); + } catch (ResourceDoesNotExist e) { + pagesManager.getDirectory("").newDirectory( + MediaDataSource.mediaTrashName); + } + + // create trash directory for deleted attachments if it doesn't exists + + try { + attManager.getDirectory(AttachmentDataSource.attTrashName); + } catch (ResourceDoesNotExist e) { + attManager.getDirectory("").newDirectory( + AttachmentDataSource.attTrashName); + } + + // load dictionary props + pageDictionary = new Properties(); + pageRevDictionary = new Properties(); + + try { + pageDictionaryFile = pagesManager + .getNode(dictionaryPropertiesFileName); + } catch (ResourceDoesNotExist e) { + pageDictionaryFile = pagesManager.getDirectory("").newNode( + dictionaryPropertiesFileName); + } + + // there is dictionary file. Load props from it + is = pageDictionaryFile.getContentInputStream(); + + try { + pageDictionary.load(is); } catch (IOException e) { - throw new DataSourceException(e.getMessage()); + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } } + // create pageRevDictionary - to make it work faster + + Set<Object> keys = pageDictionary.keySet(); + + for (Object key : keys) { + pageRevDictionary.setProperty(pageDictionary + .getProperty((String) key), (String) key); + } } public boolean preSave() { - // TODO Auto-generated method stub - return false; + return true; } + public boolean savePage(String uid, WikiPage page, String languageCode) { - // TODO Auto-generated method stub - return false; + // TODO Languages + + boolean status = false; + + if (pageExists(uid)) { // page exists on + // harddisk - add new + // version + Properties pageProps = getPageProps(uid); + + pageProps.setProperty(page.getLastVersion() + ".author", page + .getLastAuthor().getName()); + + OutputStream os = pagesManager.getNode( + "OLD/" + uid + "/page.properties").getOutputStream(); + try { + pageProps.store(os, "Saved by ShotokuDataSource of JBoss Wiki"); + } catch (Exception e) { + log + .error( + "[ShotokuDataSource]: Cannot save property file: ", + e); + } finally { + try { + os.close(); + } catch (IOException e) { + log.error(e); + } + } + + log.info("Opening files"); + + // File pageFile = new File(pathToMedia + "/OLD/" + uid + "/" + + // (page.getLastVersion() - 1) + ".txt"); + // File pageOldFile = new File(pathToMedia + "/" + uid + ".txt"); + + Node pageFile = pagesManager.getDirectory("").newNode( + "OLD/" + uid + "/" + (page.getLastVersion() - 1) + ".txt"); + Node pageOldFile = pagesManager.getDirectory("").newNode( + uid + ".txt"); + + OutputStream pageFileWriter; + InputStream pageFileReader; + + try { + pageFileWriter = pageFile.getOutputStream(); + pageFileReader = pageOldFile.getContentInputStream(); + + int b; + + while ((b = pageFileReader.read()) != -1) { + pageFileWriter.write(b); + } + + // TODO last modified + // pageFile.setLastModified(pageOldFile.lastModified()); + + pageFileWriter.close(); + + pageOldFile.delete(); + + pageFileWriter = pageOldFile.getOutputStream(); + + String pageContent = page.getContent(); + + for (int i = 0; i < pageContent.length(); i++) { + pageFileWriter.write((int) pageContent.charAt(i)); + } + + pageFileWriter.close(); + status = true; + } catch (IOException ioe) { + log.error("[ShotokuDataSource]: Cannot save page: ", ioe); + } + + } else { // there is no page on disk. We have to create new one. + + try { + + // create needed dirs + // File newPageFile = new File(pathToMedia + "/OLD/" + uid); + // newPageFile.mkdirs(); + + // pagesManager.getDirectory("").newDirectory("OLD/" + uid); + + Node newPageFile = pagesManager.getDirectory("").newDirectory( + "OLD/" + uid).newNode("page.properties"); + + // write the page + // newPageFile = new File(pathToMedia + "/OLD/" + uid + // + "/page.properties"); + + OutputStream pageWriter = newPageFile.getOutputStream(); + + Properties properties = new Properties(); + properties.setProperty(page.getLastVersion() + ".author", page + .getLastAuthor().getName()); + + properties.store(pageWriter, SAVE_COMMENT); + + pageWriter.close(); + + String pageDirName = ""; + String[] pageNames = uid.split("/"); + + for (int i = 0; i < pageNames.length - 1; i++) { + pageDirName += "/" + pageNames[i]; + } + + pagesManager.getDirectory("").newDirectory(pageDirName); + + pageWriter = pagesManager.getNode(uid + ".txt") + .getOutputStream(); + + String pageContent = page.getContent(); + + for (int i = 0; i < pageContent.length(); i++) { + pageWriter.write((int) pageContent.charAt(i)); + } + + pageWriter.close(); + + status = true; + } catch (Exception e) { + log.error("Cannot write new page: ", e); + status = false; + } + } + return status; } public boolean postSave() { - // TODO Auto-generated method stub - return false; + return true; } public boolean preGet() { return true; } + public Properties getPageProps(String pageName) { + Properties pageProps = new Properties(); + + try { + InputStream fpi = pagesManager.getNode( + "OLD/" + pageName + "/page.properties") + .getContentInputStream(); + pageProps.load(fpi); + fpi.close(); + } catch (IOException ioe) { + // There is no prop file for some reson - create one. + + Node pagePropsFile = pagesManager.getDirectory("").newDirectory( + "OLD/" + pageName).newNode("page.properties"); + + try { + if (pageExists(pageName)) { + // if page exists it looks like there is lack of the props + // structure + OutputStream fpo = pagePropsFile.getOutputStream(); + + pageProps.setProperty("1.author", "unknown"); + pageProps.store(fpo, SAVE_COMMENT); + fpo.close(); + } + + } catch (IOException e) { + log.error( + "Cannot create page props file for page: " + pageName, + e); + } + return pageProps; + } + + return pageProps; + } + + public Properties getAttProps(String pageName, String attName, + boolean createProps) { + Properties attProps = new Properties(); + + Node propFile; + + try { + propFile = attManager.getNode(pageName + + "-att/" + attName + "-dir/attachment.properties"); + + } catch (ResourceDoesNotExist e) { + + if (createProps) { + // create dirs for this property file + propFile = attManager.getDirectory("").newDirectory( + pageName + "-att/" + attName + + "-dir").newNode("attachment.properties"); + } else { + return null; + } + } + + try { + attProps.load(propFile.getContentInputStream()); + } catch (IOException ioe) { + log.error("Cannot read attachment.properties for attachement: " + + pageName + "-att/" + attName + + "-dir/attachment.properties\n", ioe); + } + + return attProps; + } + + public void saveAttProps(Properties props, String pageName, String attName) { + try { + props.store(attManager + .getNode( + pageName + "-att/" + attName + + "-dir/attachment.properties") + .getOutputStream(), SAVE_COMMENT); + } catch (Exception ioe) { + log.error("Cannot store attchement.properties for attachement: " + + pageName + "-att/" + attName + + "-dir/attachments.properties\n", ioe); + } + + } + public WikiPage getPage(String pageName) { - + // log.info("[WIKI:ShotokuDataSource]: Getting new page: " + + // pageName); + + WikiPage page = null; + try { - Node page = contentManager.getNode(pageName + pageSuffix); - - return new WikiPage(pageName, new SimpleCredentials("someone"), - page.getContent(), page.getHistory().getRevisionsCount(), page.getHistory().getRevisionsCount(), new Date(page - .getLastModification()), wikiEngine, true, true); - + Node pageFile = pagesManager.getNode(pageName + ".txt"); + Properties pageProps = getPageProps(pageName); + + if (pageProps != null) { + + int i = 1; + + // get the author of the latest version of page + while (pageProps.getProperty(String.valueOf(i + 1) + ".author") != null) { + i++; + } + + Credentials author = new SimpleCredentials(pageProps + .getProperty(String.valueOf(i) + ".author")); + + StringBuilder pageContent = new StringBuilder(); + + try { + InputStream input = pageFile.getContentInputStream(); + + int nextChr = input.read(); + + while (nextChr != -1) { + pageContent.append((char) nextChr); + nextChr = input.read(); + } + } catch (IOException ioe) { + log.error(ioe); + } + + int pageMods = getPageMod(pageName); + + page = new WikiPage(pageName, author, pageContent.toString(), + i, i, new Date(pageFile.getLastModfication()), + wikiEngine, (pageMods & VIEWABLE) == VIEWABLE, + (pageMods & EDITABLE) == EDITABLE); + + page.setLength(pageFile.getLength()); + } } catch (ResourceDoesNotExist e) { - return null; + // do nothing just return null } + + return page; + } public boolean postGet() { @@ -115,148 +498,699 @@ } public WikiPage getPage(String pageName, String languageCode) { - // TODO Auto-generated method stub - return null; + if (languageCode != null) { + log + .info("As for now, languages aren't supported in the wiki. Please do not use language codes"); + } + return getPage(pageName); } public WikiPage getPageAtVersion(WikiPage originPage, boolean loadContent, String languageCode, int version) { - // TODO Auto-generated method stub - return null; + if (languageCode != null) { + log + .info("As for now, languages aren't supported in the wiki. Please do not use language codes"); + } + + return getPageAtVersion(originPage, loadContent, version); } public WikiPage getPageAtVersion(WikiPage originPage, boolean loadContent, int version) { - // TODO Auto-generated method stub - return null; - } - public boolean pageExists(String pageName) { + /* + * Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); long + * oldMilis = cal.getTimeInMillis(); + */ + + WikiPage page = null; + try { - contentManager.getNode(pageName+".txt"); - return true; + page = (WikiPage) originPage.clone(); + } catch (CloneNotSupportedException e) { + log.error(e); } - catch (ResourceDoesNotExist e) { - return false; + + if (page != null) { + if (page.getLastVersion() < version) { + log.info("There is no version " + version + " of page " + + page.getName()); + return null; + } + + getContentAtVersion(page, loadContent, version); + + page.setLastAuthor(getAuthorAtVersion(page.getName(), version)); + + page.setVersion(version); + + page.setEditDate(getDateAtVersion(page.getName(), version)); + } + + /* + * cal.setTime(new Date()); + * + * log.info("PAGE "+page.getName()+" loaded in: "+(cal.getTimeInMillis() - + * oldMilis)); + */ + return page; } public void getContentAtVersion(WikiPage page, boolean loadContent, int version) { - // TODO Auto-generated method stub + StringBuilder ret = null; - } + Node pageFile = pagesManager.getNode("OLD/" + page.getName() + "/" + + version + ".txt"); - public Set<String> getAllPageNames() { - //TODO bleble - Set<String> ret = new TreeSet<String>(); - - List<Node> nodeList = contentManager.getDirectory("").getNodes().toList(); - - for (Node node : nodeList) { - if (node.getName().endsWith(pageSuffix)) { - ret.add(node.getName().substring(0, node.getName().length() - (pageSuffix.length()))); + if (loadContent) { + try { + InputStream input = pageFile.getContentInputStream(); + + int nextChr = input.read(); + + ret = new StringBuilder(); + + while (nextChr != -1) { + ret.append((char) nextChr); + nextChr = input.read(); + } + + page.setPageContent(ret.toString()); + page.setLength(ret.length()); + + } catch (IOException ioe) { + log.error(ioe); } + } else { + page.setLength(pageFile.getLength()); + page.setPageContent(null); } - + + } + + public Date getDateAtVersion(String pageName, int version) { + Date ret = null; + + Node pageFile = pagesManager.getNode("OLD/" + pageName + "/" + version + + ".txt"); + + ret = new Date(pageFile.getLastModfication()); + return ret; } - public Set<String> getPagesFor(String pageName) { - // TODO Auto-generated method stub - return new TreeSet<String>(); + public Credentials getAuthorAtVersion(String pageName, int version) { + return new SimpleCredentials(getPageProps(pageName).getProperty( + version + ".author")); } - public int getPageMod(String pageName) { - // TODO Auto-generated method stub - return 0; + public boolean pageExists(String pageName) { + + try { + pagesManager.getNode(pageName + ".txt"); + return true; + } catch (ResourceDoesNotExist e) { + return false; + } } - public void setPageMod(String pageName, int mods) { - // TODO Auto-generated method stub + public void addAttachment(InputStream attFile, String attName, + WikiPage page, String user) { + String pageName = page.getName(); + Properties attProps = getAttProps(pageName, attName, true); + int lastVersion = 1; + + if (attProps.size() > 0) { + // find last version of this file + while (attProps.getProperty(lastVersion + ".author") != null) { + lastVersion++; + } + } + + String[] extension = attName.split("\\."); + String fileName = pageName + + "-att/" + + attName + + "-dir/" + + lastVersion + + "." + + ((attName.indexOf('.') != -1) ? extension[extension.length - 1] + : "bin"); + + InputStream oldFile = null; + OutputStream newFile = null; + + newFile = attManager.getDirectory("").newNode(fileName) + .getOutputStream(); + oldFile = attFile; + + // writing old file to new one + try { + byte[] buffer = new byte[BUF_SIZE]; + int read; + while ((read = oldFile.read(buffer)) != -1) + newFile.write(buffer, 0, read); + } catch (Exception e2) { + // Nothing that we can really do. Just send an incomplete + // file. + } finally { + try { + newFile.close(); + oldFile.close(); + } catch (IOException e) { + log.error(e); + } + } + + attProps.setProperty(lastVersion + ".author", user); + + saveAttProps(attProps, pageName, attName); } - public boolean deletePage(String pageName) { - // TODO Auto-generated method stub - return false; + public Set<String> getAttachmentsSet(WikiPage page) { + Set<String> attSet = new HashSet<String>(); + + try { + Directory attDir = attManager.getDirectory(page.getName() + "-att"); + + if (attDir.getDirectories().size() > 0) { + List<Directory> attachements = attDir.getDirectories(); + + for (int i = 0; i < attachements.size(); i++) { + if (attachements.get(i).getName().endsWith("-dir")) { + String[] tokens = attachements.get(i).getName().split( + "/"); + + attSet.add(tokens[tokens.length - 1].substring(0, + tokens[tokens.length - 1].length() - 4)); + } + } + } else { + // page has no attachements + return null; + } + } catch (ResourceDoesNotExist e) { + return null; + } + + return attSet; } - public void setWikiEngine(WikiEngine wikiEngine) { - this.wikiEngine = wikiEngine; + public Set<String> getAllPageNames() { + Set<String> attSet = new HashSet<String>(); + + getPageNamesFor("", attSet); + + return attSet; } - public void addAttachment(File attFile, String attName, WikiPage page, - String user) { - // TODO Auto-generated method stub + private void getPageNamesFor(String directory, Set<String> attSet) { + Directory attDir = pagesManager.getDirectory(directory); + List<Node> pageFiles = attDir.getNodes().toList(); + + for (int i = 0; i < pageFiles.size(); i++) { + if ((pageFiles.get(i).getName().endsWith(".txt"))) { + // String[] tokens = pageFiles[i].getName().split("/"); + attSet.add((directory + "/" + pageFiles.get(i).getName() + .substring( + 0, + pageFiles.get(i).getName().length() + - ".txt".length())).substring(1)); + } + } + + List<Directory> pageDirs = attDir.getDirectories(); + + //TODO make "special" directories work better + for (Directory dir : pageDirs) { + if ((!dir.getName().endsWith("-att"))) { + // do not take dirs from OLD, .svn and trash folders + if ((!dir.getName().equals("OLD")) + && (!dir.getName().equals(".svn")) + && (!dir.getName().equals( + MediaDataSource.mediaTrashName)) + && (!dir.getName().equals( + AttachmentDataSource.attTrashName))) { + // get names for subpages + getPageNamesFor( + directory + "/" + dir.getName(), attSet); + } + } + } } - public Set<String> getAttachmentsSet(WikiPage page) { - // TODO Auto-generated method stub - return null; + public WikiAttachment getAttachment(String pageName, String attachementName) { + return getAttachment(pageName, attachementName, + getLastAttachmentVersion(pageName, attachementName)); } - public WikiAttachment getAttachment(String pageName, String attachmentName) { - // TODO Auto-generated method stub - return null; + public WikiAttachment getAttachment(String pageName, + String attachementName, int version) { + + String[] tokens = attachementName.split("\\."); + + Properties props = getAttProps(pageName, attachementName, false); + + if (props == null) { + return null; + } + + String user = props.getProperty(version + ".author"); + + Node file = attManager + .getNode(pageName + + "-att/" + + attachementName + + "-dir/" + + version + + "." + + ((attachementName.indexOf(".") != -1) ? tokens[tokens.length - 1] + : "bin")); + + return new WikiAttachment(attachementName, new Date(file + .getLastModfication()), user, file.getLength(), file.getContentInputStream(), version); } - public WikiAttachment getAttachment(String pageName, String attachmentName, + public int getLastAttachmentVersion(String pageName, String attachementName) { + + Properties props = getAttProps(pageName, attachementName, false); + + if (props == null) + return -1; + + // looking for the newest version + int version = 1; + + while (props.getProperty((version + 1) + ".author") != null) { + version++; + } + + return version; + } + + public long getAttachmentSize(String pageName, String attachementName, int version) { - // TODO Auto-generated method stub - return null; + String[] tokens = attachementName.split("\\."); + + Node file = attManager + .getNode(pageName + + "-att/" + + attachementName + + "-dir/" + + version + + "." + + ((attachementName.indexOf(".") != -1) ? tokens[tokens.length - 1] + : "bin")); + + return file.getLength(); } - public int getLastAttachmentVersion(String pageName, String attachmentName) { - // TODO Auto-generated method stub - return 0; + public Set<String> getPagesFor(String pageName) { + Set<String> pages = new TreeSet<String>(); + + try { + List<Node> subFiles = pagesManager.getDirectory(pageName) + .getNodes().toList(); + + for (int i = 0; i < subFiles.size(); i++) { + if (subFiles.get(i).getName().endsWith(".txt")) { + pages.add(pageName + + "/" + + subFiles.get(i).getName().substring( + 0, + subFiles.get(i).getName().length() + - ".txt".length())); + } + } + + } catch (ResourceDoesNotExist e) { + // return empty set + } + + return pages; } - public long getAttachmentSize(String pageName, String attachmentName, - int version) { - // TODO Auto-generated method stub - return 0; + public int getPageMod(String pageName) { + if (pageModProps.getProperty(pageName) == null) { + return (EDITABLE + VIEWABLE); + } else { + return Integer.valueOf(pageModProps.getProperty(pageName)); + } } - public boolean deleteAttachment(String pageName, String attachmentName) { - // TODO Auto-generated method stub - return false; + public synchronized void setPageMod(String pageName, int mods) { + + if (mods != (EDITABLE + VIEWABLE)) { + pageModProps.setProperty(pageName, String.valueOf(mods)); + } else { + pageModProps.remove(pageName); + } + + OutputStream fos = null; + + try { + fos = modFile.getOutputStream(); + + pageModProps.store(fos, SAVE_COMMENT); + } catch (IOException ioe) { + log.error("Couldn't store mod props: ", ioe); + } finally { + try { + fos.close(); + } catch (IOException ioe) { + log.error("Can't close input stream \n", ioe); + } + } } - public boolean deleteAttachments(String pageName) { - // TODO Auto-generated method stub - return false; + public synchronized boolean deletePage(String pageName) { + + if (pageExists(pageName)) { + // moving page file + String pageFileName = pageName + ".txt"; + String pageFileTrashNameBase = pageName + ".txt"; + String pageFileTrashName = pageFileTrashNameBase; + + Node pageFile = pagesManager.getNode(pageFileName); + Node pageTrashFile; + + try { + pageTrashFile = pagesManager.getDirectory("").newNode( + pageFileTrashName); + } catch (ResourceAlreadyExists e) { + + // look if page is already in trash (add i-bak) + int i = 0; + + boolean foundPage = false; + + while (!foundPage) { + pageFileTrashName = pageFileTrashNameBase + "-" + (i++) + + "-bak"; + + try { + pageTrashFile = pagesManager.getDirectory("").newNode( + pageFileTrashName); + foundPage = true; + } catch (ResourceAlreadyExists ee) { + // page exists - continue looking + } + } + + } + + Directory trashFileDir; + // create dirs to trash + try { + trashFileDir = pagesManager.getDirectory(pathToMediaTrash + "/" + + pageName); + } catch (ResourceDoesNotExist e) { + trashFileDir = pagesManager.getDirectory("").newDirectory( + pathToMediaTrash + "/" + pageName); + } + + // TODO svn mv + + /* + * if (!pageFile.renameTo(pageTrashFile)) { + * log.error("[ShotokuDataSource]: Problems with moving " + + * pageFileName + " to " + pageFileTrashName); return false; } + */ + + // moving history directory + String historyDirName = "OLD/" + pageName; + String historyTrashDirNameBase = pathToMediaTrash + "/OLD/" + + pageName; + String historyTrashDirName = historyTrashDirNameBase; + + // create dirs to trash (history) + + // TODO svn mv ?! where is it ? + + /* + * trashFileDir = new File(pathToMediaTrash + "/OLD/" + pageName); + * if (!trashFileDir.exists()) { trashFileDir.mkdirs(); } + */ + pageFile = pagesManager.getNode(historyDirName); + + try { + pageTrashFile = pagesManager.getDirectory("").newNode( + historyTrashDirName); + } catch (ResourceAlreadyExists e) { + // look if history dir is already in trash (add i-bak) + + boolean historyFound = false; + int i = 0; + while (!historyFound) { + try { + historyTrashDirName = historyTrashDirNameBase + "-" + + (i++) + "-bak"; + pageTrashFile = pagesManager.getDirectory("").newNode( + historyTrashDirName); + historyFound = true; + } catch (ResourceAlreadyExists ee) { + // continue looking + } + } + } + + // TODO move page ! + + /* + * if (!pageFile.renameTo(pageTrashFile)) { + * log.error("[ShotokuDataSource]: Problems with moving " + + * historyDirName + " to " + historyTrashDirName); // return back + * the page history pageFile = new File(pageFileName); pageTrashFile = + * new File(pageFileTrashName); + * + * if (!pageTrashFile.renameTo(pageFile)) { System.err + * .println("[ShotokuDataSource]: ERROR. I've tried moving + * previously moved page file from the trash, but there was some + * problem. This is not good and there is nothing i can do about it. + * The error ocured while moving " + pageFileTrashName + " to " + + * pageFileName); } + * + * return false; } + */ + + return true; + } else { + // no such page + log.error("There is now such page: " + pageName); + return false; + } } + public synchronized boolean deleteAttachment(String pageName, + String attachmentName) { + + String attName = pageName + "-att/" + attachmentName + "-dir"; + String attTrashNameBase = pageName + "-att/" + attachmentName + "-dir"; + String attTrashName = attTrashNameBase; + + // create dirs to trash + Directory trashFileDir; + try { + trashFileDir = attManager.getDirectory(pageName + "-att/"); + } catch (ResourceDoesNotExist e) { + trashFileDir = attManager.getDirectory("").newDirectory( + pageName + "-att/"); + } + + // moving file directory + try { + Directory attFile = attManager.getDirectory(attName); + + // look if att dir is already in trash (add i-bak) + + Directory attTrashFile; + + try { + attTrashFile = attManager.getDirectory("").newDirectory( + attTrashName); + } catch (ResourceAlreadyExists e) { + boolean pageFound = false; + + int i = 0; + + while (pageFound) { + attTrashName = attTrashNameBase + "-" + (i++) + "-bak"; + try { + attTrashFile = attManager.getDirectory("") + .newDirectory(attTrashName); + pageFound = true; + } catch (ResourceAlreadyExists ee) { + // continue looking + } + } + } + // TODO rename !!! + + /* + * if (!attFile.renameTo(attTrashFile)) { log.error("[ATTACHMENT + * DS]:Problems with moving " + attName + " to " + attTrashName); + * return false; } + */ + + return true; + + } catch (ResourceDoesNotExist e) { + log.error("Page " + pageName + " has no " + attachmentName + + " attachmnet"); + return false; + } + } + + public synchronized boolean deleteAttachments(String pageName) { + String attName = pageName + "-att"; + String attTrashNameBase = pageName + "-att"; + String attTrashName = attTrashNameBase; + + // create dirs to trash + Directory trashFileDir; + try { + trashFileDir = attManager.getDirectory(pageName); + } catch (ResourceDoesNotExist e) { + trashFileDir = attManager.getDirectory("").newDirectory(pageName); + } + + // moving file directory + try { + Directory attFile = attManager.getDirectory(attName); + Directory attTrashFile; + + // look if att dir is already in trash (add i-bak) + try { + attTrashFile = attManager.getDirectory("").newDirectory( + attTrashName); + } catch (ResourceAlreadyExists e) { + boolean pageFound = false; + + int i = 0; + + while (!pageFound) { + try { + attTrashName = attTrashNameBase + "-" + (i++) + "-bak"; + attTrashFile = attManager.getDirectory("") + .newDirectory(attTrashName); + pageFound = true; + } catch (ResourceAlreadyExists ee) { + // continue looking + } + } + } + + // TODO MOVE FILE + /* + * if (!attFile.renameTo(new File(attTrashName))) { + * log.error("[ATTACHMENT DS]:Problems with moving " + attName + " + * to " + attTrashName); return false; } + */ + + return true; + } catch (ResourceDoesNotExist e) { + log.error("Page " + pageName + " has no attachmnets"); + return false; + } + + } + public String getRealName(String uid) { - // TODO Auto-generated method stub - return null; + while (lock) + ; // wait till unlocked (dictionary is changing) + + return (pageDictionary.getProperty(uid) != null) ? pageDictionary + .getProperty(uid) : uid; } public String getUid(String realName) { - // TODO Auto-generated method stub - return null; + while (lock) + ; // wait till unlocked (dictionary is changing) + + return (pageRevDictionary.getProperty(realName) != null) ? pageRevDictionary + .getProperty(realName) + : realName; } - public void rename(String uid, String newName) throws PageRenamingException { - // TODO Auto-generated method stub + public synchronized void rename(String uid, String newName) + throws PageRenamingException { + if (wikiEngine.pageExists(newName)) { + throw new PageRenamingException( + "Page with the name you're trying rename to already exists."); + } + + lock = true; + + pageDictionary.setProperty(uid, newName); + pageRevDictionary.setProperty(newName, uid); + + // add also maping for future name + // find apropriate uid + + int i = 2; + + while ((pageExists(uid + String.valueOf(i))) + || (pageDictionary.containsKey(uid + String.valueOf(i)))) { + log.info(i++); + } + + // add maping for new (to be done) with name translated to old uid + pageDictionary.setProperty(uid + String.valueOf(i), uid); + pageRevDictionary.setProperty(uid, uid + String.valueOf(i)); + + saveDictionary(); + + log.info("Renamed (uid) " + uid + " to " + newName); + + lock = false; } + private void saveDictionary() { + // synchronize dictionary with file on disk + OutputStream fos = null; + + fos = pageDictionaryFile.getOutputStream(); + + try { + pageDictionary.store(fos, SAVE_COMMENT); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + public boolean uidInDictionary(String uid) { - // TODO Auto-generated method stub - return false; + return pageDictionary.contains(uid); } public boolean realNameInDictionary(String realName) { - // TODO Auto-generated method stub - return false; + return pageRevDictionary.contains(realName); } - public void removeDictForPage(String uid) throws PageRenamingException { - // TODO Auto-generated method stub + public synchronized void removeDictForPage(String uid) + throws PageRenamingException { + if (!pageDictionary.contains(uid)) { + throw new PageRenamingException( + "There is no dictionary entry for uid: " + uid); + } + String realName = pageDictionary.getProperty(uid); + + // remove all dict entries + pageDictionary.remove(uid); + pageRevDictionary.remove(realName); + + saveDictionary(); } - } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiAttachment.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiAttachment.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiAttachment.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -34,18 +34,18 @@ private long size; - private File file; + private InputStream inputStream; private String name; - + private int version; public WikiAttachment(String name, Date date, String user, long size, - File file, int version) { + InputStream inputStream, int version) { this.date = date; this.user = user; this.size = size; - this.file = file; + this.inputStream = inputStream; this.name = name; this.version = version; } @@ -67,19 +67,9 @@ } public InputStream getInputStream() { - try { - return new FileInputStream(file); - } catch (IOException e) { - System.err.println("Couldn't open file: " + file.getName()); - e.printStackTrace(); - return null; - } + return inputStream; } - public File getFile() { - return file; - } - public int getVersion() { return version; } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiEngine.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -22,6 +22,7 @@ package org.jboss.wiki; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; import java.util.Date; @@ -879,7 +880,7 @@ return attachmentDataSource.getAttachment(pageName, attachementName); } - public void addAttachment(File attFile, String attName, WikiPage page, + public void addAttachment(InputStream attFile, String attName, WikiPage page, String user) { attachmentDataSource.addAttachment(attFile, attName, page, user); } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiPage.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiPage.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/WikiPage.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -22,6 +22,7 @@ package org.jboss.wiki; import java.io.File; +import java.io.InputStream; import java.io.Serializable; import java.util.Calendar; import java.util.Date; @@ -439,7 +440,7 @@ this.length = length; } - public void addAttachement(File attFile, String attName, String user) { + public void addAttachement(InputStream attFile, String attName, String user) { wikiEngine.addAttachment(attFile, attName, this, user); } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AttachementInfoPlugin.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AttachementInfoPlugin.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/AttachementInfoPlugin.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -48,7 +48,7 @@ wikiPage.getName(), attachementName, Integer.valueOf((String) rollbackToVer)); wikiEngine.addAttachment( - wikiAttachment.getFile(), attachementName, wikiPage, + wikiAttachment.getInputStream(), attachementName, wikiPage, (String) wikiSession.getAttribute("user")); } Modified: trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/RecentlyChangedPagesPlugin.java =================================================================== --- trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/RecentlyChangedPagesPlugin.java 2005-11-07 16:50:19 UTC (rev 1525) +++ trunk/forge/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/plugins/RecentlyChangedPagesPlugin.java 2005-11-07 21:13:21 UTC (rev 1526) @@ -49,8 +49,6 @@ int offset = recentChanges.length(); Set<String> pages = wikiEngine.getAllPageNames(); - - log.info("GOT PAGE NAMES"); TreeMap<String, TreeSet<String>> sortedByDate = new TreeMap<String, TreeSet<String>>(); @@ -60,7 +58,6 @@ now.setTime(new Date()); for (String name : pages) { - log.info("getting page: " +name); WikiPage temp = wikiEngine.getByName(name, null); //log.debug("name: "+name+" "+temp.getName()); |