r801 - trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro
Author: mfortun Date: 2011-04-13 17:12:01 +0200 (Wed, 13 Apr 2011) New Revision: 801 Url: http://nuiton.org/repositories/revision/wikitty/801 Log: * new package for the synchro part. Added: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/FileSystemWIkittyId.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/FileSystemWIkittyId.java (from rev 798, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/FileSystemWIkittyId.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/FileSystemWIkittyId.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/FileSystemWIkittyId.java 2011-04-13 15:12:01 UTC (rev 801) @@ -0,0 +1,76 @@ +package org.nuiton.wikitty.publication.synchro; + +/** + * + * 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); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((fileName == null) ? 0 : fileName.hashCode()); + result = prime * result + ((path == null) ? 0 : path.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof FileSystemWIkittyId)) { + return false; + } + FileSystemWIkittyId other = (FileSystemWIkittyId) obj; + if (fileName == null) { + if (other.fileName != null) { + return false; + } + } else if (!fileName.equals(other.fileName)) { + return false; + } + if (path == null) { + if (other.path != null) { + return false; + } + } else if (!path.equals(other.path)) { + return false; + } + return true; + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/FileSystemWIkittyId.java ___________________________________________________________________ Added: svn:mime-type + text/plain Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java (from rev 798, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/PropertiesExtended.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java 2011-04-13 15:12:01 UTC (rev 801) @@ -0,0 +1,39 @@ +package org.nuiton.wikitty.publication.synchro; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.Properties; + +/** + * Class usefull when load properties file, update, delete, then save again + * File used to load properties is store. + * + * @author mfortun + * + */ +public class PropertiesExtended extends Properties { + + /** + * + */ + private static final long serialVersionUID = -264337198024996529L; + + protected File origin; + + public PropertiesExtended(File origin) throws Exception { + super(); + this.load(origin); + + } + + public void load(File file) throws Exception { + this.origin = file; + this.load(new FileReader(origin)); + } + + public void store() throws Exception { + this.store(new FileWriter(origin), ""); + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java ___________________________________________________________________ Added: svn:mime-type + text/plain Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java (from rev 798, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java 2011-04-13 15:12:01 UTC (rev 801) @@ -0,0 +1,803 @@ +/* + * #%L + * Wikitty :: publication + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin mfortun + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.wikitty.publication.synchro; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.collections.BidiMap; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.ApplicationConfig; +import org.nuiton.util.ArgumentsParserException; +import org.nuiton.util.CollectionUtil; +import org.nuiton.util.FileUtil; +import org.nuiton.wikitty.WikittyProxy; +import org.nuiton.wikitty.WikittyServiceFactory; +import org.nuiton.wikitty.entities.Wikitty; +import org.nuiton.wikitty.entities.WikittyLabel; +import org.nuiton.wikitty.entities.WikittyLabelHelper; +import org.nuiton.wikitty.publication.entities.WikittyPubData; +import org.nuiton.wikitty.publication.entities.WikittyPubText; +import org.nuiton.wikitty.search.Criteria; +import org.nuiton.wikitty.search.PagedResult; +import org.nuiton.wikitty.search.Search; + +/** + * Main class of the sync part of wikitty publication, this class is the entry + * point for sync operation : import, checkout, commit, delete, relocate and + * update. + * + * + * + * @author mfortun + * + */ +public class WikittyPublication { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + final static private Log log = LogFactory.getLog(WikittyPublication.class); + + static protected ApplicationConfig applicationConfig; + + /* + * static string for allias, wrong named attribut TODO mfortun-2011-04-06 + * need to set better name + */ + + /* + * FIXME mfortun-2011-04-11 need to migrate static value to another class ? + */ + static public String WITTY_SERVICE_KEY = "wikitty.service.server.url"; + static public String NO_RECURSION_KEY = "norecursion"; + static public String DIRECTORY_KEY = "directory"; + static public String HESSIAN_PROTOCOL_KEY = "hessian"; + + static public String DEFAULT_PROPERTY_NAME_SEP = "."; + + static public String PROPERTY_DIRECTORY = ".wp"; + static public String WIKITTYPUBLICATION_PROPERTIES_FILE = "ws.properties"; + + /* + * Need a different file for id and meta information about wikittiesFiles + * because with this solution we can simply read the ids with props.keySet() + */ + static public String WIKITTY_ID_PROPERTIES_FILE = "ids.properties"; + static public String WIKITTY_FILE_META_PROPERTIES_FILE = "meta.properties"; + + /* + * in the WIKITTY_FILE_META_PROP ERTIES_FILE the keys for version are: + * filename + META_SUFFIX_KEY_VERSION we save the id too because with this + * solution un case of delete we can easily read the id and make operation + * on the wikitty + */ + static public String META_SUFFIX_KEY_VERSION = "version"; + static public String META_SUFFIX_KEY_CHECKSUM = "checksum"; + static public String META_SUFFIX_KEY_ID = "id"; + + static public String META_CURRENT_LABEL = "current.label"; + + /** + * @param args + * @throws ArgumentsParserException + */ + static public void main(String[] args) throws Exception { + + /* + * + */ + + // on va creer un wikitty proxy pour le lien avec le wikitty qui stock + // mes trucs + // et on va avoir un wikittypublicationfilesystem pour stocker sur le + // local et tout + // soucis du wikittyFS c'est de savoir dans quel dossier il va taffer ? + // a moins qu'il prenne un directory de travail + + /* + * ws.properties :wikitty.service= http://www.adresse.com:8080 + * + * wikittypubs.properties script.js=numéroVersion7 id.script.js= id du + * wikitty scripttut.js=numéroVersion id.scripttut.js= id du wikitty + * image.png=numéroVersion id.image.png= id du wikitty label= + * racine.directory2, racine.directory22 + */ + + applicationConfig = new ApplicationConfig(); + + // allias for the url of the wikitty service + applicationConfig.addAlias("--ws", "--option", WITTY_SERVICE_KEY); + + applicationConfig.addAlias("--dir", "--option", DIRECTORY_KEY); + + /* + * TODO mfortun-2011-04-05 once application fixed setdefault value and + * enumclass for initialisation + */ + + // allias for norecursion + applicationConfig.addAlias("--norecursion", "--option", + NO_RECURSION_KEY, "true"); + + // allias for the protocole + applicationConfig.addAlias("--hessian", "--option", + HESSIAN_PROTOCOL_KEY, "true"); + + // allias for all the action + applicationConfig.addAlias("wp import", "--option", "import"); + applicationConfig.addAlias("wp checkout", "--option", "checkout"); + applicationConfig.addAlias("wp relocate", "--option", "relocate"); + applicationConfig.addAlias("wp commit", "--option", "commit"); + applicationConfig.addAlias("wp delete", "--option", "delete"); + applicationConfig.addAlias("wp update", "--option", "update"); + + applicationConfig + .addActionAlias("import", + "org.nuiton.wikitty.publication.WikittyPublication#importToWikitty"); + + applicationConfig + .addActionAlias("checkout", + "org.nuiton.wikitty.publication.WikittyPublication#checkoutFromWikitty"); + + applicationConfig + .addActionAlias("relocate", + "org.nuiton.wikitty.publication.WikittyPublication#relocateWikitty"); + + applicationConfig + .addActionAlias("commit", + "org.nuiton.wikitty.publication.WikittyPublication#commitToWikitty"); + + applicationConfig + .addActionAlias("delete", + "org.nuiton.wikitty.publication.WikittyPublication#deleteFromWikitty"); + + applicationConfig + .addActionAlias("update", + "org.nuiton.wikitty.publication.WikittyPublication#updateFromWikitty"); + + // parsing + applicationConfig.parse(args); + // execution + applicationConfig.doAction(0); + + } + + /** + * Method that import the content of a directory into a wikitty service + */ + static public void importToWikitty() throws Exception { + + boolean noRecur = applicationConfig + .getOptionAsBoolean(NO_RECURSION_KEY); + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + String wikittyService = applicationConfig.getOption(WITTY_SERVICE_KEY); + + log.info("import : wikittyservice: " + wikittyService + " noresursion=" + + noRecur + " directory= " + dir.getAbsolutePath()); + // usage: wp --norecursion --ws http://truc.com import --dir /home/Manou + + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + + System.out.println(applicationConfig + .getOption("wikitty.service.server.url")); + + // real code: + WikittyProxy remoteWikittyService = new WikittyProxy( + WikittyServiceFactory.buildWikittyService(applicationConfig)); + + remoteWikittyService.clear(); + // load the list of file + List<File> toTransfert = listFile(dir, !noRecur); + + List<Wikitty> listWikitty = new ArrayList<Wikitty>(); + // transform file into wikitties + + for (File fileToTransform : toTransfert) { + listWikitty.add(WikittyPublicationFileSystem.fileToWikitty( + fileToTransform, dir)); + } + + // send the wikitties + remoteWikittyService.storeWikitty(listWikitty); + + } + + /* + * TODO mfortun-2011-04-05 just prototyping, remove when really implements + * the method linked: importToWikitty + */ + static public void printDirectory(File dir, boolean recur) { + + System.out.println("<dir " + dir.getName() + ">"); + for (File child : dir.listFiles()) { + if (child.isDirectory() && recur + && !child.getName().equals(PROPERTY_DIRECTORY)) { + printDirectory(child, recur); + } else if (!child.isDirectory()) { + System.out.println(child.getAbsolutePath()); + } + } + System.out.println("</dir " + dir.getName() + ">"); + } + + /** + * Method that create the list of file needed to commit, delete, update + * import. It harvest file in order to transform them into wikitties + * + * @param starts + * harvested directory + * @param recursivly + * boolean id the directory have to be harvest + * @return list of harvested file + */ + static protected List<File> listFile(File starts, boolean recursivly) { + List<File> result = new ArrayList<File>(); + if (!starts.isDirectory()) { + result.add(starts); + } + for (File child : starts.listFiles()) { + if (child.isDirectory() && recursivly + && !child.getName().equals(PROPERTY_DIRECTORY)) { + // Directory don't have to be harvest + // result.add(child); + result.addAll(listFile(child, recursivly)); + } else if (!child.isDirectory()) { + result.add(child); + } + } + return result; + } + + /** + * Method that checkout a label recursivly or not into a local directory + * from a wikitty service + */ + static public void checkoutFromWikitty(String label) throws Exception { + + boolean noRecur = applicationConfig + .getOptionAsBoolean(NO_RECURSION_KEY); + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + String wikittyService = applicationConfig.getOption(WITTY_SERVICE_KEY); + boolean hessianProtocole = applicationConfig + .getOptionAsBoolean(HESSIAN_PROTOCOL_KEY); + + log.info("checkout : wikittyservice: " + wikittyService + + " noresursion=" + noRecur + " directory= " + + dir.getAbsolutePath() + "Label a checkout " + label + + "HessianProtocol=" + hessianProtocole); + + if (hessianProtocole) { + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceHessianClient"); + } else { + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + } + + WikittyProxy remoteWikittyService = new WikittyProxy( + WikittyServiceFactory.buildWikittyService(applicationConfig)); + WikittyPublicationFileSystem localWikittyService = new WikittyPublicationFileSystem( + dir, !noRecur, label); + + // Construct the criteria + Criteria labelCriteria; + Search mainRequest = Search.query(); + Search subRoqu = mainRequest.or(); + + // must have the type of wikittypubtext/wikittypubdata + subRoqu.exteq(WikittyPubText.EXT_WIKITTYPUBTEXT).exteq( + WikittyPubData.EXT_WIKITTYPUBDATA); + if (noRecur) { + // and extension with the name strictly equals to the label (no + // recursivity) + labelCriteria = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) + .eq(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, label) + .criteria(); + + } else { + // and extension with the name that containt the label (recursivity) + labelCriteria = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) + .sw(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, label) + .criteria(); + } + + // request to the proxy + PagedResult<Wikitty> pageResult = remoteWikittyService + .findAllByCriteria(labelCriteria); + + List<Wikitty> wikittiesToWrite = pageResult.getAll(); + + // write the proper properties file! + writeHomePropertyFile(dir); + + // write the wikities + localWikittyService.store("", wikittiesToWrite, true); + + /* + * obtain the list of wikittypub and write then in the file system with + * the appropriate wikittyservice ! + */ + + /* + * on va commencer par vérifier les arguments ''wp checkout + * [--norecursion] [url du WikittyService] [Label à extraire] [directory + * local d'accueil]'' on doit avoir trois string dans le unparsed: url, + * label, directory et potentiellement quelque chose dans le getoption + * recursion + */ + + } + + /** + * Relocate the default url of the wikitty service + */ + static public void relocateWikitty() throws Exception { + + /* + * log.info("checkout : wikittyservice: " + wikittyService + + * " noresursion=" + noRecur + " directory= " + dir.getAbsolutePath() + * +"Label a checkout " +label+ "HessianProtocol="+hessianProtocole); + */ + + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + String wikittyService = applicationConfig.getOption(WITTY_SERVICE_KEY); + boolean hessianProtocole = applicationConfig + .getOptionAsBoolean(HESSIAN_PROTOCOL_KEY); + + File wpHomeDir; + + System.out + .println("search the directory of .wp file to write new properties file"); + // search for the home directory + if (null == dir || !dir.exists()) { + wpHomeDir = searchWikittyPublicationHomeDir(new File(".")); + } else { + wpHomeDir = new File(dir.getCanonicalFile() + File.separator + + PROPERTY_DIRECTORY); + } + + Properties oldProperties = new Properties(); + + // TODO mfortun-2011-04-06 catch exception instead of throws + File propertiesFile = new File(wpHomeDir + File.separator + + WIKITTYPUBLICATION_PROPERTIES_FILE); + // load the old file just for loggin + oldProperties.load(new FileReader(propertiesFile)); + + log.info("Try relocate :" + "wikitty service:" + + oldProperties.getProperty(WITTY_SERVICE_KEY) + " by " + + wikittyService + " HessianProtocol: " + hessianProtocole + + " File : " + propertiesFile.getCanonicalPath()); + + // Creation of the new properties file + Properties props = new Properties(); + props.put(WITTY_SERVICE_KEY, wikittyService); + if (hessianProtocole) { + props.put("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceHessianClient"); + } else { + props.put("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + } + + // save the new property file + props.store(new FileWriter(propertiesFile), ""); + + /* + * on va commencer par vérifier les arguments ''wp relocate [nouvelle + * url du WikittyService par defaut] [directory a relocaliser]'' on doit + * avoir trois string dans le unparsed: url et directory + */ + + } + + /** + * commit the current wikittyworkspace into a wikitty service + */ + static public void commitToWikitty() throws Exception { + + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + + File wpHomeDir; + if (null == dir || !dir.exists()) { + /* + * si pas de dir on commit le dossier courant + */ + dir = new File("."); + + } + // on va chercher le home dir + wpHomeDir = searchWikittyPublicationHomeDir(dir); + + Properties properties = new Properties(); + + // TODO mfortun-2011-04-06 catch exception instead of throws + File propertiesFile = new File(wpHomeDir.getCanonicalPath() + + File.separator + WIKITTYPUBLICATION_PROPERTIES_FILE); + // load the old file just for loggin + properties.load(new FileReader(propertiesFile)); + + // on va rajouter les propriété de notre file + applicationConfig.setOptions(properties); + + boolean noRecur = applicationConfig + .getOptionAsBoolean(NO_RECURSION_KEY); + + String wikittyService = applicationConfig.getOption(WITTY_SERVICE_KEY); + boolean hessianProtocole = applicationConfig + .getOptionAsBoolean(HESSIAN_PROTOCOL_KEY); + + if (hessianProtocole) { + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceHessianClient"); + } else { + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + } + + log.info("commit " + " wikitty-service" + wikittyService + + "noRecurs : " + noRecur + " hessian Protocole :" + + hessianProtocole + "directory : " + dir.getAbsolutePath()); + + System.out.println("try to commit: "); + printDirectory(dir, !noRecur); + + // faire la liste des fichiers à commit + // avec récursivité si c'etransfert d'histoire entre svnst demandé. + // et les transformer en wikitty, vérifier les versions et tout. + + /* + * parcours l'arborescence des fichiers celon que on soit recursif ou + * non plutot que faire la liste fichier, on va aller lire les ids dans + * les fichiers de propriétés. + * + * Ensuite on creer le wikittyserviceFileSysteme avec le répertoire de + * travail et lui on lui donner les ids avec lesquels il va travailler + * et aller chercher les fichiers de propriétés pour retrouver les files + * et creer les wikittypub correspondant. + * + * Pour faire un commit il ne suffira pas de faire un restore + * finalement, il faudra faire un search aussi puisque on va se + * retrouver à faire des new wikitty p'etre éclaircir ce point plus + * tard. Pour le moment ne pas se poser de question trop, et faire sans + * nouveau fichier. + */ + + // création du proxy + + /* + * on va commencer par vérifier les arguments ''wp commit + * [--norecursion] [--ws (url du WikittyService)] [répertoire à + * pousser]'' on doit avoir deux string dans le unparsed et + * potentiellement quelque chose dans le getoption recursion + */ + + } + + /** + * delete a file or directory from the workspace it remove the label from + * the wikitty + * + * @throws Exception + */ + static public void deleteFromWikitty(File toDelete) throws Exception { + + // check args + if (null == toDelete || !toDelete.exists()) { + // Exception + + } else { + + File currentDir = new File(FileUtil.getCurrentDirectory() + .getAbsolutePath()); + File wpHomeDir = searchWikittyPublicationHomeDir(currentDir); + // search for the .wp home dir to load props + + // then load proxy + + // if toDelete is a directory do something different from usualy + // recursivly remove labels + + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + + Properties properties = new Properties(); + + // TODO mfortun-2011-04-06 catch exception instead of throws + File propertiesFile = new File(wpHomeDir.getCanonicalPath() + + File.separator + WIKITTYPUBLICATION_PROPERTIES_FILE); + // load the old file just for loggin + properties.load(new FileReader(propertiesFile)); + + // on va rajouter les propriété de notre file + applicationConfig.setOptions(properties); + + String wikittyService = applicationConfig + .getOption(WITTY_SERVICE_KEY); + boolean hessianProtocole = applicationConfig + .getOptionAsBoolean(HESSIAN_PROTOCOL_KEY); + + if (hessianProtocole) { + applicationConfig + .setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceHessianClient"); + } else { + applicationConfig.setOption( + "wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + } + + log.info("Delete " + " wikitty-service" + wikittyService + + " hessian Protocole :" + hessianProtocole + + "file to delete : " + toDelete.getAbsolutePath()); + // on va chercher le home dir + wpHomeDir = searchWikittyPublicationHomeDir(dir); + + WikittyProxy remoteWikittyService = new WikittyProxy( + WikittyServiceFactory + .buildWikittyService(applicationConfig)); + WikittyPublicationFileSystem localWikittyService = new WikittyPublicationFileSystem( + dir); + + if (toDelete.isDirectory()) { + + BidiMap deleteMap = WikittyPublicationFileSystem + .harvestLocalWikitties(toDelete, true); + List<String> ids = new ArrayList<String>(); + ids = new ArrayList<String>(); + ids.addAll(CollectionUtil.toGenericCollection( + deleteMap.keySet(), String.class)); + + for (String key : ids) { + // restore wikitty + Wikitty wd = remoteWikittyService.restore(key); + + // searh for the label to delete from the wikitty + FileSystemWIkittyId location = (FileSystemWIkittyId) deleteMap + .get(key); + + File wikittyFileParent = new File(location.getPath()); + PropertiesExtended metaExtended = WikittyPublicationFileSystem + .getWikittyPublicationProperties( + wikittyFileParent, + WikittyPublication.WIKITTY_FILE_META_PROPERTIES_FILE); + + String currentLabel = metaExtended + .getProperty(META_CURRENT_LABEL); + // remove label + WikittyLabelHelper.removeLabels(wd, currentLabel); + // save the wikitty + remoteWikittyService.store(wd); + } + + localWikittyService.delete("", ids); + // if it was a directory the wikitty file service does not have + // delete it + + FileUtil.deleteRecursively(toDelete); + } else { + // infos about file about to delete + String name = toDelete.getName(); + File parentFile = toDelete.getParentFile(); + // search of it's id + PropertiesExtended metaProps = WikittyPublicationFileSystem + .getWikittyPublicationProperties( + parentFile, + WikittyPublication.WIKITTY_FILE_META_PROPERTIES_FILE); + + String id = metaProps.getProperty(name + + DEFAULT_PROPERTY_NAME_SEP + META_SUFFIX_KEY_ID); + // his label + String label = metaProps + .getProperty(WikittyPublication.META_CURRENT_LABEL); + + // restore wikitty remote + + Wikitty wd = remoteWikittyService.restore(id); + + // remove label + + WikittyLabelHelper.removeLabels(wd, label); + // save the wikitty remote + remoteWikittyService.store(wd); + + List<String> ids = new ArrayList<String>(); + ids.add(id); + // delete localy + localWikittyService.delete("", ids); + } + + } + + /* + * on va commencer par vérifier les arguments ''wp delete [--ws (url du + * WikittyService)] [répertoire ou fichier à supprimer]'' on doit avoir + * un string dans le unparsed et après pour pour le wikittyservice bah + * on tape dedans en fonction de si elle a été précisé en ligne de + * commande applicationConfig se débrouille + */ + } + + /** + * update the current workspace from a wikitty + */ + static public void updateFromWikitty() throws Exception { + + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + + File wpHomeDir; + if (null == dir || !dir.exists()) { + /* + * si pas de dir on update le dossier courant + */ + dir = new File("."); + + } + // on va chercher le home dir + wpHomeDir = searchWikittyPublicationHomeDir(dir); + + Properties properties = new Properties(); + + // TODO mfortun-2011-04-06 catch exception instead of throws + File propertiesFile = new File(wpHomeDir.getCanonicalPath() + + File.separator + WIKITTYPUBLICATION_PROPERTIES_FILE); + // load the old file just for loggin + properties.load(new FileReader(propertiesFile)); + + // on va rajouter les propriété de notre file + applicationConfig.setOptions(properties); + + boolean noRecur = applicationConfig + .getOptionAsBoolean(NO_RECURSION_KEY); + + String wikittyService = applicationConfig.getOption(WITTY_SERVICE_KEY); + boolean hessianProtocole = applicationConfig + .getOptionAsBoolean(HESSIAN_PROTOCOL_KEY); + + if (hessianProtocole) { + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceHessianClient"); + } else { + applicationConfig.setOption("wikitty.WikittyService.components", + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + } + + log.info("update " + " wikitty-service" + wikittyService + + "noRecurs : " + noRecur + " hessian Protocole :" + + hessianProtocole + "directory : " + dir.getAbsolutePath()); + + System.out.println("try to update: "); + printDirectory(dir, !noRecur); + + /* + * on va commencer par vérifier les arguments * ''wp update + * [--norecursion] [--ws (url du WikittyService)] [répertoire à mettre à + * jour]'' normalement un string dans unparsed et potentiellement + * quelque chose dans le recursion + */ + } + + /** + * + * Use to search in the file system the directory that containt the .wp dir + * that containt the properties file for the wikitty service's adresse + * + * @param start + * @return File the directory container of the master property file + * @throws Exception + */ + static protected File searchWikittyPublicationHomeDir(File start) + throws Exception { + + if (start != null && start.exists() && start.isDirectory()) { + // on va retourner le dossier .wp home + + // method récursiv qui remonte dans les parents pour retrouver. + + File propertyDirectory = new File(start.getCanonicalPath() + + File.separator + PROPERTY_DIRECTORY); + + if (propertyDirectory.exists()) { + File propertie = new File(propertyDirectory.getCanonicalPath() + + File.separator + WIKITTYPUBLICATION_PROPERTIES_FILE); + if (propertie.exists()) { + return propertyDirectory; + } + } + + return searchWikittyPublicationHomeDir(start.getParentFile()); + } else { + // Exception + /* + * TODO mfortun-2011-04-06 write/set the appropriate exception here + */ + return null; + } + } + + static protected void writeHomePropertyFile(File homeDir) throws Exception { + + if (homeDir.exists()) { + File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); + String wikittyService = applicationConfig + .getOption(WITTY_SERVICE_KEY); + boolean hessianProtocole = applicationConfig + .getOptionAsBoolean(HESSIAN_PROTOCOL_KEY); + + // TODO mfortun-2011-04-11 find a better way to manage component + // property + String propertyCompo = applicationConfig + .getOption("wikitty.WikittyService.components"); + // write the proper properties file! + Properties props = new Properties(); + props.put(WITTY_SERVICE_KEY, wikittyService); + + props.put("wikitty.WikittyService.components", propertyCompo); + + // create the properties directory + File wpDirectory = new File(homeDir.getCanonicalPath() + + File.separator + PROPERTY_DIRECTORY + File.separator); + if (!wpDirectory.exists()) { + wpDirectory.mkdir(); + } + + File propertiesFiles = new File(wpDirectory.getCanonicalPath() + + File.separator + WIKITTYPUBLICATION_PROPERTIES_FILE); + + 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; + + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java (from rev 800, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationFileSystem.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-04-13 15:12:01 UTC (rev 801) @@ -0,0 +1,836 @@ +/* + * #%L + * Wikitty :: publication + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin mfortun + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.wikitty.publication.synchro; + +import java.io.BufferedInputStream; +import java.io.File; +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; + +import org.apache.commons.collections.BidiMap; +import org.apache.commons.collections.bidimap.DualHashBidiMap; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.FileUtil; +import org.nuiton.util.MD5InputStream; +import org.nuiton.util.StringUtil; +import org.nuiton.wikitty.WikittyService; +import org.nuiton.wikitty.entities.Wikitty; +import org.nuiton.wikitty.entities.WikittyExtension; +import org.nuiton.wikitty.entities.WikittyImpl; +import org.nuiton.wikitty.entities.WikittyLabelHelper; +import org.nuiton.wikitty.entities.WikittyLabelImpl; +import org.nuiton.wikitty.publication.entities.WikittyPubData; +import org.nuiton.wikitty.publication.entities.WikittyPubDataHelper; +import org.nuiton.wikitty.publication.entities.WikittyPubDataImpl; +import org.nuiton.wikitty.publication.entities.WikittyPubText; +import org.nuiton.wikitty.publication.entities.WikittyPubTextHelper; +import org.nuiton.wikitty.publication.entities.WikittyPubTextImpl; +import org.nuiton.wikitty.search.Criteria; +import org.nuiton.wikitty.search.PagedResult; +import org.nuiton.wikitty.search.TreeNodeResult; +import org.nuiton.wikitty.services.WikittyEvent; +import org.nuiton.wikitty.services.WikittyListener; + +public class WikittyPublicationFileSystem implements WikittyService { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + final static private Log log = LogFactory + .getLog(WikittyPublicationFileSystem.class); + + static public String WIKITTYLABEL_SEPARATOR = "."; + + protected File homeFile; + protected boolean recursion; + protected String label; + + /** + * Constructor with the working directory + * + * @param home + * the home directory + */ + public WikittyPublicationFileSystem(File home, boolean recur, String label) { + this.setHomeFile(home); + this.setRecursion(recur); + this.setLabel(label); + } + + + + + public WikittyPublicationFileSystem(File homeFile) { + this(homeFile, true, ""); + + } + + + + + public File getHomeFile() { + return homeFile; + } + + public void setHomeFile(File homeFile) { + this.homeFile = homeFile; + } + + public boolean isRecursion() { + return recursion; + } + + public void setRecursion(boolean recursion) { + this.recursion = recursion; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + @Override + public void addWikittyServiceListener(WikittyListener listener, + ServiceListenerType type) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // + + } + + @Override + public void removeWikittyServiceListener(WikittyListener listener, + ServiceListenerType type) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // + + } + + @Override + public String login(String login, String password) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public void logout(String securityToken) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // + + } + + @Override + public WikittyEvent clear(String securityToken) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public boolean canWrite(String securityToken, Wikitty wikitty) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return false; + + } + + @Override + public boolean canDelete(String securityToken, String wikittyId) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return false; + + } + + @Override + public boolean canRead(String securityToken, String wikittyId) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return false; + + } + + @Override + public boolean exists(String securityToken, String wikittyId) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return false; + + } + + @Override + public boolean isDeleted(String securityToken, String wikittyId) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return false; + + } + + @Override + public WikittyEvent replay(String securityToken, List<WikittyEvent> events, + boolean force) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public WikittyEvent store(String securityToken, + Collection<Wikitty> wikitties, boolean force) { + + try { + + for (Wikitty w : wikitties) { + + Set<String> set = WikittyLabelHelper.getLabels(w); + String ourDir = ""; + + // search for our label + for (String name : set) { + if (!recursion && name.equalsIgnoreCase(label)) { + ourDir = name; + } else if (recursion && name.startsWith(label)) { + ourDir = name; + } + } + + // create the directories from the label + 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 . + 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); + if (!propertieDirectory.exists() + || !propertieDirectory.isDirectory()) { + propertieDirectory.mkdir(); + } + // load/create meta propertie file + File propertieFile = new File( + propertieDirectory.getCanonicalPath() + + File.separator + + WikittyPublication.WIKITTY_FILE_META_PROPERTIES_FILE); + if (!propertieFile.exists()) { + propertieFile.createNewFile(); + } + + // load/create id propertie file + File idPropertieFile = new File( + propertieDirectory.getCanonicalPath() + + File.separator + + WikittyPublication.WIKITTY_ID_PROPERTIES_FILE); + if (!idPropertieFile.exists()) { + idPropertieFile.createNewFile(); + } + + String name = ""; + String extension = ""; + + File wikittyFile = null; + if (w.hasExtension(WikittyPubData.EXT_WIKITTYPUBDATA)) { + name = WikittyPubDataHelper.getName(w); + String mime = WikittyPubDataHelper.getMimeType(w); + byte[] content = WikittyPubDataHelper.getContent(w); + + extension = extensionFormimeType(mime); + + wikittyFile = new File(path + File.separator + name + + "." + extension); + + wikittyFile.createNewFile(); + + FileUtil.byteToFile(content, wikittyFile); + + } else if (w + .hasExtension(WikittyPubText.EXT_WIKITTYPUBTEXT)) { + name = WikittyPubTextHelper.getName(w); + String mime = WikittyPubTextHelper.getMimeType(w); + String content = WikittyPubTextHelper.getContent(w); + + extension = extensionFormimeType(mime); + + wikittyFile = new File(path + File.separator + name + + "." + extension); + + wikittyFile.createNewFile(); + + FileUtil.writeString(wikittyFile, content); + } + + if (wikittyFile != null) { + // prepare for checksum + BufferedInputStream input = new BufferedInputStream( + new FileInputStream(wikittyFile)); + + byte[] byt = MD5InputStream.hash(input); + + String localMd5 = StringUtil.asHex(byt); + + // load meta properties + 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 + + ".id", w.getId()); + metaProperties.setProperty(name + "." + extension + + ".checksum", localMd5); + // save + metaProperties.store(new FileWriter(propertieFile), ""); + // load id properties + Properties idProperties = new Properties(); + idProperties.load(new FileReader(idPropertieFile)); + // update + idProperties.setProperty(w.getId(), name + "." + + extension); + // save + idProperties.store(new FileWriter(idPropertieFile), ""); + } + } + } + + } catch (Exception e) { + e.printStackTrace(); + // TODO mfortun-2011-04-12 really handle exceptions + } + + WikittyEvent result = new WikittyEvent(this); + + return result; + } + + @Override + public List<String> getAllExtensionIds(String securityToken) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public List<String> getAllExtensionsRequires(String securityToken, + String extensionName) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public WikittyEvent storeExtension(String securityToken, + Collection<WikittyExtension> exts) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public WikittyEvent deleteExtension(String securityToken, + Collection<String> extNames) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public WikittyExtension restoreExtension(String securityToken, + String extensionId) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public WikittyExtension restoreExtensionLastVersion(String securityToken, + String name) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public List<Wikitty> restore(String securityToken, List<String> id) { + List<Wikitty> result = new ArrayList<Wikitty>(); + try { + BidiMap locations = harvestLocalWikitties(homeFile, recursion); + + for (String wikid : id) { + Object value = locations.get(wikid); + + if (value == null) { + locations = locations.inverseBidiMap(); + value = locations.get(wikid); + } + + if (value != null) { + + FileSystemWIkittyId localisation = (FileSystemWIkittyId) value; + + // create the wikitty with his id + Wikitty wikitty = new WikittyImpl(wikid); + // add label extension + wikitty.addExtension(WikittyLabelImpl.extensionWikittyLabel); + + // 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 + public WikittyEvent delete(String securityToken, Collection<String> ids) { + + try { + BidiMap location = harvestLocalWikitties(homeFile, true); + + for (String id : ids) { + + Object value = location.get(id); + + if (value == null) { + location = location.inverseBidiMap(); + value = location.get(id); + } + + if (value != null) { + + FileSystemWIkittyId localisation = (FileSystemWIkittyId) value; + + String path = localisation.getPath(); + String fileName = localisation.getFileName(); + + // load properties + PropertiesExtended propsProperties = getWikittyPublicationProperties(new File( + path), WikittyPublication.WIKITTY_FILE_META_PROPERTIES_FILE); + + // update + propsProperties.remove(fileName + + WikittyPublication.META_SUFFIX_KEY_CHECKSUM); + propsProperties.remove(fileName + + WikittyPublication.META_SUFFIX_KEY_ID); + propsProperties.remove(fileName + + WikittyPublication.META_SUFFIX_KEY_VERSION); + + // resave + propsProperties.store(); + + // load properties + PropertiesExtended idProperties = getWikittyPublicationProperties(new File( + path), WikittyPublication.WIKITTY_ID_PROPERTIES_FILE); + // update + idProperties.remove(id); + // resave + idProperties.store(); + + File wikittyFile = new File(path + File.separator + + fileName); + + if (wikittyFile.exists()) { + wikittyFile.delete(); + } + + } + } + } catch (Exception e) { + e.printStackTrace(); + // TODO mfortun-2011-04-13 really handle exception + } + + WikittyEvent result = new WikittyEvent(this); + return result; + } + + @Override + public List<PagedResult<String>> findAllByCriteria(String securityToken, + List<Criteria> criteria) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public List<String> findByCriteria(String securityToken, + List<Criteria> criteria) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public WikittyEvent deleteTree(String securityToken, String treeNodeId) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public TreeNodeResult<String> findTreeNode(String securityToken, + String wikittyId, int depth, boolean count, Criteria filter) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public Wikitty restoreVersion(String securityToken, String wikittyId, + String version) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // return null; + + } + + @Override + public void syncSearchEngine(String securityToken) { + // TODO mfortun-2011-04-05 + throw new UnsupportedOperationException("not yet implemented"); + // + + } + + /** + * Transform an object into a wikitty object in this case a File into a + * wikittyPubText/Data + * + * @param fileToTransform + * the objet to transform + * @param starts + * the home directory of the fileToTransform use to contruct the + * label + * @return the wikitty + * @throws Exception + */ + /* + * TODO mfortun-2011-04-07 correct the Exception's type + */ + static public Wikitty fileToWikitty(File fileToTransform, File starts) + throws Exception { + + String completeName = fileToTransform.getName(); + // isolate extension and file name + + String extension = FileUtil.extension(fileToTransform); + String name = FileUtil.basename(completeName, "." + extension); + // search for the mimetype + String mimeType = mimeTypeForExtension(extension); + + // prepare wikitty basics + Wikitty result = new WikittyImpl(); + result.addExtension(WikittyLabelImpl.extensionWikittyLabel); + + // creation of the label + /* + * TODO mfortun-2011-04-08 find a better way to do this ? + */ + String pathToFile = fileToTransform.getParent(); + String pathToStart = starts.getCanonicalPath(); + String startDirName = starts.getName(); + + /* + * remove path from root to start dir to have path from start to current + * working dir e.g.: if current file= + * /home/foo/bob/chaine.chaussette.tar.gz and starts dir = /home/foo/ + * then path will be = foo/bob + */ + 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 + * in the name e.g.: /home/truc.machin/bob + */ + + String label = path.replaceAll(File.separator, WIKITTYLABEL_SEPARATOR); + + WikittyLabelHelper.addLabels(result, label); + + // complete with the correct extension with content + if (isMimeWikittyPubText(mimeType)) { + result.addExtension(WikittyPubTextImpl.extensionWikittyPubText); + WikittyPubTextHelper.setName(result, name); + WikittyPubTextHelper.setMimeType(result, mimeType); + WikittyPubTextHelper.setContent(result, + FileUtil.readAsString(fileToTransform)); + } else { + result.addExtension(WikittyPubDataImpl.extensionWikittyPubData); + WikittyPubDataHelper.setName(result, name); + WikittyPubDataHelper.setMimeType(result, mimeType); + WikittyPubDataHelper.setContent(result, + FileUtil.fileToByte(fileToTransform)); + } + + return result; + + } + + /** + * Return the mime type for an extension name Extension + * + * @param ext + * @return + */ + static public String mimeTypeForExtension(String ext) { + /* + * TODO mfortun-2011-04-08 really implements this method create a double + * hashMap for this kind of key/value collection + */ + if (ext.equalsIgnoreCase("ws")) { + return "application/javascript"; + } else { + return "image/png"; + } + + } + + /** + * Return the extension for a mime type opposite to mimeTypeForExtension + * + * @param ext + * @return + */ + static public String extensionFormimeType(String mime) { + /* + * TODO mfortun-2011-04-11 really implements this method + */ + if (mime.equalsIgnoreCase("ws")) { + return "ws"; + } else { + return "ws"; + } + + } + + /** + * return if the mime type is associate to a wikittypubtext + * + * @param mime + * @return + */ + static public boolean isMimeWikittyPubText(String mime) { + /* + * TODO mfortun-2011-04-08 really implements this method + */ + if (mime.equalsIgnoreCase("application/javascript")) { + return true; + } else { + return false; + } + } + + /** + * Creates all the file system require from a label path in the working + * directory + * + * @param label + * the path string + * @return if all the path was created + * @throws Exception + */ + protected boolean createFilesFromLabelPath(String label) throws Exception { + + label = label.replace(".", File.separator); + label = label.replace(File.separator + File.separator, File.separator + + "."); + String[] pathElements = StringUtil.split(label, File.separator); + + boolean result = false; + + if (homeFile.exists() && homeFile.isDirectory()) { + String path = homeFile.getCanonicalPath(); + result = true; + for (int i = 0; i < pathElements.length; i++) { + + path = path + File.separator + pathElements[i]; + File temp = new File(path); + FileUtil.createDirectoryIfNecessary(temp); + result = result && temp.exists(); + } + } + + return result; + } + + /** + * + * @param starts + * @param recursivly + * @return + * @throws Exception + */ + static public BidiMap harvestLocalWikitties(File starts, boolean recursivly) + throws Exception { + BidiMap result = new DualHashBidiMap(); + + 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; + } + + static public PropertiesExtended getWikittyPublicationProperties(File starts, String name) + throws Exception { + + File propertieDirectory = new File(starts.getCanonicalPath() + + File.separator + WikittyPublication.PROPERTY_DIRECTORY); + + // load/create meta propertie file + File propertieFile = new File(propertieDirectory.getCanonicalPath() + + File.separator + + name); + if (!propertieFile.exists()) { + propertieFile.createNewFile(); + } + + PropertiesExtended result = new PropertiesExtended(propertieFile); + + return result; + } + + + + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL
participants (1)
-
mfortun@users.nuiton.org