Author: mfortun Date: 2011-04-12 17:31:01 +0200 (Tue, 12 Apr 2011) New Revision: 793 Url: http://nuiton.org/repositories/revision/wikitty/793 Log: * add a new class used as a value in a map when harvest for file as wikitty * complete the restore method within the wikittypublicationfilesystem * add some static method for file harvesting Added: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/FileSystemWIkittyId.java Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationFileSystem.java Added: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/FileSystemWIkittyId.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/FileSystemWIkittyId.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/FileSystemWIkittyId.java 2011-04-12 15:31:01 UTC (rev 793) @@ -0,0 +1,37 @@ +package org.nuiton.wikitty.publication; + +/** + * + * Class usefull to save location and file name of a wikitty on a file system + * + * @author mfortun + * + */ +public class FileSystemWIkittyId { + + protected String fileName; + protected String path; + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public FileSystemWIkittyId(String fileName, String path) { + super(); + this.setFileName(fileName); + this.setPath(path); + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/FileSystemWIkittyId.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java 2011-04-12 12:41:07 UTC (rev 792) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java 2011-04-12 15:31:01 UTC (rev 793) @@ -262,7 +262,7 @@ * @return list of harvested file */ static protected List<File> listFile(File starts, boolean recursivly) { - ArrayList<File> result = new ArrayList<File>(); + List<File> result = new ArrayList<File>(); if (!starts.isDirectory()) { result.add(starts); } @@ -780,4 +780,30 @@ props.store(new FileWriter(propertiesFiles), ""); } } + + + /** + * Method that create a list of the properties directory + * + * @param starts + * harvested directory + * @param recursivly + * @return list of harvested file + */ + static public List<File> harvestPropertyDirectory (File starts, boolean recursivly){ + + List<File> result = new ArrayList<File>(); + + for (File child : starts.listFiles()) { + + if (child.isDirectory() && child.getName().equals(PROPERTY_DIRECTORY)) { + result.add(child); + } else if (child.isDirectory() && recursivly){ + result.addAll(harvestPropertyDirectory(child, recursivly)); + } + } + return result; + + + } } Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationFileSystem.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationFileSystem.java 2011-04-12 12:41:07 UTC (rev 792) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationFileSystem.java 2011-04-12 15:31:01 UTC (rev 793) @@ -29,8 +29,11 @@ import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; @@ -217,19 +220,17 @@ } // create the directories from the label - boolean pathFilecreated = createFilesFromLabelPath(ourDir, - homeFile); + boolean pathFilecreated = createFilesFromLabelPath(ourDir); if (pathFilecreated) { // create the path with the label String path = homeFile.getCanonicalFile() + File.separator + ourDir.replace(".", File.separator); - - //correct the pb with directory name begin by . + + // correct the pb with directory name begin by . path = path.replace(File.separator + File.separator, File.separator + "."); - // create the propertie directory if necessary File propertieDirectory = new File(path + File.separator + WikittyPublication.PROPERTY_DIRECTORY); @@ -245,6 +246,7 @@ if (!propertieFile.exists()) { propertieFile.createNewFile(); } + // load/create id propertie file File idPropertieFile = new File( propertieDirectory.getCanonicalPath() @@ -301,6 +303,9 @@ Properties metaProperties = new Properties(); metaProperties.load(new FileReader(propertieFile)); // update + metaProperties.setProperty( + WikittyPublication.META_CURRENT_LABEL, ourDir); + metaProperties.setProperty(name + "." + extension + ".version", w.getVersion()); metaProperties.setProperty(name + "." + extension @@ -321,7 +326,6 @@ } } - } catch (Exception e) { e.printStackTrace(); // TODO mfortun-2011-04-12 really handle exceptions @@ -387,15 +391,73 @@ @Override public List<Wikitty> restore(String securityToken, List<String> id) { - // TODO mfortun-2011-04-05 + List<Wikitty> result = new ArrayList<Wikitty>(); + try { + Map<String, FileSystemWIkittyId> locations = harvestLocalWikitties( + homeFile, recursion); - /* - * - */ + for (String wikid : id) { + FileSystemWIkittyId localisation = locations.get(wikid); + // create the wikitty with his id + Wikitty wikitty = new WikittyImpl(wikid); + // add label extension + wikitty.addExtension(WikittyLabelImpl.extensionWikittyLabel); - throw new UnsupportedOperationException("not yet implemented"); - // return null; + // preparation for mime research and file research + String path = localisation.getPath(); + String completeName = localisation.getFileName(); + // search for the file + File fileToTransform = new File(path + File.separator + + completeName); + + String extension = FileUtil.extension(fileToTransform); + String name = FileUtil.basename(completeName, "." + extension); + // search for the mimetype + String mimeType = mimeTypeForExtension(extension); + + // load properties + Properties props = new Properties(); + File propsFile = new File(path + File.separator + + WikittyPublication.PROPERTY_DIRECTORY + + File.separator + + WikittyPublication.WIKITTY_FILE_META_PROPERTIES_FILE); + props.load(new FileReader(propsFile)); + // re set the id + wikitty.setVersion(props.getProperty(completeName + + WikittyPublication.META_SUFFIX_KEY_ID)); + + // set the current label + WikittyLabelHelper.addLabels(wikitty, props + .getProperty(WikittyPublication.META_CURRENT_LABEL)); + + /* + * TODO mfortun-2011-04-12 need to merge part of this code with + * filetowikitty method 'cause it is basically the same + */ + + // create the correct wikittypubxxx + if (isMimeWikittyPubText(mimeType)) { + wikitty.addExtension(WikittyPubTextImpl.extensionWikittyPubText); + WikittyPubTextHelper.setName(wikitty, name); + WikittyPubTextHelper.setMimeType(wikitty, mimeType); + WikittyPubTextHelper.setContent(wikitty, + FileUtil.readAsString(fileToTransform)); + } else { + wikitty.addExtension(WikittyPubDataImpl.extensionWikittyPubData); + WikittyPubDataHelper.setName(wikitty, name); + WikittyPubDataHelper.setMimeType(wikitty, mimeType); + WikittyPubDataHelper.setContent(wikitty, + FileUtil.fileToByte(fileToTransform)); + } + + result.add(wikitty); + } + } catch (Exception e) { + e.printStackTrace(); + // TODO mfortun-2011-01-12 really handle exception + } + return result; } @Override @@ -476,7 +538,6 @@ static public Wikitty fileToWikitty(File fileToTransform, File starts) throws Exception { - String completeName = fileToTransform.getName(); // isolate extension and file name @@ -506,11 +567,11 @@ String path = startDirName + pathToFile.replaceAll(pathToStart, ""); /* - * FIXME actually with a dot as a wikittylabel_separator, - * when restauring label to directory it destroy directory that containt dot + * FIXME actually with a dot as a wikittylabel_separator, when + * restauring label to directory it destroy directory that containt dot * in the name e.g.: /home/truc.machin/bob */ - + String label = path.replaceAll(File.separator, WIKITTYLABEL_SEPARATOR); WikittyLabelHelper.addLabels(result, label); @@ -594,13 +655,10 @@ * * @param label * the path string - * @param starts - * the working directory * @return if all the path was created * @throws Exception */ - static public boolean createFilesFromLabelPath(String label, File starts) - throws Exception { + protected boolean createFilesFromLabelPath(String label) throws Exception { label = label.replace(".", File.separator); label = label.replace(File.separator + File.separator, File.separator @@ -609,8 +667,8 @@ boolean result = false; - if (starts.exists() && starts.isDirectory()) { - String path = starts.getCanonicalPath(); + if (homeFile.exists() && homeFile.isDirectory()) { + String path = homeFile.getCanonicalPath(); result = true; for (int i = 0; i < pathElements.length; i++) { @@ -623,4 +681,56 @@ return result; } + + /** + * + * @param starts + * @param recursivly + * @return + * @throws Exception + */ + static public Map<String, FileSystemWIkittyId> harvestLocalWikitties( + File starts, boolean recursivly) throws Exception { + Map<String, FileSystemWIkittyId> result = new HashMap<String, FileSystemWIkittyId>(); + + /* + * TODO mfortun-2011-04-12 find a better way to have cohesion class that + * call another class that call last class <_<. May be a class for file + * access/harvest method + */ + List<File> propertiesDirectory = WikittyPublication + .harvestPropertyDirectory(starts, recursivly); + + for (File propsDir : propertiesDirectory) { + + Properties idProps = new Properties(); + File idFile = new File(propsDir.getCanonicalPath() + File.separator + + WikittyPublication.WIKITTY_ID_PROPERTIES_FILE); + if (idFile.exists()) { + idProps.load(new FileReader(idFile)); + } + + Set<Object> ids = idProps.keySet(); + + for (Object id : ids) { + String name = idProps.getProperty((String) id); + String path = propsDir.getParent(); + FileSystemWIkittyId value = new FileSystemWIkittyId(name, path); + + result.put((String) id, value); + } + + } + + /* + * Pour l'arborescence des fichiers on va aller chercher les fichiers de + * propriété id et meta dans le dossier .wp pour aller récup les labels + * courant et les id liés on va renvoyer la map + * + * id, label + */ + + return result; + } + }
participants (1)
-
mfortun@users.nuiton.org