Author: tchemit Date: 2008-02-08 15:14:27 +0000 (Fri, 08 Feb 2008) New Revision: 751 Modified: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java Log: downloads + encapsulation exception TODO gerer graphiquement les erreurs Modified: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java 2008-02-08 15:13:57 UTC (rev 750) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java 2008-02-08 15:14:27 UTC (rev 751) @@ -1,5 +1,5 @@ /* -* \#\#% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, * Tony Chemit, Gabriel Landais * * This program is free software; you can redistribute it and/or @@ -15,81 +15,158 @@ * 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.ui; +import com.healthmarketscience.rmiio.RemoteInputStream; +import com.healthmarketscience.rmiio.RemoteInputStreamClient; +import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.data.LoggableElement; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; import fr.cemagref.simexplorer.is.entities.metadata.Version; +import fr.cemagref.simexplorer.is.service.MockStorageServiceImpl; import fr.cemagref.simexplorer.is.service.SimExplorerServiceException; import fr.cemagref.simexplorer.is.service.StorageService; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + /** - * Une classe pour encapsuler les appels aux services Sotrage + * Une classe pour encapsuler les appels aux services Storage * * @author chemit */ public class StorageServiceHelper { - public static int getCount(SimExplorerContext context, boolean remote, String query, boolean onlyLatest) throws Exception { - StorageService service = context.getStorageService(remote); + public static int getCount(SimExplorerContext context, boolean remote, String query, boolean onlyLatest) { + StorageService service = getService(context, remote); String token = context.getToken(); int size; - if (query == null || query.isEmpty()) { - size = service.findApplicationsCount(token, onlyLatest); - } else { - size = service.findFullTextCount(token, query, onlyLatest); + try { + if (query == null || query.isEmpty()) { + size = service.findApplicationsCount(token, onlyLatest); + } else { + size = service.findFullTextCount(token, query, onlyLatest); + } + return size; + } catch (SimExplorerServiceException e) { + throw new SimExplorerRuntimeException(e); } - return size; } - public static MetaData[] getData(SimExplorerContext context, boolean remote, boolean onlyLatest, String query, long newFirstIndex, int width, int rowOrder) throws Exception { - StorageService service = context.getStorageService(remote); + public static MetaData[] getData(SimExplorerContext context, boolean remote, boolean onlyLatest, String query, long newFirstIndex, int width, int rowOrder) { + StorageService service = getService(context, remote); String token = context.getToken(); - MetaData[] data; - if (query == null || query.isEmpty()) { - data = service.findApplications(token, onlyLatest, (int) newFirstIndex, width, rowOrder); - } else { - data = service.findFullText(token, query, onlyLatest, (int) newFirstIndex, width, rowOrder); + try { + MetaData[] data; + if (query == null || query.isEmpty()) { + data = service.findApplications(token, onlyLatest, (int) newFirstIndex, width, rowOrder); + } else { + data = service.findFullText(token, query, onlyLatest, (int) newFirstIndex, width, rowOrder); + } + return data; + } catch (SimExplorerServiceException e) { + throw new SimExplorerRuntimeException(e); } - return data; } - public static LoggableElement getElement(SimExplorerContext context, boolean remote, String uuid, String version) throws Exception { - StorageService service = context.getStorageService(remote); - LoggableElement data; - data = service.getLoggableElement(context.getToken(), uuid, version); - return data; + public static LoggableElement getElement(SimExplorerContext context, boolean remote, String uuid, String version) { + try { + LoggableElement data; + data = getService(context, remote).getLoggableElement(context.getToken(), uuid, version); + return data; + } catch (SimExplorerServiceException e1) { + throw new SimExplorerRuntimeException(e1); + } } - public static Version[] getVersions(SimExplorerContext context, boolean remote, String uuid) { - StorageService service = context.getStorageService(remote); - MetaData[] data; + public static MetaData getMetaData(SimExplorerContext context, boolean remote, String uuid, String version) { try { - data = service.getVersions(context.getToken(), uuid); - } catch (SimExplorerServiceException e) { - throw new SimExplorerRuntimeException(e); + MetaData metas; + metas = getService(context, remote).getMetadata(context.getToken(), uuid, version); + return metas; + } catch (SimExplorerServiceException e1) { + throw new SimExplorerRuntimeException(e1); } - if (data == null) { - return null; + } + + public static BufferedInputStream downloadElementFull(SimExplorerContext context, boolean remote, String uuid, String version) { + try { + + InputStream stream = ((MockStorageServiceImpl) getService(context, remote)).downloadElementFull(context.getToken(), uuid, version); + BufferedInputStream bis; + bis = new BufferedInputStream(stream); + return bis; + } catch (SimExplorerServiceException e1) { + throw new SimExplorerRuntimeException(e1); } - Version[] result = new Version[data.length]; - for (int i = 0; i < data.length; i++) { - MetaData metaData = data[i]; - result[i] = metaData.getVersion(); + } + + public static BufferedInputStream downloadElementData(SimExplorerContext context, boolean remote, String uuid, String version, Attachment attachment) { + try { + InputStream stream = ((MockStorageServiceImpl) getService(context, remote)).downloadElementData(context.getToken(), uuid, version, attachment); + BufferedInputStream bis; + bis = new BufferedInputStream(stream); + return bis; + } catch (SimExplorerServiceException e1) { + throw new SimExplorerRuntimeException(e1); } - return result; } - public static MetaData getMetaData(SimExplorerContext context, boolean remote, String uuid, String version) { + + public static BufferedInputStream retrieveElementFull(SimExplorerContext context, boolean remote, String uuid, String version) { try { - StorageService service = context.getStorageService(remote); - MetaData metas; - metas = service.getMetadata(context.getToken(), uuid, version); - return metas; + RemoteInputStream ris; + ris = getService(context, remote).retrieveElementFull(context.getToken(), uuid, version); + InputStream stream = RemoteInputStreamClient.wrap(ris); + BufferedInputStream bis; + bis = new BufferedInputStream(stream); + return bis; } catch (SimExplorerServiceException e1) { throw new SimExplorerRuntimeException(e1); + } catch (IOException e) { + throw new SimExplorerRuntimeException(e); } } + + public static BufferedInputStream retrieveElementData(SimExplorerContext context, boolean remote, String uuid, String version, Attachment attachment) { + try { + RemoteInputStream ris; + ris = getService(context, remote).retrieveElementData(context.getToken(), uuid, version, attachment); + InputStream stream = RemoteInputStreamClient.wrap(ris); + BufferedInputStream bis; + bis = new BufferedInputStream(stream); + return bis; + } catch (SimExplorerServiceException e1) { + throw new SimExplorerRuntimeException(e1); + } catch (IOException e) { + throw new SimExplorerRuntimeException(e); + } + } + + public static Version[] getVersions(SimExplorerContext context, boolean remote, String uuid) { + try { + MetaData[] data; + data = getService(context, remote).getVersions(context.getToken(), uuid); + if (data == null) { + return null; + } + Version[] result = new Version[data.length]; + for (int i = 0; i < data.length; i++) { + MetaData metaData = data[i]; + result[i] = metaData.getVersion(); + } + return result; + } catch (SimExplorerServiceException e) { + throw new SimExplorerRuntimeException(e); + } + } + + protected static StorageService getService(SimExplorerContext context, boolean remote) { + StorageService service; + service = context.getStorageService(remote); + return service; + } }