Skip to content

Commit

Permalink
Merge pull request #91 from GReD-Clermont/dev6
Browse files Browse the repository at this point in the history
Prepare next major version (6.0.0)
  • Loading branch information
ppouchin authored Nov 21, 2024
2 parents cbd49f9 + 03e6e6a commit 528f046
Show file tree
Hide file tree
Showing 79 changed files with 808 additions and 1,823 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>fr.igred</groupId>
<artifactId>simple-omero-client</artifactId>
<version>5.19.0</version>
<version>6.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Simple OMERO Client</name>
Expand Down
183 changes: 33 additions & 150 deletions src/main/java/fr/igred/omero/AnnotatableWrapper.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package fr.igred.omero;


import fr.igred.omero.client.Client;
import fr.igred.omero.exception.AccessException;
import fr.igred.omero.exception.ServiceException;
import fr.igred.omero.meta.ExperimenterWrapper;
import omero.gateway.model.DataObject;
import omero.model.IObject;

import java.sql.Timestamp;
import java.util.Collection;
Expand All @@ -42,18 +42,18 @@
*
* @param <T> Subclass of {@link DataObject}
*/
public abstract class GenericObjectWrapper<T extends DataObject> {
public abstract class ObjectWrapper<T extends DataObject> {

/** Wrapped object */
protected T data;


/**
* Constructor of the class GenericObjectWrapper.
* Constructor of the class ObjectWrapper.
*
* @param o The DataObject to wrap in the GenericObjectWrapper.
* @param o The DataObject to wrap in the ObjectWrapper.
*/
protected GenericObjectWrapper(T o) {
protected ObjectWrapper(T o) {
this.data = o;
}

Expand All @@ -70,7 +70,7 @@ protected GenericObjectWrapper(T o) {
*
* @return See above.
*/
protected static <U extends DataObject, V extends GenericObjectWrapper<U>, W extends Comparable<W>> List<V>
protected static <U extends DataObject, V extends ObjectWrapper<U>, W extends Comparable<W>> List<V>
wrap(Collection<U> objects, Function<? super U, ? extends V> mapper, Function<? super V, ? extends W> sorter) {
return objects.stream()
.map(mapper)
Expand All @@ -89,9 +89,9 @@ protected GenericObjectWrapper(T o) {
*
* @return See above.
*/
protected static <U extends DataObject, V extends GenericObjectWrapper<U>> List<V>
public static <U extends DataObject, V extends ObjectWrapper<U>> List<V>
wrap(Collection<U> objects, Function<? super U, ? extends V> mapper) {
return wrap(objects, mapper, GenericObjectWrapper::getId);
return wrap(objects, mapper, ObjectWrapper::getId);
}


Expand All @@ -103,7 +103,7 @@ protected GenericObjectWrapper(T o) {
*
* @return Distinct objects list, sorted by ID.
*/
public static <T extends GenericObjectWrapper<?>> List<T> distinct(Collection<? extends T> objects) {
public static <T extends ObjectWrapper<?>> List<T> distinct(Collection<? extends T> objects) {
return objects.stream()
.collect(toMap(T::getId, o -> o, (o1, o2) -> o1))
.values()
Expand All @@ -121,7 +121,7 @@ public static <T extends GenericObjectWrapper<?>> List<T> distinct(Collection<?
*
* @return Distinct objects list, sorted by ID.
*/
public static <U extends GenericObjectWrapper<?>>
public static <U extends ObjectWrapper<?>>
List<U> flatten(Collection<? extends Collection<? extends U>> lists) {
return lists.stream()
.flatMap(Collection::stream)
Expand All @@ -143,16 +143,6 @@ public T asDataObject() {
}


/**
* Returns the contained DataObject as IObject.
*
* @return See above.
*/
IObject asIObject() {
return data.asIObject();
}


/**
* Gets the object id
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package fr.igred.omero.repository;
package fr.igred.omero;


import fr.igred.omero.AnnotatableWrapper;
import fr.igred.omero.Browser;
import fr.igred.omero.Client;
import fr.igred.omero.GatewayWrapper;
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.OMEROServerError;
import fr.igred.omero.exception.ServiceException;
import loci.formats.in.DefaultMetadataOptions;
import loci.formats.in.MetadataLevel;
Expand All @@ -36,38 +34,80 @@
import ome.formats.importer.OMEROWrapper;
import ome.formats.importer.cli.ErrorHandler;
import ome.formats.importer.cli.LoggingImportMonitor;
import omero.ServerError;
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.stream.Collectors;
import java.util.logging.Logger;

import static java.util.stream.Collectors.toList;


/**
* Generic class containing a DataObject (or a subclass) object.
*
* @param <T> Subclass of {@link DataObject}
*/
public abstract class GenericRepositoryObjectWrapper<T extends DataObject> extends AnnotatableWrapper<T> {
public abstract class RepositoryObjectWrapper<T extends DataObject> extends AnnotatableWrapper<T> {

/**
* Constructor of the class GenericRepositoryObjectWrapper.
* Constructor of the class RepositoryObjectWrapper.
*
* @param o The DataObject to wrap in the GenericRepositoryObjectWrapper.
* @param o The DataObject to wrap in the RepositoryObjectWrapper.
*/
protected GenericRepositoryObjectWrapper(T o) {
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<Pixels> importCandidates(DataObject target, ImportLibrary library, ImportConfig config, ImportCandidates candidates) {
List<Pixels> pixels = new ArrayList<>(0);

ExecutorService threadPool = Executors.newFixedThreadPool(config.parallelUpload.get());

List<ImportContainer> 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<Pixels> 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.
*
Expand All @@ -79,11 +119,11 @@ protected GenericRepositoryObjectWrapper(T o) {
* @return If the import did not exit because of an error.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws OMEROServerError Server error.
* @throws AccessException Cannot access data.
* @throws IOException Cannot read file.
*/
protected static boolean importImages(GatewayWrapper client, DataObject target, int threads, String... paths)
throws ServiceException, OMEROServerError, IOException {
throws ServiceException, AccessException, IOException {
boolean success;

ImportConfig config = new ImportConfig();
Expand All @@ -96,10 +136,8 @@ protected static boolean importImages(GatewayWrapper client, DataObject target,

OMEROMetadataStoreClient store = client.getImportStore();
try (OMEROWrapper reader = new OMEROWrapper(config)) {
ExceptionHandler.ofConsumer(store,
s -> s.logVersionInfo(config.getIniVersionNumber()))
.rethrow(ServerError.class, OMEROServerError::new,
"Cannot log version information during import.")
ExceptionHandler.ofConsumer(store, s -> s.logVersionInfo(config.getIniVersionNumber()))
.handleServerAndService("Cannot log version information during import.")
.rethrow();
reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.ALL));

Expand Down Expand Up @@ -128,21 +166,24 @@ protected static boolean importImages(GatewayWrapper client, DataObject target,
* @return The list of IDs of the newly imported images.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws OMEROServerError Server error.
* @throws AccessException Cannot access data.
* @throws IOException Cannot read file.
*/
protected static List<Long> importImage(GatewayWrapper client, DataObject target, String path)
throws ServiceException, OMEROServerError {
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> pixels = new ArrayList<>(1);
Collection<Pixels> pixels;

OMEROMetadataStoreClient store = client.getImportStore();
try (OMEROWrapper reader = new OMEROWrapper(config)) {
store.logVersionInfo(config.getIniVersionNumber());
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);
Expand All @@ -151,28 +192,15 @@ protected static List<Long> importImage(GatewayWrapper client, DataObject target
ErrorHandler handler = new ErrorHandler(config);

ImportCandidates candidates = new ImportCandidates(reader, new String[]{path}, handler);

ExecutorService uploadThreadPool = Executors.newFixedThreadPool(config.parallelUpload.get());

List<ImportContainer> containers = candidates.getContainers();
if (containers != null) {
for (int i = 0; i < containers.size(); i++) {
ImportContainer container = containers.get(i);
container.setTarget(target.asIObject());
List<Pixels> imported = library.importImage(container, uploadThreadPool, i);
pixels.addAll(imported);
}
}
uploadThreadPool.shutdown();
} catch (Throwable e) {
throw new OMEROServerError(e);
pixels = importCandidates(target, library, config, candidates);
} finally {
client.closeImport();
}

List<Long> ids = new ArrayList<>(pixels.size());
pixels.forEach(pix -> ids.add(pix.getImage().getId().getValue()));
return ids.stream().distinct().collect(Collectors.toList());
return pixels.stream()
.map(pix -> pix.getImage().getId().getValue())
.distinct()
.collect(toList());
}


Expand Down Expand Up @@ -204,27 +232,12 @@ protected static List<Long> importImage(GatewayWrapper client, DataObject target
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
@SuppressWarnings("MethodOverloadsMethodOfSuperclass")
public void copyAnnotationLinks(Client client, GenericRepositoryObjectWrapper<?> object)
public void copyAnnotationLinks(Client client, RepositoryObjectWrapper<?> object)
throws AccessException, ServiceException, ExecutionException {
super.copyAnnotationLinks(client, object);
}


/**
* @param client The data browser.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
* @deprecated Reloads the object from OMERO.
*/
@Deprecated
public void refresh(Client client)
throws ServiceException, AccessException, ExecutionException {
reload(client);
}


/**
* Reloads the object from OMERO.
*
Expand All @@ -234,10 +247,8 @@ public void refresh(Client client)
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void reload(Browser browser)
throws ServiceException, AccessException, ExecutionException {
// DO NOTHING FOR API COMPATIBILITY PURPOSES
}
public abstract void reload(Browser browser)
throws ServiceException, AccessException, ExecutionException;


/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/fr/igred/omero/annotations/AnnotationList.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import static fr.igred.omero.util.Wrapper.wrap;


/** List of GenericAnnotationWrapper objects */
public class AnnotationList extends ArrayList<GenericAnnotationWrapper<?>> {
/** List of AnnotationWrapper objects */
public class AnnotationList extends ArrayList<AnnotationWrapper<?>> {


private static final long serialVersionUID = 8792604507462788823L;
Expand Down Expand Up @@ -57,11 +57,11 @@ public AnnotationList(int initialCapacity) {
* Gets a list of elements from this list whose class is specified.
*
* @param clazz Class of the wanted elements.
* @param <T> Subclass of GenericAnnotationWrapper.
* @param <T> Subclass of AnnotationWrapper.
*
* @return See above.
*/
public <T extends GenericAnnotationWrapper<?>> List<T> getElementsOf(Class<? extends T> clazz) {
public <T extends AnnotationWrapper<?>> List<T> getElementsOf(Class<? extends T> clazz) {
return stream().filter(clazz::isInstance)
.map(clazz::cast)
.collect(Collectors.toList());
Expand Down
Loading

0 comments on commit 528f046

Please sign in to comment.