Author: mfortun Date: 2011-04-07 17:53:15 +0200 (Thu, 07 Apr 2011) New Revision: 765 Url: http://nuiton.org/repositories/revision/wikitty/765 Log: Static before visibility, method for operation delete. Checkout and import complete with the basics, need to check error case. Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java 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-07 12:29:34 UTC (rev 764) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublication.java 2011-04-07 15:53:15 UTC (rev 765) @@ -4,51 +4,71 @@ import java.io.FileReader; import java.io.FileWriter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Properties; + 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.StringUtil; import org.nuiton.wikitty.WikittyProxy; import org.nuiton.wikitty.entities.Wikitty; import org.nuiton.wikitty.entities.WikittyLabel; 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; -///XXX a faire avec applicationConfig faire des trucs et des machins -/// objectif mercredi ça affiche les trucs depuis le repo -/// et ça lit en local aussi - public class WikittyPublication { /** to use log facility, just put in your code: log.info(\"...\"); */ final static private Log log = LogFactory.getLog(WikittyPublication.class); - protected static WikittyPublicationFileSystem wikittyServiceFileSystem; - protected static ApplicationConfig applicationConfig; - protected static WikittyProxy proxyWS; + static protected WikittyPublicationFileSystem wikittyServiceFileSystem; + static protected ApplicationConfig applicationConfig; + static protected WikittyProxy proxyWS; /* * static string for allias, wrong named attribut TODO mfortun-2011-04-06 * need to set better name */ - public static String WITTY_SERVICE_KEY = "wikitty.service.server.url"; - public static String NO_RECURSION_KEY = "norecursion"; - public static String DIRECTORY_KEY = "directory"; - public static String HESSIAN_PROTOCOL_KEY = "hessian"; + 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"; - public static String PROPERTY_DIRECTORY = ".wp"; - public static String WIKITTYPUBLICATION_PROPERTIES_FILE = "ws.properties"; + 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"; + static public String META_SUB_LABEL = "sub.label"; + /** * @param args * @throws ArgumentsParserException */ - public static void main(String[] args) throws Exception { + static public void main(String[] args) throws Exception { /* * @@ -132,7 +152,7 @@ /** * Method that import the content of a directory into a wikitty service */ - public static void importToWikitty() throws Exception { + static public void importToWikitty() throws Exception { boolean noRecur = applicationConfig .getOptionAsBoolean(NO_RECURSION_KEY); @@ -167,7 +187,7 @@ * TODO mfortun-2011-04-05 just prototyping, remove when really implements * the method linked: importToWikitty */ - public static void printDirectory(File dir, boolean recur) { + static public void printDirectory(File dir, boolean recur) { System.out.println("<dir " + dir.getName() + ">"); for (File child : dir.listFiles()) { @@ -191,7 +211,7 @@ * boolean id the directory have to be harvest * @return list of harvested file */ - protected static List<File> listFile(File starts, boolean recursivly) { + static protected List<File> listFile(File starts, boolean recursivly) { ArrayList<File> result = new ArrayList<File>(); result.add(starts); for (File child : starts.listFiles()) { @@ -210,7 +230,7 @@ * Method that checkout a label recursivly or not into a local directory * from a wikitty service */ - public static void checkoutFromWikitty(String label) throws Exception { + static public void checkoutFromWikitty(String label) throws Exception { boolean noRecur = applicationConfig .getOptionAsBoolean(NO_RECURSION_KEY); @@ -296,7 +316,7 @@ /** * Relocate the default url of the wikitty service */ - public static void relocateWikitty() throws Exception { + static public void relocateWikitty() throws Exception { /* * log.info("checkout : wikittyservice: " + wikittyService + @@ -349,12 +369,6 @@ props.store(new FileWriter(propertiesFile), ""); /* - * Properties props=new Properties(); props.load(new FileReader(new - * File("/home/Manou/temp.properties"))); - * applicationConfig.setOptions(props); - */ - - /* * 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 @@ -365,7 +379,7 @@ /** * commit the current wikittyworkspace into a wikitty service */ - public static void commitToWikitty() throws Exception { + static public void commitToWikitty() throws Exception { File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); @@ -451,7 +465,7 @@ * * @throws Exception */ - public static void deleteFromWikitty(File toDelete) throws Exception { + static public void deleteFromWikitty(File toDelete) throws Exception { // check args if (null == toDelete || !toDelete.exists()) { @@ -518,7 +532,7 @@ /** * update the current workspace from a wikitty */ - public static void updateFromWikitty() throws Exception { + static public void updateFromWikitty() throws Exception { File dir = applicationConfig.getOptionAsFile(DIRECTORY_KEY); @@ -583,7 +597,7 @@ * @return File the directory container of the master property file * @throws Exception */ - protected static File searchWikittyPublicationHomeDir(File start) + static protected File searchWikittyPublicationHomeDir(File start) throws Exception { if (start != null && start.exists() && start.isDirectory()) { @@ -610,4 +624,69 @@ } } + static protected HashMap<String, String> deleteFile(File toDelete) + throws Exception { + HashMap<String, String> result = new HashMap<String, String>(); + + String pathToProperty = toDelete.getCanonicalFile() + File.separator + + PROPERTY_DIRECTORY + File.separator; + // load properties files + File metaFile = new File(pathToProperty + + WIKITTY_FILE_META_PROPERTIES_FILE); + Properties metaProperties = new Properties(); + metaProperties.load(new FileReader(metaFile)); + + File idFile = new File(pathToProperty + WIKITTY_ID_PROPERTIES_FILE); + Properties idProperties = new Properties(); + idProperties.load(new FileReader(idFile)); + + if (toDelete.isDirectory()) { + for (File child : toDelete.listFiles()) { + if (child.isDirectory() + && !child.getName().equals(PROPERTY_DIRECTORY)) { + result.putAll(deleteFile(child)); + } + } + // remove label from current label list + /* + * TODO mfortun-2011-04-07 it's ugly change this, if we keep the + * property linked + */ + String[] currents = StringUtil.split( + metaProperties.getProperty(META_SUB_LABEL), ","); + ArrayList<String> subLabel = new ArrayList<String>(); + for (String sub : currents) { + if (!sub.endsWith(toDelete.getName())) { + subLabel.add(sub); + } + } + String subLabelResult = StringUtil.join(subLabel, ",", true); + metaProperties.put(META_SUB_LABEL, subLabelResult); + + } else { + + String id = metaProperties.getProperty(toDelete.getName() + + DEFAULT_PROPERTY_NAME_SEP + META_SUFFIX_KEY_ID); + String label = metaProperties.getProperty(META_CURRENT_LABEL); + + result.put(id, label); + // remove from properties + idProperties.remove(id); + + metaProperties.remove(toDelete.getName() + + DEFAULT_PROPERTY_NAME_SEP + META_SUFFIX_KEY_ID); + metaProperties.remove(toDelete.getName() + + DEFAULT_PROPERTY_NAME_SEP + META_SUFFIX_KEY_CHECKSUM); + metaProperties.remove(toDelete.getName() + + DEFAULT_PROPERTY_NAME_SEP + META_SUFFIX_KEY_VERSION); + } + + metaProperties.store(new FileWriter(metaFile), ""); + idProperties.store(new FileWriter(idFile), ""); + + toDelete.delete(); + return result; + + } + }