diff --git a/src/main/java/omero/gateway/facility/LoadFacility.java b/src/main/java/omero/gateway/facility/LoadFacility.java index 92533c84..b13bdef6 100644 --- a/src/main/java/omero/gateway/facility/LoadFacility.java +++ b/src/main/java/omero/gateway/facility/LoadFacility.java @@ -26,19 +26,20 @@ import omero.gateway.SecurityContext; import omero.gateway.exception.DSAccessException; import omero.gateway.exception.DSOutOfServiceException; +import omero.gateway.model.DataObject; import omero.gateway.model.DatasetData; import omero.gateway.model.ImageData; import omero.gateway.model.PlateData; import omero.gateway.model.ProjectData; import omero.gateway.model.ScreenData; import omero.gateway.model.WellData; -import omero.model.DatasetI; -import omero.model.ImageI; -import omero.model.PlateI; -import omero.model.ProjectI; -import omero.model.ScreenI; -import omero.model.WellI; +import omero.model.IObject; import omero.sys.ParametersI; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; /** * A Facility for loading basic objects. Note: These are shallow objects with just @@ -47,38 +48,57 @@ */ public class LoadFacility extends Facility { - private static String GET_DATASET_QUERY = "select ds from Dataset as ds " + + private static String GET_DATASETS_QUERY = "select ds from Dataset as ds " + "left join fetch ds.imageLinks as l " + "left join fetch l.child as i " + - "where ds.id = :id"; + "where ds.id in (:ids)"; - private static String GET_PROJECT_QUERY = "select p from Project as p " + + private static String GET_DATASETS_FOR_PROJECT_QUERY = "select l.child from ProjectDatasetLink l " + + "where l.parent.id in (:ids)"; + + private static String GET_PROJECTS_QUERY = "select p from Project as p " + "left join fetch p.datasetLinks as l " + "left join fetch l.child as i " + - "where p.id = :id"; + "where p.id in (:ids)"; - private static String GET_IMAGE_QUERY = "select i from Image as i " + + private static String GET_IMAGES_QUERY = "select i from Image as i " + "left join fetch i.pixels as p " + "left join fetch p.pixelsType as pt " + - "where i.id = :id"; + "where i.id in (:ids)"; + + private static String GET_IMAGES_FOR_DATASET_QUERY = "select l.child from DatasetImageLink l " + + "where l.parent.id in (:ids)"; - private static String GET_PLATE_QUERY = "select p from Plate as p " + + private static String GET_PLATES_QUERY = "select p from Plate as p " + "left join fetch p.wells as w " + "left join fetch p.plateAcquisitions as pa " + - "where p.id = :id"; + "where p.id in (:ids)"; + + private static String GET_PLATES_FOR_SCREEN_QUERY = "select l.child from ScreenPlateLink l " + + "where l.parent.id in (:ids)"; - private static String GET_SCREEN_QUERY = "select s from Screen as s " + + private static String GET_SCREENS_QUERY = "select s from Screen as s " + "left join fetch s.plateLinks as l " + "left join fetch l.child as p " + - "where s.id = :id"; + "where s.id in (:ids)"; + + private static String GET_WELLS_QUERY = "select w from Well as w " + + "left join fetch w.wellSamples as ws " + + "left join fetch w.plate as p " + + "left join fetch ws.plateAcquisition as pa " + + "left join fetch ws.image as img " + + "left join fetch img.pixels as pix " + + "left join fetch pix.pixelsType as pt " + + "where w.id in (:ids)"; - private static String GET_WELL_QUERY = "select w from Well as w " + + private static String GET_WELLS_FOR_PLATE_QUERY = "select w from Well as w " + "left join fetch w.wellSamples as ws " + + "left join fetch w.plate as p " + "left join fetch ws.plateAcquisition as pa " + "left join fetch ws.image as img " + "left join fetch img.pixels as pix " + "left join fetch pix.pixelsType as pt " + - "where w.id = :id"; + "where p.id in (:ids)"; /** * Creates a new instance @@ -90,28 +110,81 @@ public class LoadFacility extends Facility { } /** - * Get a dataset + * Queries the DB for certain objects * @param ctx The SecurityContext - * @param id The id of the dataset - * @return The dataset or null if it can't be found + * @param query The hql query + * @param type The class of the objects expected + * @param ids The ids of the objects to insert into the query + * @return A Collection of objects + * @param * @throws DSOutOfServiceException * @throws DSAccessException */ - public DatasetData getDataset(SecurityContext ctx, long id) throws DSOutOfServiceException, - DSAccessException { + private Collection queryDb(SecurityContext ctx, String query, Class type, + Collection ids) throws DSOutOfServiceException, DSAccessException { try { IQueryPrx qs = gateway.getQueryService(ctx); ParametersI param = new ParametersI(); - param.addId(id); - DatasetI tmp = (DatasetI) qs.findByQuery(GET_DATASET_QUERY, param); - if (tmp != null) - return new DatasetData(tmp); - } catch (DSOutOfServiceException | ServerError e) { - handleException(this, e, "Could not get dataset"); + param.addIds(ids); + List tmp = qs.findAllByQuery(query, param); + List result = new ArrayList(); + if (tmp != null && !tmp.isEmpty()) { + for (IObject o : tmp) { + T inst = type.getDeclaredConstructor(o.getClass().getSuperclass()).newInstance(o); + result.add(inst); + } + } + return result; + } catch (DSOutOfServiceException | ServerError | InstantiationException | IllegalAccessException | + InvocationTargetException | NoSuchMethodException e) { + handleException(this, e, "Could not get objects"); } return null; } + /** + * Get a dataset + * @param ctx The SecurityContext + * @param id The id of the dataset + * @return The dataset or null if it can't be found + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public DatasetData getDataset(SecurityContext ctx, long id) throws DSOutOfServiceException, + DSAccessException { + Collection tmp = getDatasets(ctx, Collections.singletonList(id)); + if (tmp.isEmpty()) + return null; + return tmp.iterator().next(); + } + + /** + * Get datasets + * @param ctx The SecurityContext + * @param ids The ids of the datasets + * @return Collection of datasets (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getDatasets(SecurityContext ctx, Collection ids) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_DATASETS_QUERY, DatasetData.class, ids); + } + + /** + * Get datasets of a project + * @param ctx The SecurityContext + * @param projectId The project Id + * @return Collection of datasets (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getDatasets(SecurityContext ctx, long projectId) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_DATASETS_FOR_PROJECT_QUERY, DatasetData.class, + Collections.singletonList(projectId)); + } + /** * Get a project * @param ctx The SecurityContext @@ -122,21 +195,27 @@ public DatasetData getDataset(SecurityContext ctx, long id) throws DSOutOfServic */ public ProjectData getProject(SecurityContext ctx, long id) throws DSOutOfServiceException, DSAccessException { - try { - IQueryPrx qs = gateway.getQueryService(ctx); - ParametersI param = new ParametersI(); - param.addId(id); - ProjectI tmp = (ProjectI) qs.findByQuery(GET_PROJECT_QUERY, param); - if (tmp != null) - return new ProjectData(tmp); - } catch (DSOutOfServiceException | ServerError e) { - handleException(this, e, "Could not get project"); - } - return null; + Collection tmp = getProjects(ctx, Collections.singletonList(id)); + if (tmp.isEmpty()) + return null; + return tmp.iterator().next(); } /** - * Get an image + * Get projects + * @param ctx The SecurityContext + * @param ids The ids of the projects + * @return Collection of projects (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getProjects(SecurityContext ctx, Collection ids) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_PROJECTS_QUERY, ProjectData.class, ids); + } + + /** + * Get a image * @param ctx The SecurityContext * @param id The id of the image * @return The image or null if it can't be found @@ -145,17 +224,37 @@ public ProjectData getProject(SecurityContext ctx, long id) throws DSOutOfServic */ public ImageData getImage(SecurityContext ctx, long id) throws DSOutOfServiceException, DSAccessException { - try { - IQueryPrx qs = gateway.getQueryService(ctx); - ParametersI param = new ParametersI(); - param.addId(id); - ImageI tmp = (ImageI) qs.findByQuery(GET_IMAGE_QUERY, param); - if (tmp != null) - return new ImageData(tmp); - } catch (DSOutOfServiceException | ServerError e) { - handleException(this, e, "Could not get image"); - } - return null; + Collection tmp = getImages(ctx, Collections.singletonList(id)); + if (tmp.isEmpty()) + return null; + return tmp.iterator().next(); + } + + /** + * Get images + * @param ctx The SecurityContext + * @param ids The ids of the images + * @return Collection of images (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getImages(SecurityContext ctx, Collection ids) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_IMAGES_QUERY, ImageData.class, ids); + } + + /** + * Get images of a dataset + * @param ctx The SecurityContext + * @param datasetId The dataset Id + * @return Collection of images (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getImages(SecurityContext ctx, long datasetId) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_IMAGES_FOR_DATASET_QUERY, ImageData.class, + Collections.singletonList(datasetId)); } /** @@ -168,17 +267,23 @@ public ImageData getImage(SecurityContext ctx, long id) throws DSOutOfServiceExc */ public ScreenData getScreen(SecurityContext ctx, long id) throws DSOutOfServiceException, DSAccessException { - try { - IQueryPrx qs = gateway.getQueryService(ctx); - ParametersI param = new ParametersI(); - param.addId(id); - ScreenI tmp = (ScreenI) qs.findByQuery(GET_SCREEN_QUERY, param); - if (tmp != null) - return new ScreenData(tmp); - } catch (DSOutOfServiceException | ServerError e) { - handleException(this, e, "Could not get screen"); - } - return null; + Collection tmp = getScreens(ctx, Collections.singletonList(id)); + if (tmp.isEmpty()) + return null; + return tmp.iterator().next(); + } + + /** + * Get screens + * @param ctx The SecurityContext + * @param ids The ids of the screens + * @return Collection of screens (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getScreens(SecurityContext ctx, Collection ids) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_SCREENS_QUERY, ScreenData.class, ids); } /** @@ -191,17 +296,37 @@ public ScreenData getScreen(SecurityContext ctx, long id) throws DSOutOfServiceE */ public PlateData getPlate(SecurityContext ctx, long id) throws DSOutOfServiceException, DSAccessException { - try { - IQueryPrx qs = gateway.getQueryService(ctx); - ParametersI param = new ParametersI(); - param.addId(id); - PlateI tmp = (PlateI) qs.findByQuery(GET_PLATE_QUERY, param); - if (tmp != null) - return new PlateData(tmp); - } catch (DSOutOfServiceException | ServerError e) { - handleException(this, e, "Could not get plate"); - } - return null; + Collection tmp = getPlates(ctx, Collections.singletonList(id)); + if (tmp.isEmpty()) + return null; + return tmp.iterator().next(); + } + + /** + * Get plates + * @param ctx The SecurityContext + * @param ids The ids of the plates + * @return Collection of plates (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getPlates(SecurityContext ctx, Collection ids) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_PLATES_QUERY, PlateData.class, ids); + } + + /** + * Get plates for a screen + * @param ctx The SecurityContext + * @param screenId The screen Id + * @return Collection of plates (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getPlates(SecurityContext ctx, long screenId) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_PLATES_FOR_SCREEN_QUERY, PlateData.class, + Collections.singletonList(screenId)); } /** @@ -215,17 +340,38 @@ public PlateData getPlate(SecurityContext ctx, long id) throws DSOutOfServiceExc */ public WellData getWell(SecurityContext ctx, long id) throws DSOutOfServiceException, DSAccessException { - try { - IQueryPrx qs = gateway.getQueryService(ctx); - ParametersI param = new ParametersI(); - param.addId(id); - WellI tmp = (WellI) qs.findByQuery(GET_WELL_QUERY, param); - if (tmp != null) - return new WellData(tmp); - } catch (DSOutOfServiceException | ServerError e) { - handleException(this, e, "Could not get well"); - } - return null; + Collection tmp = getWells(ctx, Collections.singletonList(id)); + if (tmp.isEmpty()) + return null; + return tmp.iterator().next(); } + /** + * Get wells (Note: These are slightly deeper objects, + * with wellsamples and images loaded) + * @param ctx The SecurityContext + * @param ids The ids of the wells + * @return Collection of wells (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getWells(SecurityContext ctx, Collection ids) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_WELLS_QUERY, WellData.class, ids); + } + + /** + * Get wells of a plate (Note: These are slightly deeper objects, + * with wellsamples and images loaded) + * @param ctx The SecurityContext + * @param plateId The plate Id + * @return Collection of wells (can be empty) + * @throws DSOutOfServiceException + * @throws DSAccessException + */ + public Collection getWells(SecurityContext ctx, long plateId) throws DSOutOfServiceException, + DSAccessException { + return queryDb(ctx, GET_WELLS_FOR_PLATE_QUERY, WellData.class, + Collections.singletonList(plateId)); + } }