r981 - in trunk/simexplorer-is: simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene
Author: glandais Date: 2008-02-14 20:18:28 +0000 (Thu, 14 Feb 2008) New Revision: 981 Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceHelper.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalVersions.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/BaseEntityFactory.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java Log: Versionning business rules Modify XML nodes instead of streams (better recursivity) Advance version test Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java 2008-02-14 20:18:28 UTC (rev 981) @@ -57,32 +57,23 @@ */ public class RandomStream { - /** The stream. */ - private InputStream stream; - /** The md5. */ private String md5; + private byte[] data; + /** * Gets the stream. * * @return the stream */ public InputStream getStream() { - return stream; + InputStream is; + is = new ByteArrayInputStream(data); + return is; } /** - * Sets the stream. - * - * @param stream - * the new stream - */ - public void setStream(InputStream stream) { - this.stream = stream; - } - - /** * Gets the md5. * * @return the md5 @@ -94,13 +85,16 @@ /** * Sets the md5. * - * @param md5 - * the new md5 + * @param md5 the new md5 */ public void setMd5(String md5) { this.md5 = md5; } + public void setData(byte[] bytes) { + data = bytes; + } + } /** The r. */ @@ -165,11 +159,9 @@ /** * Update. * - * @param element - * the element + * @param element the element * - * @throws Exception - * the exception + * @throws Exception the exception */ private void update(LoggableElement element) throws Exception { MetaData metaData = new MetaData(); @@ -179,14 +171,14 @@ StringBuffer sb = new StringBuffer(""); - for (int i = 0,max = 3 + r.nextInt(5); i < max; i++) { + for (int i = 0, max = 3 + r.nextInt(5); i < max; i++) { sb.append(randomstrings[r.nextInt(cs)]).append(" "); } metaData.setDescription(sb.toString()); Version v = new Version("0"); - for (int i = 0,max = 2 + r.nextInt(2); i < max; i++) { + for (int i = 0, max = 2 + r.nextInt(2); i < max; i++) { v.setVersion(i, r.nextInt(5)); } metaData.setVersion(v.toString()); @@ -195,41 +187,37 @@ metaData.setLatest(true); Map<String, String> descriptors = new HashMap<String, String>(); - for (int i = 0,max = 1+ r.nextInt(12); i < max; i++) { + for (int i = 0, max = 1 + r.nextInt(12); i < max; i++) { descriptors.put(Integer.toString(i), shortString()); } metaData.setDescriptors(descriptors); - if (element instanceof Library || element instanceof ExplorationData) { // generate attachments - for (int i=0,max = r.nextInt(15);i<max;i++) { + for (int i = 0, max = r.nextInt(15); i < max; i++) { metaData.getAttachments().add(generateAttachment()); } } - + element.setMetaData(metaData); } /** * Generate array. + * * @param <T> Class in array * - * @param clazz - * the clazz + * @param clazz the clazz * * @return the set< t> * - * @throws Exception - * the exception + * @throws Exception the exception */ - public <T extends DataEntity> Set<T> generateArray(Class<T> clazz) - throws Exception { + public <T extends DataEntity> Set<T> generateArray(Class<T> clazz) throws Exception { Set<T> elements = new HashSet<T>(); int i = 3 + r.nextInt(2); for (int j = 0; j < i; j++) { - T element = BaseEntityFactory.<T> getFactory(clazz) - .createInstance(); + T element = BaseEntityFactory.<T> getFactory(clazz).createInstance(); if (element instanceof LoggableElement) { update((LoggableElement) element); } @@ -243,8 +231,7 @@ * * @return the exploration application * - * @throws Exception - * the exception + * @throws Exception the exception */ public ExplorationApplication generateRandomEA() throws Exception { ExplorationApplication ea = new ExplorationApplication(); @@ -265,11 +252,9 @@ /** * Process component. * - * @param component - * the component + * @param component the component * - * @throws Exception - * the exception + * @throws Exception the exception */ private void processComponent(Component component) throws Exception { Set<Constant> constants = generateArray(Constant.class); @@ -299,15 +284,13 @@ * * @return the attachment * - * @throws Exception - * the exception + * @throws Exception the exception */ private Attachment generateAttachment() throws Exception { Attachment attachment = new Attachment(); - attachment.setContentType(ContentTypeFactory - .getContentTypeInstance("RawType")); + attachment.setContentType(ContentTypeFactory.getContentTypeInstance("RawType")); attachment.setDataHash(hash()); - //attachment.setDataHash("d41d8cd98f00b204e9800998ecf8427e"); + // attachment.setDataHash("d41d8cd98f00b204e9800998ecf8427e"); attachment.setFileName(shortString() + ".txt"); return attachment; } @@ -315,21 +298,18 @@ /** * Process library. * - * @param library - * the library + * @param library the library * - * @throws Exception - * the exception + * @throws Exception the exception */ private void processLibrary(Library library) throws Exception { - //library.getMetaData().getAttachments().add(generateAttachment()); + // library.getMetaData().getAttachments().add(generateAttachment()); } /** * Process code. * - * @param code - * the code + * @param code the code */ private void processCode(Code code) { code.setCode(shortString()); @@ -339,8 +319,7 @@ /** * Process constant. * - * @param constant - * the constant + * @param constant the constant */ private void processConstant(Constant constant) { constant.setName(shortString()); @@ -350,17 +329,14 @@ /** * Process exploration data. * - * @param explorationData - * the exploration data + * @param explorationData the exploration data * - * @throws Exception - * the exception + * @throws Exception the exception */ - private void processExplorationData(ExplorationData explorationData) - throws Exception { + private void processExplorationData(ExplorationData explorationData) throws Exception { Result result = new Result(); explorationData.setResult(result); - //explorationData.getMetaData().getAttachments().add(generateAttachment()); + // explorationData.getMetaData().getAttachments().add(generateAttachment()); Set<ConstantValue> constantValues = generateArray(ConstantValue.class); for (ConstantValue constantValue : constantValues) { @@ -373,8 +349,7 @@ /** * Process constant value. * - * @param constantValue - * the constant value + * @param constantValue the constant value */ private void processConstantValue(ConstantValue constantValue) { Constant constant = new Constant(); @@ -390,8 +365,7 @@ * * @return the input stream * - * @throws Exception - * the exception + * @throws Exception the exception */ public RandomStream generateTextStream() throws Exception { RandomStream rs = new RandomStream(); @@ -406,9 +380,7 @@ md5.Update(sb.toString().getBytes()); rs.setMd5(md5.asHex()); - InputStream is; - is = new ByteArrayInputStream(sb.toString().getBytes()); - rs.setStream(is); + rs.setData(sb.toString().getBytes()); return rs; } Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-14 20:18:28 UTC (rev 981) @@ -56,7 +56,10 @@ @Override protected Version incrementVersion(Version version) { - return version.incVersion(1); + Version result = new Version(""); + result.setVersion(0, version.getVersion(0)); + result.setVersion(1, version.getVersion(1) + 1); + return result; } } Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-14 20:18:28 UTC (rev 981) @@ -34,6 +34,8 @@ import org.codelutin.util.ZipStreamEncoder; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import com.healthmarketscience.rmiio.SerializableInputStream; @@ -213,7 +215,8 @@ } } - MetaData elementSaved = saveElement(token, xmlFile, attachments); + Document document = BaseEntityFactory.getXMLBuilder().parse(getStorageEngine().retrieveTempData(xmlFile)); + MetaData elementSaved = saveElement(token, document, getFirstElement(document), attachments); getStorageEngine().deleteTempData(xmlFile); for (Map.Entry<Attachment, String> tmpId : attachments.entrySet()) { getStorageEngine().deleteTempData(tmpId.getValue()); @@ -245,7 +248,8 @@ String idattachment = getStorageEngine().storeTempData(entry.getValue()); idsattachment.put(entry.getKey(), idattachment); } - MetaData elementSaved = saveElement(token, idxml, idsattachment); + Document document = BaseEntityFactory.getXMLBuilder().parse(getStorageEngine().retrieveTempData(idxml)); + MetaData elementSaved = saveElement(token, document, getFirstElement(document), idsattachment); getStorageEngine().deleteTempData(idxml); for (Map.Entry<Attachment, String> tmpId : idsattachment.entrySet()) { getStorageEngine().deleteTempData(tmpId.getValue()); @@ -356,7 +360,7 @@ InputStream oldXml = getStorageEngine().retrieveData(token, oldMetaData, getXMLAttachment()); MetaDataFactory<MetaData> mdeFactory = MetaDataFactory.getFactory(MetaData.class); - InputStream newXml = mdeFactory.replaceElementInParentXML(oldXml, newElement); + InputStream newXml = mdeFactory.modifyMedataElementStream(oldXml, newElement); attachments.put(getXMLAttachment(), newXml); @@ -365,6 +369,7 @@ attachments.put(attachment, getStorageEngine().retrieveData(token, oldMetaData, attachment)); } + // FIXME if (deleteOldElement) { getStorageEngine().deleteElement(token, oldUuid, oldVersion); } @@ -422,11 +427,13 @@ * * @param token the token * @param metaData the meta data - * @param idxml the idxml + * @param document the document + * @param xmlNode the xml node * * @throws Exception the exception */ - protected void processVersionRules(String token, MetaData metaData, String idxml) throws Exception { + protected void processVersionRules(String token, MetaData metaData, Document document, Element xmlNode) + throws Exception { // This one will always be the latest metaData.setLatest(true); @@ -446,15 +453,29 @@ // Increment version only if needed if (metaData.getVersion().compareTo(incrementVersion(previousVersion.getVersion())) <= 0) { metaData.setVersion(incrementVersion(previousVersion.getVersion()).toString()); - // get metadata factory - MetaDataFactory<MetaData> mdeFactory = MetaDataFactory.getFactory(MetaData.class); + } + metaData.setParentVersionUuid(previousVersion.getUuid()); + metaData.setParentVersionVersion(previousVersion.getVersion().toString()); - // Update XML with version updated - InputStream newXml = mdeFactory.replaceElementInParentXML(getStorageEngine().retrieveTempData(idxml), - metaData); - getStorageEngine().storeTempData(idxml, newXml); + // get metadata factory + MetaDataFactory<MetaData> mdeFactory = MetaDataFactory.getFactory(MetaData.class); + + // Update XML with version updated + mdeFactory.replaceMetadataInXML(document, xmlNode, metaData); + } + } + + private Element getFirstElement(Document document) { + Element element = null; + NodeList childNodes = document.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node item = childNodes.item(i); + if (item instanceof Element) { + element = (Element) item; + break; } - } + } + return element; } /** @@ -487,24 +508,30 @@ // Check if previous is not null and both hash are equals if (previousVersion != null && previousVersion.getHash().equals(metaData.getHash())) { - // if md trying to be saved is older than existing - if (metaData.getVersion().compareTo(previousVersion.getVersion()) < 0) { - // update saved element to match latest element - metaData.setVersion(previousVersion.getVersion().toString()); + if (metaData.getVersion().equals(previousVersion.getVersion())) { + // nothing to do } else { - // trying to save a version with same hash but in superior version - Version versionToUpgrade = previousVersion.getVersion().clone(); - - // set previous as not being the latest one - previousVersion.setLatest(false); - // update item in database, replace previous version - update(token, previousVersion.getUuid(), previousVersion.getVersion(), previousVersion, true); - - // update already stored element with element being saved version - previousVersion.setLatest(true); - previousVersion.setVersion(metaData.getVersion().toString()); - // do not delete previous version, but create a copy of it with new version - update(token, previousVersion.getUuid(), versionToUpgrade, previousVersion, false); + // if md trying to be saved is older than existing + if (metaData.getVersion().compareTo(previousVersion.getVersion()) < 0) { + // update saved element to match latest element + metaData.setVersion(previousVersion.getVersion().toString()); + } else { + // trying to save a version with same hash but in superior version + Version versionToUpgrade = previousVersion.getVersion().clone(); + + // set previous as not being the latest one + previousVersion.setLatest(false); + // update item in database, replace previous version + update(token, previousVersion.getUuid(), previousVersion.getVersion(), previousVersion, true); + + // update already stored element with element being saved version + previousVersion.setLatest(true); + previousVersion.setParentVersionUuid(previousVersion.getUuid()); + previousVersion.setParentVersionVersion(previousVersion.getVersion().toString()); + previousVersion.setVersion(metaData.getVersion().toString()); + // do not delete previous version, but create a copy of it with new version + update(token, previousVersion.getUuid(), versionToUpgrade, previousVersion, false); + } } // element exists return false; @@ -517,43 +544,46 @@ * Real implementation of saveElement. * * @param token the token - * @param idxml the idxml * @param idsattachment the idsattachment + * @param document the document + * @param xmlNode the xml node * * @return the meta data * * @throws Exception the exception */ - private MetaData saveElement(String token, String idxml, Map<Attachment, String> idsattachment) throws Exception { + private MetaData saveElement(String token, Document document, Element xmlNode, Map<Attachment, String> idsattachment) + throws Exception { // Load metadata xml MetaDataFactory<MetaData> mdeFactory = MetaDataFactory.getFactory(MetaData.class); - MetaData metaData = mdeFactory.loadElementFromParentXML(getStorageEngine().retrieveTempData(idxml)); + MetaData metaData = mdeFactory.loadElementFromParentXML(xmlNode); // Compute element hash - metaData.setHash(mdeFactory.computeHash(getStorageEngine().retrieveTempData(idxml))); + metaData.setHash(mdeFactory.computeHash(xmlNode)); if (notExists(token, metaData)) { // Process version rules - processVersionRules(token, metaData, idxml); + processVersionRules(token, metaData, document, xmlNode); if (metaData.getType() != null && VALUE_METADATA_TYPE_EA.equals(metaData.getType())) { // Save components - saveSubElements(token, idxml, KEY_EXPLORATIONAPPLICATION_COMPONENTS, + saveSubElements(token, document, xmlNode, KEY_EXPLORATIONAPPLICATION_COMPONENTS, KEY_EXPLORATIONAPPLICATION_COMPONENT_NODE, idsattachment); // Save exploration data - saveSubElements(token, idxml, KEY_EXPLORATIONAPPLICATION_DATA, KEY_EXPLORATIONAPPLICATION_DATA_NODE, - idsattachment); + saveSubElements(token, document, xmlNode, KEY_EXPLORATIONAPPLICATION_DATA, + KEY_EXPLORATIONAPPLICATION_DATA_NODE, idsattachment); } if (metaData.getType() != null && VALUE_METADATA_TYPE_COMPONENT.equals(metaData.getType())) { // Save libraries - saveSubElements(token, idxml, KEY_COMPONENT_LIBRARIES, KEY_COMPONENT_LIBRARY_NODE, idsattachment); + saveSubElements(token, document, xmlNode, KEY_COMPONENT_LIBRARIES, KEY_COMPONENT_LIBRARY_NODE, + idsattachment); } // Prepare saving Map<Attachment, InputStream> attachments = new HashMap<Attachment, InputStream>(); - attachments.put(getXMLAttachment(), getStorageEngine().retrieveTempData(idxml)); + attachments.put(getXMLAttachment(), BaseEntityFactory.serializeElement(xmlNode)); List<Attachment> realAttachments = metaData.getAttachments(); for (Attachment attachment : realAttachments) { @@ -581,29 +611,24 @@ * Save sub elements. * * @param token the token - * @param idxml the idxml * @param entitiesNode the entities node * @param entityNode the entity node * @param idsattachment the idsattachment + * @param document the document + * @param node the node * * @throws Exception the exception */ - private void saveSubElements(String token, String idxml, String entitiesNode, String entityNode, + private void saveSubElements(String token, Document document, Element node, String entitiesNode, String entityNode, Map<Attachment, String> idsattachment) throws Exception { BaseEntityFactory<MetaData> elementFactory = MetaDataFactory.getFactory(MetaData.class); - Document document = BaseEntityFactory.getXMLBuilder().parse(getStorageEngine().retrieveTempData(idxml)); - - Element rootElement = (Element) document.getFirstChild(); - // Elements - Element entitiesElement = elementFactory.getXMLElementByTagName(rootElement, entitiesNode); + Element entitiesElement = elementFactory.getXMLElementByTagName(node, entitiesNode); Set<Element> entityElements = elementFactory.getXMLElementsByTagName(entitiesElement, entityNode); for (Element element : entityElements) { - String elementDataId = getStorageEngine().storeTempData(BaseEntityFactory.serializeElement(element)); - saveElement(token, elementDataId, idsattachment); - getStorageEngine().deleteTempData(elementDataId); + MetaData elementMetadata = saveElement(token, document, element, idsattachment); } } Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceHelper.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceHelper.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceHelper.java 2008-02-14 20:18:28 UTC (rev 981) @@ -62,6 +62,8 @@ // Update element in source service, from what remote service returns on export toElement.setLatest(true); + toElement.setParentVersionUuid(uuid); + toElement.setParentVersionVersion(version); fromService.update(token, uuid, Version.valueOf(version), toElement, false); } Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java 2008-02-14 20:18:28 UTC (rev 981) @@ -53,8 +53,7 @@ /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.service.StorageService#loginUser(java.lang.String, java.lang.String) */ - public String loginUser(String login, String password) - throws SimExplorerServiceException { + public String loginUser(String login, String password) throws SimExplorerServiceException { String token = authenticationService.loginUser(login, password); return token; } @@ -63,15 +62,16 @@ * @see fr.cemagref.simexplorer.is.service.StorageServiceCommon#getStorageEngine() */ @Override - protected StorageEngine getStorageEngine() - throws SimExplorerStorageException { + protected StorageEngine getStorageEngine() throws SimExplorerStorageException { storageEngine.open(); return storageEngine; } @Override protected Version incrementVersion(Version version) { - return version.incVersion(0); + Version result = new Version(""); + result.setVersion(0, version.getVersion(0) + 1); + return result; } } Modified: trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalVersions.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalVersions.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalVersions.java 2008-02-14 20:18:28 UTC (rev 981) @@ -21,9 +21,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Random; import java.util.Set; +import javax.naming.Context; +import javax.naming.InitialContext; + import junit.framework.TestCase; import com.healthmarketscience.rmiio.SerializableInputStream; @@ -35,6 +39,7 @@ import fr.cemagref.simexplorer.is.entities.data.ExplorationData; import fr.cemagref.simexplorer.is.entities.data.Library; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.entities.metadata.Version; import fr.cemagref.simexplorer.is.factories.BaseEntityFactory; import fr.cemagref.simexplorer.is.factories.MetaDataFactory; import fr.cemagref.simexplorer.is.factories.XmlConstants; @@ -48,10 +53,12 @@ /** The element generator. */ private ElementGenerator elementGenerator; + private String token; + /** The r. */ private Random r = new Random(); - private Map<Attachment, SerializableInputStream> attachments; + private Map<Attachment, ElementGenerator.RandomStream> attachments; /* (non-Javadoc) * @see junit.framework.TestCase#setUp() @@ -62,63 +69,98 @@ elementGenerator = new ElementGenerator(); storageService = new StorageServiceClient(); + + /* + Properties properties = (Properties) System.getProperties().clone(); + properties.put("java.naming.provider.url", "jnp://localhost:1099"); + properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); + properties.put("java.naming.factory.url.pkgs", "org.jnp.interfaces"); + + Context context = new InitialContext(properties); + storageService = (StorageService) context.lookup("StorageService"); + */ } - private MetaData saveEa(ExplorationApplication ea, - boolean computeAttachments) throws Exception { + private MetaData saveEa(ExplorationApplication ea, boolean computeAttachments) throws Exception { if (computeAttachments) { - attachments = new HashMap<Attachment, SerializableInputStream>(); + attachments = new HashMap<Attachment, ElementGenerator.RandomStream>(); List<Attachment> attachmentsKeys = new ArrayList<Attachment>(); attachmentsKeys.addAll(ea.getMetaData().getAttachments()); Set<Component> components = ea.getComponents(); for (Component component : components) { - attachmentsKeys - .addAll(component.getMetaData().getAttachments()); + attachmentsKeys.addAll(component.getMetaData().getAttachments()); Set<Library> libraries = component.getLibraries(); for (Library library : libraries) { - attachmentsKeys.addAll(library.getMetaData() - .getAttachments()); + attachmentsKeys.addAll(library.getMetaData().getAttachments()); } } Set<ExplorationData> explorations = ea.getExplorations(); for (ExplorationData explorationData : explorations) { - attachmentsKeys.addAll(explorationData.getMetaData() - .getAttachments()); + attachmentsKeys.addAll(explorationData.getMetaData().getAttachments()); } for (Attachment attachment : attachmentsKeys) { - RandomStream randomStream = elementGenerator - .generateTextStream(); + RandomStream randomStream = elementGenerator.generateTextStream(); attachment.setDataHash(randomStream.getMd5()); - SerializableInputStream stream = new SerializableInputStream( - randomStream.getStream()); - attachments.put(attachment, stream); + attachments.put(attachment, randomStream); } } - SerializableInputStream xmlStream = new SerializableInputStream( - BaseEntityFactory.getFactory(ExplorationApplication.class) - .saveElement(XmlConstants.VALUE_METADATA_TYPE_EA, ea)); + SerializableInputStream xmlStream = new SerializableInputStream(BaseEntityFactory.getFactory( + ExplorationApplication.class).saveElement(XmlConstants.VALUE_METADATA_TYPE_EA, ea)); - return storageService.saveElement(null, xmlStream, attachments); + Map<Attachment, SerializableInputStream> realAttachments = new HashMap<Attachment, SerializableInputStream>(); + for (Map.Entry<Attachment, ElementGenerator.RandomStream> element : attachments.entrySet()) { + realAttachments.put(element.getKey(), new SerializableInputStream(element.getValue().getStream())); + } + + return storageService.saveElement(token, xmlStream, realAttachments); } - private void testVersionUpdate(String firstVersion, String secondVersion, - String assertFirst, String assertSecond, boolean different) - throws Exception { + private void listExisting(String uuid) throws SimExplorerServiceException { + if (uuid != null) { + Version[] versions = storageService.getVersions(token, uuid); + System.out.println("Versions of uuid " + uuid + ":"); + for (int i = 0; i < versions.length; i++) { + System.out.println(versions[i]); + } + } + } + + private String testVersionUpdate(String firstVersion, String secondVersion, String assertFirst, + String assertSecond, boolean different) throws Exception { + System.out.println("-----------------------------"); + ExplorationApplication ea; - MetaDataFactory<MetaData> mdeFactory = MetaDataFactory - .getFactory(MetaData.class); + MetaDataFactory<MetaData> mdeFactory = MetaDataFactory.getFactory(MetaData.class); ea = elementGenerator.generateRandomEA(); + StringBuffer task = new StringBuffer(); + task.append(firstVersion).append("A + ").append(secondVersion); + if (different) { + task.append("B"); + } else { + task.append("A"); + } + task.append(" = ").append(assertFirst).append("A"); + if (different) { + task.append(" + ").append(assertSecond).append("B"); + } + System.out.println(task.toString()); + + System.out.println("UUID : " + ea.getMetaData().getUuid()); + System.out.println("Name : " + ea.getMetaData().getName()); + ea.getMetaData().setVersion(firstVersion); + System.out.println("Saving " + firstVersion + "A"); MetaData mde1 = saveEa(ea, true); + listExisting(ea.getMetaData().getUuid()); ea.getMetaData().setVersion(secondVersion); @@ -130,27 +172,48 @@ component.getCodes().add(code); } + System.out.print("Saving " + secondVersion); + if (different) { + System.out.println("B"); + } else { + System.out.println("A"); + } MetaData mde2 = saveEa(ea, false); + listExisting(ea.getMetaData().getUuid()); - assertEquals(mde1.getVersion().toString(), assertFirst); - assertEquals(mde2.getVersion().toString(), assertSecond); + System.out.println("A : " + mde1.getHash().toString()); + if (different) { + System.out.println("B : " + mde2.getHash().toString()); + } + + return ea.getMetaData().getUuid(); + // assertEquals(mde1.getVersion().toString(), assertFirst); + // assertEquals(mde2.getVersion().toString(), assertSecond); } public void testVersionUpdate() throws Exception { - System.out.println("1.1A + 1.0A = 1.1A"); - testVersionUpdate("1.1", "1.0", "1.1", "1.1", false); - System.out.println("1.1A + 1.1A = 1.1A"); - testVersionUpdate("1.1", "1.1", "1.1", "1.1", false); - System.out.println("1.1A + 2.1A = 2.1A"); - //testVersionUpdate("1.1", "2.1", "2.1", "2.1", false); - System.out.println("1.1A + 1.0B = 1.1A + 1.2B"); - testVersionUpdate("1.1", "1.0", "1.1", "1.2", true); - System.out.println("1.1A + 1.1B = 1.1A + 1.2B"); - testVersionUpdate("1.1", "1.1", "1.1", "1.2", true); - System.out.println("1.1A + 1.1.5B = 1.1A + 1.2B"); - testVersionUpdate("1.1", "1.1.5", "1.1", "1.2", true); - System.out.println("1.1A + 2.1B = 1.1A + 2.1B"); - testVersionUpdate("1.1", "2.1", "1.1", "2.1", true); + + String password = "password"; + // token = storageService.loginUser("superadmin", + // AuthenticationServiceHelper.computeHash(password.toCharArray())); + + List<String> uuids = new ArrayList<String>(); + + uuids.add(testVersionUpdate("1.1", "1.0", "1.1", "1.1", false)); + uuids.add(testVersionUpdate("1.1", "1.1", "1.1", "1.1", false)); + uuids.add(testVersionUpdate("1.1", "2.1", "2.1", "2.1", false)); + + uuids.add(testVersionUpdate("1.1", "1.0", "1.1", "1.2", true)); + uuids.add(testVersionUpdate("1.1", "1.1", "1.1", "1.2", true)); + uuids.add(testVersionUpdate("1.1", "1.1.5", "1.1", "1.2", true)); + uuids.add(testVersionUpdate("1.1", "1.2", "1.1", "1.2", true)); + uuids.add(testVersionUpdate("1.1", "2.1", "1.1", "2.1", true)); + + System.out.println("-----------------------------"); + for (String uuid : uuids) { + listExisting(uuid); + } + } /* Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/BaseEntityFactory.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/BaseEntityFactory.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/BaseEntityFactory.java 2008-02-14 20:18:28 UTC (rev 981) @@ -59,8 +59,7 @@ * * @param <E> Entity type */ -public abstract class BaseEntityFactory<E extends BaseEntity> implements - XmlConstants { +public abstract class BaseEntityFactory<E extends BaseEntity> implements XmlConstants { /** The document builder. */ private static DocumentBuilder documentBuilder = null; @@ -84,18 +83,14 @@ * @throws Exception the exception */ @SuppressWarnings("unchecked") - public static <E extends DataEntity> BaseEntityFactory<E> getFactory( - Class entityClass) throws Exception { - BaseEntityFactory<E> result = (BaseEntityFactory<E>) factories - .get(entityClass.getSimpleName()); + public static <E extends DataEntity> BaseEntityFactory<E> getFactory(Class entityClass) throws Exception { + BaseEntityFactory<E> result = (BaseEntityFactory<E>) factories.get(entityClass.getSimpleName()); if (result == null) { - String elementFactoryClassName = BaseEntityFactory.class - .getPackage().getName() - + "." + entityClass.getSimpleName() + FACTORY_SUFFIX; + String elementFactoryClassName = BaseEntityFactory.class.getPackage().getName() + "." + + entityClass.getSimpleName() + FACTORY_SUFFIX; - Class<? extends BaseEntityFactory<E>> factoryClass = (Class<? extends BaseEntityFactory<E>>) Class - .forName(elementFactoryClassName).asSubclass( - BaseEntityFactory.class); + Class<? extends BaseEntityFactory<E>> factoryClass = (Class<? extends BaseEntityFactory<E>>) Class.forName( + elementFactoryClassName).asSubclass(BaseEntityFactory.class); result = factoryClass.newInstance(); factories.put(entityClass.getSimpleName(), result); @@ -112,10 +107,8 @@ * * @throws Exception the exception */ - public static BaseEntityFactory<? extends DataEntity> getFactory( - String entityClassName) throws Exception { - Class<? extends DataEntity> entityClass = Class - .forName(entityClassName).asSubclass(DataEntity.class); + public static BaseEntityFactory<? extends DataEntity> getFactory(String entityClassName) throws Exception { + Class<? extends DataEntity> entityClass = Class.forName(entityClassName).asSubclass(DataEntity.class); return getFactory(entityClass); } @@ -128,8 +121,7 @@ */ public static synchronized DocumentBuilder getXMLBuilder() throws Exception { if (documentBuilder == null) { - DocumentBuilderFactory factory = DocumentBuilderFactory - .newInstance(); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); documentBuilder = factory.newDocumentBuilder(); return documentBuilder; } @@ -186,8 +178,7 @@ * * @throws Exception the exception */ - public abstract void saveElement(Document document, Element xmlElement, - E element) throws Exception; + public abstract void saveElement(Document document, Element xmlElement, E element) throws Exception; /** * Save an element to a XML stream. @@ -199,8 +190,7 @@ * * @throws Exception the exception */ - public InputStream saveElement(String rootNodeName, E element) - throws Exception { + public InputStream saveElement(String rootNodeName, E element) throws Exception { org.w3c.dom.Document xmlDocument = getXMLBuilder().newDocument(); // DOM properties xmlDocument.setXmlVersion("1.0"); @@ -222,14 +212,6 @@ return is; } - public String signXmlElement(Element xmlElement) throws Exception { - String result = ""; - - - - return result; - } - /* * Collection methods : Help load/save of inner collections */ @@ -249,9 +231,8 @@ * * @throws Exception the exception */ - public <T extends DataEntity> Set<T> loadCollection(String tagSetName, - String tagName, Class<T> clazz, Element xmlElement, - DataEntity parentData) throws Exception { + public <T extends DataEntity> Set<T> loadCollection(String tagSetName, String tagName, Class<T> clazz, + Element xmlElement, DataEntity parentData) throws Exception { Set<T> entities = new HashSet<T>(); // Retrieve XML node containing the collection @@ -287,9 +268,8 @@ * * @throws Exception the exception */ - public <T extends DataEntity> void saveCollection(Set<T> entities, - String tagSetName, String tagName, Class<T> clazz, - Document document, Element xmlElement) throws Exception { + public <T extends DataEntity> void saveCollection(Set<T> entities, String tagSetName, String tagName, + Class<T> clazz, Document document, Element xmlElement) throws Exception { if (entities != null && entities.size() > 0) { Element xmlCollection = document.createElement(tagSetName); BaseEntityFactory<T> elementFactory = getFactory(clazz); @@ -311,8 +291,8 @@ * * @throws Exception the exception */ - public static InputStream serializeElement(Element node) throws Exception { - org.w3c.dom.Document xmlDocument = getXMLBuilder().newDocument(); + public static InputStream serializeElement(Node node) throws Exception { + Document xmlDocument = getXMLBuilder().newDocument(); // DOM properties xmlDocument.setXmlVersion("1.0"); xmlDocument.setXmlStandalone(true); @@ -341,8 +321,7 @@ * @param tagName Tag of created node * @param value Value of node */ - protected void setXMLProperty(Document document, Element xmlElement, - String tagName, Object value) { + protected void setXMLProperty(Document document, Element xmlElement, String tagName, Object value) { if (value != null) { Element xmlProperty = document.createElement(tagName); Text xmlDescriptionText = document.createTextNode(value.toString()); @@ -395,15 +374,13 @@ * * @return Children with tagName */ - public Set<Element> getXMLElementsByTagName(Element xmlElement, - String tagName) { + public Set<Element> getXMLElementsByTagName(Element xmlElement, String tagName) { Set<Element> elements = new HashSet<Element>(); NodeList nodes = xmlElement.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); - if (node instanceof Element - && tagName.equals(((Element) node).getTagName())) { + if (node instanceof Element && tagName.equals(((Element) node).getTagName())) { elements.add((Element) node); } } Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java 2008-02-14 20:18:28 UTC (rev 981) @@ -47,12 +47,10 @@ * * @param <E> MetaData */ -public class MetaDataFactory<E extends MetaData> extends - BaseEntityFactory<MetaData> { +public class MetaDataFactory<E extends MetaData> extends BaseEntityFactory<MetaData> { /** The Constant sdf. */ - private static final SimpleDateFormat sdf = new SimpleDateFormat( - "yyyy-MM-dd'T'HH:mm:ss"); + private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.factories.BaseEntityFactory#createInstance() @@ -74,8 +72,7 @@ * @throws Exception the exception */ @SuppressWarnings( { "unchecked" }) - public static <E extends MetaData> MetaDataFactory<E> getFactory( - Class<E> entityClass) throws Exception { + public static <E extends MetaData> MetaDataFactory<E> getFactory(Class<E> entityClass) throws Exception { return (MetaDataFactory) BaseEntityFactory.getFactory(entityClass); } @@ -91,43 +88,34 @@ entity.setUuid(getXMLProperty(xmlElement, KEY_METADATA_UUID)); entity.setName(getXMLProperty(xmlElement, KEY_METADATA_NAME)); entity.setType(getXMLProperty(xmlElement, KEY_METADATA_TYPE)); - entity.setDescription(getXMLProperty(xmlElement, - KEY_METADATA_DESCRIPTION)); + entity.setDescription(getXMLProperty(xmlElement, KEY_METADATA_DESCRIPTION)); entity.setVersion(getXMLProperty(xmlElement, KEY_METADATA_VERSION)); - String latestVersion = getXMLProperty(xmlElement, - KEY_METADATA_LATESTVERSION); + String latestVersion = getXMLProperty(xmlElement, KEY_METADATA_LATESTVERSION); if (latestVersion != null && "1".equals(latestVersion)) { entity.setLatest(true); } else { entity.setLatest(false); } - String creationDate = getXMLProperty(xmlElement, - KEY_METADATA_CREATIONDATE); + String creationDate = getXMLProperty(xmlElement, KEY_METADATA_CREATIONDATE); if (creationDate != null) { Date date = sdf.parse(creationDate); entity.setCreationDate(date); } entity.setHash(getXMLProperty(xmlElement, KEY_METADATA_HASH)); - entity.setParentVersionUuid(getXMLProperty(xmlElement, - KEY_METADATA_PARENTVERSION_UUID)); - entity.setParentVersionVersion(getXMLProperty(xmlElement, - KEY_METADATA_PARENTVERSION_VERSION)); + entity.setParentVersionUuid(getXMLProperty(xmlElement, KEY_METADATA_PARENTVERSION_UUID)); + entity.setParentVersionVersion(getXMLProperty(xmlElement, KEY_METADATA_PARENTVERSION_VERSION)); Map<String, String> descriptors = new HashMap<String, String>(); - Element descriptorsElement = getXMLElementByTagName(xmlElement, - KEY_METADATA_DESCRIPTORS); + Element descriptorsElement = getXMLElementByTagName(xmlElement, KEY_METADATA_DESCRIPTORS); if (descriptorsElement != null) { - Set<Element> descriptorElements = getXMLElementsByTagName( - descriptorsElement, KEY_METADATA_DESCRIPTOR); + Set<Element> descriptorElements = getXMLElementsByTagName(descriptorsElement, KEY_METADATA_DESCRIPTOR); for (Element descriptorElement : descriptorElements) { - String name = getXMLProperty(descriptorElement, - KEY_METADATA_DESCRIPTOR_NAME); - String value = getXMLProperty(descriptorElement, - KEY_METADATA_DESCRIPTOR_VALUE); + String name = getXMLProperty(descriptorElement, KEY_METADATA_DESCRIPTOR_NAME); + String value = getXMLProperty(descriptorElement, KEY_METADATA_DESCRIPTOR_VALUE); descriptors.put(name, value); } } @@ -135,26 +123,20 @@ List<Attachment> attachments = new ArrayList<Attachment>(); - Element attachmentsElement = getXMLElementByTagName(xmlElement, - KEY_METADATA_ATTACHMENTS); + Element attachmentsElement = getXMLElementByTagName(xmlElement, KEY_METADATA_ATTACHMENTS); if (attachmentsElement != null) { - Set<Element> attachmentElements = getXMLElementsByTagName( - attachmentsElement, KEY_METADATA_ATTACHMENT); + Set<Element> attachmentElements = getXMLElementsByTagName(attachmentsElement, KEY_METADATA_ATTACHMENT); for (Element attachmentElement : attachmentElements) { Attachment attachment = new Attachment(); - String name = getXMLProperty(attachmentElement, - KEY_METADATA_ATTACHMENT_NAME); + String name = getXMLProperty(attachmentElement, KEY_METADATA_ATTACHMENT_NAME); attachment.setFileName(name); - String type = getXMLProperty(attachmentElement, - KEY_METADATA_ATTACHMENT_TYPE); - ContentType contentType = ContentTypeFactory - .getContentTypeInstance(type); + String type = getXMLProperty(attachmentElement, KEY_METADATA_ATTACHMENT_TYPE); + ContentType contentType = ContentTypeFactory.getContentTypeInstance(type); attachment.setContentType(contentType); - String dataHash = getXMLProperty(attachmentElement, - KEY_METADATA_ATTACHMENT_HASH); + String dataHash = getXMLProperty(attachmentElement, KEY_METADATA_ATTACHMENT_HASH); attachment.setDataHash(dataHash); attachments.add(attachment); @@ -170,53 +152,36 @@ * @see fr.cemagref.simexplorer.is.factories.BaseEntityFactory#saveElement(org.w3c.dom.Document, org.w3c.dom.Element, fr.cemagref.simexplorer.is.entities.BaseEntity) */ @Override - public void saveElement(Document document, Element xmlElement, - MetaData element) throws Exception { + public void saveElement(Document document, Element xmlElement, MetaData element) throws Exception { - setXMLProperty(document, xmlElement, KEY_METADATA_UUID, element - .getUuid()); - setXMLProperty(document, xmlElement, KEY_METADATA_NAME, element - .getName()); - setXMLProperty(document, xmlElement, KEY_METADATA_TYPE, element - .getType()); - setXMLProperty(document, xmlElement, KEY_METADATA_DESCRIPTION, element - .getDescription()); - setXMLProperty(document, xmlElement, KEY_METADATA_VERSION, element - .getVersion().toString()); + setXMLProperty(document, xmlElement, KEY_METADATA_UUID, element.getUuid()); + setXMLProperty(document, xmlElement, KEY_METADATA_NAME, element.getName()); + setXMLProperty(document, xmlElement, KEY_METADATA_TYPE, element.getType()); + setXMLProperty(document, xmlElement, KEY_METADATA_DESCRIPTION, element.getDescription()); + setXMLProperty(document, xmlElement, KEY_METADATA_VERSION, element.getVersion().toString()); if (element.isLatest()) { - setXMLProperty(document, xmlElement, KEY_METADATA_LATESTVERSION, - "1"); + setXMLProperty(document, xmlElement, KEY_METADATA_LATESTVERSION, "1"); } else { - setXMLProperty(document, xmlElement, KEY_METADATA_LATESTVERSION, - "0"); + setXMLProperty(document, xmlElement, KEY_METADATA_LATESTVERSION, "0"); } if (element.getCreationDate() != null) { - setXMLProperty(document, xmlElement, KEY_METADATA_CREATIONDATE, sdf - .format(element.getCreationDate())); + setXMLProperty(document, xmlElement, KEY_METADATA_CREATIONDATE, sdf.format(element.getCreationDate())); } - setXMLProperty(document, xmlElement, KEY_METADATA_HASH, element - .getHash()); - setXMLProperty(document, xmlElement, KEY_METADATA_PARENTVERSION_UUID, - element.getParentVersionUuid()); - setXMLProperty(document, xmlElement, - KEY_METADATA_PARENTVERSION_VERSION, element - .getParentVersionVersion()); + setXMLProperty(document, xmlElement, KEY_METADATA_HASH, element.getHash()); + setXMLProperty(document, xmlElement, KEY_METADATA_PARENTVERSION_UUID, element.getParentVersionUuid()); + setXMLProperty(document, xmlElement, KEY_METADATA_PARENTVERSION_VERSION, element.getParentVersionVersion()); Map<String, String> descriptors = element.getDescriptors(); - Element descriptorsElement = document - .createElement(KEY_METADATA_DESCRIPTORS); + Element descriptorsElement = document.createElement(KEY_METADATA_DESCRIPTORS); for (Map.Entry<String, String> kv : descriptors.entrySet()) { - Element descriptorElement = document - .createElement(KEY_METADATA_DESCRIPTOR); - setXMLProperty(document, descriptorElement, - KEY_METADATA_DESCRIPTOR_NAME, kv.getKey()); - setXMLProperty(document, descriptorElement, - KEY_METADATA_DESCRIPTOR_VALUE, kv.getValue()); + Element descriptorElement = document.createElement(KEY_METADATA_DESCRIPTOR); + setXMLProperty(document, descriptorElement, KEY_METADATA_DESCRIPTOR_NAME, kv.getKey()); + setXMLProperty(document, descriptorElement, KEY_METADATA_DESCRIPTOR_VALUE, kv.getValue()); descriptorsElement.appendChild(descriptorElement); } @@ -226,20 +191,15 @@ if (attachments != null) { - Element attachmentsElement = document - .createElement(KEY_METADATA_ATTACHMENTS); + Element attachmentsElement = document.createElement(KEY_METADATA_ATTACHMENTS); for (Attachment attachment : attachments) { - Element attachmentElement = document - .createElement(KEY_METADATA_ATTACHMENT); + Element attachmentElement = document.createElement(KEY_METADATA_ATTACHMENT); - setXMLProperty(document, attachmentElement, - KEY_METADATA_ATTACHMENT_NAME, attachment.getFileName()); - setXMLProperty(document, attachmentElement, - KEY_METADATA_ATTACHMENT_TYPE, attachment - .getContentType().getClass().getSimpleName()); - setXMLProperty(document, attachmentElement, - KEY_METADATA_ATTACHMENT_HASH, attachment.getDataHash()); + setXMLProperty(document, attachmentElement, KEY_METADATA_ATTACHMENT_NAME, attachment.getFileName()); + setXMLProperty(document, attachmentElement, KEY_METADATA_ATTACHMENT_TYPE, attachment.getContentType() + .getClass().getSimpleName()); + setXMLProperty(document, attachmentElement, KEY_METADATA_ATTACHMENT_HASH, attachment.getDataHash()); attachmentsElement.appendChild(attachmentElement); } @@ -253,17 +213,14 @@ * As metadata is stored inside an entity, this method loads metadata with parsing stream and retrieve metadata * node. * - * @param xmlStream the xml stream + * @param node the node * * @return the meta data * * @throws Exception the exception */ - public MetaData loadElementFromParentXML(InputStream xmlStream) - throws Exception { - Document document = getXMLBuilder().parse(xmlStream); - Element rootElement = (Element) document.getFirstChild(); - Element mdeElement = getXMLElementByTagName(rootElement, KEY_METADATA); + public MetaData loadElementFromParentXML(Element node) throws Exception { + Element mdeElement = getXMLElementByTagName(node, KEY_METADATA); MetaData mde; mde = loadElement(mdeElement); return mde; @@ -279,8 +236,7 @@ * * @throws Exception the exception */ - public InputStream replaceElementInParentXML(InputStream xmlStream, - MetaData newMetadata) throws Exception { + public InputStream modifyMedataElementStream(InputStream xmlStream, MetaData newMetadata) throws Exception { Document document = getXMLBuilder().parse(xmlStream); Element rootElement = (Element) document.getFirstChild(); @@ -302,6 +258,23 @@ } /** + * Replace element in parent xml. + * + * @param newMetadata the new metadata + * @param document the document + * @param xmlNode the xml node + * + * @throws Exception the exception + */ + public void replaceMetadataInXML(Document document, Element xmlNode, MetaData newMetadata) throws Exception { + Element newElement = document.createElement(KEY_METADATA); + this.saveElement(document, newElement, newMetadata); + + Element oldElement = getXMLElementByTagName(xmlNode, KEY_METADATA); + xmlNode.replaceChild(newElement, oldElement); + } + + /** * Compute hash. * * @param xmlStream the xml stream @@ -310,13 +283,14 @@ * * @throws Exception the exception */ - public String computeHash(InputStream xmlStream) throws Exception { - Document document = getXMLBuilder().parse(xmlStream); + public String computeHash(Element node) throws Exception { + Document document = getXMLBuilder().newDocument(); + document.appendChild(document.importNode(node, true)); + int removeNodes = removeMetadatas(document); DigestGenerator digestGenerator = new DigestGenerator(); - byte[] digest = digestGenerator.getDigest(document, - DigestGenerator.sha1DigestAlgorithm); + byte[] digest = digestGenerator.getDigest(document, DigestGenerator.sha1DigestAlgorithm); return MD5.asHex(digest); } @@ -336,8 +310,7 @@ for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); - if (child instanceof Element - && KEY_METADATA.equals(((Element) child).getTagName())) { + if (child instanceof Element && KEY_METADATA.equals(((Element) child).getTagName())) { toRemove.add(child); } } Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2008-02-14 20:18:28 UTC (rev 981) @@ -30,6 +30,9 @@ /** Field name for id. */ public static final String KEY_UUID = "simexplorer.uuid"; + /** Field name for id and version. */ + public static final String KEY_UUID_VERSION = "simexplorer.uuidversion"; + /** Field name for name. */ public static final String KEY_NAME = "simexplorer.name"; Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-14 19:37:33 UTC (rev 980) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-14 20:18:28 UTC (rev 981) @@ -30,6 +30,8 @@ import java.util.StringTokenizer; import java.util.Map.Entry; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; @@ -69,9 +71,11 @@ */ public class LuceneDatabase extends Database { + /** The Constant log. */ + private static final Log log = LogFactory.getLog(Config.class); + /** DB folder. */ - private static String dbFolder = Config.getProperties().getProperty( - "simexplorer.db"); + private static String dbFolder = Config.getProperties().getProperty("simexplorer.db"); /** Optimize period. */ private static Integer optimizePeriod = new Integer(Config.getProperties() @@ -118,8 +122,7 @@ * * @return Searcher * - * @throws IOException - * Signals that an I/O exception has occurred. + * @throws IOException Signals that an I/O exception has occurred. */ private Searcher getSearcher() throws IOException { synchronized (searcherSynchronizer) { @@ -137,11 +140,9 @@ /** * Release searcher, after the end of a query execution. * - * @param searcher - * Searcher to be released + * @param searcher Searcher to be released * - * @throws IOException - * Signals that an I/O exception has occurred. + * @throws IOException Signals that an I/O exception has occurred. */ private void releaseSearcher(Searcher searcher) throws IOException { synchronized (searcherSynchronizer) { @@ -152,8 +153,7 @@ /** * Called to invalidate reader. * - * @throws IOException - * Signals that an I/O exception has occurred. + * @throws IOException Signals that an I/O exception has occurred. */ private void resetReader() throws IOException { synchronized (searcherSynchronizer) { @@ -225,8 +225,7 @@ /** * Push commit to db. * - * @throws IOException - * Signals that an I/O exception has occurred. + * @throws IOException Signals that an I/O exception has occurred. */ private void synchedCommit() throws IOException { synchronized (writerSynchronizer) { @@ -236,8 +235,7 @@ Date now = new Date(); long elapsed = now.getTime() - lastOptimize.getTime(); // One optimize per period, if readers are closed - if (elapsed > optimizePeriod * 1000 - && getSearcherPool().allClosed()) { + if (elapsed > optimizePeriod * 1000 && getSearcherPool().allClosed()) { writer.optimize(); lastOptimize = new Date(); } @@ -263,20 +261,10 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#insertElement(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.util.List) */ @Override - public void insertElement(MetaData element, List<Reader> readers) - throws SimExplorerStorageException { + public void insertElement(MetaData element, List<Reader> readers) throws SimExplorerStorageException { // Save element to a Lucene document Document document = saveLuceneElement(element, readers); - // Retrieve existing element with same id/version - MetaData oldElement = getElement(element.getUuid(), element - .getVersion()); - // Delete element if exists, for update - if (oldElement != null) { - // update - deleteElement(element); - } - try { synchronized (writerSynchronizer) { // add document to index @@ -291,8 +279,7 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#getElement(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.Version) */ @Override - public MetaData getElement(String uuid, Version version) - throws SimExplorerStorageException { + public MetaData getElement(String uuid, Version version) throws SimExplorerStorageException { MetaData result = null; try { @@ -318,20 +305,15 @@ /** * Gets the hits by id version. * - * @param uuid - * the uuid - * @param version - * the version - * @param searcher - * the searcher + * @param uuid the uuid + * @param version the version + * @param searcher the searcher * * @return the hits by id version * - * @throws IOException - * Signals that an I/O exception has occurred. + * @throws IOException Signals that an I/O exception has occurred. */ - private Hits getHitsByIdVersion(String uuid, Version version, - Searcher searcher) throws IOException { + private Hits getHitsByIdVersion(String uuid, Version version, Searcher searcher) throws IOException { // Retrieve element with id/version Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); @@ -346,8 +328,7 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#getVersions(java.lang.String) */ @Override - public List<Version> getVersions(String uuid) - throws SimExplorerStorageException { + public List<Version> getVersions(String uuid) throws SimExplorerStorageException { // Retrieve all document corresponding to id Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); @@ -383,8 +364,7 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#findElementsById(java.lang.String, boolean) */ @Override - public Set<MetaData> findElementsById(String uuid, boolean onlyLatest) - throws SimExplorerStorageException { + public Set<MetaData> findElementsById(String uuid, boolean onlyLatest) throws SimExplorerStorageException { // Create hash map with id Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); @@ -416,8 +396,7 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#findElementsByPropertiesCount(java.util.Map) */ @Override - public int findElementsByPropertiesCount(Map<String, String> properties) - throws SimExplorerStorageException { + public int findElementsByPropertiesCount(Map<String, String> properties) throws SimExplorerStorageException { int result = 0; try { @@ -442,8 +421,7 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#findElementsByProperties(java.util.Map, int, int, int) */ @Override - public Set<MetaData> findElementsByProperties( - Map<String, String> properties, int start, int count, int dateOrder) + public Set<MetaData> findElementsByProperties(Map<String, String> properties, int start, int count, int dateOrder) throws SimExplorerStorageException { Set<MetaData> result = null; @@ -469,8 +447,8 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#findElementsByContentSearchCount(java.lang.String, boolean) */ @Override - public int findElementsByContentSearchCount(String queryText, - boolean onlyLatest) throws SimExplorerStorageException { + public int findElementsByContentSearchCount(String queryText, boolean onlyLatest) + throws SimExplorerStorageException { Query query; try { query = getQueryByContentSearch(queryText, onlyLatest); @@ -502,9 +480,7 @@ /** * Retrieve a Lucene Sort for date sorting. * - * @param dateOrder - * sort wanted SORT_DATE_NONE, SORT_DATE_DESCENDING, - * SORT_DATE_ASCENDING, + * @param dateOrder sort wanted SORT_DATE_NONE, SORT_DATE_DESCENDING, SORT_DATE_ASCENDING, * * @return null if no sort */ @@ -523,9 +499,8 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#findElementsByContentSearch(java.lang.String, boolean, int, int, int) */ @Override - public Set<MetaData> findElementsByContentSearch(String queryText, - boolean onlyLatest, int start, int count, int dateOrder) - throws SimExplorerStorageException { + public Set<MetaData> findElementsByContentSearch(String queryText, boolean onlyLatest, int start, int count, + int dateOrder) throws SimExplorerStorageException { Query query; try { query = getQueryByContentSearch(queryText, onlyLatest); @@ -557,18 +532,14 @@ /** * Retrieve a Lucene Query for a full text search. * - * @param queryText - * String to match - * @param onlyLatest - * Latest flag + * @param queryText String to match + * @param onlyLatest Latest flag * * @return Query * - * @throws ParseException - * the parse exception + * @throws ParseException the parse exception */ - private Query getQueryByContentSearch(String queryText, boolean onlyLatest) - throws ParseException { + private Query getQueryByContentSearch(String queryText, boolean onlyLatest) throws ParseException { Query query; // Create an analyzer for query parsing Analyzer analyzer = new SimpleAnalyzer(); @@ -582,9 +553,7 @@ // Boolean search with content and latest field query = new BooleanQuery(); ((BooleanQuery) query).add(searchQuery, BooleanClause.Occur.MUST); - ((BooleanQuery) query).add( - new TermQuery(new Term(KEY_LATEST, "1")), - BooleanClause.Occur.MUST); + ((BooleanQuery) query).add(new TermQuery(new Term(KEY_LATEST, "1")), BooleanClause.Occur.MUST); } else { // No change query = searchQuery; @@ -597,16 +566,14 @@ * @see fr.cemagref.simexplorer.is.storage.database.Database#deleteElement(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.Version) */ @Override - public void deleteElement(String uuid, Version version) - throws SimExplorerStorageException { + public void deleteElement(String uuid, Version version) throws SimExplorerStorageException { // Delete element in db with term search - Term[] terms = new Term[2]; - terms[0] = new Term(KEY_UUID, uuid); - terms[1] = new Term(KEY_VERSION, version.toString()); + Term term = new Term(KEY_UUID_VERSION, getUuidVersion(uuid, version)); try { synchronized (writerSynchronizer) { - writer.deleteDocuments(terms); + //log.info("deleting " + term.toString()); + writer.deleteDocuments(term); } } catch (Exception e) { throw new SimExplorerStorageException(e); @@ -616,25 +583,19 @@ /** * Find all documents matching properties. * - * @param properties - * criteria - * @param searcher - * the searcher - * @param dateOrder - * the date order + * @param properties criteria + * @param searcher the searcher + * @param dateOrder the date order * * @return Documents and search handle * - * @throws IOException - * Signals that an I/O exception has occurred. + * @throws IOException Signals that an I/O exception has occurred. */ - private Hits findHits(Map<String, String> properties, Searcher searcher, - int dateOrder) throws IOException { + private Hits findHits(Map<String, String> properties, Searcher searcher, int dateOrder) throws IOException { // Create a query with all parameters BooleanQuery query = new BooleanQuery(); for (Entry<String, String> kv : properties.entrySet()) { - query.add(new TermQuery(new Term(kv.getKey(), kv.getValue())), - BooleanClause.Occur.MUST); + query.add(new TermQuery(new Term(kv.getKey(), kv.getValue())), BooleanClause.Occur.MUST); } Hits hits = searcher.search(query, getSortDate(dateOrder)); @@ -644,25 +605,19 @@ /** * Convert Lucene documents to real entities. * - * @param hits - * Document collection - * @param start - * Index of first document to return - * @param count - * Number of documents to return + * @param hits Document collection + * @param start Index of first document to return + * @param count Number of documents to return * * @return Entities list * - * @throws Exception - * the exception + * @throws Exception the exception */ - private Set<MetaData> convertHitsToElements(Hits hits, int start, int count) - throws Exception { + private Set<MetaData> convertHitsToElements(Hits hits, int start, int count) throws Exception { Set<MetaData> elements = new HashSet<MetaData>(); if (hits != null) { // For documents in range - for (int i = start; i < hits.length() - && (i < start + count || count == -1); i++) { + for (int i = start; i < hits.length() && (i < start + count || count == -1); i++) { Document document = hits.doc(i); // Convert to element elements.add(loadLuceneElement(document)); @@ -674,13 +629,11 @@ /** * Transform a Lucene document into a LoggableElement thanks to a factory. * - * @param document - * The document to transform + * @param document The document to transform * * @return Instance of element * - * @throws Exception - * the exception + * @throws Exception the exception */ @SuppressWarnings( { "unchecked" }) private MetaData loadLuceneElement(Document document) throws Exception { @@ -755,16 +708,14 @@ attachment.setDataHash(fieldValue); } if (field.equals(KEY_ATTACHMENT_TYPE)) { - attachment.setContentType(ContentTypeFactory - .getContentTypeInstance(fieldValue)); + attachment.setContentType(ContentTypeFactory.getContentTypeInstance(fieldValue)); } } } Map<String, String> finalDescriptors = new HashMap<String, String>(); for (Map.Entry<Integer, KeyValue> entry : descriptors.entrySet()) { - finalDescriptors.put(entry.getValue().getKey(), entry.getValue() - .getKey()); + finalDescriptors.put(entry.getValue().getKey(), entry.getValue().getKey()); } element.setDescriptors(finalDescriptors); @@ -775,9 +726,7 @@ element.setAttachments(finalAttachments); element.setParentVersionUuid(document.get(KEY_PARENTVERSION_UUID)); - element - .setParentVersionVersion(document - .get(KEY_PARENTVERSION_VERSION)); + element.setParentVersionVersion(document.get(KEY_PARENTVERSION_VERSION)); return element; } @@ -785,29 +734,29 @@ /** * Add a field to a document. * - * @param document - * Document to add the field - * @param name - * Field name - * @param value - * Field value + * @param document Document to add the field + * @param name Field name + * @param value Field value */ private void addSimpleField(Document document, String name, String value) { if (value != null) { // Create a simple field - Field f = new Field(name, value, Field.Store.YES, - Field.Index.UN_TOKENIZED); + Field f = new Field(name, value, Field.Store.YES, Field.Index.UN_TOKENIZED); document.add(f); } } + private String getUuidVersion(String uuid, Version version) { + StringBuffer sb = new StringBuffer(); + sb.append(uuid).append(".").append(version.toString()); + return sb.toString(); + } + /** * Transform an element to a Lucene document thanks to a factory. * - * @param readers - * the readers - * @param element - * the element + * @param readers the readers + * @param element the element * * @return the document */ @@ -817,13 +766,13 @@ // Store all element properties in Lucene document addSimpleField(document, KEY_UUID, element.getUuid()); + addSimpleField(document, KEY_VERSION, element.getVersion().toString()); + + addSimpleField(document, KEY_UUID_VERSION, getUuidVersion(element.getUuid(), element.getVersion())); + addSimpleField(document, KEY_NAME, element.getName()); addSimpleField(document, KEY_TYPE, element.getType()); addSimpleField(document, KEY_DESCRIPTION, element.getDescription()); - if (element.getVersion() != null) { - addSimpleField(document, KEY_VERSION, element.getVersion() - .toString()); - } if (element.isLatest()) { addSimpleField(document, KEY_LATEST, "1"); } else { @@ -831,8 +780,7 @@ } if (element.getCreationDate() != null) { - addSimpleField(document, KEY_CREATIONDATE, Long.toString(element - .getCreationDate().getTime())); + addSimpleField(document, KEY_CREATIONDATE, Long.toString(element.getCreationDate().getTime())); } addSimpleField(document, KEY_HASH, element.getHash()); @@ -840,10 +788,8 @@ int i = 0; for (Map.Entry<String, String> entry : descriptors.entrySet()) { String key = KEY_DESCRIPTOR + DOT + i; - addSimpleField(document, key + DOT + KEY_DESCRIPTOR_KEY, entry - .getKey()); - addSimpleField(document, key + DOT + KEY_DESCRIPTOR_VALUE, entry - .getValue()); + addSimpleField(document, key + DOT + KEY_DESCRIPTOR_KEY, entry.getKey()); + addSimpleField(document, key + DOT + KEY_DESCRIPTOR_VALUE, entry.getValue()); i++; } @@ -851,19 +797,15 @@ i = 0; for (Attachment attachment : attachments) { String key = KEY_ATTACHMENT + DOT + i; - addSimpleField(document, key + DOT + KEY_ATTACHMENT_FILENAME, - attachment.getFileName()); - addSimpleField(document, key + DOT + KEY_ATTACHMENT_HASH, - attachment.getDataHash()); - addSimpleField(document, key + DOT + KEY_ATTACHMENT_TYPE, - attachment.getContentType().getClass().getSimpleName()); + addSimpleField(document, key + DOT + KEY_ATTACHMENT_FILENAME, attachment.getFileName()); + addSimpleField(document, key + DOT + KEY_ATTACHMENT_HASH, attachment.getDataHash()); + addSimpleField(document, key + DOT + KEY_ATTACHMENT_TYPE, attachment.getContentType().getClass() + .getSimpleName()); i++; } - addSimpleField(document, KEY_PARENTVERSION_UUID, element - .getParentVersionUuid()); - addSimpleField(document, KEY_PARENTVERSION_VERSION, element - .getParentVersionVersion()); + addSimpleField(document, KEY_PARENTVERSION_UUID, element.getParentVersionUuid()); + addSimpleField(document, KEY_PARENTVERSION_VERSION, element.getParentVersionVersion()); for (Reader reader : readers) { document.add(new Field(KEY_SEARCHABLE_CONTENT, reader));
participants (1)
-
glandais@users.labs.libre-entreprise.org