-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTransferFacilityHelper.java
223 lines (203 loc) · 7.62 KB
/
TransferFacilityHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (C) 2015 University of Dundee & Open Microscopy Environment.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package omero.gateway.facility;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import omero.RType;
import omero.ServerError;
import omero.api.IQueryPrx;
import omero.api.RawFileStorePrx;
import omero.gateway.Gateway;
import omero.gateway.SecurityContext;
import omero.gateway.exception.DSAccessException;
import omero.gateway.exception.DSOutOfServiceException;
import omero.gateway.model.ImageData;
import omero.model.Fileset;
import omero.model.FilesetEntry;
import omero.model.OriginalFile;
import omero.sys.ParametersI;
import org.apache.commons.collections.CollectionUtils;
/**
* Encapsulates some functionality needed by the {@link TransferFacility}
*
* @author Dominik Lindner <a
* href="mailto:[email protected]">[email protected]</a>
*/
public class TransferFacilityHelper {
/** Maximum size of bytes read at once. */
private static final int INC = 262144;//
private BrowseFacility browse;
private Gateway gateway;
/**
* Creates a new instance.
*
* @param gateway
* Reference to the gateway.
* @param datamanager
* Reference to the manager facility.
* @param parent
* Reference to the parent.
* @throws ExecutionException
*/
TransferFacilityHelper(Gateway gateway) throws ExecutionException {
this.gateway = gateway;
this.browse = gateway.getFacility(BrowseFacility.class);
}
/**
* Downloads the original file of an image from the server.
*
* @param context
* The security context.
* @param targetPath
* Path to the file.
* @param imageId
* The identifier of the image.
* @return See above
* @throws DSOutOfServiceException
* @throws DSAccessException
*/
List<File> downloadImage(SecurityContext context, String targetPath,
long imageId) throws DSAccessException, DSOutOfServiceException {
List<File> files = new ArrayList<File>();
ImageData image = browse.findObject(context, ImageData.class, imageId,
true);
String query;
List<?> filesets;
try {
IQueryPrx service = gateway.getQueryService(context);
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;
}
filesets = service.findAllByQuery(query, param);
} catch (Exception e) {
throw new DSAccessException("Cannot retrieve original file", e);
}
if (CollectionUtils.isEmpty(filesets))
return files;
Iterator<?> i;
List<OriginalFile> values = new ArrayList<OriginalFile>();
if (image.isFSImage()) {
i = filesets.iterator();
Fileset set;
List<FilesetEntry> entries;
Iterator<FilesetEntry> j;
while (i.hasNext()) {
set = (Fileset) i.next();
entries = set.copyUsedFiles();
j = entries.iterator();
while (j.hasNext()) {
FilesetEntry fs = j.next();
values.add(fs.getOriginalFile());
}
}
} else
values.addAll((List<OriginalFile>) filesets);
RawFileStorePrx store = null;
OriginalFile of;
long size;
FileOutputStream stream = null;
long offset = 0;
i = values.iterator();
File f = null;
while (i.hasNext()) {
of = (OriginalFile) i.next();
try {
store = gateway.getRawFileService(context);
store.setFileId(of.getId().getValue());
f = new File(targetPath, of.getName().getValue());
files.add(f);
stream = new FileOutputStream(f);
size = of.getSize().getValue();
try {
try {
for (offset = 0; (offset + INC) < size;) {
stream.write(store.read(offset, INC));
offset += INC;
}
} finally {
stream.write(store.read(offset, (int) (size - offset)));
stream.close();
}
} catch (Exception e) {
if (stream != null)
stream.close();
if (f != null) {
f.delete();
files.remove(f);
}
}
} catch (IOException e) {
if (f != null) {
f.delete();
files.remove(f);
}
throw new DSAccessException("Cannot create file in folderPath",
e);
} catch (Throwable t) {
throw new DSAccessException("ServerError on retrieveArchived",
t);
} finally {
try {
store.close();
} catch (ServerError e) {
}
}
}
return files;
}
/**
* Creates the query to load the file set corresponding to a given image.
*
* @return See above.
*/
private String createFileSetQuery() {
StringBuffer buffer = new StringBuffer();
buffer.append("select fs from Fileset as fs ");
buffer.append("join fetch fs.images as image ");
buffer.append("left outer join fetch fs.usedFiles as usedFile ");
buffer.append("join fetch usedFile.originalFile as f ");
buffer.append("join fetch f.hasher ");
buffer.append("where image.id in (:imageIds)");
return buffer.toString();
}
}