Author: glandais Date: 2008-02-12 18:20:30 +0000 (Tue, 12 Feb 2008) New Revision: 873 Added: trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalReplace.java Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.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/StorageServiceServer.java trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceMassInsert.java trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceTest.java trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementImport.java trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/Install.java Log: Usage of magic streams as standard for interface Reformat Update business rules (hash, check previous version) Simple StorageServiceLocal test Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java 2008-02-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java 2008-02-12 18:20:30 UTC (rev 873) @@ -22,8 +22,6 @@ import javax.ejb.Remote; -import com.healthmarketscience.rmiio.RemoteInputStream; - import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.data.LoggableElement; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; @@ -40,67 +38,53 @@ /** * Login user onto system Not used for local usages. * - * @param login - * Login - * @param password - * Password + * @param login Login + * @param password Password * * @return Token as logon evidence * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ String loginUser(String login, String password) throws SimExplorerServiceException; /** - * Save a remote element. + * Save an element. * - * @param token - * the token - * @param zipRemoteStream - * RIIO stream containing data inside a zip + * @param token the token + * @param zipStream the zip stream * * @return Metadata of element imported * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ - MetaData saveElement(String token, RemoteInputStream zipRemoteStream) + MetaData saveElement(String token, SimExplorerServiceStream zipStream) throws SimExplorerServiceException; /** - * Save a remote element. + * Save an element. * - * @param token - * the token - * @param xmlRemoteStream - * RIIO stream containing XML Stream - * @param attachmentsRemoteStream - * RIIO stream containing attached data. Key matches metadata - * attachment keys + * @param token the token + * @param xmlStream the xml stream + * @param attachmentsStream the attachments stream * * @return Metadata of element saved * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ - MetaData saveElement(String token, RemoteInputStream xmlRemoteStream, - Map<Attachment, RemoteInputStream> attachmentsRemoteStream) + MetaData saveElement(String token, SimExplorerServiceStream xmlStream, + Map<Attachment, SimExplorerServiceStream> attachmentsStream) throws SimExplorerServiceException; /** * Retrieve metadata (latest version). * - * @param token - * the token - * @param uuid - * the uuid + * @param token the token + * @param uuid the uuid * * @return the metadata * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ MetaData getMetadata(String token, String uuid) throws SimExplorerServiceException; @@ -108,17 +92,13 @@ /** * Retrieve metadata. * - * @param token - * the token - * @param uuid - * the uuid - * @param version - * the version + * @param token the token + * @param uuid the uuid + * @param version the version * * @return the metadata * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ MetaData getMetadata(String token, String uuid, String version) throws SimExplorerServiceException; @@ -126,17 +106,13 @@ /** * Export XML of element to a stream. * - * @param token - * the token - * @param uuid - * the uuid - * @param version - * the version + * @param token the token + * @param uuid the uuid + * @param version the version * * @return RMIIO stream with data * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ InputStream retrieveElementXML(String token, String uuid, String version) throws SimExplorerServiceException; @@ -144,17 +120,13 @@ /** * Export full element to a stream as a zip. * - * @param token - * the token - * @param uuid - * the uuid - * @param version - * the version + * @param token the token + * @param uuid the uuid + * @param version the version * * @return the input stream * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ InputStream retrieveElementFull(String token, String uuid, String version) throws SimExplorerServiceException; @@ -162,19 +134,14 @@ /** * Retrieve data related to an entity. * - * @param token - * the token - * @param uuid - * the uuid - * @param version - * the version - * @param attachment - * the attachment + * @param token the token + * @param uuid the uuid + * @param version the version + * @param attachment the attachment * * @return the input stream * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ InputStream retrieveElementData(String token, String uuid, String version, Attachment attachment) throws SimExplorerServiceException; @@ -182,19 +149,14 @@ /** * Export element to another service. * - * @param token - * the token - * @param otherService - * the other service - * @param uuid - * the uuid - * @param version - * the version + * @param token the token + * @param otherService the other service + * @param uuid the uuid + * @param version the version * * @return the version * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ Version exportElementTo(String token, StorageService otherService, String uuid, String version) throws SimExplorerServiceException; @@ -202,17 +164,13 @@ /** * Retrieve number of elements matching query. * - * @param token - * the token - * @param query - * the query - * @param onlyLatest - * the only latest + * @param token the token + * @param query the query + * @param onlyLatest the only latest * * @return the int * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ int findFullTextCount(String token, String query, boolean onlyLatest) throws SimExplorerServiceException; @@ -220,23 +178,16 @@ /** * Find elements matching query. * - * @param token - * the token - * @param query - * the query - * @param onlyLatest - * the only latest - * @param indexStart - * the index start - * @param count - * the count - * @param dateOrder - * the date order + * @param token the token + * @param query the query + * @param onlyLatest the only latest + * @param indexStart the index start + * @param count the count + * @param dateOrder the date order * * @return the meta data[] * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ MetaData[] findFullText(String token, String query, boolean onlyLatest, int indexStart, int count, int dateOrder) @@ -245,15 +196,12 @@ /** * Retrieve number of applications. * - * @param token - * the token - * @param onlyLatest - * the only latest + * @param token the token + * @param onlyLatest the only latest * * @return the int * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ int findApplicationsCount(String token, boolean onlyLatest) throws SimExplorerServiceException; @@ -261,21 +209,15 @@ /** * Get a list of applications. * - * @param token - * the token - * @param onlyLatest - * the only latest - * @param start - * the start - * @param count - * the count - * @param dateOrder - * the date order + * @param token the token + * @param onlyLatest the only latest + * @param start the start + * @param count the count + * @param dateOrder the date order * * @return the meta data[] * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ MetaData[] findApplications(String token, boolean onlyLatest, int start, int count, int dateOrder) throws SimExplorerServiceException; @@ -283,17 +225,13 @@ /** * Fully load an element. * - * @param token - * the token - * @param uuid - * the uuid - * @param version - * the version + * @param token the token + * @param uuid the uuid + * @param version the version * * @return the element * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ LoggableElement getLoggableElement(String token, String uuid, String version) throws SimExplorerServiceException; @@ -301,15 +239,12 @@ /** * Get all version of an element. * - * @param token - * the token - * @param uuid - * the uuid + * @param token the token + * @param uuid the uuid * * @return the versions * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ Version[] getVersions(String token, String uuid) throws SimExplorerServiceException; @@ -317,15 +252,11 @@ /** * Delete element. * - * @param token - * the token - * @param uuid - * the uuid - * @param version - * the version + * @param token the token + * @param uuid the uuid + * @param version the version * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ void deleteElement(String token, String uuid, String version) throws SimExplorerServiceException; @@ -333,13 +264,10 @@ /** * Delete element. * - * @param token - * the token - * @param uuid - * the uuid + * @param token the token + * @param uuid the uuid * - * @throws SimExplorerServiceException - * the sim explorer service exception + * @throws SimExplorerServiceException the sim explorer service exception */ void deleteElement(String token, String uuid) throws SimExplorerServiceException; 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-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-12 18:20:30 UTC (rev 873) @@ -23,6 +23,7 @@ import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.entities.metadata.Version; import fr.cemagref.simexplorer.is.storage.SimExplorerStorageException; import fr.cemagref.simexplorer.is.storage.engine.StorageEngine; import fr.cemagref.simexplorer.is.storage.engine.StorageEngineImpl; @@ -57,50 +58,9 @@ return storageEngine; } - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageServiceCommon#processVersionRules(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.MetaData) - */ @Override - protected void processVersionRules(String token, MetaData metaData) - throws Exception { - // Check existing version in storage - MetaData previousVersion = getStorageEngine().getMetadata(token, - metaData.getUuid(), metaData.getVersion()); - // Version rules - if (previousVersion != null) { - previousVersion.setLatest(false); - update(token, previousVersion.getUuid(), previousVersion - .getVersion(), previousVersion); - - metaData.setLatest(true); - metaData.setVersion(previousVersion.getVersion().incVersion(1) - .toString()); - } + protected Version incrementVersion(Version version) { + return version.incVersion(1); } - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementData(java.lang.String, java.lang.String, java.lang.String, fr.cemagref.simexplorer.is.attachment.Attachment) - */ - public InputStream retrieveElementData(String token, String uuid, - String version, Attachment attachment) - throws SimExplorerServiceException { - return retrieveElementDataCommon(token, uuid, version, attachment); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementFull(java.lang.String, java.lang.String, java.lang.String) - */ - public InputStream retrieveElementFull(String token, String uuid, - String version) throws SimExplorerServiceException { - return retrieveElementFullCommon(token, uuid, version); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementXML(java.lang.String, java.lang.String, java.lang.String) - */ - public InputStream retrieveElementXML(String token, String uuid, - String version) throws SimExplorerServiceException { - return retrieveElementXMLCommon(token, uuid, version); - } - } 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-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-12 18:20:30 UTC (rev 873) @@ -19,7 +19,6 @@ import static org.codelutin.i18n.I18n._; -import java.io.IOException; import java.io.InputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; @@ -37,9 +36,6 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; -import com.healthmarketscience.rmiio.RemoteInputStream; -import com.healthmarketscience.rmiio.RemoteInputStreamClient; - import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.data.DataEntity; import fr.cemagref.simexplorer.is.entities.data.LoggableElement; @@ -85,43 +81,6 @@ } /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#saveElement(java.lang.String, com.healthmarketscience.rmiio.RemoteInputStream) - */ - public MetaData saveElement(String token, RemoteInputStream zipRemoteStream) - throws SimExplorerServiceException { - InputStream zipStream; - try { - zipStream = RemoteInputStreamClient.wrap(zipRemoteStream); - return saveElement(token, zipStream); - } catch (IOException e) { - throw new SimExplorerServiceException(e); - } - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#saveElement(java.lang.String, com.healthmarketscience.rmiio.RemoteInputStream, java.util.Map) - */ - public MetaData saveElement(String token, - RemoteInputStream xmlRemoteStream, - Map<Attachment, RemoteInputStream> attachmentsRemoteStream) - throws SimExplorerServiceException { - try { - InputStream xmlStream = RemoteInputStreamClient - .wrap(xmlRemoteStream); - Map<Attachment, InputStream> attachmentStreams = new HashMap<Attachment, InputStream>(); - for (Map.Entry<Attachment, RemoteInputStream> entry : attachmentsRemoteStream - .entrySet()) { - InputStream stream = RemoteInputStreamClient.wrap(entry - .getValue()); - attachmentStreams.put(entry.getKey(), stream); - } - return saveElement(token, xmlStream, attachmentStreams); - } catch (Exception e) { - throw new SimExplorerServiceException(e); - } - } - - /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.service.StorageService#getMetadata(java.lang.String, java.lang.String) */ public MetaData getMetadata(String token, String uuid) @@ -232,7 +191,7 @@ * * @throws SimExplorerServiceException the sim explorer service exception */ - private MetaData saveElement(String token, InputStream zipStream) + public MetaData saveElement(String token, SimExplorerServiceStream zipStream) throws SimExplorerServiceException { String xmlFile = null; Map<Attachment, String> attachments = new HashMap<Attachment, String>(); @@ -298,28 +257,34 @@ * * @return the meta data * - * @throws Exception the exception + * @throws SimExplorerServiceException the exception */ - private MetaData saveElement(String token, InputStream xmlFile, - Map<Attachment, InputStream> attachments) throws Exception { - // Store temporary data - String idxml = getStorageEngine().storeTempData(xmlFile); - Map<Attachment, String> idsattachment = new HashMap<Attachment, String>(); - for (Map.Entry<Attachment, InputStream> entry : attachments.entrySet()) { - String idattachment = getStorageEngine().storeTempData( - entry.getValue()); - idsattachment.put(entry.getKey(), idattachment); + public MetaData saveElement(String token, SimExplorerServiceStream xmlFile, + Map<Attachment, SimExplorerServiceStream> attachments) + throws SimExplorerServiceException { + try { + // Store temporary data + String idxml = getStorageEngine().storeTempData(xmlFile); + Map<Attachment, String> idsattachment = new HashMap<Attachment, String>(); + for (Map.Entry<Attachment, SimExplorerServiceStream> entry : attachments + .entrySet()) { + String idattachment = getStorageEngine().storeTempData( + entry.getValue()); + idsattachment.put(entry.getKey(), idattachment); + } + MetaData elementSaved = saveElement(token, idxml, idsattachment); + getStorageEngine().deleteTempData(idxml); + for (Map.Entry<Attachment, String> tmpId : idsattachment.entrySet()) { + getStorageEngine().deleteTempData(tmpId.getValue()); + } + return elementSaved; + } catch (Exception e) { + throw new SimExplorerServiceException(e); } - MetaData elementSaved = saveElement(token, idxml, idsattachment); - getStorageEngine().deleteTempData(idxml); - for (Map.Entry<Attachment, String> tmpId : idsattachment.entrySet()) { - getStorageEngine().deleteTempData(tmpId.getValue()); - } - return elementSaved; } /** - * Retrieve element data common. + * Retrieve element data. * * @param token the token * @param uuid the uuid @@ -330,7 +295,7 @@ * * @throws SimExplorerServiceException the sim explorer service exception */ - protected InputStream retrieveElementDataCommon(String token, String uuid, + public InputStream retrieveElementData(String token, String uuid, String version, Attachment attachment) throws SimExplorerServiceException { try { @@ -338,8 +303,8 @@ InputStream stream = getStorageEngine().retrieveData(token, mde, attachment); - return stream; - } catch (SimExplorerStorageException e) { + return new SimExplorerServiceStream(stream); + } catch (Exception e) { throw new SimExplorerServiceException(e); } } @@ -355,21 +320,21 @@ * * @throws SimExplorerServiceException the sim explorer service exception */ - public InputStream retrieveElementXMLCommon(String token, String uuid, + public InputStream retrieveElementXML(String token, String uuid, String version) throws SimExplorerServiceException { MetaData mde = getMetadata(token, uuid, version); try { InputStream xmlStream = getStorageEngine().retrieveData(token, mde, getXMLAttachment()); - return xmlStream; - } catch (SimExplorerStorageException e) { + return new SimExplorerServiceStream(xmlStream); + } catch (Exception e) { throw new SimExplorerServiceException(e); } } /** - * Retrieve element full common. + * Retrieve element full. * * @param token the token * @param uuid the uuid @@ -379,7 +344,7 @@ * * @throws SimExplorerServiceException the sim explorer service exception */ - public InputStream retrieveElementFullCommon(String token, String uuid, + public InputStream retrieveElementFull(String token, String uuid, String version) throws SimExplorerServiceException { LoggableElement element = getLoggableElement(token, uuid, version); MetaData mde = element.getMetaData(); @@ -406,11 +371,9 @@ ZipStreamEncoder zse = new ZipStreamEncoder(files, pipedOut); zse.start(); - return pipedIn; - } catch (SimExplorerStorageException e) { + return new SimExplorerServiceStream(pipedIn); + } catch (Exception e) { throw new SimExplorerServiceException(e); - } catch (IOException e) { - throw new SimExplorerServiceException(e); } } @@ -435,51 +398,65 @@ .loadElementFromParentXML(getStorageEngine().retrieveTempData( idxml)); - if (metaData.getType() != null - && VALUE_METADATA_TYPE_EA.equals(metaData.getType())) { - // Save components - saveSubElements(token, idxml, - KEY_EXPLORATIONAPPLICATION_COMPONENTS, - KEY_EXPLORATIONAPPLICATION_COMPONENT_NODE, idsattachment); - // Save exploration data - saveSubElements(token, idxml, 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); - } + // Compute element hash + metaData.setHash(mdeFactory.computeHash(getStorageEngine() + .retrieveTempData(idxml))); - // Process version rules - processVersionRules(token, metaData); + if (notExists(token, metaData)) { - // Prepare saving - Map<Attachment, InputStream> attachments = new HashMap<Attachment, InputStream>(); + // Process version rules + if (processVersionRules(token, metaData)) { + // Update XML + InputStream newXml = mdeFactory.replaceElementInParentXML( + getStorageEngine().retrieveTempData(idxml), metaData); + getStorageEngine().storeTempData(idxml, newXml); + } - attachments.put(getXMLAttachment(), getStorageEngine() - .retrieveTempData(idxml)); + if (metaData.getType() != null + && VALUE_METADATA_TYPE_EA.equals(metaData.getType())) { + // Save components + saveSubElements(token, idxml, + KEY_EXPLORATIONAPPLICATION_COMPONENTS, + KEY_EXPLORATIONAPPLICATION_COMPONENT_NODE, + idsattachment); + // Save exploration data + saveSubElements(token, idxml, 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); + } - List<Attachment> realAttachments = metaData.getAttachments(); - for (Attachment attachment : realAttachments) { - String idAttachment = idsattachment.get(attachment); + // Prepare saving + Map<Attachment, InputStream> attachments = new HashMap<Attachment, InputStream>(); - // Check hash of attachments - if (idAttachment == null) { + attachments.put(getXMLAttachment(), getStorageEngine() + .retrieveTempData(idxml)); - throw new SimExplorerServiceException( - _("simexplorer.service.attachmentnotfund")); + 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)); + } - attachments.put(attachment, getStorageEngine().retrieveTempData( - idAttachment)); + getStorageEngine().saveElement(token, metaData, attachments); + getStorageEngine().commit(); + } - getStorageEngine().saveElement(token, metaData, attachments); - getStorageEngine().commit(); - return metaData; } @@ -537,9 +514,16 @@ MetaData oldMetaData = getStorageEngine().getMetadata(token, oldUuid, oldVersion); - attachments.put(getXMLAttachment(), getStorageEngine() - .retrieveData(token, oldMetaData, getXMLAttachment())); + InputStream oldXml = getStorageEngine().retrieveData(token, + oldMetaData, getXMLAttachment()); + MetaDataFactory<MetaData> mdeFactory = MetaDataFactory + .getFactory(MetaData.class); + InputStream newXml = mdeFactory.replaceElementInParentXML(oldXml, + newVersion); + + attachments.put(getXMLAttachment(), newXml); + List<Attachment> realAttachments = oldMetaData.getAttachments(); for (Attachment attachment : realAttachments) { attachments.put(attachment, getStorageEngine().retrieveData( @@ -548,7 +532,7 @@ getStorageEngine().saveElement(token, newVersion, attachments); getStorageEngine().commit(); - } catch (SimExplorerStorageException e) { + } catch (Exception e) { throw new SimExplorerServiceException(e); } } @@ -559,7 +543,9 @@ @Override public Version exportElementTo(String token, StorageService otherService, String uuid, String version) throws SimExplorerServiceException { + // TODO Auto-generated method stub + return null; } @@ -569,11 +555,48 @@ * @param token the token * @param metaData the meta data * + * @return true, if process version rules + * * @throws Exception the exception */ - protected abstract void processVersionRules(String token, MetaData metaData) - throws Exception; + protected boolean processVersionRules(String token, MetaData metaData) + throws Exception { + // Check existing version in storage + MetaData previousVersion = getStorageEngine().getMetadata(token, + metaData.getUuid(), metaData.getVersion()); + // Version rules + if (previousVersion != null) { + previousVersion.setLatest(false); + update(token, previousVersion.getUuid(), previousVersion + .getVersion(), previousVersion); + metaData.setLatest(true); + metaData.setVersion(incrementVersion(previousVersion.getVersion()) + .toString()); + return true; + } + return false; + } + + private boolean notExists(String token, MetaData metaData) throws Exception { + MetaData previousVersion = getStorageEngine().getMetadata(token, + metaData.getUuid(), metaData.getVersion()); + if (previousVersion != null + && previousVersion.getHash().equals(metaData.getHash())) { + return false; + } + return true; + } + + /** + * Increment version. + * + * @param version the version + * + * @return the version + */ + protected abstract Version incrementVersion(Version version); + /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.service.StorageService#getVersions(java.lang.String, java.lang.String) */ @@ -581,14 +604,14 @@ throws SimExplorerServiceException { try { Set<MetaData> versions = getStorageEngine().getElementVersions( - token, uuid); - Iterator<MetaData> iterator = versions.iterator(); - Version[] result = new Version[versions.size()]; + token, uuid); + Iterator<MetaData> iterator = versions.iterator(); + Version[] result = new Version[versions.size()]; int i = 0; for (MetaData metaData : versions) { result[i] = metaData.getVersion(); i++; - } + } return result; } catch (SimExplorerStorageException e) { throw new SimExplorerServiceException(e); 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-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java 2008-02-12 18:20:30 UTC (rev 873) @@ -29,8 +29,8 @@ import com.healthmarketscience.rmiio.RemoteInputStreamServer; import com.healthmarketscience.rmiio.SimpleRemoteInputStream; -import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.entities.metadata.Version; import fr.cemagref.simexplorer.is.storage.SimExplorerStorageException; import fr.cemagref.simexplorer.is.storage.engine.StorageEngine; @@ -69,72 +69,9 @@ return storageEngine; } - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageServiceCommon#processVersionRules(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.MetaData) - */ @Override - protected void processVersionRules(String token, MetaData metaData) - throws Exception { - // Check existing version in storage - MetaData previousVersion = getStorageEngine().getMetadata(token, - metaData.getUuid(), metaData.getVersion()); - // Version rules - if (previousVersion != null) { - previousVersion.setLatest(false); -// update(token, previousVersion.getUuid(), previousVersion -// .getVersion(), previousVersion); - - metaData.setLatest(true); - metaData.setVersion(previousVersion.getVersion().incVersion(0) - .toString()); - } + protected Version incrementVersion(Version version) { + return version.incVersion(0); } - /** - * Wrap stream. - * - * @param streamToWrap - * the stream to wrap - * - * @return the input stream - * - * @throws SimExplorerServiceException - * the sim explorer service exception - */ - private InputStream wrapStream(InputStream streamToWrap) - throws SimExplorerServiceException { - try { - RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( - streamToWrap); - return new SimExplorerServiceStream(remoteStream.export()); - } catch (RemoteException e) { - throw new SimExplorerServiceException(e); - } - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementData(java.lang.String, java.lang.String, java.lang.String, fr.cemagref.simexplorer.is.attachment.Attachment) - */ - public InputStream retrieveElementData(String token, String uuid, - String version, Attachment attachment) - throws SimExplorerServiceException { - return wrapStream(retrieveElementDataCommon(token, uuid, version, - attachment)); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementFull(java.lang.String, java.lang.String, java.lang.String) - */ - public InputStream retrieveElementFull(String token, String uuid, - String version) throws SimExplorerServiceException { - return wrapStream(retrieveElementFullCommon(token, uuid, version)); - } - - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementXML(java.lang.String, java.lang.String, java.lang.String) - */ - public InputStream retrieveElementXML(String token, String uuid, - String version) throws SimExplorerServiceException { - return wrapStream(retrieveElementXMLCommon(token, uuid, version)); - } } Added: trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalReplace.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalReplace.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceLocalReplace.java 2008-02-12 18:20:30 UTC (rev 873) @@ -0,0 +1,119 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.service; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import junit.framework.TestCase; +import fr.cemagref.simexplorer.is.attachment.Attachment; +import fr.cemagref.simexplorer.is.entities.data.Component; +import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; +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.factories.BaseEntityFactory; +import fr.cemagref.simexplorer.is.factories.MetaDataFactory; +import fr.cemagref.simexplorer.is.factories.XmlConstants; +import fr.cemagref.simexplorer.is.service.ElementGenerator.RandomStream; + +public class StorageServiceLocalReplace extends TestCase { + + /** The storage service. */ + private StorageService storageService; + + /** The element generator. */ + private ElementGenerator elementGenerator; + + /** The r. */ + private Random r = new Random(); + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + elementGenerator = new ElementGenerator(); + + storageService = new StorageServiceClient(); + } + + private MetaData saveEa(ExplorationApplication ea) throws Exception { + Map<Attachment, SimExplorerServiceStream> attachments = new HashMap<Attachment, SimExplorerServiceStream>(); + + 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()); + Set<Library> libraries = component.getLibraries(); + for (Library library : libraries) { + attachmentsKeys.addAll(library.getMetaData().getAttachments()); + } + } + Set<ExplorationData> explorations = ea.getExplorations(); + for (ExplorationData explorationData : explorations) { + attachmentsKeys.addAll(explorationData.getMetaData() + .getAttachments()); + } + + for (Attachment attachment : attachmentsKeys) { + RandomStream randomStream = elementGenerator.generateTextStream(); + attachment.setDataHash(randomStream.getMd5()); + SimExplorerServiceStream stream = new SimExplorerServiceStream( + randomStream.getStream()); + attachments.put(attachment, stream); + } + + SimExplorerServiceStream xmlStream = new SimExplorerServiceStream( + BaseEntityFactory.getFactory(ExplorationApplication.class) + .saveElement(XmlConstants.VALUE_METADATA_TYPE_EA, ea)); + + return storageService.saveElement(null, xmlStream, attachments); + } + + public void testReplace() throws Exception { + ExplorationApplication ea; + MetaData mde; + InputStream stream; + + MetaDataFactory<MetaData> mdeFactory = MetaDataFactory + .getFactory(MetaData.class); + + ea = elementGenerator.generateRandomEA(); + mde = saveEa(ea); + stream = storageService.retrieveElementXML(null, mde.getUuid(), mde + .getVersion().toString()); + System.out.println(mdeFactory.computeHash(stream)); + + ea.getMetaData().setVersion( + ea.getMetaData().getVersion().incVersion(0).toString()); + + mde = saveEa(ea); + stream = storageService.retrieveElementXML(null, mde.getUuid(), mde + .getVersion().toString()); + System.out.println(mdeFactory.computeHash(stream)); + } +} Modified: trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceMassInsert.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceMassInsert.java 2008-02-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceMassInsert.java 2008-02-12 18:20:30 UTC (rev 873) @@ -80,8 +80,7 @@ /** * Test mass insert. * - * @throws Exception - * the exception + * @throws Exception the exception */ public void testMassInsert() throws Exception { int c = 5; @@ -106,7 +105,7 @@ ea.getMetaData().getVersion().incVersion(r.nextInt(3)) .toString()); - Map<Attachment, RemoteInputStream> attachments = new HashMap<Attachment, RemoteInputStream>(); + Map<Attachment, SimExplorerServiceStream> attachments = new HashMap<Attachment, SimExplorerServiceStream>(); List<Attachment> attachmentsKeys = new ArrayList<Attachment>(); @@ -131,19 +130,17 @@ RandomStream randomStream = elementGenerator .generateTextStream(); attachment.setDataHash(randomStream.getMd5()); - RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( + SimExplorerServiceStream stream = new SimExplorerServiceStream( randomStream.getStream()); - attachments.put(attachment, remoteStream.export()); + attachments.put(attachment, stream); } - InputStream xmlStream = BaseEntityFactory.getFactory( - ExplorationApplication.class).saveElement( - XmlConstants.VALUE_METADATA_TYPE_EA, ea); + SimExplorerServiceStream xmlStream = new SimExplorerServiceStream( + BaseEntityFactory.getFactory( + ExplorationApplication.class).saveElement( + XmlConstants.VALUE_METADATA_TYPE_EA, ea)); - RemoteInputStreamServer xmlRemoteStream = new SimpleRemoteInputStream( - xmlStream); - storageService.saveElement(token, xmlRemoteStream.export(), - attachments); + storageService.saveElement(token, xmlStream, attachments); } if (i > 0 && i % 100 == 0) { Modified: trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceTest.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceTest.java 2008-02-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-service/src/test/fr/cemagref/simexplorer/is/service/StorageServiceTest.java 2008-02-12 18:20:30 UTC (rev 873) @@ -56,7 +56,7 @@ public void testInsertElementZip() throws Exception { InputStream zipStream = new FileInputStream( "./src/ressources/testImport/test.zip"); - RemoteInputStreamServer zipRemoteStream = new SimpleRemoteInputStream( + SimExplorerServiceStream zipRemoteStream = new SimExplorerServiceStream( zipStream); storageService.saveElement(null, zipRemoteStream); assertNotNull(storageService.getMetadata(null, "abcd")); Modified: trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java 2008-02-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java 2008-02-12 18:20:30 UTC (rev 873) @@ -60,14 +60,14 @@ throw new IllegalStateException("not implemented"); } - public MetaData saveElement(String token, RemoteInputStream zipRemoteStream) + public MetaData saveElement(String token, SimExplorerServiceStream zipRemoteStream) throws SimExplorerServiceException { return (MetaData) checkImplemented(); } public MetaData saveElement(String token, - RemoteInputStream xmlRemoteStream, - Map<Attachment, RemoteInputStream> attachmentsRemoteStream) + SimExplorerServiceStream xmlRemoteStream, + Map<Attachment, SimExplorerServiceStream> attachmentsRemoteStream) throws SimExplorerServiceException { return (MetaData) checkImplemented(); } Modified: trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementImport.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementImport.java 2008-02-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementImport.java 2008-02-12 18:20:30 UTC (rev 873) @@ -25,6 +25,7 @@ import de.hsofttec.t5components.annotations.SetterGetter; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.service.SimExplorerServiceStream; import fr.cemagref.simexplorer.is.ui.web.SimExplorerWebException; import fr.cemagref.simexplorer.is.ui.web.pages.security.UserPage; import fr.cemagref.simexplorer.is.ui.web.services.RemoteStorageService; @@ -54,21 +55,29 @@ public Object onSuccess() { MetaData mde = null; if (_fileEA != null) { - RemoteInputStreamServer zipRemoteStream = new SimpleRemoteInputStream( + SimExplorerServiceStream zipStream = new SimExplorerServiceStream( _fileEA.getStream()); + // RemoteInputStreamServer zipRemoteStream = new SimpleRemoteInputStream( + // _fileEA.getStream()); try { + // mde = RemoteStorageService.getStorageService().saveElement( + // getToken(), zipRemoteStream.export()); mde = RemoteStorageService.getStorageService().saveElement( - getToken(), zipRemoteStream.export()); + getToken(), zipStream); } catch (Exception e) { throw new SimExplorerWebException(e); } } else { if (_fileLib != null) { - RemoteInputStreamServer zipRemoteStream = new SimpleRemoteInputStream( + SimExplorerServiceStream zipStream = new SimExplorerServiceStream( _fileLib.getStream()); + // RemoteInputStreamServer zipRemoteStream = new SimpleRemoteInputStream( + // _fileLib.getStream()); try { + // mde = RemoteStorageService.getStorageService().saveElement( + // getToken(), zipRemoteStream.export()); mde = RemoteStorageService.getStorageService().saveElement( - getToken(), zipRemoteStream.export()); + getToken(), zipStream); } catch (Exception e) { throw new SimExplorerWebException(); } Modified: trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/Install.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/Install.java 2008-02-12 18:19:23 UTC (rev 872) +++ trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/Install.java 2008-02-12 18:20:30 UTC (rev 873) @@ -27,10 +27,6 @@ import org.apache.tapestry.annotations.InjectPage; -import com.healthmarketscience.rmiio.RemoteInputStream; -import com.healthmarketscience.rmiio.RemoteInputStreamServer; -import com.healthmarketscience.rmiio.SimpleRemoteInputStream; - import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.data.Component; import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; @@ -40,6 +36,7 @@ import fr.cemagref.simexplorer.is.factories.BaseEntityFactory; import fr.cemagref.simexplorer.is.factories.XmlConstants; import fr.cemagref.simexplorer.is.service.ElementGenerator; +import fr.cemagref.simexplorer.is.service.SimExplorerServiceStream; import fr.cemagref.simexplorer.is.service.ElementGenerator.RandomStream; import fr.cemagref.simexplorer.is.ui.web.SimExplorerWebException; import fr.cemagref.simexplorer.is.ui.web.pages.security.SuperAdminPage; @@ -81,17 +78,18 @@ parentEa = elementGenerator.generateRandomEA(); int v = 2 + r.nextInt(5); - Version version = new Version(parentEa.getMetaData().getVersion().toString()); + Version version = new Version(parentEa.getMetaData() + .getVersion().toString()); - for (int j = 0; j < v; j++) { + for (int j = 0; j < v; j++) { ExplorationApplication ea = elementGenerator .generateRandomEA(); ea.getMetaData().setUuid(parentEa.getMetaData().getUuid()); - + version = version.incVersion(r.nextInt(3)); ea.getMetaData().setVersion(version.toString()); - Map<Attachment, RemoteInputStream> attachments = new HashMap<Attachment, RemoteInputStream>(); + Map<Attachment, SimExplorerServiceStream> attachments = new HashMap<Attachment, SimExplorerServiceStream>(); List<Attachment> attachmentsKeys = new ArrayList<Attachment>(); @@ -116,19 +114,19 @@ RandomStream randomStream = elementGenerator .generateTextStream(); attachment.setDataHash(randomStream.getMd5()); - RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( + SimExplorerServiceStream remoteStream = new SimExplorerServiceStream( randomStream.getStream()); - attachments.put(attachment, remoteStream.export()); + attachments.put(attachment, remoteStream); } InputStream xmlStream = BaseEntityFactory.getFactory( ExplorationApplication.class).saveElement( XmlConstants.VALUE_METADATA_TYPE_EA, ea); - RemoteInputStreamServer xmlRemoteStream = new SimpleRemoteInputStream( + SimExplorerServiceStream xmlRemoteStream = new SimExplorerServiceStream( xmlStream); RemoteStorageService.getStorageService().saveElement( - getToken(), xmlRemoteStream.export(), attachments); + getToken(), xmlRemoteStream, attachments); } }