|
43 | 43 | import java.util.concurrent.ExecutionException; |
44 | 44 |
|
45 | 45 | import omero.gateway.facility.RawDataFacility; |
| 46 | +import omero.gateway.facility.TransferFacility; |
46 | 47 | import omero.gateway.model.FolderData; |
47 | 48 | import omero.gateway.model.PixelsData; |
48 | 49 | import org.apache.commons.collections4.CollectionUtils; |
|
172 | 173 | import omero.model.ExperimenterGroupI; |
173 | 174 | import omero.model.FileAnnotation; |
174 | 175 | import omero.model.Fileset; |
175 | | -import omero.model.FilesetEntry; |
176 | 176 | import omero.model.GroupExperimenterMap; |
177 | 177 | import omero.model.IObject; |
178 | 178 | import omero.model.Image; |
@@ -3042,177 +3042,25 @@ Set<GroupData> getAvailableGroups(SecurityContext ctx, |
3042 | 3042 | /** |
3043 | 3043 | * Retrieves the archived files if any for the specified set of pixels. |
3044 | 3044 | * |
3045 | | - * @param ctx The security context. |
3046 | | - * @param file The location where to save the files. |
3047 | | - * @param image The image to retrieve. |
3048 | | - * @param keepOriginalPaths Pass <code>true</code> to preserve the original folder structure |
3049 | | - * @return See above. |
3050 | | - * @throws DSOutOfServiceException If the connection is broken, or not logged in |
3051 | | - * @throws DSAccessException If an error occurred while trying to |
3052 | | - * retrieve data from OMERO service. |
3053 | | - */ |
3054 | | - Map<Boolean, Object> getArchivedFiles( |
3055 | | - SecurityContext ctx, File file, ImageData image, boolean keepOriginalPaths) |
3056 | | - throws DSAccessException, DSOutOfServiceException |
3057 | | - { |
3058 | | - return retrieveArchivedFiles(ctx, file, image); |
3059 | | - } |
3060 | | - |
3061 | | - /** |
3062 | | - * Retrieves the archived files if any for the specified set of pixels. |
3063 | | - * |
3064 | | - * @param ctx The security context. |
3065 | | - * @param dir The location where to save the files. |
| 3045 | + * @param ctx The security context. |
| 3046 | + * @param dir The location where to save the files. |
3066 | 3047 | * @param image The image to retrieve. |
3067 | 3048 | * @return See above. |
3068 | 3049 | * @throws DSAccessException If an error occurred while trying to |
3069 | | - * retrieve data from OMERO service. |
| 3050 | + * retrieve data from OMERO service. |
3070 | 3051 | */ |
3071 | | - private Map<Boolean, Object> retrieveArchivedFiles( |
| 3052 | + public List<File> getArchivedFiles( |
3072 | 3053 | SecurityContext ctx, File dir, ImageData image) |
3073 | | - throws DSAccessException |
3074 | | - { |
3075 | | - List<?> files = null; |
3076 | | - String query; |
| 3054 | + throws DSAccessException, DSOutOfServiceException { |
| 3055 | + TransferFacility fc = null; |
3077 | 3056 | try { |
3078 | | - IQueryPrx service = gw.getQueryService(ctx); |
3079 | | - ParametersI param = new ParametersI(); |
3080 | | - long id; |
3081 | | - if (image.isFSImage()) { |
3082 | | - id = image.getId(); |
3083 | | - List<RType> l = new ArrayList<RType>(); |
3084 | | - l.add(omero.rtypes.rlong(id)); |
3085 | | - param.add("imageIds", omero.rtypes.rlist(l)); |
3086 | | - query = createFileSetQuery(); |
3087 | | - } else { //Prior to FS |
3088 | | - if (image.isArchived()) { |
3089 | | - StringBuffer buffer = new StringBuffer(); |
3090 | | - id = image.getDefaultPixels().getId(); |
3091 | | - buffer.append("select ofile from OriginalFile as ofile "); |
3092 | | - buffer.append("join fetch ofile.hasher "); |
3093 | | - buffer.append("left join ofile.pixelsFileMaps as pfm "); |
3094 | | - buffer.append("left join pfm.child as child "); |
3095 | | - buffer.append("where child.id = :id"); |
3096 | | - param.map.put("id", omero.rtypes.rlong(id)); |
3097 | | - query = buffer.toString(); |
3098 | | - } else return null; |
3099 | | - } |
3100 | | - files = service.findAllByQuery(query, param); |
3101 | | - } catch (Exception e) { |
3102 | | - handleConnectionException(e); |
3103 | | - throw new DSAccessException("Cannot retrieve original file", e); |
3104 | | - } |
3105 | | - |
3106 | | - Map<Boolean, Object> result = new HashMap<Boolean, Object>(); |
3107 | | - if (CollectionUtils.isEmpty(files)) return result; |
3108 | | - List<File> downloaded = new ArrayList<File>(); |
3109 | | - List<String> notDownloaded = new ArrayList<String>(); |
3110 | | - result.put(Boolean.valueOf(true), downloaded); |
3111 | | - result.put(Boolean.valueOf(false), notDownloaded); |
3112 | | - |
3113 | | - if (image.isFSImage()) { |
3114 | | - for (Object tmp : files) { |
3115 | | - Fileset fs = (Fileset) tmp; |
3116 | | - File filesetDir = new File(dir.getAbsolutePath()+File.separator+"Fileset_"+fs.getId().getValue()); |
3117 | | - filesetDir.mkdir(); |
3118 | | - String repoPath = fs.getTemplatePrefix().getValue(); |
3119 | | - for (FilesetEntry fse: fs.copyUsedFiles()) { |
3120 | | - OriginalFile of = fse.getOriginalFile(); |
3121 | | - String ofDir = of.getPath().getValue().replace(repoPath, ""); |
3122 | | - File outDir = new File(filesetDir.getAbsolutePath()+File.separator+ofDir); |
3123 | | - outDir.mkdirs(); |
3124 | | - File saved = saveOriginalFile(ctx, of, outDir); |
3125 | | - if (saved != null) |
3126 | | - downloaded.add(saved); |
3127 | | - else |
3128 | | - notDownloaded.add(of.getName().getValue()); |
3129 | | - } |
3130 | | - } |
3131 | | - } |
3132 | | - else { //Prior to FS |
3133 | | - for (Object tmp : files) { |
3134 | | - OriginalFile of = (OriginalFile) tmp; |
3135 | | - File outDir = new File(dir.getAbsolutePath()); |
3136 | | - File saved = saveOriginalFile(ctx, of, outDir); |
3137 | | - if (saved != null) |
3138 | | - downloaded.add(saved); |
3139 | | - else |
3140 | | - notDownloaded.add(of.getName().getValue()); |
3141 | | - } |
| 3057 | + fc = gw.getFacility(TransferFacility.class); |
| 3058 | + } catch (ExecutionException e) { |
| 3059 | + throw new DSOutOfServiceException(e.getMessage()); |
3142 | 3060 | } |
3143 | | - |
3144 | | - return result; |
| 3061 | + return fc.downloadImage(ctx, dir.getAbsolutePath(), image.getId()); |
3145 | 3062 | } |
3146 | 3063 |
|
3147 | | - /** |
3148 | | - * Save an OriginalFile of into directory dir |
3149 | | - * @param ctx The SecurityContext |
3150 | | - * @param of The OriginalFile |
3151 | | - * @param dir The output directory |
3152 | | - * @return The File if the operation was successfull, null if it wasn't. |
3153 | | - */ |
3154 | | - private File saveOriginalFile(SecurityContext ctx, OriginalFile of, File dir) { |
3155 | | - File out = new File(dir, of.getName().getValue()); |
3156 | | - if (out.exists()) { |
3157 | | - log(out.getAbsolutePath()+" already exists."); |
3158 | | - return null; |
3159 | | - } |
3160 | | - |
3161 | | - try { |
3162 | | - RawFileStorePrx store = gw.getRawFileService(ctx); |
3163 | | - store.setFileId(of.getId().getValue()); |
3164 | | - |
3165 | | - long size = of.getSize().getValue(); |
3166 | | - long offset = 0; |
3167 | | - try (FileOutputStream stream = new FileOutputStream(out)) |
3168 | | - { |
3169 | | - for (offset = 0; (offset+INC) < size;) { |
3170 | | - stream.write(store.read(offset, INC)); |
3171 | | - offset += INC; |
3172 | | - } |
3173 | | - stream.write(store.read(offset, (int) (size-offset))); |
3174 | | - } |
3175 | | - } catch (Exception e) { |
3176 | | - handleConnectionException(e); |
3177 | | - return null; |
3178 | | - } |
3179 | | - return out; |
3180 | | - } |
3181 | | - |
3182 | | - /** |
3183 | | - * Checks if the given path already exists and if so, generates a new path |
3184 | | - * name path(N), where N is a consecutive number |
3185 | | - * |
3186 | | - * @param path |
3187 | | - * The path name to check |
3188 | | - * @param isFile |
3189 | | - * Pass <code>true</code> if the path name represents a file |
3190 | | - * @return A unique path name based on the given path or the path itself if |
3191 | | - * it doesn't exist yet |
3192 | | - */ |
3193 | | - private String generateUniquePathname(String path, boolean isFile) { |
3194 | | - File tmp = new File(path); |
3195 | | - if (tmp.exists()) { |
3196 | | - String fileExt = null; |
3197 | | - if (isFile && path.matches(".+\\..+")) { |
3198 | | - fileExt = path.substring(path.lastIndexOf('.'), path.length()); |
3199 | | - path = path.substring(0, path.lastIndexOf('.')); |
3200 | | - } |
3201 | | - if (path.matches(".+\\(\\d+\\)")) { |
3202 | | - int n = Integer.parseInt(path.substring( |
3203 | | - path.lastIndexOf('(') + 1, path.lastIndexOf(')'))); |
3204 | | - n++; |
3205 | | - path = path.substring(0, path.lastIndexOf('(')) + "(" + n + ")"; |
3206 | | - } else { |
3207 | | - path += "(1)"; |
3208 | | - } |
3209 | | - if (fileExt != null) |
3210 | | - path += fileExt; |
3211 | | - return generateUniquePathname(path, isFile); |
3212 | | - } |
3213 | | - return path; |
3214 | | - } |
3215 | | - |
3216 | 3064 | /** |
3217 | 3065 | * Downloads a file previously uploaded to the server. |
3218 | 3066 | * |
|
0 commit comments