diff --git a/src/main/java/omero/gateway/Gateway.java b/src/main/java/omero/gateway/Gateway.java index 8246e58a..ccd7df87 100644 --- a/src/main/java/omero/gateway/Gateway.java +++ b/src/main/java/omero/gateway/Gateway.java @@ -452,6 +452,24 @@ public String getSessionId(ExperimenterData user) return null; } + /** + * Return the host + * + * @param user The user to get the host for + * @return See above + * @throws DSOutOfServiceException + * If the connection is broken, or not logged in + */ + public String getHost(ExperimenterData user) + throws DSOutOfServiceException { + Connector c = getConnector(new SecurityContext(user.getGroupId()), + false, false); + if (c == null) { + return null; + } + return login.getServer().getHost(); + } + /** * Get the version of the server the Gateway is connected to * diff --git a/src/main/java/omero/gateway/facility/TransferFacility.java b/src/main/java/omero/gateway/facility/TransferFacility.java index e103cde6..df6e0a48 100644 --- a/src/main/java/omero/gateway/facility/TransferFacility.java +++ b/src/main/java/omero/gateway/facility/TransferFacility.java @@ -28,6 +28,9 @@ import omero.gateway.SecurityContext; import omero.gateway.exception.DSAccessException; import omero.gateway.exception.DSOutOfServiceException; +import omero.gateway.model.DataObject; + +import org.apache.commons.collections.CollectionUtils; /** * {@link Facility} which provides data transfer functionality, i.e. download @@ -70,4 +73,26 @@ public List downloadImage(SecurityContext context, String targetPath, return helper.downloadImage(context, targetPath, imageId); } + /** + * Uploads the specified images to the server. + * + * @param ctx The security context. + * @param paths The files to import. + * @param target The container if any. + * @return See above + * @throws DSOutOfServiceException + * If the connection is broken, or not logged in + * @throws DSAccessException + * If an error occurred while trying to retrieve data from OMERO + * service. + */ + public Boolean uploadImagesDirect(final SecurityContext ctx, final List paths, + final DataObject target) + throws DSAccessException, DSOutOfServiceException{ + if (CollectionUtils.isEmpty(paths)) { + return Boolean.FALSE; + } + return helper.uploadImagesDirect(ctx, paths, target); + } + } diff --git a/src/main/java/omero/gateway/facility/TransferFacilityHelper.java b/src/main/java/omero/gateway/facility/TransferFacilityHelper.java index e6e3a06d..219df78d 100644 --- a/src/main/java/omero/gateway/facility/TransferFacilityHelper.java +++ b/src/main/java/omero/gateway/facility/TransferFacilityHelper.java @@ -35,11 +35,25 @@ import omero.gateway.SecurityContext; import omero.gateway.exception.DSAccessException; import omero.gateway.exception.DSOutOfServiceException; +import omero.gateway.model.DataObject; +import omero.gateway.model.ExperimenterData; +import omero.gateway.model.DatasetData; import omero.gateway.model.ImageData; import omero.model.Fileset; import omero.model.FilesetEntry; +import omero.model.IObject; import omero.model.OriginalFile; import omero.sys.ParametersI; +import ome.formats.importer.ImportConfig; +import ome.formats.importer.OMEROWrapper; +import ome.formats.importer.ImportLibrary; +import ome.formats.importer.ImportCandidates; +import ome.formats.importer.ImportContainer; +import ome.formats.importer.cli.ErrorHandler; +import ome.formats.importer.cli.LoggingImportMonitor; + +import loci.formats.in.DynamicMetadataOptions; +import loci.formats.in.MetadataLevel; import org.apache.commons.collections.CollectionUtils; @@ -85,7 +99,10 @@ public class TransferFacilityHelper { * The identifier of the image. * @return See above * @throws DSOutOfServiceException + * If the connection is broken, or not logged in * @throws DSAccessException + * If an error occurred while trying to retrieve data from OMERO + * service. */ List downloadImage(SecurityContext context, String targetPath, long imageId) throws DSAccessException, DSOutOfServiceException { @@ -205,6 +222,55 @@ List downloadImage(SecurityContext context, String targetPath, return files; } + /** + * Import the specified files into the given container if any. + * Returns the corresponding pixels Data object. + * + * @param context The security context. + * @param paths The paths to the file to import. + * @param object The container where to import the image. + * @return + * @throws DSOutOfServiceException + * If the connection is broken, or not logged in + * @throws DSAccessException + * If an error occurred while trying to retrieve data from OMERO + * service. + */ + Boolean uploadImagesDirect(SecurityContext ctx, List paths, DataObject object) + throws DSAccessException, DSOutOfServiceException + { + String[] values = paths.toArray(new String[0]); + ExperimenterData user = gateway.getLoggedInUser(); + String sessionKey = gateway.getSessionId(user); + + ImportConfig config = new ImportConfig(); + config.hostname.set(gateway.getHost(user)); + config.sessionKey.set(sessionKey); + + OMEROWrapper reader = new OMEROWrapper(config); + + ImportLibrary library = null; + try { + library = new ImportLibrary(config.createStore(), reader); + } catch (Exception e) { + throw new DSAccessException("Cannot create an import store", e); + } + ErrorHandler error_handler = new ErrorHandler(config); + + library.addObserver(new LoggingImportMonitor()); + ImportCandidates candidates = new ImportCandidates(reader, values, error_handler); + if (object instanceof DatasetData) { + IObject dataset = browse.findIObject(ctx, "omero.model.Dataset", object.getId()); + for (ImportContainer c : candidates.getContainers()) { + c.setTarget(dataset); + } + } + + reader.setMetadataOptions(new DynamicMetadataOptions(MetadataLevel.ALL)); + + return library.importCandidates(config, candidates); + } + /** * Creates the query to load the file set corresponding to a given image. *