Author: glandais Date: 2008-01-22 14:11:00 +0000 (Tue, 22 Jan 2008) New Revision: 413 Removed: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java Log: Deleted: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2008-01-22 14:09:33 UTC (rev 412) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2008-01-22 14:11:00 UTC (rev 413) @@ -1,330 +0,0 @@ -package fr.cemagref.simexplorer.is.storage.engine; - -import java.io.InputStream; -import java.io.Reader; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; - -import fr.cemagref.simexplorer.is.contenttype.ContentType; -import fr.cemagref.simexplorer.is.entities.metadata.MetaDataEntity; -import fr.cemagref.simexplorer.is.entities.metadata.Version; -import fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler; -import fr.cemagref.simexplorer.is.storage.attachment.FileSystemAttachmentHandler; -import fr.cemagref.simexplorer.is.storage.database.Database; -import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; - -/** - * Handle data storage and indexing - * - * @author landais - * - */ -public class StorageEngine { - - /** - * Indexing - */ - protected Database database; - - /** - * Storage - */ - protected AttachmentHandler attachmentHandler; - - /** - * Metadata handling temporary data - */ - private MetaDataEntity mdTmp; - - /** - * Default constructor with default implementations of storage and indexing - */ - public StorageEngine() { - super(); - // Indexing - database = new LuceneDatabase(); - // Storage - attachmentHandler = new FileSystemAttachmentHandler(); - // Storing tmp data - mdTmp = new MetaDataEntity(); - mdTmp.setUuid(UUID.randomUUID().toString()); - mdTmp.setVersion("0"); - } - - /** - * Open storage - * - * @throws Exception - */ - public void open() throws Exception { - database.open(); - } - - /** - * Close storage - * - * @throws Exception - */ - public void close() throws Exception { - database.close(); - } - - /** - * Commit changes to storage - * - * @throws Exception - */ - public void commit() throws Exception { - database.commit(); - } - - // Create / update - - /** - * Save an element to storage - * - * @param element - * Element to save - * @param attachments - * Attachments related - * @throws Exception - */ - public void saveElement(MetaDataEntity element, - Map<String, InputStream> attachments) throws Exception { - - // Save all attachments in system - for (Map.Entry<String, InputStream> entry : attachments.entrySet()) { - attachmentHandler.storeData(element, entry.getKey(), entry - .getValue()); - } - - // Parse all attachments for indexing - List<Reader> readers = new ArrayList<Reader>(); - for (Map.Entry<String, InputStream> entry : attachments.entrySet()) { - String field = entry.getKey(); - InputStream content = attachmentHandler - .retrieveData(element, field); - if (element.getAttachments() != null) { - ContentType contentType = element.getAttachments().get(field); - if (contentType != null) { - // Transform stream into indexable text - Reader reader = contentType.renderToText(content); - readers.add(reader); - } - } - } - - if (element.getName() != null) { - Reader reader = new StringReader(element.getName()); - readers.add(reader); - } - if (element.getDescription() != null) { - Reader reader = new StringReader(element.getDescription()); - readers.add(reader); - } - - // Insert element in database - database.insertElement(element, readers); - } - - // Read - - /** - * Retrieve an element - * - * @param uuid - * Id of the element - * @return The element - * @throws Exception - */ - public MetaDataEntity getMetadata(String uuid) throws Exception { - MetaDataEntity mde = null; - Set<MetaDataEntity> set = database.findElementsById(uuid, true); - if (!set.isEmpty()) { - mde = set.iterator().next(); - } - return mde; - } - - /** - * Retrieve versions of an element<br> - * Empty list if no element with this id - * - * @param uuid - * Id of the element - * @return List of versions of the element<br> - * - * @throws Exception - */ - public List<Version> getVersions(String uuid) throws Exception { - return database.getVersions(uuid); - } - - /** - * Retrieve an element in a specific version - * - * @param uuid - * @param version - * @return - * @throws Exception - */ - public MetaDataEntity getMetadata(String uuid, Version version) - throws Exception { - MetaDataEntity mde = database.getElement(uuid, version); - return mde; - } - - /** - * Get data associated to an element - * - * @param entity - * Element related - * @param field - * Data field - * @return Data stream - * @throws Exception - */ - public InputStream retrieveData(MetaDataEntity entity, String field) - throws Exception { - InputStream result = attachmentHandler.retrieveData(entity, field); - return result; - } - - /** - * Get number of items corresponding to query - * - * @param query - * @param onlyLatest - * @return - * @throws Exception - */ - public int findFullTextCount(String query, boolean onlyLatest) - throws Exception { - return database.findElementsByContentSearchCount(query, onlyLatest); - } - - /** - * Retrieve list of items corresponding to query - * - * @param query - * @param onlyLatest - * @param indexStart - * @param count - * @param dateOrder - * @return - * @throws Exception - */ - public MetaDataEntity[] findFullText(String query, boolean onlyLatest, - int indexStart, int count, int dateOrder) throws Exception { - MetaDataEntity[] result = database.findElementsByContentSearch(query, - onlyLatest, indexStart, count, dateOrder).toArray( - new MetaDataEntity[0]); - return result; - } - - /** - * Retrieve list of items of type wanted - * - * @param type - * @param onlyLatest - * @param start - * @param count - * @param dateOrder - * @return - * @throws Exception - */ - public MetaDataEntity[] findElementsByType(String type, boolean onlyLatest, - int start, int count, int dateOrder) throws Exception { - MetaDataEntity[] result = database.findElementsByType(type, onlyLatest, - start, count, dateOrder).toArray(new MetaDataEntity[0]); - return result; - } - - /** - * Retrieve number of items of type wanted - * - * @param type - * @param onlyLatest - * @return - * @throws Exception - */ - public int findElementsByTypeCount(String type, boolean onlyLatest) - throws Exception { - return database.findElementsByTypeCount(type, onlyLatest); - } - - // Delete - - /** - * Delete elements - * - * @param uuid - * @throws Exception - */ - public void deleteElements(String uuid) throws Exception { - List<Version> versions = getVersions(uuid); - for (Version version : versions) { - deleteElement(uuid, version); - } - } - - /** - * Delete one element - * - * @param uuid - * @param version - * @throws Exception - */ - public void deleteElement(String uuid, Version version) throws Exception { - MetaDataEntity element = getMetadata(uuid, version); - Map<String, ContentType> attachments = element.getAttachments(); - for (Map.Entry<String, ContentType> entry : attachments.entrySet()) { - attachmentHandler.deleteData(element, entry.getKey()); - } - database.deleteElement(element); - } - - // Tools - - /** - * Store temporary data, for stream duplication - * - * @param stream - * Stream to store - * @return id for retrieval - * @throws Exception - */ - public String storeTempData(InputStream stream) throws Exception { - String id = UUID.randomUUID().toString(); - attachmentHandler.storeData(mdTmp, id, stream); - return id; - } - - /** - * Retrieve temporary data - * - * @param id - * Id associated - * @return Data stream - * @throws Exception - */ - public InputStream retrieveTempData(String id) throws Exception { - InputStream is = attachmentHandler.retrieveData(mdTmp, id); - return is; - } - - /** - * Delete temporary data - * - * @param id - * @throws Exception - */ - public void deleteTempData(String id) throws Exception { - attachmentHandler.deleteData(mdTmp, id); - } - -}