Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Oct 8, 2024
1 parent adb9493 commit e794738
Showing 1 changed file with 70 additions and 50 deletions.
120 changes: 70 additions & 50 deletions src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import fr.igred.omero.repository.PlateWrapper;
import fr.igred.omero.repository.ProjectWrapper;
import fr.igred.omero.repository.ScreenWrapper;
import fr.igred.omero.repository.WellSampleWrapper;
import fr.igred.omero.repository.WellWrapper;
import fr.igred.omero.roi.ROIWrapper;
import ij.IJ;
Expand All @@ -58,7 +57,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -305,7 +303,7 @@ private GenericObjectWrapper<?> getObject(String type, long id) {
String singularType = singularType(type);

GenericObjectWrapper<?> object = null;
if (singularType.equals(TAG)) {
if (TAG.equals(singularType)) {
try {
object = client.getTag(id);
} catch (OMEROServerError | ServiceException e) {
Expand Down Expand Up @@ -475,31 +473,17 @@ private List<? extends GenericObjectWrapper<?>> listForScreen(String type, long
String singularType = singularType(type);

List<? extends GenericObjectWrapper<?>> objects = new ArrayList<>(0);
ScreenWrapper screen = client.getScreen(id);
List<PlateWrapper> plates = screen.getPlates();

ScreenWrapper screen = client.getScreen(id);
switch (singularType) {
case PLATE:
objects = plates;
objects = screen.getPlates();
break;
case WELL:
List<WellWrapper> wells = new ArrayList<>();
for (PlateWrapper plate : plates) {
wells.addAll(plate.getWells(client));
}
wells.sort(Comparator.comparing(GenericObjectWrapper::getId));
objects = wells;
objects = screen.getWells(client);
break;
case IMAGE:
List<ImageWrapper> images = new ArrayList<>();
for (PlateWrapper plate : plates) {
for (WellWrapper well : plate.getWells(client)) {
images.addAll(well.getWellSamples().stream()
.map(WellSampleWrapper::getImage)
.collect(Collectors.toList()));
}
}
images.sort(Comparator.comparing(GenericObjectWrapper::getId));
objects = images;
objects = screen.getImages(client);
break;
case TAG:
objects = screen.getTags(client);
Expand All @@ -524,17 +508,14 @@ private List<? extends GenericObjectWrapper<?>> listForPlate(String type, long i
String singularType = singularType(type);

List<? extends GenericObjectWrapper<?>> objects = new ArrayList<>(0);
PlateWrapper plate = client.getPlate(id);

PlateWrapper plate = client.getPlate(id);
switch (singularType) {
case WELL:
objects = plate.getWells(client);
break;
case IMAGE:
objects = plate.getWells(client)
.stream()
.flatMap(w -> w.getWellSamples().stream())
.map(WellSampleWrapper::getImage)
.collect(Collectors.toList());
objects = plate.getImages(client);
break;
case TAG:
objects = plate.getTags(client);
Expand All @@ -546,6 +527,60 @@ private List<? extends GenericObjectWrapper<?>> listForPlate(String type, long i
}


/**
* Lists the objects of the specified type inside a well.
*
* @param type The object type.
* @param id The well id.
*
* @return A list of GenericObjectWrappers.
*/
private List<? extends GenericObjectWrapper<?>> listForWell(String type, long id)
throws AccessException, ServiceException, ExecutionException {
String singularType = singularType(type);

List<? extends GenericObjectWrapper<?>> objects = new ArrayList<>(0);

WellWrapper well = client.getWell(id);
switch (singularType) {
case IMAGE:
objects = well.getImages();
break;
case TAG:
objects = well.getTags(client);
break;
default:
IJ.error(String.format(ERROR_POSSIBLE_VALUES, INVALID, type, "images or tags."));
break;
}
return objects;
}


/**
* Lists the objects of the specified type linked to an image.
*
* @param type The object type.
* @param id The image id.
*
* @return A list of GenericObjectWrappers.
*/
private List<? extends GenericObjectWrapper<?>> listForImage(String type, long id)
throws AccessException, ServiceException, ExecutionException {
String singularType = singularType(type);

List<? extends GenericObjectWrapper<?>> objects = new ArrayList<>(0);

ImageWrapper image = client.getImage(id);
if (TAG.equals(singularType)) {
objects = image.getTags(client);
} else {
IJ.error(String.format(ERROR_POSSIBLE_VALUES, INVALID, type, "tags."));
}
return objects;
}


/**
* Connects the client to OMERO.
*
Expand Down Expand Up @@ -605,8 +640,7 @@ public String downloadImage(long imageId, String path) {
List<File> files = new ArrayList<>(0);
try {
files = client.getImage(imageId).download(client, path);
} catch (ServiceException | AccessException | OMEROServerError | ExecutionException |
NoSuchElementException e) {
} catch (ServiceException | AccessException | OMEROServerError | ExecutionException | NoSuchElementException e) {
IJ.error("Could not download image: " + e.getMessage());
}
return files.stream().map(File::toString).collect(Collectors.joining(","));
Expand Down Expand Up @@ -987,7 +1021,6 @@ public String list(String type, String name) {
*/
public String list(String type, String parent, long id) {
String singularParent = singularType(parent);
String singularType = singularType(type);

Collection<? extends GenericObjectWrapper<?>> objects = new ArrayList<>(0);
try {
Expand All @@ -1008,23 +1041,10 @@ public String list(String type, String parent, long id) {
objects = listForPlate(type, id);
break;
case WELL:
WellWrapper well = client.getWell(id);
if (IMAGE.equals(singularType)) {
objects = well.getWellSamples().stream()
.map(WellSampleWrapper::getImage)
.collect(Collectors.toList());
} else if (TAG.equals(singularType)) {
objects = well.getTags(client);
} else {
IJ.error(String.format(ERROR_POSSIBLE_VALUES, INVALID, type, "images or tags."));
}
objects = listForWell(type, id);
break;
case IMAGE:
if (TAG.equals(singularType)) {
objects = client.getImage(id).getTags(client);
} else {
IJ.error("Invalid type: " + type + ". Only possible value is: tags.");
}
objects = listForImage(type, id);
break;
default:
String msg = String.format(ERROR_POSSIBLE_VALUES, INVALID, parent,
Expand Down Expand Up @@ -1090,8 +1110,8 @@ public void link(String type1, long id1, String type2, long id2) {

try {
// Link tag to repository object
if (t1.equals(TAG) ^ t2.equals(TAG)) {
String obj = t1.equals(TAG) ? t2 : t1;
if (TAG.equals(t1) ^ TAG.equals(t2)) {
String obj = TAG.equals(t1) ? t2 : t1;

GenericRepositoryObjectWrapper<?> object = getRepositoryObject(obj, map.get(obj));
if (object != null) {
Expand Down Expand Up @@ -1136,8 +1156,8 @@ public void unlink(String type1, long id1, String type2, long id2) {

try {
// Unlink tag from repository object
if (t1.equals(TAG) ^ t2.equals(TAG)) {
String obj = t1.equals(TAG) ? t2 : t1;
if (TAG.equals(t1) ^ TAG.equals(t2)) {
String obj = TAG.equals(t1) ? t2 : t1;

GenericRepositoryObjectWrapper<?> object = getRepositoryObject(obj, map.get(obj));
if (object != null) {
Expand Down

0 comments on commit e794738

Please sign in to comment.