@@ -27,6 +27,24 @@ public class DatafilesResponse {
2727 public final List <Long > ids = new ArrayList <>();
2828 public final Set <String > missing = new HashSet <>();
2929 public long totalSize = 0L ;
30+
31+ /**
32+ * Submit a query for Datafiles, then appends the ids, increments the size, and
33+ * records any missing file locations.
34+ *
35+ * @param query Query to submit
36+ * @throws TopcatException If the query returns not authorized, or not found.
37+ */
38+ public void submitDatafilesQuery (String query )
39+ throws TopcatException {
40+ JsonArray jsonArray = submitQuery (query );
41+ for (JsonObject jsonObject : jsonArray .getValuesAs (JsonObject .class )) {
42+ JsonObject datafile = jsonObject .getJsonObject ("Datafile" );
43+ ids .add (datafile .getJsonNumber ("id" ).longValueExact ());
44+ missing .remove (datafile .getString ("location" ));
45+ totalSize += datafile .getJsonNumber ("fileSize" ).longValueExact ();
46+ }
47+ }
3048 }
3149
3250 private Logger logger = LoggerFactory .getLogger (IcatClient .class );
@@ -185,13 +203,7 @@ public DatafilesResponse getDatafiles(List<String> files) throws TopcatException
185203 String quotedFile = "'" + file + "'" ;
186204 int encodedFileLength = URLEncoder .encode (quotedFile , "UTF8" ).length ();
187205 if (chunkSize + 3 + encodedFileLength > chunkLimit ) {
188- JsonArray jsonArray = submitQuery (queryPrefix + chunkedFiles + querySuffix );
189- for (JsonObject jsonObject : jsonArray .getValuesAs (JsonObject .class )) {
190- JsonObject datafile = jsonObject .getJsonObject ("Datafile" );
191- response .ids .add (datafile .getJsonNumber ("id" ).longValueExact ());
192- response .missing .remove (datafile .getString ("location" ));
193- response .totalSize += datafile .getJsonNumber ("fileSize" ).longValueExact ();
194- }
206+ response .submitDatafilesQuery (queryPrefix + chunkedFiles + querySuffix );
195207
196208 chunkedFiles = quotedFile ;
197209 chunkSize = encodedFileLength ;
@@ -202,13 +214,7 @@ public DatafilesResponse getDatafiles(List<String> files) throws TopcatException
202214 response .missing .add (file );
203215 }
204216 }
205- JsonArray jsonArray = submitQuery (queryPrefix + chunkedFiles + querySuffix );
206- for (JsonObject jsonObject : jsonArray .getValuesAs (JsonObject .class )) {
207- JsonObject datafile = jsonObject .getJsonObject ("Datafile" );
208- response .ids .add (datafile .getJsonNumber ("id" ).longValueExact ());
209- response .missing .remove (datafile .getString ("location" ));
210- response .totalSize += datafile .getJsonNumber ("fileSize" ).longValueExact ();
211- }
217+ response .submitDatafilesQuery (queryPrefix + chunkedFiles + querySuffix );
212218
213219 return response ;
214220 }
@@ -238,13 +244,9 @@ public long getDatasetFileCount(long datasetId) throws TopcatException {
238244 * @throws TopcatException
239245 */
240246 public long getDatasetFileSize (long datasetId ) throws TopcatException {
241- String query = "SELECT datafile.fileSize FROM Datafile datafile WHERE datafile.dataset.id = " + datasetId ;
247+ String query = "SELECT SUM( datafile.fileSize) FROM Datafile datafile WHERE datafile.dataset.id = " + datasetId ;
242248 JsonArray jsonArray = submitQuery (query );
243- long size = 0L ;
244- for (JsonNumber number : jsonArray .getValuesAs (JsonNumber .class )) {
245- size += number .longValueExact ();
246- }
247- return size ;
249+ return jsonArray .getJsonNumber (0 ).longValueExact ();
248250 }
249251
250252 /**
@@ -276,6 +278,11 @@ private JsonArray submitQuery(String query) throws TopcatException {
276278 /**
277279 * Gets a single Entity of the specified type, without any other conditions.
278280 *
281+ * NOTE: This function is written and intended for getting Investigation,
282+ * Dataset or Datafile entities as part of the tests. It does not handle casing of
283+ * entities containing multiple words, or querying for a specific instance of an
284+ * entity.
285+ *
279286 * @param entityType Type of ICAT Entity to get
280287 * @return A single ICAT Entity of the specified type as a JsonObject
281288 * @throws TopcatException
@@ -429,7 +436,8 @@ public int getQueuePriority(String userName) throws TopcatException {
429436 }
430437 }
431438
432- if (!userName .startsWith (Properties .getInstance ().getProperty ("anonUserName" ))) {
439+ String anonUserName = Properties .getInstance ().getProperty ("anonUserName" );
440+ if (anonUserName == null || !userName .startsWith (anonUserName )) {
433441 // The anonymous cart username will end with the user's sessionId so cannot do .equals
434442 return priorityMap .getAuthenticatedPriority ();
435443 } else {
0 commit comments