Author: mfortun Date: 2011-07-21 10:38:06 +0200 (Thu, 21 Jul 2011) New Revision: 1082 Url: http://nuiton.org/repositories/revision/wikitty/1082 Log: * remove wikitty struts from publication * organize packages Added: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/AbstractWikittyFileService.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/MimeTypePubHelper.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/PropertiesExtended.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyFileUtil.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LoginInterceptor.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LogoutInterceptor.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationSynchronize.java trunk/wikitty-publication/src/main/resources/wikitty-publication-ws-jar.properties Removed: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LoginInterceptor.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LogoutInterceptor.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.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/WikittyFileUtil.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java Modified: trunk/wikitty-publication/pom.xml trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationConfig.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyPublicationExternalize.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java trunk/wikitty-publication/src/main/resources/struts.xml Modified: trunk/wikitty-publication/pom.xml =================================================================== --- trunk/wikitty-publication/pom.xml 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/pom.xml 2011-07-21 08:38:06 UTC (rev 1082) @@ -37,13 +37,6 @@ </dependency> <dependency> - <groupId>org.nuiton.wikitty</groupId> - <artifactId>wikitty-struts</artifactId> - <version>${project.version}</version> - <scope>runtime</scope> - </dependency> - - <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> @@ -113,7 +106,7 @@ <groupId>org.apache.struts.xwork</groupId> <artifactId>xwork-core</artifactId> </dependency> - + </dependencies> <!-- ************************************************************* --> Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/AbstractWikittyFileService.java (from rev 1078, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/AbstractWikittyFileService.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/AbstractWikittyFileService.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,541 @@ +package org.nuiton.wikitty.publication; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.wikitty.WikittyException; +import org.nuiton.wikitty.WikittyService; +import org.nuiton.wikitty.WikittyUtil; +import org.nuiton.wikitty.entities.FieldType; +import org.nuiton.wikitty.entities.FieldType.TYPE; +import org.nuiton.wikitty.entities.Wikitty; +import org.nuiton.wikitty.search.Criteria; +import org.nuiton.wikitty.search.PagedResult; +import org.nuiton.wikitty.search.operators.And; +import org.nuiton.wikitty.search.operators.AssociatedRestriction; +import org.nuiton.wikitty.search.operators.Between; +import org.nuiton.wikitty.search.operators.BinaryOperator; +import org.nuiton.wikitty.search.operators.Contains; +import org.nuiton.wikitty.search.operators.Element; +import org.nuiton.wikitty.search.operators.False; +import org.nuiton.wikitty.search.operators.In; +import org.nuiton.wikitty.search.operators.Keyword; +import org.nuiton.wikitty.search.operators.Not; +import org.nuiton.wikitty.search.operators.Null; +import org.nuiton.wikitty.search.operators.Or; +import org.nuiton.wikitty.search.operators.Restriction; +import org.nuiton.wikitty.search.operators.RestrictionName; +import org.nuiton.wikitty.search.operators.True; + + +/** + * This service must be extends by wikitty service that are over file system + * directly or this kind of storage for wikitty. + * + * + * + * @author mfortun + * + */ +public abstract class AbstractWikittyFileService implements WikittyService{ + + + final static Log log = LogFactory.getLog(AbstractWikittyFileService.class); + + public AbstractWikittyFileService() { + super(); + } + + /** + * Method that must be implemented by the extended service. This must return + * all the wikitty stored. The result will be use to checkrestriction for + * a find. + * @return a Map with wikittyId as Key and wikitty as value + */ + protected abstract Map<String, Wikitty> getAllWikitties(); + + + + /** + * Write by jcouteau, used to check if a wikitty check a restriction + * + * @see org.nuiton.wikitty.storage.WikittySearchEngineInMemory#checkRestriction + * + * @param restriction + * the restriction + * @param w + * the wikitty to check + * @return if the wikitty check the restriction + */ + public boolean checkRestriction(Restriction restriction, Wikitty w) { + + + if (restriction instanceof BinaryOperator) { + BinaryOperator binOp = (BinaryOperator) restriction; + + String fqfieldName = binOp.getElement().getName(); + + // Checks on extensions + if (Element.ELT_EXTENSION.equals(fqfieldName)) { + boolean checked = false; + + switch (restriction.getName()) { + case NOT_EQUALS: + checked = !w.getExtensionNames().contains(binOp.getValue()); + break; + case EQUALS: + checked = w.getExtensionNames().contains(binOp.getValue()); + break; + } + + return checked; + + // Checks on id + } else if (Element.ELT_ID.equals(fqfieldName)) { + + boolean checked = false; + + switch (restriction.getName()) { + case NOT_EQUALS: + checked = !w.getId().equals(binOp.getValue()); + break; + case EQUALS: + checked = w.getId().equals(binOp.getValue()); + break; + } + + return checked; + } + + // si les wikitty n'ont meme pas l'extension concerné + // Le check restriction, ne doit pas tester les champs + // si les wikitty n'ont meme pas l'extension concerné + String[] extName = fqfieldName.split("\\."); + if (!w.hasField(extName[0], extName[1])) { + + // return true in case of not equals + if (RestrictionName.NOT_EQUALS == restriction.getName()) { + return true; + } + + return false; + } + // recupere la valeur dans le wikitty + Object o = w.getFqField(fqfieldName); + + // recupere le type de la valeur + FieldType t = w.getFieldType(fqfieldName); + // convertie la valeur a verifier dans le meme type que la valeur + // du wikitty + Object value = binOp.getValue(); + if (!(value instanceof Collection) && t.isCollection()) { + // on doit encapsuler dans une collection, car la creation + // de la requete ajoute autant de v == o && ... que de valeurs + // dans la collection (champs multi-value solr). Mais + // dans le inmemory on doit retrouve des collections et non pas + // des objets seuls :( + value = Collections.singleton(value); + } + value = t.getValidValue(value); + + boolean checked = false; + + switch (restriction.getName()) { + case EQUALS: + + if (value instanceof String && o instanceof String) { + String pattern = (String) value; + pattern = pattern.replace("*", "\\p{ASCII}*"); + pattern = pattern.replace("?", "\\p{ASCII}"); + + Pattern p = Pattern.compile(pattern); + Matcher m = p.matcher((String) o); + checked = m.matches(); + } else { + checked = value.equals(o); + } + break; + case LESS: + checked = ((Comparable) o).compareTo(value) < 0; + break; + case LESS_OR_EQUAL: + checked = ((Comparable) o).compareTo(value) <= 0; + break; + case GREATER: + checked = ((Comparable) o).compareTo(value) > 0; + break; + case GREATER_OR_EQUAL: + checked = ((Comparable) o).compareTo(value) >= 0; + break; + case NOT_EQUALS: + checked = !value.equals(o); + break; + case ENDS_WITH: + if (t.getType() != TYPE.STRING) { + throw new WikittyException( + "Can't search for contents that 'ends with' on attribute type different of String. " + + "Attribute " + + fqfieldName + + " is " + + t.getType().name()); + } + checked = ((String) o).endsWith((String) value); + break; + case STARTS_WITH: + if (t.getType() != TYPE.STRING) { + throw new WikittyException( + "Can't search for contents that 'starts with' on attribute type different of String. " + + "Attribute " + + fqfieldName + + " is " + + t.getType().name()); + } + + // FIXME mfortun-2011-04-20 rustine pour champs multivalué de + // type string + // et restriction startwith dessus. ça marche mais faudrait + // quelque chose de plus + // propre, et surtout généraliser pour toutes les restrictions + + if (o instanceof Collection<?> + && value instanceof Collection<?>) { + + for (Object val : (Collection) value) { + + String valu = (String) val; + + for (Object oo : (Collection) o) { + String cotainedO = (String) oo; + if (cotainedO != null) { + checked = checked || cotainedO.startsWith(valu); + } + } + + } + + } else { + + checked = ((String) o).startsWith((String) value); + } + break; + } + return checked; + } else if (restriction instanceof Null) { + Null nullRes = (Null) restriction; + + String fqfieldName = nullRes.getFieldName(); + + // check my wikitty got the right extension before doing anything. + String[] extName = fqfieldName.split("\\."); + if (!w.hasField(extName[0], extName[1])) { + return false; + } + // get the value in the wikitty + Object o = w.getFqField(fqfieldName); + + // No null on extensions, always return false + if (fqfieldName.equals(Element.ELT_EXTENSION)) { + return false; + } + + // No null on ids, always return false + if (fqfieldName.equals(Element.ELT_ID)) { + return false; + } + + boolean checked = false; + + switch (nullRes.getName()) { + case IS_NULL: + checked = (o == null); + break; + case IS_NOT_NULL: + checked = (o != null); + break; + } + + return checked; + + } else if (restriction instanceof In) { + In in = (In) restriction; + String fqfieldName = in.getElement().getName(); + String testedValue = String.valueOf(w.getFqField(fqfieldName)); + for (String value : in.getValue()) { + if (testedValue.equals(value)) { + return true; + } + } + + return false; + + } else if (restriction instanceof True) { + return true; + } else if (restriction instanceof False) { + return false; + } else if (restriction instanceof Contains) { + Contains contains = (Contains) restriction; + + String fqfieldName = contains.getElement().getName(); + List<String> values = contains.getValue(); + + String extension = WikittyUtil + .getExtensionNameFromFQFieldName(fqfieldName); + String fieldName = WikittyUtil + .getFieldNameFromFQFieldName(fqfieldName); + + if (!w.hasField(extension, fieldName)) { + return false; + } + + // Get field as string and then split it to take into account not + // multivalued fields. + String testedValuesAsString = w.getFieldAsString(extension, + fieldName); + + if ('[' == testedValuesAsString.charAt(0)) { + testedValuesAsString = testedValuesAsString.substring(1, + testedValuesAsString.length()); + } + + List<String> testedValues = Arrays.asList(testedValuesAsString + .split(",")); + + for (Object value : values) { + if (!testedValues.contains(String.valueOf(value))) { + return false; + } + } + + return true; + + } else if (restriction instanceof And) { + And and = (And) restriction; + for (Restriction sub : and.getRestrictions()) { + if (!checkRestriction(sub, w)) { + return false; + } + } + return true; + } else if (restriction instanceof Or) { + Or or = (Or) restriction; + for (Restriction sub : or.getRestrictions()) { + if (checkRestriction(sub, w)) { + return true; + } + } + return false; + } else if (restriction instanceof Keyword) { + Keyword keyword = (Keyword) restriction; + + String value = keyword.getValue(); + + //TODO mfortun-2011-07-06 hack to ensure that * is intepreted as + // the real meaning aka any + if (value.equals("*")){ + return true; + } + + for (String fieldName : w.getAllFieldNames()) { + String testedValue = String.valueOf(w.getFqField(fieldName)); + if (testedValue.contains(value)) { + return true; + } + } + return false; + } else if (restriction instanceof Not) { + Not or = (Not) restriction; + Restriction sub = or.getRestriction(); + return !checkRestriction(sub, w); + } else if (restriction instanceof AssociatedRestriction) { + + AssociatedRestriction ass = (AssociatedRestriction) restriction; + + String fqfieldName = ass.getElement().getName(); + + // check my wikitty got the right extension before doing anything. + String[] extName = fqfieldName.split("\\."); + if (!w.hasField(extName[0], extName[1])) { + return false; + } + // get the value in the wikitty, it is a wikitty's id + Object o = w.getFqField(fqfieldName); + + // Get sub-restriction + Restriction sub = ass.getRestriction(); + + Criteria associatedSearch = new Criteria(); + associatedSearch.setRestriction(sub); + + // find everything that validate the sub-restriction + + List<Criteria> dummyList = new ArrayList<Criteria>(); + dummyList.add(associatedSearch); + // same as proxy for "fix" unique param to list param + PagedResult<String> associatedResult = findAllByCriteria("", + dummyList).get(0); + + List<String> associatedList = associatedResult.getAll(); + + // Check that my field is contained in the sub-restriction results. + return associatedList.contains(String.valueOf(o)); + } else if (restriction instanceof Between) { + + Between op = (Between) restriction; + + Object max = op.getMax(); + Object min = op.getMin(); + + // No between on extensions, always return false + if (op.getElement().getName().equals(Element.ELT_EXTENSION)) { + return false; + } + + // No between on ids, always return false + if (op.getElement().getName().equals(Element.ELT_ID)) { + return false; + } + + String fqfieldName = op.getElement().getName(); + + // si les wikitty n'ont meme pas l'extension concerné + // Le check restriction, ne doit pas tester les champs + // si les wikitty n'ont meme pas l'extension concerné + String[] extName = fqfieldName.split("\\."); + if (!w.hasField(extName[0], extName[1])) { + return false; + } + + // recupere la valeur dans le wikitty + Object o = w.getFqField(fqfieldName); + + // recupere le type de la valeur + FieldType t = w.getFieldType(fqfieldName); + + if (!(min instanceof Collection) && t.isCollection()) { + // on doit encapsuler dans une collection, car la creation + // de la requete ajoute autant de v == o && ... que de valeurs + // dans la collection (champs multi-value solr). Mais + // dans le inmemory on doit retrouve des collections et non pas + // des objets seuls :( + min = Collections.singleton(min); + } + min = t.getValidValue(min); + + if (!(max instanceof Collection) && t.isCollection()) { + // on doit encapsuler dans une collection, car la creation + // de la requete ajoute autant de v == o && ... que de valeurs + // dans la collection (champs multi-value solr). Mais + // dans le inmemory on doit retrouve des collections et non pas + // des objets seuls :( + max = Collections.singleton(max); + } + max = t.getValidValue(max); + + return ((Comparable) o).compareTo(min) >= 0 + && ((Comparable) o).compareTo(max) <= 0; + } else { + throw new UnsupportedOperationException(restriction.getName() + + " Search Not yet implemented"); + } + } + + + @Override + public List<PagedResult<String>> findAllByCriteria(String securityToken, + List<Criteria> criteria) { + + + List<PagedResult<String>> result = new ArrayList<PagedResult<String>>(); + + Map<String, Wikitty> wikitties = getAllWikitties(); + + // for each criteria + for (Criteria cr : criteria) { + // prepare restriction on result + int firstIndex = cr.getFirstIndex(); + int endIndex = cr.getEndIndex(); + List<String> ids = new LinkedList<String>(); + int currentIndex = 0; + Restriction restriction = cr.getRestriction(); + // for each wikitty check if it match the resttriction + for (Entry<String, Wikitty> entry : wikitties.entrySet()) { + String id = entry.getKey(); + Wikitty w = entry.getValue(); + // if macth + + + if (checkRestriction(restriction, w)) { + + // increment result number + currentIndex++; + if (currentIndex > firstIndex) { + ids.add(id); + } + // if the number of wikitty found is match + // stop the search for other wikitty + if (endIndex >= 0 && currentIndex >= endIndex) { + break; + } + } + } + result.add(new PagedResult<String>(firstIndex, ids.size(), + restriction.toString(), null, ids)); + + } + return result; + } + + + @Override + public List<String> findByCriteria(String securityToken, + List<Criteria> criteria) { + List<String> result = new ArrayList<String>(); + + Map<String, Wikitty> wikitties = getAllWikitties(); + // for each criteria + for (Criteria cr : criteria) { + // prepare restriction on result + int firstIndex = cr.getFirstIndex(); + int endIndex = cr.getEndIndex(); + List<String> ids = new LinkedList<String>(); + int currentIndex = 0; + Restriction restriction = cr.getRestriction(); + // for each wikitty check if it match the resttriction + for (Entry<String, Wikitty> entry : wikitties.entrySet()) { + String id = entry.getKey(); + Wikitty w = entry.getValue(); + // if macth + + log.debug("Check restriction for wikitty: " + w + + " Restriction:" + restriction); + + if (checkRestriction(restriction, w)) { + // increment result number + currentIndex++; + if (currentIndex > firstIndex) { + ids.add(id); + } + // if the number of wikitty found is match + // stop the search for other wikitty + if (endIndex >= 0 && currentIndex >= endIndex) { + break; + } + } + } + result.addAll(ids); + } + return result; + } + + +} \ No newline at end of file Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/AbstractWikittyFileService.java ___________________________________________________________________ Added: svn:mime-type + text/plain Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LoginInterceptor.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LoginInterceptor.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LoginInterceptor.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,80 +0,0 @@ -/* - * #%L - * bow - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2010 - 2011 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.nuiton.wikitty.publication; - -import java.util.Map; -import javax.servlet.http.HttpServletRequest; -import org.apache.struts2.ServletActionContext; -import org.nuiton.wikitty.entities.WikittyUser; - - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.interceptor.AbstractInterceptor; - -/** - * Interceptor used to redirect a non-logged user if he tries to access a page - * where logging is mandatory - */ -public class LoginInterceptor extends AbstractInterceptor { - private static final long serialVersionUID = -7520186185205372272L; - - protected String error; - - public String getError() { - return error; - } - - public void setError(String error) { - this.error = error; - } - - @Override - public String intercept(ActionInvocation invocation) throws Exception { - - Map<String, Object> session = ActionContext.getContext().getSession(); - - WikittyPublicationSession pubSession = WikittyPublicationSession - .getWikittyPublicationSession(session); - WikittyUser user = pubSession.getUser(); - String result = null; - - HttpServletRequest request = ServletActionContext.getRequest(); - - - // Construct redirect url. - String redirect = request.getContextPath() + error ; - redirect += "?success="+request.getServletPath(); - - // If the user isn't logged in - if (user == null) { - ServletActionContext.getResponse().sendRedirect(redirect); - } else { - result = invocation.invoke(); - } - - return result; - } - -} Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LogoutInterceptor.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LogoutInterceptor.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LogoutInterceptor.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,51 +0,0 @@ -/* - * #%L - * bow - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2010 - 2011 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.nuiton.wikitty.publication; - -import java.util.Map; - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.interceptor.AbstractInterceptor; - -/** - * Interceptor used to remove all trace of user in session. Used for login page - */ -public class LogoutInterceptor extends AbstractInterceptor { - - /** - * - */ - private static final long serialVersionUID = -66045004020326043L; - - - - @Override - public String intercept(ActionInvocation invocation) throws Exception { - Map<String, Object> session = ActionContext.getContext().getSession(); - WikittyPublicationSession.invalidate(session); - String result = invocation.invoke(); - return result; - } -} Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/MimeTypePubHelper.java (from rev 1078, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/MimeTypePubHelper.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/MimeTypePubHelper.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,147 @@ +/* + * #%L + * Wikitty :: publication + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin + * %% + * 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; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Class used to determine mimetype for an extension, used to determine type of + * wikittypub (data or text) with the extension and the mime type + * + * + * @author mfortun + * + */ +public class MimeTypePubHelper { + + /** + * Mapping between extention and mime type, key: extension, value: mimeType + */ + protected Map<String, String> mapExtensionMime; + + /** + * White list of mime type that have to be transform as a wikittyPubText + */ + protected List<String> mimePubText; + + /** + * The default mime type + */ + public static String DEFAULT_MIME_TYPE = "application/octet-stream"; + + public MimeTypePubHelper() { + this.mapExtensionMime = new HashMap<String, String>(); + this.mimePubText = new ArrayList<String>(); + + // TODO create a property file to store and handle mimetype for pub + // text + mimePubText.add("application/javascript"); + + mapExtensionMime.put("wp", "application/javascript"); + mapExtensionMime.put("js", "application/javascript"); + mapExtensionMime.put("jpg", "image/jpeg"); + mapExtensionMime.put("png", "image/png"); + } + + public Map<String, String> getMapExtensionMime() { + return mapExtensionMime; + } + + public void setMapExtensionMime(Map<String, String> mapExtensionMime) { + this.mapExtensionMime = mapExtensionMime; + } + + public List<String> getMimePubText() { + return mimePubText; + } + + public void setMimePubText(List<String> mimePubText) { + this.mimePubText = mimePubText; + } + + /** + * Return the corresponding mime Type for an extension, default if not + * recognized + * + * @param ext + * the extension + * @return the corresponding mimeTypeForExt DEFAULT_MIME_TYPE if ext not + * recognized + */ + public String getMimeForExtension(String ext) { + String result = mapExtensionMime.get(ext); + + if (result == null) { + result = DEFAULT_MIME_TYPE; + } + + return result; + } + + /** + * Used to check if a file have to be converted as a wikittyPubText + * + * @param mimeType + * of the file + * @return if the mimetype of a file correspond to a wikittyPubText + */ + public boolean isPubTextMime(String mimeType) { + return mimePubText.contains(mimeType); + } + + /** + * used to check if a file have to be converted as a wikittyPubText + * with his extension similar to : + * <br>isPubTextMime(getMimeForExtension(extension))</br> + * @param extension the file extension + * @return if the extension correspond to a wikittyPubText + */ + public boolean isPubTextExtension(String extension) { + return isPubTextMime(getMimeForExtension(extension)); + } + + /** + * Add a mimeType corresponding to a WikittyPubText + * + * @param mime + */ + public void addMimeForPubText(String mime) { + mimePubText.add(mime); + } + + /** + * Add an entry in the map that store mapping between extension and + * mimeType. + * + * @param extension + * @param mime + */ + public void addExtensionMime(String extension, String mime) { + mapExtensionMime.put(extension, mime); + } +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/MimeTypePubHelper.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision HeadURL Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/PropertiesExtended.java (from rev 1076, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/PropertiesExtended.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/PropertiesExtended.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,115 @@ +/* + * #%L + * Wikitty :: publication + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin + * %% + * 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; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; + +/** + * Class usefull when load properties file, update, delete, then save again File + * used to load properties is store, and this allow to save again the properties + * inside the file used to load them. + * + * @author mfortun + * + */ +public class PropertiesExtended extends Properties { + + /** + * + */ + private static final long serialVersionUID = -264337198024996529L; + + /** + * The original file used to load and create properties + */ + protected File origin; + + + public File getOrigin() { + return origin; + } + + public void setOrigin(File origin) { + this.origin = origin; + } + + + + /** + * Default constructor, need a file from whom load the properties. This is + * equivalent of a basic creation of properties and load after property from + * a file. + * + * @param origin + * the file from whom load the property, it is save for store. + * @throws IOException + * if error while reading the file + * @throws FileNotFoundException + * if file not found + */ + public PropertiesExtended(File origin) throws FileNotFoundException, + IOException { + super(); + this.load(origin); + + } + + + /** + * Load the property from the file, and store the file, for storage. + * + * @param file + * the file from whom to load the property, it replace the file + * use to create this class + * @throws IOException + * if error while reading the file + * @throws FileNotFoundException + * if file not found + */ + public void load(File file) throws FileNotFoundException, IOException { + this.origin = file; + this.load(new FileReader(origin)); + } + + /** + * Store the properties inside the last file used to load the property (or + * by default file used to create the class), equivalent of store(new + * FileWriter('File'),""); + * + * @throws IOException + * if error while reading the file + * @throws FileNotFoundException + * if file not found + */ + public void store() throws IOException { + this.store(new FileWriter(origin), ""); + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/PropertiesExtended.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision HeadURL Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyFileUtil.java (from rev 1078, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyFileUtil.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyFileUtil.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyFileUtil.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,93 @@ +package org.nuiton.wikitty.publication; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.FileUtil; +import org.nuiton.util.StringUtil; + +/** + * + * Class that containt utils method when handle wikitty store as file. + * + * @author mfortun + * + */ +public class WikittyFileUtil { + + /** + * The file name of the meta property file + */ + static public String WIKITTY_FILE_META_PROPERTIES_FILE = "meta.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() + */ + /** + * The file Name of the id property file + */ + static public String WIKITTY_ID_PROPERTIES_FILE = "ids.properties"; + + final static Log log = LogFactory.getLog(WikittyFileUtil.class); + + /** + * Construct correctly the path from a label + * + * @param label + * the label + * @return the correct path + */ + public static String labelToPath(String label) { + + String result = label; + + result = result.replace(".", File.separator); + + // correct the pb with directory name begin by . + result = result.replace(File.separator + File.separator, File.separator + + "."); + + log.info("Convert label to path: " + label + " path:" + result); + + return result; + } + + /** + * 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 IOException + */ + public static boolean createFilesFromLabelPath(File homeFile, String label) + throws IOException { + + label = labelToPath(label); + + log.info("Create directory from path:" + label); + + 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; + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyFileUtil.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationConfig.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationConfig.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/WikittyPublicationConfig.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -54,7 +54,7 @@ public enum Option implements ApplicationConfig.OptionDef { CONFIG_FILE(ApplicationConfig.CONFIG_FILE_NAME, _("wikitty-publication.config.configFileName.description"), - "wikitty-publication-ws-jar.properties", String.class, false, + "wikitty-publication-ws-default.properties", String.class, false, false); Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyPublicationExternalize.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyPublicationExternalize.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyPublicationExternalize.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -15,15 +15,15 @@ import org.nuiton.wikitty.entities.Wikitty; import org.nuiton.wikitty.entities.WikittyLabel; import org.nuiton.wikitty.entities.WikittyLabelHelper; +import org.nuiton.wikitty.publication.PropertiesExtended; +import org.nuiton.wikitty.publication.WikittyFileUtil; import org.nuiton.wikitty.publication.WikittyPublicationConstant; import org.nuiton.wikitty.publication.entities.WikittyPubData; import org.nuiton.wikitty.publication.entities.WikittyPubDataHelper; 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.publication.synchro.PropertiesExtended; -import org.nuiton.wikitty.publication.synchro.WikittyFileUtil; -import org.nuiton.wikitty.publication.synchro.WikittyPublication; +import org.nuiton.wikitty.publication.synchro.WikittyPublicationSynchronize; import org.nuiton.wikitty.publication.synchro.WikittyPublicationFileSystem; import org.nuiton.wikitty.search.Criteria; import org.nuiton.wikitty.search.PagedResult; @@ -71,7 +71,7 @@ String Label = currentFile.getName(); // String urlFileSystem = "file:///home/User/testWP#wp"; - String urlFileSystem = currentFile.getParent() + WikittyPublication.LABEL_DELIM + Label; + String urlFileSystem = currentFile.getParent() + WikittyPublicationSynchronize.LABEL_DELIM + Label; appconfig.setOption(WikittyConfigOption.WIKITTY_SERVER_URL.getKey(), urlFileSystem); Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/externalize/WikittyServiceJarLoader.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -18,6 +18,9 @@ import org.nuiton.wikitty.entities.WikittyLabel; import org.nuiton.wikitty.entities.WikittyLabelHelper; import org.nuiton.wikitty.entities.WikittyLabelImpl; +import org.nuiton.wikitty.publication.AbstractWikittyFileService; +import org.nuiton.wikitty.publication.MimeTypePubHelper; +import org.nuiton.wikitty.publication.WikittyFileUtil; import org.nuiton.wikitty.publication.entities.WikittyPubData; import org.nuiton.wikitty.publication.entities.WikittyPubDataHelper; import org.nuiton.wikitty.publication.entities.WikittyPubDataImpl; @@ -27,9 +30,6 @@ import org.nuiton.wikitty.publication.entities.WikittyPubTextCompiledImpl; import org.nuiton.wikitty.publication.entities.WikittyPubTextHelper; import org.nuiton.wikitty.publication.entities.WikittyPubTextImpl; -import org.nuiton.wikitty.publication.synchro.AbstractWikittyFileService; -import org.nuiton.wikitty.publication.synchro.MimeTypePubHelper; -import org.nuiton.wikitty.publication.synchro.WikittyFileUtil; import org.nuiton.wikitty.publication.synchro.WikittyPublicationFileSystem; import org.nuiton.wikitty.search.Criteria; import org.nuiton.wikitty.search.TreeNodeResult; Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LoginInterceptor.java (from rev 1076, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LoginInterceptor.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LoginInterceptor.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LoginInterceptor.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,81 @@ +/* + * #%L + * bow + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.nuiton.wikitty.publication.interceptor; + +import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import org.apache.struts2.ServletActionContext; +import org.nuiton.wikitty.entities.WikittyUser; +import org.nuiton.wikitty.publication.WikittyPublicationSession; + + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.interceptor.AbstractInterceptor; + +/** + * Interceptor used to redirect a non-logged user if he tries to access a page + * where logging is mandatory + */ +public class LoginInterceptor extends AbstractInterceptor { + private static final long serialVersionUID = -7520186185205372272L; + + protected String error; + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + @Override + public String intercept(ActionInvocation invocation) throws Exception { + + Map<String, Object> session = ActionContext.getContext().getSession(); + + WikittyPublicationSession pubSession = WikittyPublicationSession + .getWikittyPublicationSession(session); + WikittyUser user = pubSession.getUser(); + String result = null; + + HttpServletRequest request = ServletActionContext.getRequest(); + + + // Construct redirect url. + String redirect = request.getContextPath() + error ; + redirect += "?success="+request.getServletPath(); + + // If the user isn't logged in + if (user == null) { + ServletActionContext.getResponse().sendRedirect(redirect); + } else { + result = invocation.invoke(); + } + + return result; + } + +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LoginInterceptor.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision HeadURL Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LogoutInterceptor.java (from rev 1076, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/LogoutInterceptor.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LogoutInterceptor.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LogoutInterceptor.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,53 @@ +/* + * #%L + * bow + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 - 2011 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.nuiton.wikitty.publication.interceptor; + +import java.util.Map; + +import org.nuiton.wikitty.publication.WikittyPublicationSession; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.interceptor.AbstractInterceptor; + +/** + * Interceptor used to remove all trace of user in session. Used for login page + */ +public class LogoutInterceptor extends AbstractInterceptor { + + /** + * + */ + private static final long serialVersionUID = -66045004020326043L; + + + + @Override + public String intercept(ActionInvocation invocation) throws Exception { + Map<String, Object> session = ActionContext.getContext().getSession(); + WikittyPublicationSession.invalidate(session); + String result = invocation.invoke(); + return result; + } +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/interceptor/LogoutInterceptor.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision HeadURL Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/AbstractWikittyFileService.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,541 +0,0 @@ -package org.nuiton.wikitty.publication.synchro; - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.wikitty.WikittyException; -import org.nuiton.wikitty.WikittyService; -import org.nuiton.wikitty.WikittyUtil; -import org.nuiton.wikitty.entities.FieldType; -import org.nuiton.wikitty.entities.FieldType.TYPE; -import org.nuiton.wikitty.entities.Wikitty; -import org.nuiton.wikitty.search.Criteria; -import org.nuiton.wikitty.search.PagedResult; -import org.nuiton.wikitty.search.operators.And; -import org.nuiton.wikitty.search.operators.AssociatedRestriction; -import org.nuiton.wikitty.search.operators.Between; -import org.nuiton.wikitty.search.operators.BinaryOperator; -import org.nuiton.wikitty.search.operators.Contains; -import org.nuiton.wikitty.search.operators.Element; -import org.nuiton.wikitty.search.operators.False; -import org.nuiton.wikitty.search.operators.In; -import org.nuiton.wikitty.search.operators.Keyword; -import org.nuiton.wikitty.search.operators.Not; -import org.nuiton.wikitty.search.operators.Null; -import org.nuiton.wikitty.search.operators.Or; -import org.nuiton.wikitty.search.operators.Restriction; -import org.nuiton.wikitty.search.operators.RestrictionName; -import org.nuiton.wikitty.search.operators.True; - - -/** - * This service must be extends by wikitty service that are over file system - * directly or this kind of storage for wikitty. - * - * - * - * @author mfortun - * - */ -public abstract class AbstractWikittyFileService implements WikittyService{ - - - final static Log log = LogFactory.getLog(AbstractWikittyFileService.class); - - public AbstractWikittyFileService() { - super(); - } - - /** - * Method that must be implemented by the extended service. This must return - * all the wikitty stored. The result will be use to checkrestriction for - * a find. - * @return a Map with wikittyId as Key and wikitty as value - */ - protected abstract Map<String, Wikitty> getAllWikitties(); - - - - /** - * Write by jcouteau, used to check if a wikitty check a restriction - * - * @see org.nuiton.wikitty.storage.WikittySearchEngineInMemory#checkRestriction - * - * @param restriction - * the restriction - * @param w - * the wikitty to check - * @return if the wikitty check the restriction - */ - public boolean checkRestriction(Restriction restriction, Wikitty w) { - - - if (restriction instanceof BinaryOperator) { - BinaryOperator binOp = (BinaryOperator) restriction; - - String fqfieldName = binOp.getElement().getName(); - - // Checks on extensions - if (Element.ELT_EXTENSION.equals(fqfieldName)) { - boolean checked = false; - - switch (restriction.getName()) { - case NOT_EQUALS: - checked = !w.getExtensionNames().contains(binOp.getValue()); - break; - case EQUALS: - checked = w.getExtensionNames().contains(binOp.getValue()); - break; - } - - return checked; - - // Checks on id - } else if (Element.ELT_ID.equals(fqfieldName)) { - - boolean checked = false; - - switch (restriction.getName()) { - case NOT_EQUALS: - checked = !w.getId().equals(binOp.getValue()); - break; - case EQUALS: - checked = w.getId().equals(binOp.getValue()); - break; - } - - return checked; - } - - // si les wikitty n'ont meme pas l'extension concerné - // Le check restriction, ne doit pas tester les champs - // si les wikitty n'ont meme pas l'extension concerné - String[] extName = fqfieldName.split("\\."); - if (!w.hasField(extName[0], extName[1])) { - - // return true in case of not equals - if (RestrictionName.NOT_EQUALS == restriction.getName()) { - return true; - } - - return false; - } - // recupere la valeur dans le wikitty - Object o = w.getFqField(fqfieldName); - - // recupere le type de la valeur - FieldType t = w.getFieldType(fqfieldName); - // convertie la valeur a verifier dans le meme type que la valeur - // du wikitty - Object value = binOp.getValue(); - if (!(value instanceof Collection) && t.isCollection()) { - // on doit encapsuler dans une collection, car la creation - // de la requete ajoute autant de v == o && ... que de valeurs - // dans la collection (champs multi-value solr). Mais - // dans le inmemory on doit retrouve des collections et non pas - // des objets seuls :( - value = Collections.singleton(value); - } - value = t.getValidValue(value); - - boolean checked = false; - - switch (restriction.getName()) { - case EQUALS: - - if (value instanceof String && o instanceof String) { - String pattern = (String) value; - pattern = pattern.replace("*", "\\p{ASCII}*"); - pattern = pattern.replace("?", "\\p{ASCII}"); - - Pattern p = Pattern.compile(pattern); - Matcher m = p.matcher((String) o); - checked = m.matches(); - } else { - checked = value.equals(o); - } - break; - case LESS: - checked = ((Comparable) o).compareTo(value) < 0; - break; - case LESS_OR_EQUAL: - checked = ((Comparable) o).compareTo(value) <= 0; - break; - case GREATER: - checked = ((Comparable) o).compareTo(value) > 0; - break; - case GREATER_OR_EQUAL: - checked = ((Comparable) o).compareTo(value) >= 0; - break; - case NOT_EQUALS: - checked = !value.equals(o); - break; - case ENDS_WITH: - if (t.getType() != TYPE.STRING) { - throw new WikittyException( - "Can't search for contents that 'ends with' on attribute type different of String. " - + "Attribute " - + fqfieldName - + " is " - + t.getType().name()); - } - checked = ((String) o).endsWith((String) value); - break; - case STARTS_WITH: - if (t.getType() != TYPE.STRING) { - throw new WikittyException( - "Can't search for contents that 'starts with' on attribute type different of String. " - + "Attribute " - + fqfieldName - + " is " - + t.getType().name()); - } - - // FIXME mfortun-2011-04-20 rustine pour champs multivalué de - // type string - // et restriction startwith dessus. ça marche mais faudrait - // quelque chose de plus - // propre, et surtout généraliser pour toutes les restrictions - - if (o instanceof Collection<?> - && value instanceof Collection<?>) { - - for (Object val : (Collection) value) { - - String valu = (String) val; - - for (Object oo : (Collection) o) { - String cotainedO = (String) oo; - if (cotainedO != null) { - checked = checked || cotainedO.startsWith(valu); - } - } - - } - - } else { - - checked = ((String) o).startsWith((String) value); - } - break; - } - return checked; - } else if (restriction instanceof Null) { - Null nullRes = (Null) restriction; - - String fqfieldName = nullRes.getFieldName(); - - // check my wikitty got the right extension before doing anything. - String[] extName = fqfieldName.split("\\."); - if (!w.hasField(extName[0], extName[1])) { - return false; - } - // get the value in the wikitty - Object o = w.getFqField(fqfieldName); - - // No null on extensions, always return false - if (fqfieldName.equals(Element.ELT_EXTENSION)) { - return false; - } - - // No null on ids, always return false - if (fqfieldName.equals(Element.ELT_ID)) { - return false; - } - - boolean checked = false; - - switch (nullRes.getName()) { - case IS_NULL: - checked = (o == null); - break; - case IS_NOT_NULL: - checked = (o != null); - break; - } - - return checked; - - } else if (restriction instanceof In) { - In in = (In) restriction; - String fqfieldName = in.getElement().getName(); - String testedValue = String.valueOf(w.getFqField(fqfieldName)); - for (String value : in.getValue()) { - if (testedValue.equals(value)) { - return true; - } - } - - return false; - - } else if (restriction instanceof True) { - return true; - } else if (restriction instanceof False) { - return false; - } else if (restriction instanceof Contains) { - Contains contains = (Contains) restriction; - - String fqfieldName = contains.getElement().getName(); - List<String> values = contains.getValue(); - - String extension = WikittyUtil - .getExtensionNameFromFQFieldName(fqfieldName); - String fieldName = WikittyUtil - .getFieldNameFromFQFieldName(fqfieldName); - - if (!w.hasField(extension, fieldName)) { - return false; - } - - // Get field as string and then split it to take into account not - // multivalued fields. - String testedValuesAsString = w.getFieldAsString(extension, - fieldName); - - if ('[' == testedValuesAsString.charAt(0)) { - testedValuesAsString = testedValuesAsString.substring(1, - testedValuesAsString.length()); - } - - List<String> testedValues = Arrays.asList(testedValuesAsString - .split(",")); - - for (Object value : values) { - if (!testedValues.contains(String.valueOf(value))) { - return false; - } - } - - return true; - - } else if (restriction instanceof And) { - And and = (And) restriction; - for (Restriction sub : and.getRestrictions()) { - if (!checkRestriction(sub, w)) { - return false; - } - } - return true; - } else if (restriction instanceof Or) { - Or or = (Or) restriction; - for (Restriction sub : or.getRestrictions()) { - if (checkRestriction(sub, w)) { - return true; - } - } - return false; - } else if (restriction instanceof Keyword) { - Keyword keyword = (Keyword) restriction; - - String value = keyword.getValue(); - - //TODO mfortun-2011-07-06 hack to ensure that * is intepreted as - // the real meaning aka any - if (value.equals("*")){ - return true; - } - - for (String fieldName : w.getAllFieldNames()) { - String testedValue = String.valueOf(w.getFqField(fieldName)); - if (testedValue.contains(value)) { - return true; - } - } - return false; - } else if (restriction instanceof Not) { - Not or = (Not) restriction; - Restriction sub = or.getRestriction(); - return !checkRestriction(sub, w); - } else if (restriction instanceof AssociatedRestriction) { - - AssociatedRestriction ass = (AssociatedRestriction) restriction; - - String fqfieldName = ass.getElement().getName(); - - // check my wikitty got the right extension before doing anything. - String[] extName = fqfieldName.split("\\."); - if (!w.hasField(extName[0], extName[1])) { - return false; - } - // get the value in the wikitty, it is a wikitty's id - Object o = w.getFqField(fqfieldName); - - // Get sub-restriction - Restriction sub = ass.getRestriction(); - - Criteria associatedSearch = new Criteria(); - associatedSearch.setRestriction(sub); - - // find everything that validate the sub-restriction - - List<Criteria> dummyList = new ArrayList<Criteria>(); - dummyList.add(associatedSearch); - // same as proxy for "fix" unique param to list param - PagedResult<String> associatedResult = findAllByCriteria("", - dummyList).get(0); - - List<String> associatedList = associatedResult.getAll(); - - // Check that my field is contained in the sub-restriction results. - return associatedList.contains(String.valueOf(o)); - } else if (restriction instanceof Between) { - - Between op = (Between) restriction; - - Object max = op.getMax(); - Object min = op.getMin(); - - // No between on extensions, always return false - if (op.getElement().getName().equals(Element.ELT_EXTENSION)) { - return false; - } - - // No between on ids, always return false - if (op.getElement().getName().equals(Element.ELT_ID)) { - return false; - } - - String fqfieldName = op.getElement().getName(); - - // si les wikitty n'ont meme pas l'extension concerné - // Le check restriction, ne doit pas tester les champs - // si les wikitty n'ont meme pas l'extension concerné - String[] extName = fqfieldName.split("\\."); - if (!w.hasField(extName[0], extName[1])) { - return false; - } - - // recupere la valeur dans le wikitty - Object o = w.getFqField(fqfieldName); - - // recupere le type de la valeur - FieldType t = w.getFieldType(fqfieldName); - - if (!(min instanceof Collection) && t.isCollection()) { - // on doit encapsuler dans une collection, car la creation - // de la requete ajoute autant de v == o && ... que de valeurs - // dans la collection (champs multi-value solr). Mais - // dans le inmemory on doit retrouve des collections et non pas - // des objets seuls :( - min = Collections.singleton(min); - } - min = t.getValidValue(min); - - if (!(max instanceof Collection) && t.isCollection()) { - // on doit encapsuler dans une collection, car la creation - // de la requete ajoute autant de v == o && ... que de valeurs - // dans la collection (champs multi-value solr). Mais - // dans le inmemory on doit retrouve des collections et non pas - // des objets seuls :( - max = Collections.singleton(max); - } - max = t.getValidValue(max); - - return ((Comparable) o).compareTo(min) >= 0 - && ((Comparable) o).compareTo(max) <= 0; - } else { - throw new UnsupportedOperationException(restriction.getName() - + " Search Not yet implemented"); - } - } - - - @Override - public List<PagedResult<String>> findAllByCriteria(String securityToken, - List<Criteria> criteria) { - - - List<PagedResult<String>> result = new ArrayList<PagedResult<String>>(); - - Map<String, Wikitty> wikitties = getAllWikitties(); - - // for each criteria - for (Criteria cr : criteria) { - // prepare restriction on result - int firstIndex = cr.getFirstIndex(); - int endIndex = cr.getEndIndex(); - List<String> ids = new LinkedList<String>(); - int currentIndex = 0; - Restriction restriction = cr.getRestriction(); - // for each wikitty check if it match the resttriction - for (Entry<String, Wikitty> entry : wikitties.entrySet()) { - String id = entry.getKey(); - Wikitty w = entry.getValue(); - // if macth - - - if (checkRestriction(restriction, w)) { - - // increment result number - currentIndex++; - if (currentIndex > firstIndex) { - ids.add(id); - } - // if the number of wikitty found is match - // stop the search for other wikitty - if (endIndex >= 0 && currentIndex >= endIndex) { - break; - } - } - } - result.add(new PagedResult<String>(firstIndex, ids.size(), - restriction.toString(), null, ids)); - - } - return result; - } - - - @Override - public List<String> findByCriteria(String securityToken, - List<Criteria> criteria) { - List<String> result = new ArrayList<String>(); - - Map<String, Wikitty> wikitties = getAllWikitties(); - // for each criteria - for (Criteria cr : criteria) { - // prepare restriction on result - int firstIndex = cr.getFirstIndex(); - int endIndex = cr.getEndIndex(); - List<String> ids = new LinkedList<String>(); - int currentIndex = 0; - Restriction restriction = cr.getRestriction(); - // for each wikitty check if it match the resttriction - for (Entry<String, Wikitty> entry : wikitties.entrySet()) { - String id = entry.getKey(); - Wikitty w = entry.getValue(); - // if macth - - log.debug("Check restriction for wikitty: " + w - + " Restriction:" + restriction); - - if (checkRestriction(restriction, w)) { - // increment result number - currentIndex++; - if (currentIndex > firstIndex) { - ids.add(id); - } - // if the number of wikitty found is match - // stop the search for other wikitty - if (endIndex >= 0 && currentIndex >= endIndex) { - break; - } - } - } - result.addAll(ids); - } - return result; - } - - -} \ No newline at end of file Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,147 +0,0 @@ -/* - * #%L - * Wikitty :: publication - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2010 - 2011 CodeLutin - * %% - * 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.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Class used to determine mimetype for an extension, used to determine type of - * wikittypub (data or text) with the extension and the mime type - * - * - * @author mfortun - * - */ -public class MimeTypePubHelper { - - /** - * Mapping between extention and mime type, key: extension, value: mimeType - */ - protected Map<String, String> mapExtensionMime; - - /** - * White list of mime type that have to be transform as a wikittyPubText - */ - protected List<String> mimePubText; - - /** - * The default mime type - */ - public static String DEFAULT_MIME_TYPE = "application/octet-stream"; - - public MimeTypePubHelper() { - this.mapExtensionMime = new HashMap<String, String>(); - this.mimePubText = new ArrayList<String>(); - - // TODO create a property file to store and handle mimetype for pub - // text - mimePubText.add("application/javascript"); - - mapExtensionMime.put("wp", "application/javascript"); - mapExtensionMime.put("js", "application/javascript"); - mapExtensionMime.put("jpg", "image/jpeg"); - mapExtensionMime.put("png", "image/png"); - } - - public Map<String, String> getMapExtensionMime() { - return mapExtensionMime; - } - - public void setMapExtensionMime(Map<String, String> mapExtensionMime) { - this.mapExtensionMime = mapExtensionMime; - } - - public List<String> getMimePubText() { - return mimePubText; - } - - public void setMimePubText(List<String> mimePubText) { - this.mimePubText = mimePubText; - } - - /** - * Return the corresponding mime Type for an extension, default if not - * recognized - * - * @param ext - * the extension - * @return the corresponding mimeTypeForExt DEFAULT_MIME_TYPE if ext not - * recognized - */ - public String getMimeForExtension(String ext) { - String result = mapExtensionMime.get(ext); - - if (result == null) { - result = DEFAULT_MIME_TYPE; - } - - return result; - } - - /** - * Used to check if a file have to be converted as a wikittyPubText - * - * @param mimeType - * of the file - * @return if the mimetype of a file correspond to a wikittyPubText - */ - public boolean isPubTextMime(String mimeType) { - return mimePubText.contains(mimeType); - } - - /** - * used to check if a file have to be converted as a wikittyPubText - * with his extension similar to : - * <br>isPubTextMime(getMimeForExtension(extension))</br> - * @param extension the file extension - * @return if the extension correspond to a wikittyPubText - */ - public boolean isPubTextExtension(String extension) { - return isPubTextMime(getMimeForExtension(extension)); - } - - /** - * Add a mimeType corresponding to a WikittyPubText - * - * @param mime - */ - public void addMimeForPubText(String mime) { - mimePubText.add(mime); - } - - /** - * Add an entry in the map that store mapping between extension and - * mimeType. - * - * @param extension - * @param mime - */ - public void addExtensionMime(String extension, String mime) { - mapExtensionMime.put(extension, mime); - } -} Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,115 +0,0 @@ -/* - * #%L - * Wikitty :: publication - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2010 - 2011 CodeLutin - * %% - * 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.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Properties; - -/** - * Class usefull when load properties file, update, delete, then save again File - * used to load properties is store, and this allow to save again the properties - * inside the file used to load them. - * - * @author mfortun - * - */ -public class PropertiesExtended extends Properties { - - /** - * - */ - private static final long serialVersionUID = -264337198024996529L; - - /** - * The original file used to load and create properties - */ - protected File origin; - - - public File getOrigin() { - return origin; - } - - public void setOrigin(File origin) { - this.origin = origin; - } - - - - /** - * Default constructor, need a file from whom load the properties. This is - * equivalent of a basic creation of properties and load after property from - * a file. - * - * @param origin - * the file from whom load the property, it is save for store. - * @throws IOException - * if error while reading the file - * @throws FileNotFoundException - * if file not found - */ - public PropertiesExtended(File origin) throws FileNotFoundException, - IOException { - super(); - this.load(origin); - - } - - - /** - * Load the property from the file, and store the file, for storage. - * - * @param file - * the file from whom to load the property, it replace the file - * use to create this class - * @throws IOException - * if error while reading the file - * @throws FileNotFoundException - * if file not found - */ - public void load(File file) throws FileNotFoundException, IOException { - this.origin = file; - this.load(new FileReader(origin)); - } - - /** - * Store the properties inside the last file used to load the property (or - * by default file used to create the class), equivalent of store(new - * FileWriter('File'),""); - * - * @throws IOException - * if error while reading the file - * @throws FileNotFoundException - * if file not found - */ - public void store() throws IOException { - this.store(new FileWriter(origin), ""); - } - -} Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyFileUtil.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyFileUtil.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyFileUtil.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,96 +0,0 @@ -package org.nuiton.wikitty.publication.synchro; - -import java.io.File; -import java.io.IOException; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.FileUtil; -import org.nuiton.util.StringUtil; - - -/** - * - * Class that containt utils method when handle wikitty store as file. - * - * @author mfortun - * - */ -public class WikittyFileUtil { - - /** - * The file name of the meta property file - */ - static public String WIKITTY_FILE_META_PROPERTIES_FILE = "meta.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() - */ - /** - * The file Name of the id property file - */ - static public String WIKITTY_ID_PROPERTIES_FILE = "ids.properties"; - - - - final static Log log = LogFactory.getLog(WikittyFileUtil.class); - - /** - * Construct correctly the path from a label - * - * @param label - * the label - * @return the correct path - */ - public static String labelToPath(String label) { - - String result = label; - - result = result.replace(".", File.separator); - - // correct the pb with directory name begin by . - result = result.replace(File.separator + File.separator, File.separator - + "."); - - log.info("Convert label to path: " + label + " path:" + result); - - return result; - } - - /** - * 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 IOException - */ - public static boolean createFilesFromLabelPath(File homeFile, String label) throws IOException - { - - label = labelToPath(label); - - log.info("Create directory from path:" + label); - - 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; - } - -} Deleted: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -1,678 +0,0 @@ -/* - * #%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.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map.Entry; -import java.util.Set; - -import org.apache.commons.lang.StringUtils; -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.FileUtil; -import org.nuiton.wikitty.WikittyConfigOption; -import org.nuiton.wikitty.WikittyProxy; -import org.nuiton.wikitty.WikittyServiceFactory; -import org.nuiton.wikitty.WikittyUtil; -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.Search; - -/** - * Main class of the sync part of wikitty publication, this class is the entry - * point for sync operation. Existing, delete 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); - - /** - * Key for the other uri, usefull in the case of commit/update with a file - * system wikitty service. - */ - static public String WIKITTY_SERVICE_INTERLOCUTEUR = "wikitty.service.interlocuteur"; - - static protected ApplicationConfig applicationConfig; - - /** - * for recursion option - */ - static public String IS_RECURSION_OPTION = "isRecur"; - /** - * for delete option - */ - static public String IS_DELETE_OPTION = "delete"; - /** - * for existing option - */ - static public String IS_EXISTING_OPTION = "existing"; - - /** - * Use to save the label by the wikitty publication file system - */ - static public String LABEL_KEY = "working.label"; - - /** - * the string that mark the beginnning of the label - */ - static public String LABEL_DELIM = "#"; - /** - * regex to select the label part of the uri - */ - static public String LABEL_REGEX = "\\#.*"; - - /** - * the prefix use in file url - */ - static public String FILE_URI_PREFIX = "file"; - - /** - * the prefix use in cajo url - */ - static public String CAJO_URI_PREFIX = "cajo"; - - /** - * the prefix use in hessian url - */ - static public String HESSIAN_URI_PREFIX = "hessian"; - - - - /* - * Class don't have to be instantiate - */ - private WikittyPublication() { - - } - - /** - * @param args - * @throws ArgumentsParserException - */ - static public void main(String[] args) throws Exception { - - applicationConfig = new ApplicationConfig(); - - /* - * TODO mfortun-2011-04-14 construct option def instance to initialize - * correctly application config - */ - - applicationConfig.setDefaultOption(IS_DELETE_OPTION, "false"); - applicationConfig.setDefaultOption(IS_EXISTING_OPTION, "false"); - applicationConfig.setDefaultOption(IS_RECURSION_OPTION, "true"); - - // allias for norecursion - applicationConfig.addAlias("--norecursion", "--option", - WikittyPublication.IS_RECURSION_OPTION, "false"); - - applicationConfig.addAlias("--delete", "--option", IS_DELETE_OPTION, - "true"); - - applicationConfig.addAlias("--existing", "--option", - IS_EXISTING_OPTION, "true"); - - // allias for all the action - applicationConfig.addAlias("wp sync", "--option", "sync"); - - applicationConfig.addAlias("wp commit", "--option", "commit"); - - applicationConfig.addAlias("wp update", "--option", "update"); - - applicationConfig - .addActionAlias("sync", - "org.nuiton.wikitty.publication.synchro.WikittyPublication#synchronisation"); - - applicationConfig - .addActionAlias("commit", - "org.nuiton.wikitty.publication.synchro.WikittyPublication#commit"); - - applicationConfig - .addActionAlias("update", - "org.nuiton.wikitty.publication.synchro.WikittyPublication#update"); - // parsing - applicationConfig.parse(args); - - // execution - applicationConfig.doAction(0); - - } - - static public void synchronisation(String origin, String target) - throws URISyntaxException { - - boolean isRecur = applicationConfig - .getOptionAsBoolean(IS_RECURSION_OPTION); - - boolean isDelete = applicationConfig - .getOptionAsBoolean(IS_DELETE_OPTION); - boolean isExisting = applicationConfig - .getOptionAsBoolean(IS_EXISTING_OPTION); - // update operation is the default operation - boolean isUpdate = !isDelete && !isExisting; - - log.info("Sync uri origin: " + origin + " uri target: " + target - + " isRecur:" + isRecur + " isUpdate:" + isUpdate - + " isDelete:" + isDelete + "isExisting:" + isExisting); - - URI uriOrigin = new URI(origin); - - URI uriTarget = new URI(target); - - /* - * necessary to have property correctly initialize in order to obtain - * the correct implementation of the wikitty service - */ - - // once on the service origin - applicationConfig.setOption(WIKITTY_SERVICE_INTERLOCUTEUR, new String( - target.replaceAll(LABEL_REGEX, StringUtils.EMPTY))); - - ApplicationConfig temp1 = setUpApplicationConfigServerConnector(uriOrigin); - - WikittyProxy proxyOrigin = new WikittyProxy( - WikittyServiceFactory.buildWikittyService(temp1)); - - // store the other uri in the application, if the service is a - // wikittypublication file system - // it can need it to store wikittyservice property - applicationConfig.setOption(WIKITTY_SERVICE_INTERLOCUTEUR, new String( - origin.replaceAll(LABEL_REGEX, StringUtils.EMPTY))); - // once on the service target - ApplicationConfig temp2 = setUpApplicationConfigServerConnector(uriTarget); - - WikittyProxy proxyTarget = new WikittyProxy( - WikittyServiceFactory.buildWikittyService(temp2)); - - String labelOrigin = uriOrigin.getFragment(); - String labelTarget = uriTarget.getFragment(); - - Criteria critOrigin = constructCriteriaLabelRecur(labelOrigin, isRecur); - Criteria critTarget = constructCriteriaLabelRecur(labelTarget, isRecur); - - List<String> listOrigin = proxyOrigin.findAllIdByCriteria(critOrigin) - .getAll(); - List<String> listTarget = proxyTarget.findAllIdByCriteria(critTarget) - .getAll(); - - // construct list of wikitty contained in both location - List<String> existInBoth = new ArrayList<String>(); - existInBoth.addAll(listOrigin); - existInBoth.retainAll(listTarget); - - // construct list of wikitty contained only in origin location - List<String> existOnlyOnOrigin = new ArrayList<String>(); - existOnlyOnOrigin.addAll(listOrigin); - existOnlyOnOrigin.removeAll(listTarget); - - // construct list of wikitty contained only in target location - List<String> existOnlyOnTarget = new ArrayList<String>(); - existOnlyOnTarget.addAll(listTarget); - existOnlyOnTarget.removeAll(listOrigin); - - if (log.isDebugEnabled()) { - log.debug("Wikitty exist on both: " + existInBoth.size()); - for (String ex : existInBoth) { - log.debug(ex); - } - - log.debug("Wikitty exist only on origin: " - + existOnlyOnOrigin.size()); - for (String ex : existOnlyOnOrigin) { - log.debug(ex); - } - - log.debug("Wikitty exist only on target: " - + existOnlyOnTarget.size()); - for (String ex : existOnlyOnTarget) { - log.debug(ex); - } - } - - /* - * FIXME mfortun-2011-04-27 remove all that stuff for the safety of the - * version when a solution rise for the wikitty version - */ - - /* - * if option is update send wikitty that are not in the target. - */ - if (isUpdate) { - - log.info("Store on target new wikitty"); - - List<Wikitty> newWikitties = proxyOrigin.restore(existOnlyOnOrigin); - - for (Wikitty wikittyNew : newWikitties) { - - Set<String> saveLabelOrigin = WikittyLabelHelper - .getLabels(wikittyNew); - - Set<String> targetLabels = new HashSet<String>(); - - // prepare set of target label - // we save all the label except the one corresponding - // to origin, determine by isRecur - for (String labels : saveLabelOrigin) { - - if (isRecur && labels.startsWith(labelOrigin)) { - String finalLabelTarge = new String(labels.replace( - labelOrigin, labelTarget)); - targetLabels.add(finalLabelTarge); - } else if (!isRecur && labels.equals(labelOrigin)) { - targetLabels.add(labelTarget); - } else { - targetLabels.add(labels); - } - - } - - // save the version before reset label - String wikittyVersionLocal = wikittyNew.getVersion(); - WikittyLabelHelper.setLabels(wikittyNew, targetLabels); - // restore the version - wikittyNew.setVersion(wikittyVersionLocal); - proxyTarget.store(wikittyNew); - - String versionSaveTarget = wikittyNew.getVersion(); - - WikittyLabelHelper.setLabels(wikittyNew, saveLabelOrigin); - wikittyNew.setVersion(versionSaveTarget); - - // we re store on the origin to ensure wikitty version - - try { - proxyOrigin.store(wikittyNew); - } catch (Exception e) { - - // FIXME when a wikitty service store a - // wikitty that he does'nt know - // he set the version to 1.0 - e.printStackTrace(); - } - } - - } - /* - * if option delete remove those who are on the target but not on origin - */ - if (isDelete) { - - log.info("Remove from target deleted wikitty"); - - for (String id : existOnlyOnTarget) { - - Wikitty w = proxyTarget.restore(id); - - WikittyLabelHelper.removeLabels(w, labelTarget); - - proxyTarget.store(w); - - } - } else { - - log.info("Update existing wikitty"); - /* - * case existing and update, update those which are on both location - */ - for (String id : existInBoth) { - Wikitty fromTarget = proxyTarget.restore(id); - Wikitty fromOrigin = proxyOrigin.restore(id); - - String versionTarget = fromTarget.getVersion(); - - String versionOrigin = fromOrigin.getVersion(); - - // check version for update - - /* - * we replace origin labels by target's labels if we udpate. - */ - if (WikittyUtil - .versionGreaterThan(versionOrigin, versionTarget)) { - Set<String> setLabelTarget = WikittyLabelHelper - .getLabels(fromTarget); - - Set<String> setLabelOrigin = WikittyLabelHelper - .getLabels(fromOrigin); - - // save version before reset the label - String versionLocalSave = fromOrigin.getVersion(); - // replace labels origins, by targets label - WikittyLabelHelper.setLabels(fromOrigin, setLabelTarget); - // restore the version - fromOrigin.setVersion(versionLocalSave); - // send modified origin to target wikitty service - proxyTarget.store(fromOrigin); - // re store on origin to ensure version is the same on both - - // save the version case re set labels increment version - String saveVersion = fromOrigin.getVersion(); - // re set correctly labels - WikittyLabelHelper.setLabels(fromOrigin, setLabelOrigin); - // re set the version - fromOrigin.setVersion(saveVersion); - - // re store the wikitty with the correct version - // and labels - proxyOrigin.store(fromOrigin); - } - } - - }// */ - - } - - /** - * Used to construct criteria on wikittypubdata and pubtext on the - * wikittylabel extension - * - * @param label - * the label criteria - * @param isRecur - * is recusion - * @return the constructed criteria - */ - static protected Criteria constructCriteriaLabelRecur(String label, - boolean isRecur) { - - log.info("Construct criteria with label: " + label + " isRecur:" - + isRecur); - - // Construct the criteria - Criteria criteriaOnLabels; - 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 (isRecur) { - - // and extension with the name that containt the label (recursivity) - criteriaOnLabels = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) - .sw(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, label) - .criteria(); - - } else { - - // and extension with the name strictly equals to the label (no - // recursivity) - criteriaOnLabels = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) - .eq(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, label) - .criteria(); - - } - - log.debug(criteriaOnLabels); - - return criteriaOnLabels; - - } - - /** - * Use to setup correct url property in the application config and correct - * component for the wikittyservice - * - * @param uri - * of the targeted wikitty service - */ - static protected ApplicationConfig setUpApplicationConfigServerConnector( - URI uri) { - - log.info("Construct application config for uri: " + uri); - - // prepare new application config - ApplicationConfig result = new ApplicationConfig(); - - // transfert main properties to new application config - result.setOptions(applicationConfig.getFlatOptions()); - String url = uri.toASCIIString(); - - if (uri.getScheme().equals(FILE_URI_PREFIX)) { - - result.setOption( - WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS - .getKey(), WikittyPublicationFileSystem.class - .getName()); - } else if (uri.getScheme().equals(CAJO_URI_PREFIX)) { - result.setOption( - WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS - .getKey(), - "org.nuiton.wikitty.services.WikittyServiceCajoClient"); - - // remove fragment from the uri. - url = new String(url.replaceAll(LABEL_REGEX, StringUtils.EMPTY)); - - } else if (uri.getScheme().equals(HESSIAN_URI_PREFIX)) { - result.setOption( - WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS - .getKey(), - "org.nuiton.wikitty.services.WikittyServiceHessianClient"); - // remove fragment from the uri. - url = new String(url.replaceAll(LABEL_REGEX, StringUtils.EMPTY)); - } - - // set protocol to http, no use finally - /* - * url = url.replaceFirst("["+uri.getScheme()+"]", "http"); - */ - - log.info("set url " - + url - + " with component :" - + result.getOption(WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS - .getKey())); - - result.setOption(WikittyConfigOption.WIKITTY_SERVER_URL.getKey(), url); - - log.debug("Application config: " + result.getFlatOptions()); - - return result; - - } - - static public void update(String label, String... uriFileSystem) - throws Exception { - - // only difference between update and commit is the param's order - // when calling synchronisation method - commitUpdateDelegate(label, false, uriFileSystem); - - } - - static public void commit(String label, String... uriFileSystem) - throws Exception { - - // only difference between update and commit is the param's order - // when calling synchronisation method - commitUpdateDelegate(label, true, uriFileSystem); - - } - - static protected void commitUpdateDelegate(String label, boolean isCommit, - String... uriFileSystem) throws Exception { - - File currentDir = new File(FileUtil.getCurrentDirectory() - .getAbsolutePath()); - - if (isCommit) { - log.info("Commit args.length:+" + uriFileSystem.length); - } else { - log.info("Update args.length:+" + uriFileSystem.length); - } - - if (log.isDebugEnabled()) { - for (String args : uriFileSystem) { - log.debug(args); - } - log.debug("Current dir:" + currentDir); - } - - /* - * Alors c'est facile si on a un élément dans le param c'est que on - * spécifie l'endroit du FS à updater/commit, ce qui veut dire que on - * doit aller chercher ensuite dans le dossier donné l'adresse du - * wikitty service. Notons que si il y a un élément ça doit forcément - * être une uri avec le protocole file. après on appelle simplement la - * méthode synchro avec l'ordre correct entre uritarget et uri origin. - * - * dans le cas ou ya pas d'argument ça veut dire que on doit - * commit/update le dossier courant donc aller chercher dans - * l'arborescence l'adresse du wikitty service. - * - * et pareil on va construire les adresses pour faire une synchro - */ - - // update is from wikitty service store to wikitty service File System - - String wikittyServiceInter = StringUtils.EMPTY; - String wikittyServiceFileSystem = StringUtils.EMPTY; - - String labelInitial = StringUtils.EMPTY; - PropertiesExtended homeProperty = null; - // Check number of argument - switch (uriFileSystem.length) { - // if none, then the current dir have to be commit. - case 0: - // search for the home property dir that containt uri to the wikitty - // service - File homePropertyDir = WikittyPublicationFileSystem - .searchWikittyPublicationHomePropertie(currentDir); - - homeProperty = WikittyPublicationFileSystem - .getWikittyPublicationProperties(homePropertyDir, - WikittyPublicationFileSystem.WIKITTY_FILE_SERVICE); - - // load the wikitty service uri (distant one) - wikittyServiceInter = homeProperty - .getProperty(WIKITTY_SERVICE_INTERLOCUTEUR); - - // construct the current uri for wikitty service file system - // search the current label - PropertiesExtended metaPropertiesExtended = WikittyPublicationFileSystem - .getWikittyPublicationProperties(currentDir, - WikittyFileUtil.WIKITTY_FILE_META_PROPERTIES_FILE); - String labelCurrent = metaPropertiesExtended - .getProperty(WikittyPublicationFileSystem.META_CURRENT_LABEL); - - // construct uri of the wikitty publication file system - wikittyServiceFileSystem = "file://" - + homePropertyDir.getAbsolutePath() + LABEL_DELIM - + labelCurrent; - labelInitial = homeProperty.getProperty(LABEL_KEY); - - wikittyServiceInter += LABEL_DELIM + label - + labelCurrent.replaceFirst(labelInitial, ""); - - break; - // if a param is set it mean that the uri of the File system is set - case 1: - - wikittyServiceFileSystem = uriFileSystem[0]; - URI originUri = new URI(wikittyServiceFileSystem); - // check if uri of wikitty publication file system is well formed, - // must be a file "protocol" - if (!originUri.getScheme().equals(FILE_URI_PREFIX)) { - // Exception - } - // then search for the home property file to load the wikitty - // service uri - File workingDir = new File(originUri.getPath()); - homeProperty = WikittyPublicationFileSystem - .getWikittyPublicationProperties(workingDir, - WikittyPublicationFileSystem.WIKITTY_FILE_SERVICE); - - wikittyServiceInter = (String) homeProperty - .get(WIKITTY_SERVICE_INTERLOCUTEUR); - - labelInitial = homeProperty.getProperty(LABEL_KEY); - - wikittyServiceInter += LABEL_DELIM + label - + originUri.getFragment().replaceFirst(labelInitial, ""); - - break; - - // Exception, correct number of argument is 0 or 1 - default: - - // exception - break; - } - - if (log.isDebugEnabled()) { - - log.debug("homeProperty :" + homeProperty.getOrigin()); - for (Entry<Object, Object> ee : homeProperty.entrySet()) { - log.debug(ee.getKey() + "=" + ee.getValue()); - } - - log.debug("wikitty Inter:" + wikittyServiceInter - + " wikittyFileSystem" + wikittyServiceFileSystem); - } - - // delegate to synchronisation - // only difference between update and commit is the param's order - if (isCommit) { - synchronisation(wikittyServiceFileSystem, wikittyServiceInter); - } else { - synchronisation(wikittyServiceInter, wikittyServiceFileSystem); - } - - } - - static public void usage() { - - System.out.println(""); - - String usage = "Usage" - + "\n" - + "''wp sync [--norecursion] " - + "[--delete|--existing] [URI origin] [URI target]''" - + "\n \nwith URI :\n" - + "file:///truc/machin/#label\n" - + "hessian://www.adresse.com:8827/etc/etc#label\n" - + "cajo://www.adresse.com:8827/etc/etc#label" - + " \n\n or: \n" - + "''wp [update|commit] [--norecursion] [--delete|--existing] [URI file]''"; - System.out.println(usage); - } -} Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -56,6 +56,10 @@ import org.nuiton.wikitty.entities.WikittyLabel; import org.nuiton.wikitty.entities.WikittyLabelHelper; import org.nuiton.wikitty.entities.WikittyLabelImpl; +import org.nuiton.wikitty.publication.AbstractWikittyFileService; +import org.nuiton.wikitty.publication.MimeTypePubHelper; +import org.nuiton.wikitty.publication.PropertiesExtended; +import org.nuiton.wikitty.publication.WikittyFileUtil; import org.nuiton.wikitty.publication.entities.WikittyPubData; import org.nuiton.wikitty.publication.entities.WikittyPubDataHelper; import org.nuiton.wikitty.publication.entities.WikittyPubDataImpl; @@ -66,7 +70,6 @@ import org.nuiton.wikitty.search.TreeNodeResult; import org.nuiton.wikitty.services.WikittyEvent; import org.nuiton.wikitty.services.WikittyListener; -import org.nuiton.wikitty.publication.synchro.WikittyFileUtil; public class WikittyPublicationFileSystem extends AbstractWikittyFileService { @@ -194,30 +197,30 @@ homeFile, WIKITTY_FILE_SERVICE); // the original label use to create if not exist - if (!propertyWikittyService.containsKey(WikittyPublication.LABEL_KEY)) { + if (!propertyWikittyService.containsKey(WikittyPublicationSynchronize.LABEL_KEY)) { log.debug("Writing home property label" + propertyWikittyService.getOrigin()); - propertyWikittyService.setProperty(WikittyPublication.LABEL_KEY, + propertyWikittyService.setProperty(WikittyPublicationSynchronize.LABEL_KEY, this.label); } // the service use to update or commit String uriService = app - .getOption(WikittyPublication.WIKITTY_SERVICE_INTERLOCUTEUR); + .getOption(WikittyPublicationSynchronize.WIKITTY_SERVICE_INTERLOCUTEUR); if (uriService !=null) { log.debug("Writing home property service on:" + propertyWikittyService.getOrigin() + " uri" + uriService); propertyWikittyService.setProperty( - WikittyPublication.WIKITTY_SERVICE_INTERLOCUTEUR, uriService); + WikittyPublicationSynchronize.WIKITTY_SERVICE_INTERLOCUTEUR, uriService); } propertyWikittyService.store(); recursion = true; - if (app.getOptions().containsKey(WikittyPublication.IS_RECURSION_OPTION)) { + if (app.getOptions().containsKey(WikittyPublicationSynchronize.IS_RECURSION_OPTION)) { this.recursion = app - .getOptionAsBoolean(WikittyPublication.IS_RECURSION_OPTION); + .getOptionAsBoolean(WikittyPublicationSynchronize.IS_RECURSION_OPTION); } // TODO mfotun-2011-04-28 add a support for filtered file with a // property file Copied: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationSynchronize.java (from rev 1079, trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java) =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationSynchronize.java (rev 0) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationSynchronize.java 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,680 @@ +/* + * #%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.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.commons.lang.StringUtils; +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.FileUtil; +import org.nuiton.wikitty.WikittyConfigOption; +import org.nuiton.wikitty.WikittyProxy; +import org.nuiton.wikitty.WikittyServiceFactory; +import org.nuiton.wikitty.WikittyUtil; +import org.nuiton.wikitty.entities.Wikitty; +import org.nuiton.wikitty.entities.WikittyLabel; +import org.nuiton.wikitty.entities.WikittyLabelHelper; +import org.nuiton.wikitty.publication.PropertiesExtended; +import org.nuiton.wikitty.publication.WikittyFileUtil; +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.Search; + +/** + * Main class of the sync part of wikitty publication, this class is the entry + * point for sync operation. Existing, delete and update. + * + * + * @author mfortun + * + */ +public class WikittyPublicationSynchronize { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + final static private Log log = LogFactory.getLog(WikittyPublicationSynchronize.class); + + /** + * Key for the other uri, usefull in the case of commit/update with a file + * system wikitty service. + */ + static public String WIKITTY_SERVICE_INTERLOCUTEUR = "wikitty.service.interlocuteur"; + + static protected ApplicationConfig applicationConfig; + + /** + * for recursion option + */ + static public String IS_RECURSION_OPTION = "isRecur"; + /** + * for delete option + */ + static public String IS_DELETE_OPTION = "delete"; + /** + * for existing option + */ + static public String IS_EXISTING_OPTION = "existing"; + + /** + * Use to save the label by the wikitty publication file system + */ + static public String LABEL_KEY = "working.label"; + + /** + * the string that mark the beginnning of the label + */ + static public String LABEL_DELIM = "#"; + /** + * regex to select the label part of the uri + */ + static public String LABEL_REGEX = "\\#.*"; + + /** + * the prefix use in file url + */ + static public String FILE_URI_PREFIX = "file"; + + /** + * the prefix use in cajo url + */ + static public String CAJO_URI_PREFIX = "cajo"; + + /** + * the prefix use in hessian url + */ + static public String HESSIAN_URI_PREFIX = "hessian"; + + + + /* + * Class don't have to be instantiate + */ + private WikittyPublicationSynchronize() { + + } + + /** + * @param args + * @throws ArgumentsParserException + */ + static public void main(String[] args) throws Exception { + + applicationConfig = new ApplicationConfig(); + + /* + * TODO mfortun-2011-04-14 construct option def instance to initialize + * correctly application config + */ + + applicationConfig.setDefaultOption(IS_DELETE_OPTION, "false"); + applicationConfig.setDefaultOption(IS_EXISTING_OPTION, "false"); + applicationConfig.setDefaultOption(IS_RECURSION_OPTION, "true"); + + // allias for norecursion + applicationConfig.addAlias("--norecursion", "--option", + WikittyPublicationSynchronize.IS_RECURSION_OPTION, "false"); + + applicationConfig.addAlias("--delete", "--option", IS_DELETE_OPTION, + "true"); + + applicationConfig.addAlias("--existing", "--option", + IS_EXISTING_OPTION, "true"); + + // allias for all the action + applicationConfig.addAlias("wp sync", "--option", "sync"); + + applicationConfig.addAlias("wp commit", "--option", "commit"); + + applicationConfig.addAlias("wp update", "--option", "update"); + + applicationConfig + .addActionAlias("sync", + "org.nuiton.wikitty.publication.synchro.WikittyPublication#synchronisation"); + + applicationConfig + .addActionAlias("commit", + "org.nuiton.wikitty.publication.synchro.WikittyPublication#commit"); + + applicationConfig + .addActionAlias("update", + "org.nuiton.wikitty.publication.synchro.WikittyPublication#update"); + // parsing + applicationConfig.parse(args); + + // execution + applicationConfig.doAction(0); + + } + + static public void synchronisation(String origin, String target) + throws URISyntaxException { + + boolean isRecur = applicationConfig + .getOptionAsBoolean(IS_RECURSION_OPTION); + + boolean isDelete = applicationConfig + .getOptionAsBoolean(IS_DELETE_OPTION); + boolean isExisting = applicationConfig + .getOptionAsBoolean(IS_EXISTING_OPTION); + // update operation is the default operation + boolean isUpdate = !isDelete && !isExisting; + + log.info("Sync uri origin: " + origin + " uri target: " + target + + " isRecur:" + isRecur + " isUpdate:" + isUpdate + + " isDelete:" + isDelete + "isExisting:" + isExisting); + + URI uriOrigin = new URI(origin); + + URI uriTarget = new URI(target); + + /* + * necessary to have property correctly initialize in order to obtain + * the correct implementation of the wikitty service + */ + + // once on the service origin + applicationConfig.setOption(WIKITTY_SERVICE_INTERLOCUTEUR, new String( + target.replaceAll(LABEL_REGEX, StringUtils.EMPTY))); + + ApplicationConfig temp1 = setUpApplicationConfigServerConnector(uriOrigin); + + WikittyProxy proxyOrigin = new WikittyProxy( + WikittyServiceFactory.buildWikittyService(temp1)); + + // store the other uri in the application, if the service is a + // wikittypublication file system + // it can need it to store wikittyservice property + applicationConfig.setOption(WIKITTY_SERVICE_INTERLOCUTEUR, new String( + origin.replaceAll(LABEL_REGEX, StringUtils.EMPTY))); + // once on the service target + ApplicationConfig temp2 = setUpApplicationConfigServerConnector(uriTarget); + + WikittyProxy proxyTarget = new WikittyProxy( + WikittyServiceFactory.buildWikittyService(temp2)); + + String labelOrigin = uriOrigin.getFragment(); + String labelTarget = uriTarget.getFragment(); + + Criteria critOrigin = constructCriteriaLabelRecur(labelOrigin, isRecur); + Criteria critTarget = constructCriteriaLabelRecur(labelTarget, isRecur); + + List<String> listOrigin = proxyOrigin.findAllIdByCriteria(critOrigin) + .getAll(); + List<String> listTarget = proxyTarget.findAllIdByCriteria(critTarget) + .getAll(); + + // construct list of wikitty contained in both location + List<String> existInBoth = new ArrayList<String>(); + existInBoth.addAll(listOrigin); + existInBoth.retainAll(listTarget); + + // construct list of wikitty contained only in origin location + List<String> existOnlyOnOrigin = new ArrayList<String>(); + existOnlyOnOrigin.addAll(listOrigin); + existOnlyOnOrigin.removeAll(listTarget); + + // construct list of wikitty contained only in target location + List<String> existOnlyOnTarget = new ArrayList<String>(); + existOnlyOnTarget.addAll(listTarget); + existOnlyOnTarget.removeAll(listOrigin); + + if (log.isDebugEnabled()) { + log.debug("Wikitty exist on both: " + existInBoth.size()); + for (String ex : existInBoth) { + log.debug(ex); + } + + log.debug("Wikitty exist only on origin: " + + existOnlyOnOrigin.size()); + for (String ex : existOnlyOnOrigin) { + log.debug(ex); + } + + log.debug("Wikitty exist only on target: " + + existOnlyOnTarget.size()); + for (String ex : existOnlyOnTarget) { + log.debug(ex); + } + } + + /* + * FIXME mfortun-2011-04-27 remove all that stuff for the safety of the + * version when a solution rise for the wikitty version + */ + + /* + * if option is update send wikitty that are not in the target. + */ + if (isUpdate) { + + log.info("Store on target new wikitty"); + + List<Wikitty> newWikitties = proxyOrigin.restore(existOnlyOnOrigin); + + for (Wikitty wikittyNew : newWikitties) { + + Set<String> saveLabelOrigin = WikittyLabelHelper + .getLabels(wikittyNew); + + Set<String> targetLabels = new HashSet<String>(); + + // prepare set of target label + // we save all the label except the one corresponding + // to origin, determine by isRecur + for (String labels : saveLabelOrigin) { + + if (isRecur && labels.startsWith(labelOrigin)) { + String finalLabelTarge = new String(labels.replace( + labelOrigin, labelTarget)); + targetLabels.add(finalLabelTarge); + } else if (!isRecur && labels.equals(labelOrigin)) { + targetLabels.add(labelTarget); + } else { + targetLabels.add(labels); + } + + } + + // save the version before reset label + String wikittyVersionLocal = wikittyNew.getVersion(); + WikittyLabelHelper.setLabels(wikittyNew, targetLabels); + // restore the version + wikittyNew.setVersion(wikittyVersionLocal); + proxyTarget.store(wikittyNew); + + String versionSaveTarget = wikittyNew.getVersion(); + + WikittyLabelHelper.setLabels(wikittyNew, saveLabelOrigin); + wikittyNew.setVersion(versionSaveTarget); + + // we re store on the origin to ensure wikitty version + + try { + proxyOrigin.store(wikittyNew); + } catch (Exception e) { + + // FIXME when a wikitty service store a + // wikitty that he does'nt know + // he set the version to 1.0 + e.printStackTrace(); + } + } + + } + /* + * if option delete remove those who are on the target but not on origin + */ + if (isDelete) { + + log.info("Remove from target deleted wikitty"); + + for (String id : existOnlyOnTarget) { + + Wikitty w = proxyTarget.restore(id); + + WikittyLabelHelper.removeLabels(w, labelTarget); + + proxyTarget.store(w); + + } + } else { + + log.info("Update existing wikitty"); + /* + * case existing and update, update those which are on both location + */ + for (String id : existInBoth) { + Wikitty fromTarget = proxyTarget.restore(id); + Wikitty fromOrigin = proxyOrigin.restore(id); + + String versionTarget = fromTarget.getVersion(); + + String versionOrigin = fromOrigin.getVersion(); + + // check version for update + + /* + * we replace origin labels by target's labels if we udpate. + */ + if (WikittyUtil + .versionGreaterThan(versionOrigin, versionTarget)) { + Set<String> setLabelTarget = WikittyLabelHelper + .getLabels(fromTarget); + + Set<String> setLabelOrigin = WikittyLabelHelper + .getLabels(fromOrigin); + + // save version before reset the label + String versionLocalSave = fromOrigin.getVersion(); + // replace labels origins, by targets label + WikittyLabelHelper.setLabels(fromOrigin, setLabelTarget); + // restore the version + fromOrigin.setVersion(versionLocalSave); + // send modified origin to target wikitty service + proxyTarget.store(fromOrigin); + // re store on origin to ensure version is the same on both + + // save the version case re set labels increment version + String saveVersion = fromOrigin.getVersion(); + // re set correctly labels + WikittyLabelHelper.setLabels(fromOrigin, setLabelOrigin); + // re set the version + fromOrigin.setVersion(saveVersion); + + // re store the wikitty with the correct version + // and labels + proxyOrigin.store(fromOrigin); + } + } + + }// */ + + } + + /** + * Used to construct criteria on wikittypubdata and pubtext on the + * wikittylabel extension + * + * @param label + * the label criteria + * @param isRecur + * is recusion + * @return the constructed criteria + */ + static protected Criteria constructCriteriaLabelRecur(String label, + boolean isRecur) { + + log.info("Construct criteria with label: " + label + " isRecur:" + + isRecur); + + // Construct the criteria + Criteria criteriaOnLabels; + 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 (isRecur) { + + // and extension with the name that containt the label (recursivity) + criteriaOnLabels = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) + .sw(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, label) + .criteria(); + + } else { + + // and extension with the name strictly equals to the label (no + // recursivity) + criteriaOnLabels = mainRequest.exteq(WikittyLabel.EXT_WIKITTYLABEL) + .eq(WikittyLabel.FQ_FIELD_WIKITTYLABEL_LABELS, label) + .criteria(); + + } + + log.debug(criteriaOnLabels); + + return criteriaOnLabels; + + } + + /** + * Use to setup correct url property in the application config and correct + * component for the wikittyservice + * + * @param uri + * of the targeted wikitty service + */ + static protected ApplicationConfig setUpApplicationConfigServerConnector( + URI uri) { + + log.info("Construct application config for uri: " + uri); + + // prepare new application config + ApplicationConfig result = new ApplicationConfig(); + + // transfert main properties to new application config + result.setOptions(applicationConfig.getFlatOptions()); + String url = uri.toASCIIString(); + + if (uri.getScheme().equals(FILE_URI_PREFIX)) { + + result.setOption( + WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS + .getKey(), WikittyPublicationFileSystem.class + .getName()); + } else if (uri.getScheme().equals(CAJO_URI_PREFIX)) { + result.setOption( + WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS + .getKey(), + "org.nuiton.wikitty.services.WikittyServiceCajoClient"); + + // remove fragment from the uri. + url = new String(url.replaceAll(LABEL_REGEX, StringUtils.EMPTY)); + + } else if (uri.getScheme().equals(HESSIAN_URI_PREFIX)) { + result.setOption( + WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS + .getKey(), + "org.nuiton.wikitty.services.WikittyServiceHessianClient"); + // remove fragment from the uri. + url = new String(url.replaceAll(LABEL_REGEX, StringUtils.EMPTY)); + } + + // set protocol to http, no use finally + /* + * url = url.replaceFirst("["+uri.getScheme()+"]", "http"); + */ + + log.info("set url " + + url + + " with component :" + + result.getOption(WikittyConfigOption.WIKITTY_WIKITTYSERVICE_COMPONENTS + .getKey())); + + result.setOption(WikittyConfigOption.WIKITTY_SERVER_URL.getKey(), url); + + log.debug("Application config: " + result.getFlatOptions()); + + return result; + + } + + static public void update(String label, String... uriFileSystem) + throws Exception { + + // only difference between update and commit is the param's order + // when calling synchronisation method + commitUpdateDelegate(label, false, uriFileSystem); + + } + + static public void commit(String label, String... uriFileSystem) + throws Exception { + + // only difference between update and commit is the param's order + // when calling synchronisation method + commitUpdateDelegate(label, true, uriFileSystem); + + } + + static protected void commitUpdateDelegate(String label, boolean isCommit, + String... uriFileSystem) throws Exception { + + File currentDir = new File(FileUtil.getCurrentDirectory() + .getAbsolutePath()); + + if (isCommit) { + log.info("Commit args.length:+" + uriFileSystem.length); + } else { + log.info("Update args.length:+" + uriFileSystem.length); + } + + if (log.isDebugEnabled()) { + for (String args : uriFileSystem) { + log.debug(args); + } + log.debug("Current dir:" + currentDir); + } + + /* + * Alors c'est facile si on a un élément dans le param c'est que on + * spécifie l'endroit du FS à updater/commit, ce qui veut dire que on + * doit aller chercher ensuite dans le dossier donné l'adresse du + * wikitty service. Notons que si il y a un élément ça doit forcément + * être une uri avec le protocole file. après on appelle simplement la + * méthode synchro avec l'ordre correct entre uritarget et uri origin. + * + * dans le cas ou ya pas d'argument ça veut dire que on doit + * commit/update le dossier courant donc aller chercher dans + * l'arborescence l'adresse du wikitty service. + * + * et pareil on va construire les adresses pour faire une synchro + */ + + // update is from wikitty service store to wikitty service File System + + String wikittyServiceInter = StringUtils.EMPTY; + String wikittyServiceFileSystem = StringUtils.EMPTY; + + String labelInitial = StringUtils.EMPTY; + PropertiesExtended homeProperty = null; + // Check number of argument + switch (uriFileSystem.length) { + // if none, then the current dir have to be commit. + case 0: + // search for the home property dir that containt uri to the wikitty + // service + File homePropertyDir = WikittyPublicationFileSystem + .searchWikittyPublicationHomePropertie(currentDir); + + homeProperty = WikittyPublicationFileSystem + .getWikittyPublicationProperties(homePropertyDir, + WikittyPublicationFileSystem.WIKITTY_FILE_SERVICE); + + // load the wikitty service uri (distant one) + wikittyServiceInter = homeProperty + .getProperty(WIKITTY_SERVICE_INTERLOCUTEUR); + + // construct the current uri for wikitty service file system + // search the current label + PropertiesExtended metaPropertiesExtended = WikittyPublicationFileSystem + .getWikittyPublicationProperties(currentDir, + WikittyFileUtil.WIKITTY_FILE_META_PROPERTIES_FILE); + String labelCurrent = metaPropertiesExtended + .getProperty(WikittyPublicationFileSystem.META_CURRENT_LABEL); + + // construct uri of the wikitty publication file system + wikittyServiceFileSystem = "file://" + + homePropertyDir.getAbsolutePath() + LABEL_DELIM + + labelCurrent; + labelInitial = homeProperty.getProperty(LABEL_KEY); + + wikittyServiceInter += LABEL_DELIM + label + + labelCurrent.replaceFirst(labelInitial, ""); + + break; + // if a param is set it mean that the uri of the File system is set + case 1: + + wikittyServiceFileSystem = uriFileSystem[0]; + URI originUri = new URI(wikittyServiceFileSystem); + // check if uri of wikitty publication file system is well formed, + // must be a file "protocol" + if (!originUri.getScheme().equals(FILE_URI_PREFIX)) { + // Exception + } + // then search for the home property file to load the wikitty + // service uri + File workingDir = new File(originUri.getPath()); + homeProperty = WikittyPublicationFileSystem + .getWikittyPublicationProperties(workingDir, + WikittyPublicationFileSystem.WIKITTY_FILE_SERVICE); + + wikittyServiceInter = (String) homeProperty + .get(WIKITTY_SERVICE_INTERLOCUTEUR); + + labelInitial = homeProperty.getProperty(LABEL_KEY); + + wikittyServiceInter += LABEL_DELIM + label + + originUri.getFragment().replaceFirst(labelInitial, ""); + + break; + + // Exception, correct number of argument is 0 or 1 + default: + + // exception + break; + } + + if (log.isDebugEnabled()) { + + log.debug("homeProperty :" + homeProperty.getOrigin()); + for (Entry<Object, Object> ee : homeProperty.entrySet()) { + log.debug(ee.getKey() + "=" + ee.getValue()); + } + + log.debug("wikitty Inter:" + wikittyServiceInter + + " wikittyFileSystem" + wikittyServiceFileSystem); + } + + // delegate to synchronisation + // only difference between update and commit is the param's order + if (isCommit) { + synchronisation(wikittyServiceFileSystem, wikittyServiceInter); + } else { + synchronisation(wikittyServiceInter, wikittyServiceFileSystem); + } + + } + + static public void usage() { + + System.out.println(""); + + String usage = "Usage" + + "\n" + + "''wp sync [--norecursion] " + + "[--delete|--existing] [URI origin] [URI target]''" + + "\n \nwith URI :\n" + + "file:///truc/machin/#label\n" + + "hessian://www.adresse.com:8827/etc/etc#label\n" + + "cajo://www.adresse.com:8827/etc/etc#label" + + " \n\n or: \n" + + "''wp [update|commit] [--norecursion] [--delete|--existing] [URI file]''"; + System.out.println(usage); + } +} Property changes on: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationSynchronize.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/wikitty-publication/src/main/resources/struts.xml =================================================================== --- trunk/wikitty-publication/src/main/resources/struts.xml 2011-07-20 15:26:30 UTC (rev 1081) +++ trunk/wikitty-publication/src/main/resources/struts.xml 2011-07-21 08:38:06 UTC (rev 1082) @@ -28,7 +28,7 @@ <package name="loginArea" extends="publicArea"> <interceptors> <interceptor name="logout" - class="org.nuiton.wikitty.publication.LogoutInterceptor" /> + class="org.nuiton.wikitty.publication.interceptor.LogoutInterceptor" /> <interceptor-stack name="loginAreaStack"> <interceptor-ref name="logout" /> <interceptor-ref name="publicAreaStack" /> @@ -41,7 +41,7 @@ <package name="restrictedArea" extends="publicArea"> <interceptors> <interceptor name="login" - class="org.nuiton.wikitty.publication.LoginInterceptor"> + class="org.nuiton.wikitty.publication.interceptor.LoginInterceptor"> <param name="error">/login_input.action</param> </interceptor> <interceptor-stack name="restrictedAreaStack"> Added: trunk/wikitty-publication/src/main/resources/wikitty-publication-ws-jar.properties =================================================================== --- trunk/wikitty-publication/src/main/resources/wikitty-publication-ws-jar.properties (rev 0) +++ trunk/wikitty-publication/src/main/resources/wikitty-publication-ws-jar.properties 2011-07-21 08:38:06 UTC (rev 1082) @@ -0,0 +1,29 @@ +### +# #%L +# Wikitty :: publication +# +# $Id: wikitty-publication-ws-default.properties 823 2011-04-20 14:45:47Z mfortun $ +# $HeadURL: http://svn.nuiton.org/svn/wikitty/trunk/wikitty-publication/src/main/resourc... $ +# %% +# Copyright (C) 2009 - 2010 CodeLutin, Benjamin Poussin +# %% +# 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% +### + + +wikitty.WikittyService.components=org.nuiton.wikitty.publication.externalize.WikittyServiceJarLoader +wikitty.publication.repository.jar=/home/Manou/testWP/pub-externalized.jar + Property changes on: trunk/wikitty-publication/src/main/resources/wikitty-publication-ws-jar.properties ___________________________________________________________________ Added: svn:mime-type + text/plain