Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Gateway method for downloading image #409

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3166,7 +3166,6 @@ else if (!filesetIds.contains(id)) {
p = new DownloadArchivedActivityParam(new File(path), images, icon);
p.setOverride(override);
p.setZip(false);
p.setKeepOriginalPaths(true);
un.notifyActivity(ctx, p);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3709,7 +3709,6 @@ else if (!filesetIds.contains(id)) {
folder, archived, icon);
p.setOverride(override);
p.setZip(false);
p.setKeepOriginalPaths(true);
un.notifyActivity(ctx, p);
}
}
Expand Down
174 changes: 11 additions & 163 deletions src/main/java/org/openmicroscopy/shoola/env/data/OMEROGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.concurrent.ExecutionException;

import omero.gateway.facility.RawDataFacility;
import omero.gateway.facility.TransferFacility;
import omero.gateway.model.FolderData;
import omero.gateway.model.PixelsData;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -172,7 +173,6 @@
import omero.model.ExperimenterGroupI;
import omero.model.FileAnnotation;
import omero.model.Fileset;
import omero.model.FilesetEntry;
import omero.model.GroupExperimenterMap;
import omero.model.IObject;
import omero.model.Image;
Expand Down Expand Up @@ -3042,177 +3042,25 @@ Set<GroupData> getAvailableGroups(SecurityContext ctx,
/**
* Retrieves the archived files if any for the specified set of pixels.
*
* @param ctx The security context.
* @param file The location where to save the files.
* @param image The image to retrieve.
* @param keepOriginalPaths Pass <code>true</code> to preserve the original folder structure
* @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.
*/
Map<Boolean, Object> getArchivedFiles(
SecurityContext ctx, File file, ImageData image, boolean keepOriginalPaths)
throws DSAccessException, DSOutOfServiceException
{
return retrieveArchivedFiles(ctx, file, image);
}

/**
* Retrieves the archived files if any for the specified set of pixels.
*
* @param ctx The security context.
* @param dir The location where to save the files.
* @param ctx The security context.
* @param dir The location where to save the files.
* @param image The image to retrieve.
* @return See above.
* @throws DSAccessException If an error occurred while trying to
* retrieve data from OMERO service.
* retrieve data from OMERO service.
*/
private Map<Boolean, Object> retrieveArchivedFiles(
public List<File> getArchivedFiles(
SecurityContext ctx, File dir, ImageData image)
throws DSAccessException
{
List<?> files = null;
String query;
throws DSAccessException, DSOutOfServiceException {
TransferFacility fc = null;
try {
IQueryPrx service = gw.getQueryService(ctx);
ParametersI param = new ParametersI();
long id;
if (image.isFSImage()) {
id = image.getId();
List<RType> l = new ArrayList<RType>();
l.add(omero.rtypes.rlong(id));
param.add("imageIds", omero.rtypes.rlist(l));
query = createFileSetQuery();
} else { //Prior to FS
if (image.isArchived()) {
StringBuffer buffer = new StringBuffer();
id = image.getDefaultPixels().getId();
buffer.append("select ofile from OriginalFile as ofile ");
buffer.append("join fetch ofile.hasher ");
buffer.append("left join ofile.pixelsFileMaps as pfm ");
buffer.append("left join pfm.child as child ");
buffer.append("where child.id = :id");
param.map.put("id", omero.rtypes.rlong(id));
query = buffer.toString();
} else return null;
}
files = service.findAllByQuery(query, param);
} catch (Exception e) {
handleConnectionException(e);
throw new DSAccessException("Cannot retrieve original file", e);
}

Map<Boolean, Object> result = new HashMap<Boolean, Object>();
if (CollectionUtils.isEmpty(files)) return result;
List<File> downloaded = new ArrayList<File>();
List<String> notDownloaded = new ArrayList<String>();
result.put(Boolean.valueOf(true), downloaded);
result.put(Boolean.valueOf(false), notDownloaded);

if (image.isFSImage()) {
for (Object tmp : files) {
Fileset fs = (Fileset) tmp;
File filesetDir = new File(dir.getAbsolutePath()+File.separator+"Fileset_"+fs.getId().getValue());
filesetDir.mkdir();
String repoPath = fs.getTemplatePrefix().getValue();
for (FilesetEntry fse: fs.copyUsedFiles()) {
OriginalFile of = fse.getOriginalFile();
String ofDir = of.getPath().getValue().replace(repoPath, "");
File outDir = new File(filesetDir.getAbsolutePath()+File.separator+ofDir);
outDir.mkdirs();
File saved = saveOriginalFile(ctx, of, outDir);
if (saved != null)
downloaded.add(saved);
else
notDownloaded.add(of.getName().getValue());
}
}
}
else { //Prior to FS
for (Object tmp : files) {
OriginalFile of = (OriginalFile) tmp;
File outDir = new File(dir.getAbsolutePath());
File saved = saveOriginalFile(ctx, of, outDir);
if (saved != null)
downloaded.add(saved);
else
notDownloaded.add(of.getName().getValue());
}
fc = gw.getFacility(TransferFacility.class);
} catch (ExecutionException e) {
throw new DSOutOfServiceException(e.getMessage());
}

return result;
return fc.downloadImage(ctx, dir.getAbsolutePath(), image.getId());
}

/**
* Save an OriginalFile of into directory dir
* @param ctx The SecurityContext
* @param of The OriginalFile
* @param dir The output directory
* @return The File if the operation was successfull, null if it wasn't.
*/
private File saveOriginalFile(SecurityContext ctx, OriginalFile of, File dir) {
File out = new File(dir, of.getName().getValue());
if (out.exists()) {
log(out.getAbsolutePath()+" already exists.");
return null;
}

try {
RawFileStorePrx store = gw.getRawFileService(ctx);
store.setFileId(of.getId().getValue());

long size = of.getSize().getValue();
long offset = 0;
try (FileOutputStream stream = new FileOutputStream(out))
{
for (offset = 0; (offset+INC) < size;) {
stream.write(store.read(offset, INC));
offset += INC;
}
stream.write(store.read(offset, (int) (size-offset)));
}
} catch (Exception e) {
handleConnectionException(e);
return null;
}
return out;
}

/**
* Checks if the given path already exists and if so, generates a new path
* name path(N), where N is a consecutive number
*
* @param path
* The path name to check
* @param isFile
* Pass <code>true</code> if the path name represents a file
* @return A unique path name based on the given path or the path itself if
* it doesn't exist yet
*/
private String generateUniquePathname(String path, boolean isFile) {
File tmp = new File(path);
if (tmp.exists()) {
String fileExt = null;
if (isFile && path.matches(".+\\..+")) {
fileExt = path.substring(path.lastIndexOf('.'), path.length());
path = path.substring(0, path.lastIndexOf('.'));
}
if (path.matches(".+\\(\\d+\\)")) {
int n = Integer.parseInt(path.substring(
path.lastIndexOf('(') + 1, path.lastIndexOf(')')));
n++;
path = path.substring(0, path.lastIndexOf('(')) + "(" + n + ")";
} else {
path += "(1)";
}
if (fileExt != null)
path += fileExt;
return generateUniquePathname(path, isFile);
}
return path;
}

/**
* Downloads a file previously uploaded to the server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import omero.api.StatefulServiceInterfacePrx;

import omero.gateway.model.FolderData;
import org.openmicroscopy.shoola.env.data.model.DeletableObject;

import omero.gateway.Gateway;
import omero.gateway.SecurityContext;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
Expand Down Expand Up @@ -282,20 +280,18 @@ public void cutAndPaste(SecurityContext ctx, Map toPaste, Map toCut)
throws DSOutOfServiceException, DSAccessException;

/**
* Retrieves and saves locally the archived files.
*
* @param ctx The security context.
* @param location The location where to save the files.
* @param imageID The ID of the image.
* @param keepOriginalPath Pass <code>true</code> to preserve the original
* path structure
* @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 Map<Boolean, Object> getArchivedImage(SecurityContext ctx,
File location, long imageID, boolean keepOriginalPath)
* Retrieves and saves locally the archived files.
*
* @param ctx The security context.
* @param location The location where to save the files.
* @param imageID The ID 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.
*/
public List<File> getArchivedImage(SecurityContext ctx,
File location, long imageID)
throws DSOutOfServiceException, DSAccessException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import omero.cmd.Request;
import omero.cmd.graphs.ChildOption;
import omero.gateway.facility.DataManagerFacility;
import omero.gateway.model.FolderData;
import omero.model.Annotation;
import omero.model.AnnotationAnnotationLink;
Expand Down Expand Up @@ -72,9 +70,6 @@

import omero.gateway.util.PojoMapper;

import org.openmicroscopy.shoola.env.data.util.SearchDataContext;

import omero.gateway.Gateway;
import omero.gateway.SecurityContext;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
Expand Down Expand Up @@ -495,14 +490,14 @@ public void cutAndPaste(SecurityContext ctx, Map toPaste, Map toCut)
/**
* Implemented as specified by {@link OmeroDataService}.
*/
public Map<Boolean, Object> getArchivedImage(SecurityContext ctx,
File file, long imageID, boolean keepOriginalPath)
public List<File> getArchivedImage(SecurityContext ctx,
File file, long imageID)
throws DSOutOfServiceException, DSAccessException
{
context.getLogger().debug(this, file.getAbsolutePath());
//Check the image is archived.
ImageData image = gateway.getImage(ctx, imageID, null);
return gateway.getArchivedFiles(ctx, file, image, keepOriginalPath);
return gateway.getArchivedFiles(ctx, file, image);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public class DownloadArchivedActivityParam
/** Flag for zipping the downloaded images */
private boolean zip = false;

/** Flag for preserving the original folder structure */
private boolean keepOriginalPaths = true;

/**
* Creates a new instance.
*
Expand Down Expand Up @@ -130,24 +127,4 @@ public void setZip(boolean zip) {
this.zip = zip;
}

/**
* Returns if the original folder structure should be preserved
*
* @return See above
*/
public boolean isKeepOriginalPaths() {
return keepOriginalPaths;
}

/**
* Sets the keepOriginalPaths flag
*
* @param keepOriginalPaths
* Pass <code>true</code> to preserve the original folder
* structure
*/
public void setKeepOriginalPaths(boolean keepOriginalPaths) {
this.keepOriginalPaths = keepOriginalPaths;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,11 @@ public CallHandle loadOriginalFiles(SecurityContext ctx,
* @param override Flag indicating to override the existing file if it
* exists, <code>false</code> otherwise.
* @param zip Pass <code>true</code> to create a zip file
* @param keepOriginalPaths Pass <code>true</code> to preserve the original folder structure
* @param observer Call-back handler.
* @return A handle that can be used to cancel the call.
*/
public CallHandle loadArchivedImage(SecurityContext ctx, List<DataObject> objects,
File location, boolean override, boolean zip, boolean keepOriginalPaths,
File location, boolean override, boolean zip,
AgentEventListener observer);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ public CallHandle loadOriginalFiles(SecurityContext ctx,
*/
public CallHandle loadArchivedImage(SecurityContext ctx,
List<DataObject> objects, File location,
boolean override, boolean zip, boolean keepOriginalPaths,
boolean override, boolean zip,
AgentEventListener observer) {
BatchCallTree cmd = new ArchivedImageLoader(ctx, objects, location,
override, zip, keepOriginalPaths);
override, zip);
return cmd.exec(observer);
}

Expand Down
Loading