Author: glandais Date: 2008-02-15 10:15:39 +0000 (Fri, 15 Feb 2008) New Revision: 984 Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.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 trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java Log: Saving element hierarchy 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-15 10:15:06 UTC (rev 983) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-15 10:15:39 UTC (rev 984) @@ -526,21 +526,26 @@ // 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(); + if (getStorageEngine().canCreateSubVersion(token, metaData.getUuid())) { + // 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); + // 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); + // 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); + } else { + // element has to be saved as a new element as we don't have rights to update existing item + return true; + } } } // element exists @@ -578,16 +583,16 @@ if (metaData.getType() != null && VALUE_METADATA_TYPE_EA.equals(metaData.getType())) { // Save components - saveSubElements(token, document, xmlNode, KEY_EXPLORATIONAPPLICATION_COMPONENTS, + saveSubElements(token, metaData, document, xmlNode, KEY_EXPLORATIONAPPLICATION_COMPONENTS, KEY_EXPLORATIONAPPLICATION_COMPONENT_NODE, idsattachment); // Save exploration data - saveSubElements(token, document, xmlNode, KEY_EXPLORATIONAPPLICATION_DATA, + saveSubElements(token, metaData, 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, document, xmlNode, KEY_COMPONENT_LIBRARIES, KEY_COMPONENT_LIBRARY_NODE, - idsattachment); + saveSubElements(token, metaData, document, xmlNode, KEY_COMPONENT_LIBRARIES, + KEY_COMPONENT_LIBRARY_NODE, idsattachment); } // Prepare saving @@ -598,15 +603,11 @@ List<Attachment> realAttachments = metaData.getAttachments(); for (Attachment attachment : realAttachments) { String idAttachment = idsattachment.get(attachment); - // Check hash of attachments if (idAttachment == null) { - throw new SimExplorerServiceException(_("simexplorer.service.attachmentnotfund")); - } attachments.put(attachment, getStorageEngine().retrieveTempData(idAttachment)); - } getStorageEngine().saveElement(token, metaData, attachments); @@ -629,8 +630,8 @@ * * @throws Exception the exception */ - private void saveSubElements(String token, Document document, Element node, String entitiesNode, String entityNode, - Map<Attachment, String> idsattachment) throws Exception { + private void saveSubElements(String token, MetaData parent, Document document, Element node, String entitiesNode, + String entityNode, Map<Attachment, String> idsattachment) throws Exception { BaseEntityFactory<MetaData> elementFactory = MetaDataFactory.getFactory(MetaData.class); @@ -638,7 +639,9 @@ Element entitiesElement = elementFactory.getXMLElementByTagName(node, entitiesNode); Set<Element> entityElements = elementFactory.getXMLElementsByTagName(entitiesElement, entityNode); for (Element element : entityElements) { - saveElement(token, document, element, idsattachment); + MetaData elementMetaData = saveElement(token, document, element, idsattachment); + getStorageEngine().saveElementHierarchy(token, parent, elementMetaData); + // FIXME store that this element is used } } Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2008-02-15 10:15:06 UTC (rev 983) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2008-02-15 10:15:39 UTC (rev 984) @@ -37,8 +37,7 @@ /** * Open database connection. * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ public void open() throws SimExplorerStorageException { open(false); @@ -47,111 +46,96 @@ /** * Open database connection. * - * @param create - * Create database + * @param create Create database * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract void open(boolean create) - throws SimExplorerStorageException; + public abstract void open(boolean create) throws SimExplorerStorageException; /** * Close database connection. * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ public abstract void close() throws SimExplorerStorageException; /** * Commit pending modifications. * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ public abstract void commit() throws SimExplorerStorageException; // Create / Update /** - * Insert an element into database Delete existing element with same - * id/version if exists. + * Insert an element into database Delete existing element with same id/version if exists. * - * @param element - * Element to insert - * @param readers - * the readers + * @param element Element to insert + * @param readers the readers * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract void insertElement(MetaData element, List<Reader> readers) - throws SimExplorerStorageException; + public abstract void insertElement(MetaData element, List<Reader> readers) throws SimExplorerStorageException; + /** + * Save element hierarchy, saving the fact that parent uses child + * + * @param parent the parent + * @param child the child + * @throws SimExplorerStorageException + */ + public abstract void saveElementHierarchy(MetaData parent, MetaData child) throws SimExplorerStorageException; + // Read /** * Get an element from its id and its version null if not fund. * - * @param uuid - * Id - * @param version - * version + * @param uuid Id + * @param version version * * @return Fund element, null if not fund * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract MetaData getElement(String uuid, Version version) - throws SimExplorerStorageException; + public abstract MetaData getElement(String uuid, Version version) throws SimExplorerStorageException; /** * Get all version of an element thanks to its id <br> * Empty list if no element with this id. * - * @param uuid - * Id + * @param uuid Id * * @return Versions * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract List<Version> getVersions(String uuid) - throws SimExplorerStorageException; + public abstract List<Version> getVersions(String uuid) throws SimExplorerStorageException; /** * Retrieve all elements by id Empty list if no element with this id. * - * @param uuid - * Id - * @param onlyLatest - * the only latest + * @param uuid Id + * @param onlyLatest the only latest * * @return Element list * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract Set<MetaData> findElementsById(String uuid, - boolean onlyLatest) throws SimExplorerStorageException; + public abstract Set<MetaData> findElementsById(String uuid, boolean onlyLatest) throws SimExplorerStorageException; /** * Retrieve elements with specific properties<br> * Empty list if no element. * - * @param properties - * Matching properties needed + * @param properties Matching properties needed * * @return Element list * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public Set<MetaData> findElementsByProperties(Map<String, String> properties) - throws SimExplorerStorageException { + public Set<MetaData> findElementsByProperties(Map<String, String> properties) throws SimExplorerStorageException { return findElementsByProperties(properties, 0, -1, 0); } @@ -159,50 +143,39 @@ * Retrieve elements with specific properties<br> * Empty list if no element. * - * @param properties - * Matching properties needed - * @param start - * Index of first element returned - * @param count - * Number of elements to return - * @param dateOrder - * the date order + * @param properties Matching properties needed + * @param start Index of first element returned + * @param count Number of elements to return + * @param dateOrder the date order * * @return Element list * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract Set<MetaData> findElementsByProperties( - Map<String, String> properties, int start, int count, int dateOrder) - throws SimExplorerStorageException; + public abstract Set<MetaData> findElementsByProperties(Map<String, String> properties, int start, int count, + int dateOrder) throws SimExplorerStorageException; /** * Retrieve element count specific properties. * - * @param properties - * Matching properties needed + * @param properties Matching properties needed * * @return Number of elements fund * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract int findElementsByPropertiesCount( - Map<String, String> properties) throws SimExplorerStorageException; + public abstract int findElementsByPropertiesCount(Map<String, String> properties) + throws SimExplorerStorageException; /** * Prepare properties for query. * - * @param type - * Type of entity wanted - * @param onlyLatest - * Only wanted elements + * @param type Type of entity wanted + * @param onlyLatest Only wanted elements * * @return Properties for query */ - private Map<String, String> getPropertiesByType(String type, - boolean onlyLatest) { + private Map<String, String> getPropertiesByType(String type, boolean onlyLatest) { Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_TYPE, type); if (onlyLatest) { @@ -214,116 +187,86 @@ /** * Retrieve elements of a certain type. * - * @param type - * Type wanted - * @param onlyLatest - * Only latest elements - * @param start - * Start index - * @param count - * Number of documents - * @param dateOrder - * Sorting + * @param type Type wanted + * @param onlyLatest Only latest elements + * @param start Start index + * @param count Number of documents + * @param dateOrder Sorting * * @return Element list * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public Set<MetaData> findElementsByType(String type, boolean onlyLatest, - int start, int count, int dateOrder) + public Set<MetaData> findElementsByType(String type, boolean onlyLatest, int start, int count, int dateOrder) throws SimExplorerStorageException { - return findElementsByProperties(getPropertiesByType(type, onlyLatest), - start, count, dateOrder); + return findElementsByProperties(getPropertiesByType(type, onlyLatest), start, count, dateOrder); } /** * Number of elements of the type. * - * @param type - * Type wanted - * @param onlyLatest - * Only latest elements + * @param type Type wanted + * @param onlyLatest Only latest elements * * @return Number of elements * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public int findElementsByTypeCount(String type, boolean onlyLatest) - throws SimExplorerStorageException { - return findElementsByPropertiesCount(getPropertiesByType(type, - onlyLatest)); + public int findElementsByTypeCount(String type, boolean onlyLatest) throws SimExplorerStorageException { + return findElementsByPropertiesCount(getPropertiesByType(type, onlyLatest)); } /** * Retrieve elements by content search<br> * Empty list if no element. * - * @param queryText - * Searched text - * @param onlyLatest - * the only latest - * @param start - * Index of first element returned - * @param count - * Number of elements to return - * @param dateOrder - * the date order + * @param queryText Searched text + * @param onlyLatest the only latest + * @param start Index of first element returned + * @param count Number of elements to return + * @param dateOrder the date order * * @return Element list * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract Set<MetaData> findElementsByContentSearch(String queryText, - boolean onlyLatest, int start, int count, int dateOrder) - throws SimExplorerStorageException; + public abstract Set<MetaData> findElementsByContentSearch(String queryText, boolean onlyLatest, int start, + int count, int dateOrder) throws SimExplorerStorageException; /** * Retrieve element count with specific content. * - * @param queryText - * Searched text - * @param onlyLatest - * the only latest + * @param queryText Searched text + * @param onlyLatest the only latest * * @return Number of elements fund * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract int findElementsByContentSearchCount(String queryText, - boolean onlyLatest) throws SimExplorerStorageException; + public abstract int findElementsByContentSearchCount(String queryText, boolean onlyLatest) + throws SimExplorerStorageException; // Delete /** * Delete an a specific element. * - * @param element - * Element to delete + * @param element Element to delete * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public void deleteElement(MetaData element) - throws SimExplorerStorageException { + public void deleteElement(MetaData element) throws SimExplorerStorageException { deleteElement(element.getUuid(), element.getVersion()); } /** * Delete an element by id and version. * - * @param uuid - * Id - * @param version - * Version + * @param uuid Id + * @param version Version * - * @throws SimExplorerStorageException - * the sim explorer storage exception + * @throws SimExplorerStorageException the sim explorer storage exception */ - public abstract void deleteElement(String uuid, Version version) - throws SimExplorerStorageException; + public abstract void deleteElement(String uuid, Version version) throws SimExplorerStorageException; } 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-15 10:15:06 UTC (rev 983) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2008-02-15 10:15:39 UTC (rev 984) @@ -33,6 +33,12 @@ /** Field name for id and version. */ public static final String KEY_UUID_VERSION = "simexplorer.uuidversion"; + /** Field name for id and version. */ + public static final String KEY_UUID_VERSION_PARENT = "simexplorer.parent.uuidversion"; + + /** Field name for id and version. */ + public static final String KEY_UUID_VERSION_CHILD = "simexplorer.child.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-15 10:15:06 UTC (rev 983) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-15 10:15:39 UTC (rev 984) @@ -276,6 +276,24 @@ } /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.storage.database.Database#saveElementHierarchy(fr.cemagref.simexplorer.is.entities.metadata.MetaData, fr.cemagref.simexplorer.is.entities.metadata.MetaData) + */ + @Override + public void saveElementHierarchy(MetaData parent, MetaData child) throws SimExplorerStorageException { + // Save element to a Lucene document + Document document = saveLuceneElementHierarchy(parent, child); + + try { + synchronized (writerSynchronizer) { + // add document to index + writer.addDocument(document); + } + } catch (IOException e) { + throw new SimExplorerStorageException(e); + } + } + + /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.storage.database.Database#getElement(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.Version) */ @Override @@ -572,7 +590,7 @@ try { synchronized (writerSynchronizer) { - //log.info("deleting " + term.toString()); + // log.info("deleting " + term.toString()); writer.deleteDocuments(term); } } catch (Exception e) { @@ -753,6 +771,23 @@ } /** + * Transform an element hierarchy to a Lucene document + * + * @param parent the parent + * @param child the child + * + * @return the document + */ + private Document saveLuceneElementHierarchy(MetaData parent, MetaData child) { + Document document = new Document(); + + addSimpleField(document, KEY_UUID_VERSION_PARENT, getUuidVersion(parent.getUuid(), parent.getVersion())); + addSimpleField(document, KEY_UUID_VERSION_CHILD, getUuidVersion(child.getUuid(), child.getVersion())); + + return document; + } + + /** * Transform an element to a Lucene document thanks to a factory. * * @param readers the readers @@ -813,4 +848,5 @@ return document; } + } Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2008-02-15 10:15:06 UTC (rev 983) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2008-02-15 10:15:39 UTC (rev 984) @@ -69,6 +69,17 @@ throws SimExplorerStorageException; /** + * Save element hierarchy. + * + * @param token the token + * @param parent the parent + * @param child the child + * @throws SimExplorerStorageException + */ + public abstract void saveElementHierarchy(String token, MetaData parent, MetaData child) + throws SimExplorerStorageException; + + /** * Can create sub version for element. * * @param token the token Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java 2008-02-15 10:15:06 UTC (rev 983) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java 2008-02-15 10:15:39 UTC (rev 984) @@ -140,6 +140,13 @@ database.insertElement(element, readers); } + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#saveElementHierarchy(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.MetaData, fr.cemagref.simexplorer.is.entities.metadata.MetaData) + */ + public void saveElementHierarchy(String token, MetaData parent, MetaData child) throws SimExplorerStorageException { + database.saveElementHierarchy(parent, child); + } + // Read /* (non-Javadoc)