From fe52f305b37e4f84fff6f410fba830391972ee0b Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Mon, 27 Feb 2023 00:43:32 +0100 Subject: [PATCH 1/2] Rename RepositoryObjectWrapper.java to ReplacePolicy.java --- .../{RepositoryObjectWrapper.java => util/ReplacePolicy.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/fr/igred/omero/{RepositoryObjectWrapper.java => util/ReplacePolicy.java} (100%) diff --git a/src/main/java/fr/igred/omero/RepositoryObjectWrapper.java b/src/main/java/fr/igred/omero/util/ReplacePolicy.java similarity index 100% rename from src/main/java/fr/igred/omero/RepositoryObjectWrapper.java rename to src/main/java/fr/igred/omero/util/ReplacePolicy.java From cfc2a256a1acc44be005ac9ccf63b33b8f28e612 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Mon, 27 Feb 2023 00:45:10 +0100 Subject: [PATCH 2/2] Only keep ReplacePolicy in ReplacePolicy.java --- .../fr/igred/omero/util/ReplacePolicy.java | 253 +----------------- 1 file changed, 9 insertions(+), 244 deletions(-) diff --git a/src/main/java/fr/igred/omero/util/ReplacePolicy.java b/src/main/java/fr/igred/omero/util/ReplacePolicy.java index e2ae7936..24257bd9 100644 --- a/src/main/java/fr/igred/omero/util/ReplacePolicy.java +++ b/src/main/java/fr/igred/omero/util/ReplacePolicy.java @@ -15,254 +15,19 @@ * Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.igred.omero; - - -import fr.igred.omero.client.Browser; -import fr.igred.omero.client.Client; -import fr.igred.omero.client.GatewayWrapper; -import fr.igred.omero.exception.AccessException; -import fr.igred.omero.exception.ExceptionHandler; -import fr.igred.omero.exception.ServiceException; -import loci.formats.in.DefaultMetadataOptions; -import loci.formats.in.MetadataLevel; -import ome.formats.OMEROMetadataStoreClient; -import ome.formats.importer.ImportCandidates; -import ome.formats.importer.ImportConfig; -import ome.formats.importer.ImportContainer; -import ome.formats.importer.ImportLibrary; -import ome.formats.importer.OMEROWrapper; -import ome.formats.importer.cli.ErrorHandler; -import ome.formats.importer.cli.LoggingImportMonitor; -import omero.gateway.model.DataObject; -import omero.gateway.util.PojoMapper; -import omero.model.Pixels; - -import java.io.IOException; -import java.lang.invoke.MethodHandles; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.logging.Logger; - -import static java.util.stream.Collectors.toList; +package fr.igred.omero.util; /** - * Generic class containing a DataObject (or a subclass) object. - * - * @param Subclass of {@link DataObject} + * Policy to specify how to handle objects when they are replaced. */ -public abstract class RepositoryObjectWrapper extends AnnotatableWrapper { - - /** - * Constructor of the class RepositoryObjectWrapper. - * - * @param o The DataObject to wrap in the RepositoryObjectWrapper. - */ - protected RepositoryObjectWrapper(T o) { - super(o); - } - - - /** - * Method used for importing a number of import candidates. - * - * @param target The import target. - * @param library The importer. - * @param config The configuration information. - * @param candidates Hosts information about the files to import. - * - * @return The list of imported pixels. - */ - private static List importCandidates(DataObject target, ImportLibrary library, ImportConfig config, ImportCandidates candidates) { - List pixels = new ArrayList<>(0); - - ExecutorService threadPool = Executors.newFixedThreadPool(config.parallelUpload.get()); - - List containers = candidates.getContainers(); - if (containers != null) { - pixels = new ArrayList<>(containers.size()); - for (int i = 0; i < containers.size(); i++) { - ImportContainer container = containers.get(i); - container.setTarget(target.asIObject()); - List imported = new ArrayList<>(1); - try { - imported = library.importImage(container, threadPool, i); - } catch (Throwable e) { - String filename = container.getFile().getName(); - String error = String.format("Error during image import for: %s", filename); - Logger.getLogger(MethodHandles.lookup().lookupClass().getName()).severe(error); - if (Boolean.FALSE.equals(config.contOnError.get())) { - return pixels; - } - } - pixels.addAll(imported); - } - } - threadPool.shutdown(); - return pixels; - } - - - /** - * Imports all images candidates in the paths to the target in OMERO. - * - * @param client The client handling the connection. - * @param target The import target. - * @param threads The number of threads (same value used for filesets and uploads). - * @param paths Paths to the image files on the computer. - * - * @return If the import did not exit because of an error. - * - * @throws ServiceException Cannot connect to OMERO. - * @throws AccessException Cannot access data. - * @throws IOException Cannot read file. - */ - protected static boolean importImages(GatewayWrapper client, DataObject target, int threads, String... paths) - throws ServiceException, AccessException, IOException { - boolean success; - - ImportConfig config = new ImportConfig(); - String type = PojoMapper.getGraphType(target.getClass()); - config.target.set(type + ":" + target.getId()); - config.username.set(client.getUser().getUserName()); - config.email.set(client.getUser().getEmail()); - config.parallelFileset.set(threads); - config.parallelUpload.set(threads); - - OMEROMetadataStoreClient store = client.getImportStore(); - try (OMEROWrapper reader = new OMEROWrapper(config)) { - ExceptionHandler.ofConsumer(store, s -> s.logVersionInfo(config.getIniVersionNumber())) - .handleServerAndService("Cannot log version information during import.") - .rethrow(); - reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL)); - - ImportLibrary library = new ImportLibrary(store, reader); - library.addObserver(new LoggingImportMonitor()); - - ErrorHandler handler = new ErrorHandler(config); - - ImportCandidates candidates = new ImportCandidates(reader, paths, handler); - success = library.importCandidates(config, candidates); - } finally { - client.closeImport(); - } - - return success; - } - - - /** - * Imports one image file to the target in OMERO. - * - * @param client The client handling the connection. - * @param target The import target. - * @param path Path to the image file on the computer. - * - * @return The list of IDs of the newly imported images. - * - * @throws ServiceException Cannot connect to OMERO. - * @throws AccessException Cannot access data. - * @throws IOException Cannot read file. - */ - protected static List importImage(GatewayWrapper client, DataObject target, String path) - throws ServiceException, AccessException, IOException { - ImportConfig config = new ImportConfig(); - String type = PojoMapper.getGraphType(target.getClass()); - config.target.set(type + ":" + target.getId()); - config.username.set(client.getUser().getUserName()); - config.email.set(client.getUser().getEmail()); - - Collection pixels; - - OMEROMetadataStoreClient store = client.getImportStore(); - try (OMEROWrapper reader = new OMEROWrapper(config)) { - ExceptionHandler.ofConsumer(store, s -> s.logVersionInfo(config.getIniVersionNumber())) - .handleServerAndService("Cannot log version information during import.") - .rethrow(); - reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL)); - - ImportLibrary library = new ImportLibrary(store, reader); - library.addObserver(new LoggingImportMonitor()); - - ErrorHandler handler = new ErrorHandler(config); - - ImportCandidates candidates = new ImportCandidates(reader, new String[]{path}, handler); - pixels = importCandidates(target, library, config, candidates); - } finally { - client.closeImport(); - } - - return pixels.stream() - .map(pix -> pix.getImage().getId().getValue()) - .distinct() - .collect(toList()); - } - - - /** - * Gets the object name. - * - * @return See above. - */ - public abstract String getName(); - - - /** - * Gets the object description - * - * @return See above. - */ - public abstract String getDescription(); - - - /** - * Copies annotation links from some other object to this one. - *

Kept for API compatibility purposes.

- * - * @param client The client handling the connection. - * @param object Other repository object to copy annotations from. - * - * @throws ServiceException Cannot connect to OMERO. - * @throws AccessException Cannot access data. - * @throws ExecutionException A Facility can't be retrieved or instantiated. - */ - @SuppressWarnings("MethodOverloadsMethodOfSuperclass") - public void copyAnnotationLinks(Client client, RepositoryObjectWrapper object) - throws AccessException, ServiceException, ExecutionException { - super.copyAnnotationLinks(client, object); - } - - - /** - * Reloads the object from OMERO. - * - * @param browser The data browser. - * - * @throws ServiceException Cannot connect to OMERO. - * @throws AccessException Cannot access data. - * @throws ExecutionException A Facility can't be retrieved or instantiated. - */ - public abstract void reload(Browser browser) - throws ServiceException, AccessException, ExecutionException; - - - /** - * Policy to specify how to handle objects when they are replaced. - */ - public enum ReplacePolicy { - /** Unlink objects only */ - UNLINK, - - /** Delete all objects */ - DELETE, +public enum ReplacePolicy { + /** Unlink objects only */ + UNLINK, - /** Delete orphaned objects */ - DELETE_ORPHANED - } + /** Delete all objects */ + DELETE, + /** Delete orphaned objects */ + DELETE_ORPHANED }