Author: glandais Date: 2007-12-07 16:39:19 +0000 (Fri, 07 Dec 2007) New Revision: 56 Added: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java Removed: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/LuceneStorageEngine.java Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/LoggableElement.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/EntityFactory.java trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/FileEntityFactory.java trunk/simexplorer-si-storage/src/test/fr/cemagref/simexplorer/is/storage/test/LuceneDatabaseTestCase.java Log: Commentaires et refactoring Deleted: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java 2007-12-07 16:39:19 UTC (rev 56) @@ -1,61 +0,0 @@ -package fr.cemagref.simexplorer.is.storage.attachment.type; - -import java.io.InputStream; - -/** - * Content handler - * - * @author landais - * - */ -public abstract class ContentType { - - /** - * MIME type - */ - private String mimeType; - /** - * Text description - */ - private String description; - - /** - * Index content - * - * @param is - * Input stream - * @return Indexable text - */ - public abstract InputStream renderToText(InputStream is); - - /** - * @return the mimeType - */ - public String getMimeType() { - return mimeType; - } - - /** - * @param mimeType - * the mimeType to set - */ - public void setMimeType(String mimeType) { - this.mimeType = mimeType; - } - - /** - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * @param description - * the description to set - */ - public void setDescription(String description) { - this.description = description; - } - -} Added: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/type/ContentType.java 2007-12-07 16:39:19 UTC (rev 56) @@ -0,0 +1,61 @@ +package fr.cemagref.simexplorer.is.storage.attachment.type; + +import java.io.InputStream; + +/** + * Content handler + * + * @author landais + * + */ +public abstract class ContentType { + + /** + * MIME type + */ + private String mimeType; + /** + * Text description + */ + private String description; + + /** + * Index content + * + * @param is + * Input stream + * @return Indexable text + */ + public abstract InputStream renderToText(InputStream is); + + /** + * @return the mimeType + */ + public String getMimeType() { + return mimeType; + } + + /** + * @param mimeType + * the mimeType to set + */ + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + + /** + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * @param description + * the description to set + */ + public void setDescription(String description) { + this.description = description; + } + +} Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/Database.java 2007-12-07 16:39:19 UTC (rev 56) @@ -1,13 +1,11 @@ package fr.cemagref.simexplorer.is.storage.database; -import java.io.InputStream; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import fr.cemagref.simexplorer.is.storage.entities.Entity; -import fr.cemagref.simexplorer.is.storage.entities.FileEntity; import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; import fr.cemagref.simexplorer.is.storage.entities.Version; @@ -42,21 +40,8 @@ * Element to insert * @throws Exception */ - public abstract void pushElement(LoggableElement element) throws Exception; + public abstract void insertElement(LoggableElement element) throws Exception; - /** - * Attatch content to an element FIXME move to storage engine - * - * @param entity - * Related entity - * @param field - * Target field - * @param is - * Content - * @throws Exception - */ - public abstract void storeData(FileEntity entity, String field, - InputStream is) throws Exception; // Read @@ -70,7 +55,7 @@ * @return Fund element, null if not fund * @throws Exception */ - public abstract LoggableElement findElement(String uuid, Version version) + public abstract LoggableElement getElement(String uuid, Version version) throws Exception; /** @@ -108,7 +93,7 @@ if (versions.size() > 0) { Version version = versions.get(0); // get element corresponding to latest version - result = findElement(uuid, version); + result = getElement(uuid, version); } else { result = null; } @@ -161,21 +146,8 @@ * @throws Exception */ public abstract Set<LoggableElement> findElementsByContentSearch( - String searchedText) throws Exception; + String queryText) throws Exception; - /** - * Retrieve content - * - * @param entity - * Entity related to content - * @param field - * Unique field for content - * @return Content - * @throws Exception - */ - public abstract InputStream retrieveData(FileEntity entity, String field) - throws Exception; - // Delete /** @@ -190,18 +162,6 @@ } /** - * Delete content - * - * @param entity - * Entity related to content - * @param field - * Unique field for content - * @throws Exception - */ - public abstract void deleteData(FileEntity entity, String field) - throws Exception; - - /** * Delete all elements by id * * @param uuid Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/DatabaseConstants.java 2007-12-07 16:39:19 UTC (rev 56) @@ -41,8 +41,12 @@ */ public static final String KEY_CREATIONDATE = "simexplorer.creationdate"; /** - * Field name for xml content + * Field name for id */ - public static final String KEY_XML = "simexplorer.xml"; + public static final String KEY_PARENTVERSION_UUID = "simexplorer.parentversion.uuid"; + /** + * Field name for id + */ + public static final String KEY_PARENTVERSION_VERSION = "simexplorer.parentversion.version"; } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2007-12-07 16:39:19 UTC (rev 56) @@ -18,9 +18,11 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; +import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; @@ -29,36 +31,59 @@ import fr.cemagref.simexplorer.is.storage.database.Database; import fr.cemagref.simexplorer.is.storage.entities.Entity; -import fr.cemagref.simexplorer.is.storage.entities.FileEntity; import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; import fr.cemagref.simexplorer.is.storage.entities.Version; import fr.cemagref.simexplorer.is.storage.factories.EntityFactory; -import fr.cemagref.simexplorer.is.storage.factories.FileEntityFactory; import fr.cemagref.simexplorer.is.storage.factories.LoggableElementFactory; +/** + * Lucene implementation + * + * @author landais + * + */ public class LuceneDatabase extends Database { + /** + * Content analyzer + */ private Analyzer analyser; + /** + * Index location + */ private String indexDirectory; + /** + * Database writer + */ private IndexWriter writer; + /** + * Init flag + */ private boolean initok = false; @Override public void open() throws Exception { if (!initok) { + // Create a per field analyze analyser = new PerFieldAnalyzerWrapper(new SimpleAnalyzer()); + + // Specific analyzers // analyser.addAnalyzer("firstname", new KeywordAnalyzer()); // analyser.addAnalyzer("lastname", new KeywordAnalyzer()); + // FIXME index location indexDirectory = "./index/"; + // Create index if it does not exist boolean create = false; if (!IndexReader.indexExists(indexDirectory)) { create = true; } + // Create a Store for Lucene DB Directory dir = FSDirectory.getDirectory(indexDirectory); + // Create Lucene indexer writer = new IndexWriter(dir, true, analyser, create); initok = true; } @@ -66,65 +91,94 @@ @Override public void close() throws Exception { + // Close writer writer.close(); } @Override - public void pushElement(LoggableElement element) throws Exception { + public void insertElement(LoggableElement element) throws Exception { + // Add all children to database Set<LoggableElement> children = element.getChildren(); for (LoggableElement loggableElement : children) { - pushElement(loggableElement); + insertElement(loggableElement); } + // Retrieve element factory Class<? extends LoggableElement> clazz = element.getClass(); - LoggableElementFactory<LoggableElement> elementFactory = (LoggableElementFactory<LoggableElement>) LoggableElementFactory .getFactory(clazz); + + // Save element to a Lucene document Document document = saveLuceneElement(elementFactory, element); - LoggableElement oldElement = findElement(element.getUuid(), element + // Retrieve existing element with same id/version + LoggableElement oldElement = getElement(element.getUuid(), element .getVersion()); + // Delete element if exists, for update if (oldElement != null) { // update deleteElement(element); } - InputStream is = new ByteArrayInputStream(document.get(KEY_XML) - .getBytes()); - FileEntityFactory.getHandler().storeData(element, KEY_XML, is); - + // FIXME thread safe + // add document to index writer.addDocument(document); + // write index writer.flush(); } @Override - public LoggableElement findElement(String uuid, Version version) + public LoggableElement getElement(String uuid, Version version) throws Exception { LoggableElement result = null; + // Retrieve element with id/version Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); properties.put(KEY_VERSION, version.toString()); + // Search elements LuceneHits hits = findHits(properties); if (hits.getHits().length() != 0) { + // convert first document to element result = convertDocumentToElement(hits.getHits().doc(0)); } + hits.closeSearcher(); return result; } @Override + public List<Version> getVersions(String uuid) throws Exception { + // Retrieve all document corresponding to id + Map<String, String> properties = new HashMap<String, String>(); + properties.put(KEY_UUID, uuid); + LuceneHits hits = findHits(properties); + + // Add all versions to a list + List<Version> versions = new ArrayList<Version>(); + for (int i = 0; i < hits.getHits().length(); i++) { + Document doc = hits.getHits().doc(i); + versions.add(new Version(doc.get(KEY_VERSION))); + } + + return versions; + } + + @Override public Set<LoggableElement> findElementsById(String uuid) throws Exception { + // Create hash map with id Map<String, String> properties = new HashMap<String, String>(); properties.put(KEY_UUID, uuid); + // Return all elements parsed from documents fund return convertHitsToElements(findHits(properties)); } @Override public Set<LoggableElement> findElementsByProperties( Map<String, String> properties) throws Exception { + // Return all elements parsed from documents fund return convertHitsToElements(findHits(properties)); } @@ -132,26 +186,43 @@ public Set<LoggableElement> findElementsByProperties( Map<String, String> properties, Class<? extends Entity> entityClass) throws Exception { + // Create hash map with properties and entity class name Map<String, String> props = new HashMap<String, String>(); props.putAll(properties); props.put(KEY_CLASSNAME, entityClass.getName()); + // Return all elements parsed from documents fund return convertHitsToElements(findHits(properties)); } + @Override + public Set<LoggableElement> findElementsByContentSearch(String queryText) + throws Exception { + // Create a searcher + Searcher searcher = new IndexSearcher(indexDirectory); + + Analyzer analyzer = new SimpleAnalyzer(); + QueryParser parser = new QueryParser("__all__", analyzer); + Query luceneQuery = parser.parse(queryText); + + // Create a bean with hits and searcher + LuceneHits luceneHits = new LuceneHits(searcher.search(luceneQuery), searcher); + + // Convert hits to elements + Set<LoggableElement> result = convertHitsToElements(luceneHits); + + return result; + } + + @Override public void deleteElements(String uuid) throws Exception { - Set<LoggableElement> set = findElementsById(uuid); - for (LoggableElement loggableElement : set) { - deleteData(loggableElement, KEY_XML); - } + // Delete all documents by term search writer.deleteDocuments(new Term(KEY_UUID, uuid)); writer.flush(); } + @Override public void deleteElement(String uuid, Version version) throws Exception { - - LoggableElement loggableElement = findElement(uuid, version); - deleteData(loggableElement, KEY_XML); - + // Delete element in db with term search Term[] terms = new Term[3]; terms[0] = new Term(KEY_UUID, uuid); terms[1] = new Term(KEY_VERSION, version.toString()); @@ -159,55 +230,89 @@ writer.flush(); } + /** + * Find all documents matching properties + * @param properties criteria + * @return Documents and search handle + * @throws Exception + */ private LuceneHits findHits(Map<String, String> properties) throws Exception { + // Create a searcher Searcher searcher = new IndexSearcher(indexDirectory); - // FIXME disablecoords? - BooleanQuery query = new BooleanQuery(false); + // 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); } + // Create a bean with hits and searcher LuceneHits luceneHits = new LuceneHits(searcher.search(query), searcher); return luceneHits; } + /** + * Convert a Lucene Document to a real entity + * @param document Storage + * @return Element + * @throws Exception + */ private LoggableElement convertDocumentToElement(Document document) throws Exception { + // Retrieve element factory via class name String className = document.get(KEY_CLASSNAME); LoggableElementFactory<LoggableElement> factory = (LoggableElementFactory<LoggableElement>) LoggableElementFactory .getFactory(className); + // Rebuild element return loadLuceneElement(factory, document); } + /** + * Convert all Lucene documents to real entities + * @param hits Document collection + * @return Entities list + * @throws Exception + */ private Set<LoggableElement> convertHitsToElements(LuceneHits hits) throws Exception { + // For all document Set<LoggableElement> elements = new HashSet<LoggableElement>(); for (int i = 0; i < hits.getHits().length(); i++) { Document document = hits.getHits().doc(i); + // Convert to element elements.add(convertDocumentToElement(document)); } + // Close handle hits.closeSearcher(); return elements; } + /** + * Transform a Lucene document into a LoggableElement thanks to a factory + * @param factory The element factory + * @param document The document to transform + * @return Instance of element + * @throws Exception + */ private LoggableElement loadLuceneElement( LoggableElementFactory<LoggableElement> factory, Document document) - throws Exception { - String strXMLElement = document.get(KEY_XML); - - InputStream is = new ByteArrayInputStream(strXMLElement.getBytes()); - - org.w3c.dom.Document xmlDocument = EntityFactory.getBuilder().parse(is); - - LoggableElement element = factory.loadXMLElement((Element) xmlDocument - .getFirstChild()); - + throws Exception { + LoggableElement element = factory.createInstance(); + element.setName(document.get(KEY_NAME)); + // FIXME toutes les metadonnées + return element; } + /** + * Transform an element to a Lucene document thanks to a factory + * @param factory + * @param element + * @return + * @throws Exception + */ private Document saveLuceneElement( LoggableElementFactory<LoggableElement> factory, LoggableElement element) throws Exception { @@ -223,49 +328,10 @@ document.add(new Field(KEY_VERSION, element.getVersion().toString() .toString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); - document.add(new Field(KEY_XML, factory.getElementXML(element), - Field.Store.COMPRESS, Field.Index.NO)); + // FIXME ne pas serializer + // Serialize element into document return document; } - @Override - public List<Version> getVersions(String uuid) throws Exception { - Map<String, String> properties = new HashMap<String, String>(); - properties.put(KEY_UUID, uuid); - LuceneHits hits = findHits(properties); - - List<Version> versions = new ArrayList<Version>(); - for (int i = 0; i < hits.getHits().length(); i++) { - Document doc = hits.getHits().doc(i); - versions.add(new Version(doc.get(KEY_VERSION))); - } - - return versions; - } - - @Override - public void deleteData(FileEntity entity, String field) throws Exception { - FileEntityFactory.getHandler().deleteData(entity, field); - } - - @Override - public InputStream retrieveData(FileEntity entity, String field) - throws Exception { - return FileEntityFactory.getHandler().retrieveData(entity, field); - } - - @Override - public void storeData(FileEntity entity, String field, InputStream is) - throws Exception { - FileEntityFactory.getHandler().storeData(entity, field, is); - } - - @Override - public Set<LoggableElement> findElementsByContentSearch(String searchedText) - throws Exception { - // TODO Auto-generated method stub - return null; - } - } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneHits.java 2007-12-07 16:39:19 UTC (rev 56) @@ -3,33 +3,70 @@ import org.apache.lucene.search.Hits; import org.apache.lucene.search.Searcher; +/** + * Bean handling hits collection and searcher handle + * + * @author landais + * + */ public class LuceneHits { + /** + * Hits collection + */ private Hits hits; + /** + * Searcher handle + */ private Searcher searcher; + /** + * Default constructor + * + * @param hits + * @param searcher + */ public LuceneHits(Hits hits, Searcher searcher) { super(); this.hits = hits; this.searcher = searcher; } + /** + * Close handle + * + * @throws Exception + */ public void closeSearcher() throws Exception { searcher.close(); } + /** + * @return the hits + */ public Hits getHits() { return hits; } + /** + * @param hits + * the hits to set + */ public void setHits(Hits hits) { this.hits = hits; } + /** + * @return the searcher + */ public Searcher getSearcher() { return searcher; } + /** + * @param searcher + * the searcher to set + */ public void setSearcher(Searcher searcher) { this.searcher = searcher; } Copied: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java (from rev 55, trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/LuceneStorageEngine.java) =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/FSStorageEngine.java 2007-12-07 16:39:19 UTC (rev 56) @@ -0,0 +1,159 @@ +package fr.cemagref.simexplorer.is.storage.engine; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import java.util.List; + +import org.apache.lucene.document.Field; +import org.w3c.dom.Element; + +import fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler; +import fr.cemagref.simexplorer.is.storage.attachment.fs.FileSystemAttachmentHandler; +import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; +import fr.cemagref.simexplorer.is.storage.entities.FileEntity; +import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; +import fr.cemagref.simexplorer.is.storage.entities.Version; +import fr.cemagref.simexplorer.is.storage.factories.EntityFactory; +import fr.cemagref.simexplorer.is.storage.factories.FileEntityFactory; +import fr.cemagref.simexplorer.is.storage.factories.LoggableElementFactory; + +public class FSStorageEngine extends StorageEngine { + + private static AttachmentHandler handler; + private static boolean handlerInit = false; + + public static AttachmentHandler getHandler() { + if (!handlerInit) { + handler = new FileSystemAttachmentHandler(); + } + return handler; + } + + @Override + public void close() throws Exception { + database.close(); + } + + @Override + public void storeData(FileEntity entity, String field, InputStream is) + throws Exception { + getHandler().storeData(entity, field, is); + } + + + @Override + public void exportElement(LoggableElement element, OutputStream os) + throws Exception { + LoggableElement completeElement = database.getElement(element + .getUuid(), element.getVersion()); + LoggableElementFactory<LoggableElement> factory = (LoggableElementFactory<LoggableElement>) EntityFactory + .getFactory(completeElement.getClass()); + factory.getElementXML(element, os); + } + + @Override + public LoggableElement getElement(String uuid) throws Exception { + return database.getElementLatestVersion(uuid); + } + + @Override + public LoggableElement getElementVersion(String uuid, Version version) + throws Exception { + + /* + // FIXME + String strXMLElement = document.get(KEY_XML); + InputStream is = new ByteArrayInputStream(strXMLElement.getBytes()); + org.w3c.dom.Document xmlDocument = EntityFactory.getBuilder().parse(is); + LoggableElement element = factory.loadXMLElement((Element) xmlDocument + .getFirstChild()); + */ + + return database.getElement(uuid, version); + } + + @Override + public List<Version> getVersions(String uuid) throws Exception { + return database.getVersions(uuid); + } + + @Override + public LoggableElement importElement(InputStream inputStream) + throws Exception { + LoggableElement importedElement = null; + + database.insertElement(importedElement); + return importedElement; + } + + @Override + public LoggableElement newElement(LoggableElement element) throws Exception { + if (getElement(element.getUuid()) != null) { + throw new Exception(element.getUuid() + " already exist"); + } + element.setVersion("1"); + /* + * FIXME + document.add(new Field(KEY_XML, factory.getElementXML(element), + Field.Store.COMPRESS, Field.Index.NO)); + */ + + database.insertElement(element); + return getElement(element.getUuid()); + } + + @Override + public void open() throws Exception { + database = new LuceneDatabase(); + database.open(); + } + + @Override + public LoggableElement saveElement(LoggableElement element) + throws Exception { + Version version = null; + List<Version> versions = getVersions(element.getUuid()); + if (versions.size() > 0) { + Collections.sort(versions); + Collections.reverse(versions); + + version = versions.get(0); + version.incVersion(0); + } else { + version = new Version("1"); + } + + element.setVersion(version.toString()); + + /* + * FIXME + document.add(new Field(KEY_XML, factory.getElementXML(element), + Field.Store.COMPRESS, Field.Index.NO)); + */ + + database.insertElement(element); + return getElement(element.getUuid()); + } + + @Override + public LoggableElement synchronizeElement(LoggableElement element) + throws Exception { + // TODO Auto-generated method stub + return null; + } + + @Override + public void deleteData(FileEntity entity, String field) throws Exception { + getHandler().deleteData(entity, field); + } + + @Override + public InputStream retrieveData(FileEntity entity, String field) + throws Exception { + return getHandler().retrieveData(entity, field); + } + + +} Deleted: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/LuceneStorageEngine.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/LuceneStorageEngine.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/LuceneStorageEngine.java 2007-12-07 16:39:19 UTC (rev 56) @@ -1,100 +0,0 @@ -package fr.cemagref.simexplorer.is.storage.engine; - -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Collections; -import java.util.List; - -import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; -import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; -import fr.cemagref.simexplorer.is.storage.entities.Version; -import fr.cemagref.simexplorer.is.storage.factories.EntityFactory; -import fr.cemagref.simexplorer.is.storage.factories.LoggableElementFactory; - -public class LuceneStorageEngine extends StorageEngine { - - @Override - public void close() throws Exception { - database.close(); - } - - @Override - public void exportElement(LoggableElement element, OutputStream os) - throws Exception { - LoggableElement completeElement = database.findElement(element - .getUuid(), element.getVersion()); - LoggableElementFactory<LoggableElement> factory = (LoggableElementFactory<LoggableElement>) EntityFactory - .getFactory(completeElement.getClass()); - factory.getElementXML(element, os); - } - - @Override - public LoggableElement getElement(String uuid) throws Exception { - return database.getElementLatestVersion(uuid); - } - - @Override - public LoggableElement getElementVersion(String uuid, Version version) - throws Exception { - return database.findElement(uuid, version); - } - - @Override - public List<Version> getVersions(String uuid) throws Exception { - return database.getVersions(uuid); - } - - @Override - public LoggableElement importElement(InputStream inputStream) - throws Exception { - LoggableElement importedElement = null; - - database.pushElement(importedElement); - return importedElement; - } - - @Override - public LoggableElement newElement(LoggableElement element) throws Exception { - if (getElement(element.getUuid()) != null) { - throw new Exception(element.getUuid() + " already exist"); - } - element.setVersion("1"); - database.pushElement(element); - return getElement(element.getUuid()); - } - - @Override - public void open() throws Exception { - database = new LuceneDatabase(); - database.open(); - } - - @Override - public LoggableElement saveElement(LoggableElement element) - throws Exception { - Version version = null; - List<Version> versions = getVersions(element.getUuid()); - if (versions.size() > 0) { - Collections.sort(versions); - Collections.reverse(versions); - - version = versions.get(0); - version.incVersion(0); - } else { - version = new Version("1"); - } - - element.setVersion(version.toString()); - - database.pushElement(element); - return getElement(element.getUuid()); - } - - @Override - public LoggableElement synchronizeElement(LoggableElement element) - throws Exception { - // TODO Auto-generated method stub - return null; - } - -} Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2007-12-07 16:39:19 UTC (rev 56) @@ -5,6 +5,7 @@ import java.util.List; import fr.cemagref.simexplorer.is.storage.database.Database; +import fr.cemagref.simexplorer.is.storage.entities.FileEntity; import fr.cemagref.simexplorer.is.storage.entities.LoggableElement; import fr.cemagref.simexplorer.is.storage.entities.Version; @@ -38,4 +39,43 @@ public abstract LoggableElement importElement(InputStream inputStream) throws Exception; + /** + * Attatch content to an element FIXME move to storage engine + * + * @param entity + * Related entity + * @param field + * Target field + * @param is + * Content + * @throws Exception + */ + public abstract void storeData(FileEntity entity, String field, + InputStream is) throws Exception; + + /** + * Retrieve content + * + * @param entity + * Entity related to content + * @param field + * Unique field for content + * @return Content + * @throws Exception + */ + public abstract InputStream retrieveData(FileEntity entity, String field) + throws Exception; + + /** + * Delete content + * + * @param entity + * Entity related to content + * @param field + * Unique field for content + * @throws Exception + */ + public abstract void deleteData(FileEntity entity, String field) + throws Exception; + } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/LoggableElement.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/LoggableElement.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/entities/LoggableElement.java 2007-12-07 16:39:19 UTC (rev 56) @@ -13,86 +13,136 @@ private Date creationDate; private String hash; private Set<Descriptor> descriptors; + private LoggableElement parentVersion; - /* - * private LoggableElement parent; private Set<LoggableElement> children; + public abstract Set<LoggableElement> getChildren(); + + /** + * @return the uuid */ - public String getUuid() { return uuid; } + /** + * @param uuid the uuid to set + */ public void setUuid(String uuid) { this.uuid = uuid; } + /** + * @return the name + */ public String getName() { return name; } + /** + * @param name the name to set + */ public void setName(String name) { this.name = name; } + /** + * @return the type + */ public String getType() { return type; } + /** + * @param type the type to set + */ public void setType(String type) { this.type = type; } + /** + * @return the description + */ public String getDescription() { return description; } + /** + * @param description the description to set + */ public void setDescription(String description) { this.description = description; } + /** + * @return the version + */ public Version getVersion() { return version; } + /** + * @param version the version to set + */ public void setVersion(String version) { this.version = new Version(version); } + /** + * @return the creationDate + */ public Date getCreationDate() { return creationDate; } + /** + * @param creationDate the creationDate to set + */ public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } + /** + * @return the hash + */ public String getHash() { return hash; } + /** + * @param hash the hash to set + */ public void setHash(String hash) { this.hash = hash; } + /** + * @return the descriptors + */ public Set<Descriptor> getDescriptors() { return descriptors; } + /** + * @param descriptors the descriptors to set + */ public void setDescriptors(Set<Descriptor> descriptors) { this.descriptors = descriptors; } - public abstract Set<LoggableElement> getChildren(); + /** + * @return the parentVersion + */ + public LoggableElement getParentVersion() { + return parentVersion; + } - /* - * public LoggableElement getParent() { return parent; } - * - * public void setParent(LoggableElement parent) { this.parent = parent; } - * - * public Set<LoggableElement> getVersionChildren() { return children; } - * - * public void setVersionChildren(Set<LoggableElement> children) { - * this.children = children; } + /** + * @param parentVersion the parentVersion to set */ + public void setParentVersion(LoggableElement parentVersion) { + this.parentVersion = parentVersion; + } + + } Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/EntityFactory.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/EntityFactory.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/EntityFactory.java 2007-12-07 16:39:19 UTC (rev 56) @@ -128,7 +128,9 @@ // Création de l'arborescence du DOM Element racine = xmlDocument.createElement(element.getClass() .getSimpleName()); + saveXMLElement(xmlDocument, racine, element); + xmlDocument.appendChild(racine); DOMSource domSource = new DOMSource(xmlDocument); StreamResult result = new StreamResult(os); Modified: trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/FileEntityFactory.java =================================================================== --- trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/FileEntityFactory.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/java/fr/cemagref/simexplorer/is/storage/factories/FileEntityFactory.java 2007-12-07 16:39:19 UTC (rev 56) @@ -8,14 +8,4 @@ public abstract class FileEntityFactory<E extends FileEntity> extends EntityFactory<E> implements DatabaseConstants { - private static AttachmentHandler handler; - private static boolean handlerInit = false; - - public static AttachmentHandler getHandler() { - if (!handlerInit) { - handler = new FileSystemAttachmentHandler(); - } - return handler; - } - } Modified: trunk/simexplorer-si-storage/src/test/fr/cemagref/simexplorer/is/storage/test/LuceneDatabaseTestCase.java =================================================================== --- trunk/simexplorer-si-storage/src/test/fr/cemagref/simexplorer/is/storage/test/LuceneDatabaseTestCase.java 2007-12-07 14:36:43 UTC (rev 55) +++ trunk/simexplorer-si-storage/src/test/fr/cemagref/simexplorer/is/storage/test/LuceneDatabaseTestCase.java 2007-12-07 16:39:19 UTC (rev 56) @@ -49,9 +49,9 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); - LoggableElement element = database.findElement(uuid1, new Version( + LoggableElement element = database.getElement(uuid1, new Version( "5.12")); assertNotNull(element); assertEquals(description1, element.getDescription()); @@ -61,9 +61,9 @@ explorationData2.setUuid(uuid1); explorationData2.setVersion("5.12"); explorationData2.setDescription(description2); - database.pushElement(explorationData2); + database.insertElement(explorationData2); - assertEquals(description2, database.findElement(uuid1, + assertEquals(description2, database.getElement(uuid1, new Version("5.12")).getDescription()); String description3 = UUID.randomUUID().toString(); @@ -71,9 +71,9 @@ explorationData3.setUuid(uuid1); explorationData3.setVersion("5.13"); explorationData3.setDescription(description3); - database.pushElement(explorationData3); + database.insertElement(explorationData3); - assertEquals(description3, database.findElement(uuid1, + assertEquals(description3, database.getElement(uuid1, new Version("5.13")).getDescription()); } @@ -84,14 +84,14 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); String description2 = UUID.randomUUID().toString(); ExplorationData explorationData2 = new ExplorationData(); explorationData2.setUuid(uuid1); explorationData2.setVersion("5.13"); explorationData2.setDescription(description2); - database.pushElement(explorationData2); + database.insertElement(explorationData2); assertEquals(description2, database.getElementLatestVersion(uuid1) .getDescription()); @@ -101,7 +101,7 @@ explorationData3.setUuid(uuid1); explorationData3.setVersion("6.0"); explorationData3.setDescription(description3); - database.pushElement(explorationData3); + database.insertElement(explorationData3); assertEquals(description3, database.getElementLatestVersion(uuid1) .getDescription()); @@ -114,21 +114,21 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); String description2 = UUID.randomUUID().toString(); ExplorationData explorationData2 = new ExplorationData(); explorationData2.setUuid(uuid1); explorationData2.setVersion("5.13"); explorationData2.setDescription(description2); - database.pushElement(explorationData2); + database.insertElement(explorationData2); String description3 = UUID.randomUUID().toString(); ExplorationData explorationData3 = new ExplorationData(); explorationData3.setUuid(uuid1); explorationData3.setVersion("6.0"); explorationData3.setDescription(description3); - database.pushElement(explorationData3); + database.insertElement(explorationData3); Set<LoggableElement> elements = database.findElementsById(uuid1); assertEquals(3, elements.size()); @@ -144,20 +144,20 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); ExplorationData explorationData2 = new ExplorationData(); explorationData2.setUuid(uuid1); explorationData2.setVersion("5.13"); explorationData2.setDescription(description1); - database.pushElement(explorationData2); + database.insertElement(explorationData2); String description3 = UUID.randomUUID().toString(); ExplorationData explorationData3 = new ExplorationData(); explorationData3.setUuid(uuid1); explorationData3.setVersion("6.0"); explorationData3.setDescription(description3); - database.pushElement(explorationData3); + database.insertElement(explorationData3); // TODO add some other classes in db @@ -180,20 +180,20 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); ExplorationData explorationData2 = new ExplorationData(); explorationData2.setUuid(uuid1); explorationData2.setVersion("5.13"); explorationData2.setDescription(description1); - database.pushElement(explorationData2); + database.insertElement(explorationData2); String description3 = UUID.randomUUID().toString(); ExplorationData explorationData3 = new ExplorationData(); explorationData3.setUuid(uuid1); explorationData3.setVersion("6.0"); explorationData3.setDescription(description3); - database.pushElement(explorationData3); + database.insertElement(explorationData3); Map<String, String> properties = new HashMap<String, String>(); properties.put(DatabaseConstants.KEY_UUID, uuid1); @@ -213,10 +213,10 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); database.deleteElement(explorationData1); - assertNull(database.findElement(uuid1, new Version("5.12"))); + assertNull(database.getElement(uuid1, new Version("5.12"))); } @@ -228,21 +228,21 @@ explorationData1.setUuid(uuid1); explorationData1.setVersion("5.12"); explorationData1.setDescription(description1); - database.pushElement(explorationData1); + database.insertElement(explorationData1); String description2 = UUID.randomUUID().toString(); ExplorationData explorationData2 = new ExplorationData(); explorationData2.setUuid(uuid1); explorationData2.setVersion("5.13"); explorationData2.setDescription(description2); - database.pushElement(explorationData2); + database.insertElement(explorationData2); String description3 = UUID.randomUUID().toString(); ExplorationData explorationData3 = new ExplorationData(); explorationData3.setUuid(uuid1); explorationData3.setVersion("6.0"); explorationData3.setDescription(description3); - database.pushElement(explorationData3); + database.insertElement(explorationData3); database.deleteElements(uuid1);