This repository has been archived by the owner on Jul 2, 2018. It is now read-only.
/
ItemController.java
809 lines (740 loc) · 24.8 KB
/
ItemController.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/**
* License: src/main/resources/license/escidoc.license
*/
package de.mpg.imeji.logic.controller.resource;
import static com.google.common.base.Strings.isNullOrEmpty;
import static de.mpg.imeji.logic.storage.util.StorageUtils.calculateChecksum;
import static de.mpg.imeji.logic.storage.util.StorageUtils.getMimeType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import de.mpg.imeji.exceptions.BadRequestException;
import de.mpg.imeji.exceptions.ImejiException;
import de.mpg.imeji.exceptions.NotAllowedError;
import de.mpg.imeji.exceptions.NotFoundException;
import de.mpg.imeji.exceptions.UnprocessableError;
import de.mpg.imeji.logic.Imeji;
import de.mpg.imeji.logic.auth.util.AuthUtil;
import de.mpg.imeji.logic.controller.util.ImejiFactory;
import de.mpg.imeji.logic.reader.ReaderFacade;
import de.mpg.imeji.logic.search.Search;
import de.mpg.imeji.logic.search.Search.SearchObjectTypes;
import de.mpg.imeji.logic.search.factory.SearchFactory;
import de.mpg.imeji.logic.search.factory.SearchFactory.SEARCH_IMPLEMENTATIONS;
import de.mpg.imeji.logic.search.jenasearch.ImejiSPARQL;
import de.mpg.imeji.logic.search.jenasearch.JenaCustomQueries;
import de.mpg.imeji.logic.search.model.SearchQuery;
import de.mpg.imeji.logic.search.model.SearchResult;
import de.mpg.imeji.logic.search.model.SortCriterion;
import de.mpg.imeji.logic.storage.Storage;
import de.mpg.imeji.logic.storage.StorageController;
import de.mpg.imeji.logic.storage.UploadResult;
import de.mpg.imeji.logic.storage.util.StorageUtils;
import de.mpg.imeji.logic.util.ObjectHelper;
import de.mpg.imeji.logic.util.TempFileUtil;
import de.mpg.imeji.logic.vo.CollectionImeji;
import de.mpg.imeji.logic.vo.Container;
import de.mpg.imeji.logic.vo.Item;
import de.mpg.imeji.logic.vo.Item.Visibility;
import de.mpg.imeji.logic.vo.MetadataSet;
import de.mpg.imeji.logic.vo.Properties.Status;
import de.mpg.imeji.logic.vo.User;
import de.mpg.imeji.logic.vo.predefinedMetadata.Metadata;
import de.mpg.imeji.logic.writer.WriterFacade;
import de.mpg.j2j.helper.J2JHelper;
/**
* Implements CRUD and Search methods for {@link Item}
*
* @author saquet (initial creation)
* @author $Author$ (last modification)
* @version $Revision$ $LastChangedDate$
*/
public class ItemController extends ImejiController {
private static final Logger LOGGER = Logger.getLogger(ItemController.class);
private static final ReaderFacade READER = new ReaderFacade(Imeji.imageModel);
private static final WriterFacade WRITER = new WriterFacade(Imeji.imageModel);
public static final String NO_THUMBNAIL_URL = "NO_THUMBNAIL_URL";
private final Search search =
SearchFactory.create(SearchObjectTypes.ITEM, SEARCH_IMPLEMENTATIONS.ELASTIC);
/**
* Controller constructor
*/
public ItemController() {
super();
}
/**
* Create an {@link Item} in a {@link CollectionImeji}
*
* @param item
* @param coll
* @param user
* @throws ImejiException
* @return
*/
public Item create(Item item, URI coll, User user) throws ImejiException {
create(Arrays.asList(item), coll, user);
return item;
}
/**
* Create an {@link Item} for a {@link File}.
*
* @param f
* @param filename (optional)
* @param c - the collection in which the file is uploaded
* @param user
* @return
* @throws ImejiException
*/
public Item createWithFile(Item item, File f, String filename, CollectionImeji c, User user)
throws ImejiException {
StorageController sc = new StorageController();
if (!AuthUtil.staticAuth().createContent(user, c)) {
throw new NotAllowedError(
"User not Allowed to upload files in collection " + c.getIdString());
}
String guessedNotAllowedFormat = sc.guessNotAllowedFormat(f);
if (StorageUtils.BAD_FORMAT.equals(guessedNotAllowedFormat)) {
throw new UnprocessableError("upload_format_not_allowed");
}
// To check the user Quota, it always has to be provided with the Admin User
UserController uc = new UserController(user);
uc.checkQuota(f, c);
UploadResult uploadResult = sc.upload(filename, f, c.getIdString());
if (item == null) {
item = ImejiFactory.newItem(c);
}
if (filename == null || filename.equals("")) {
throw new UnprocessableError("Filename must not be empty!");
}
validateChecksum(c.getId(), f, false);
String mimeType = StorageUtils.getMimeType(guessedNotAllowedFormat);
item = ImejiFactory.newItem(item, c, user, uploadResult.getId(), filename,
URI.create(uploadResult.getOrginal()), URI.create(uploadResult.getThumb()),
URI.create(uploadResult.getWeb()), mimeType);
item.setChecksum(uploadResult.getChecksum());
item.setFileSize(uploadResult.getFileSize());
if (uploadResult.getWidth() > 0 && uploadResult.getHeight() > 0) {
item.setWidth(uploadResult.getWidth());
item.setHeight(uploadResult.getHeight());
}
return create(item, c.getId(), user);
}
/**
* Create an {@link Item} with an external {@link File} according to its URL
*
* @param item
* @param c
* @param externalFileUrl
* @param download
* @param user
* @return
* @throws IOException
* @throws ImejiException
*/
public Item createWithExternalFile(Item item, CollectionImeji c, String externalFileUrl,
String filename, boolean download, User user) throws ImejiException {
String origName = FilenameUtils.getName(externalFileUrl);
if ("".equals(filename) || filename == null) {
filename = origName;
} else {
filename = filename + "." + FilenameUtils.getExtension(origName);
}
if (filename == null || filename.equals("")) {
throw new BadRequestException(
"Could not derive the filename. Please provide the filename with the request!");
}
if (externalFileUrl == null || externalFileUrl.equals("")) {
throw new BadRequestException("Please provide fetchUrl or referenceUrl with the request!");
}
if (item == null) {
item = ImejiFactory.newItem(c);
}
if (download) {
// download the file in storage
File tmp = readFile(externalFileUrl);
item = createWithFile(item, tmp, filename, c, user);
} else {
// Reference the file
item.setFilename(filename);
item.setFullImageUrl(URI.create(externalFileUrl));
item.setThumbnailImageUrl(URI.create(NO_THUMBNAIL_URL));
item.setWebImageUrl(URI.create(NO_THUMBNAIL_URL));
item = create(item, c.getId(), user);
}
return item;
}
/**
* Create a {@link List} of {@link Item} in a {@link CollectionImeji}. This method is faster than
* using create(Item item, URI coll) when creating many items
*
* @param items
* @param coll
* @throws ImejiException
*/
public void create(Collection<Item> items, URI coll, User user) throws ImejiException {
CollectionController cc = new CollectionController();
CollectionImeji ic = cc.retrieveLazy(coll, user);
for (Item img : items) {
prepareCreate(img, user);
if (Status.PENDING.equals(ic.getStatus())) {
img.setVisibility(Visibility.PRIVATE);
} else {
img.setVisibility(Visibility.PUBLIC);
}
img.setFilename(FilenameUtils.getName(img.getFilename()));
img.setStatus(ic.getStatus());
img.setCollection(coll);
img.getMetadataSet().setProfile(ic.getProfile());
ic.getImages().add(img.getId());
}
cleanMetadata(items);
ProfileController pc = new ProfileController();
WRITER.create(J2JHelper.cast2ObjectList((List<?>) items),
pc.retrieve(items.iterator().next().getMetadataSet().getProfile(), user), user);
// Update the collection
cc.update(cc.retrieve(coll, user), Imeji.adminUser);
}
/**
* Create an {@link Item}
*
* @param item
* @param uploadedFile
* @param filename
* @param u
* @param fetchUrl
* @param referenceUrl
* @return
* @throws ImejiException
*/
public Item create(Item item, File uploadedFile, String filename, User u, String fetchUrl,
String referenceUrl) throws ImejiException {
isLoggedInUser(u);
if (uploadedFile != null && isNullOrEmpty(filename)) {
throw new BadRequestException("Filename for the uploaded file must not be empty!");
}
if (uploadedFile == null && isNullOrEmpty(fetchUrl) && isNullOrEmpty(referenceUrl)) {
throw new BadRequestException(
"Please upload a file or provide one of the fetchUrl or referenceUrl as input.");
}
Item newItem = new Item(item);
CollectionController cc = new CollectionController();
CollectionImeji collection;
try {
collection = cc.retrieve(item.getCollection(), u);
} catch (Exception e) {
throw new UnprocessableError(
"There was a problem with specified collection: " + e.getLocalizedMessage());
}
if (uploadedFile != null) {
newItem = createWithFile(item, uploadedFile, filename, collection, u);
} else if (getExternalFileUrl(fetchUrl, referenceUrl) != null) {
// If no file, but either a fetchUrl or a referenceUrl
newItem = createWithExternalFile(item, collection, getExternalFileUrl(fetchUrl, referenceUrl),
filename, downloadFile(fetchUrl), u);
} else {
throw new BadRequestException("Filename or reference must not be empty!");
}
return newItem;
}
/**
* User ObjectLoader to load image
*
* @param imgUri
* @return
* @throws ImejiException
*/
public Item retrieve(URI imgUri, User user) throws ImejiException {
return (Item) READER.read(imgUri.toString(), user, new Item());
}
public Item retrieveLazy(URI imgUri, User user) throws ImejiException {
return (Item) READER.readLazy(imgUri.toString(), user, new Item());
}
/**
* Lazy Retrieve the Item containing the file with the passed storageid
*
* @param storageId
* @param user
* @return
* @throws ImejiException
*/
public Item retrieveLazyForFile(String storageId, User user) throws ImejiException {
Search s = SearchFactory.create(SEARCH_IMPLEMENTATIONS.JENA);
List<String> r =
s.searchString(JenaCustomQueries.selectItemOfFile(storageId), null, null, 0, -1)
.getResults();
if (!r.isEmpty() && r.get(0) != null) {
return retrieveLazy(URI.create(r.get(0)), user);
} else {
throw new NotFoundException("Can not find the resource requested");
}
}
/**
* Retrieve the items lazy (without the metadata)
*
* @param uris
* @param limit
* @param offset
* @return
* @throws ImejiException
*/
public Collection<Item> retrieveBatch(List<String> uris, int limit, int offset, User user)
throws ImejiException {
List<Item> items = uris2Items(uris, limit, offset);
READER.read(J2JHelper.cast2ObjectList(items), user);
return items;
}
/**
* Retrieve the items fully (with all metadata)
*
* @param uris
* @param limit
* @param offset
* @param user
* @return
* @throws ImejiException
*/
public Collection<Item> retrieveBatchLazy(List<String> uris, int limit, int offset, User user)
throws ImejiException {
List<Item> items = uris2Items(uris, limit, offset);
READER.readLazy(J2JHelper.cast2ObjectList(items), user);
return items;
}
/**
* Transform a list of uris into a list of Item
*
* @param uris
* @param limit
* @param offset
* @return
*/
private List<Item> uris2Items(List<String> uris, int limit, int offset) {
List<String> retrieveUris;
if (limit < 0) {
retrieveUris = uris;
} else {
retrieveUris = uris.size() > 0 && limit > 0
? uris.subList(offset, getMin(offset + limit, uris.size())) : new ArrayList<String>();
}
List<Item> items = new ArrayList<Item>();
for (String s : retrieveUris) {
items.add((Item) J2JHelper.setId(new Item(), URI.create(s)));
}
return items;
}
/**
* Retrieve all {@link Item} (all status, all users) in imeji
*
* @return
* @throws ImejiException
*/
public Collection<Item> retrieveAll(User user) throws ImejiException {
List<String> uris = ImejiSPARQL.exec(JenaCustomQueries.selectItemAll(), Imeji.imageModel);
return retrieveBatch(uris, -1, 0, user);
}
/**
* Update an {@link Item} in the database
*
* @param item
* @param user
* @throws ImejiException
*/
public Item update(Item item, User user) throws ImejiException {
updateBatch(Arrays.asList(item), user);
return retrieve(item.getId(), user);
}
/**
* Update a {@link Collection} of {@link Item}
*
* @param items
* @param user
* @throws ImejiException
*/
public void updateBatch(Collection<Item> items, User user) throws ImejiException {
if (items != null && !items.isEmpty()) {
List<Object> imBeans = new ArrayList<Object>();
List<String> profileLists = new ArrayList<String>();
String profileToAdd = "";
for (Item item : items) {
profileToAdd = item.getMetadataSet().getProfile() != null
? item.getMetadataSet().getProfile().toString() : "profileisnull";
if (!profileLists.contains(profileToAdd)) {
profileLists.add(profileToAdd);
}
if (profileLists.size() > 1) {
throw new UnprocessableError(
"Error during batch update, items for batch update do not have same metadata profile!");
}
prepareUpdate(item, user);
item.setFilename(FilenameUtils.getName(item.getFilename()));
imBeans.add(createFulltextForMetadata(item));
}
cleanMetadata(items);
ProfileController pc = new ProfileController();
WRITER.update(imBeans,
pc.retrieve(items.iterator().next().getMetadataSet().getProfile(), user), user, true);
}
}
/**
* Update the File of an {@link Item}
*
* @param item
* @param f
* @param user
* @return
* @throws ImejiException
*/
public Item updateFile(Item item, File f, String filename, User user) throws ImejiException {
validateChecksum(item.getCollection(), f, true);
// First remove the old File from the Internal Storage if its there
if (!isNullOrEmpty(item.getStorageId())) {
removeFileFromStorage(item.getStorageId());
}
CollectionController cc = new CollectionController();
CollectionImeji col = cc.retrieveLazy(item.getCollection(), user);
UserController uc = new UserController(user);
uc.checkQuota(f, col);
StorageController sc = new StorageController();
UploadResult uploadResult = sc.upload(item.getFilename(), f, col.getIdString());
item.setFiletype(getMimeType(f));
item.setChecksum(calculateChecksum(f));
item.setStorageId(uploadResult.getId());
item.setFullImageUrl(URI.create(uploadResult.getOrginal()));
item.setThumbnailImageUrl(URI.create(uploadResult.getThumb()));
item.setWebImageUrl(URI.create(uploadResult.getWeb()));
item.setFileSize(uploadResult.getFileSize());
if (uploadResult.getWidth() > 0 && uploadResult.getHeight() > 0) {
item.setWidth(uploadResult.getWidth());
item.setHeight(uploadResult.getHeight());
}
if (filename != null) {
item.setFilename(filename);
}
return update(item, user);
}
/**
* Update the {@link Item} with External link to File.
*
* @param item
* @param externalFileUrl
* @param filename
* @param download
* @param u
* @return
* @throws ImejiException
*/
public Item updateWithExternalFile(Item item, String externalFileUrl, String filename,
boolean download, User u) throws ImejiException {
String origName = FilenameUtils.getName(externalFileUrl);
filename =
isNullOrEmpty(filename) ? origName : filename + "." + FilenameUtils.getExtension(origName);
item.setFilename(filename);
if (download) {
// download the file in storage
File tmp = readFile(externalFileUrl);
item = updateFile(item, tmp, filename, u);
} else {
removeFileFromStorage(item.getStorageId());
// Reference the file
item.setFullImageUrl(URI.create(externalFileUrl));
item.setThumbnailImageUrl(URI.create(NO_THUMBNAIL_URL));
item.setWebImageUrl(URI.create(NO_THUMBNAIL_URL));
item.setChecksum("");
item.setFiletype("");
item = update(item, u);
}
return item;
}
/**
*
* Update only the thumbnail and the Web Resolution (doesn't change the original file)
*
* @param item
* @param f
* @param user
* @return
* @throws ImejiException
*/
public Item updateThumbnail(Item item, File f, User user) throws ImejiException {
StorageController sc = new StorageController();
sc.update(item.getWebImageUrl().toString(), f);
sc.update(item.getThumbnailImageUrl().toString(), f);
return update(item, user);
}
/**
* Delete a {@link List} of {@link Item} inclusive all files stored in the {@link Storage}
*
* @param items
* @param user
* @return
* @throws ImejiException
*/
public void delete(List<Item> items, User user) throws ImejiException {
WRITER.delete(new ArrayList<Object>(items), user);
for (Item item : items) {
removeFileFromStorage(item.getStorageId());
}
}
/**
* Delete a {@link List} of {@link Item} inclusive all files stored in the {@link Storage}
*
* @param itemId
* @param u
* @return
* @throws ImejiException
*/
public void delete(String itemId, User u) throws ImejiException {
Item item = retrieve(ObjectHelper.getURI(Item.class, itemId), u);
delete(Arrays.asList(item), u);
}
/**
* Search {@link Item}
*
* @param containerUri - if the search is done within a {@link Container}
* @param searchQuery - the {@link SearchQuery}
* @param sortCri - the {@link SortCriterion}
* @param user
* @param size
* @param offset
* @return
*/
public SearchResult search(URI containerUri, SearchQuery searchQuery, SortCriterion sortCri,
User user, String spaceId, int size, int offset) {
return search.search(searchQuery, sortCri, user,
containerUri != null ? containerUri.toString() : null, spaceId, offset, size);
}
/**
* load items of a container. Perform a search to load all items: is faster than to read the
* complete container
*
* @param c
* @param user
*/
public Container searchAndSetContainerItems(Container c, User user, int limit, int offset) {
List<String> newUris = search(c.getId(), null, null, user, null, limit, 0).getResults();
c.getImages().clear();
for (String s : newUris) {
c.getImages().add(URI.create(s));
}
return c;
}
/**
* Retrieve all items filtered by query
*
* @param user
* @param q
* @return
* @throws ImejiException
*/
public List<Item> searchAndRetrieve(URI containerUri, SearchQuery q, SortCriterion sort,
User user, String spaceId, int offset, int size) throws ImejiException, IOException {
List<Item> itemList = new ArrayList<Item>();
try {
List<String> results =
search(containerUri, q, sort, user, spaceId, size, offset).getResults();
itemList = (List<Item>) retrieveBatch(results, -1, 0, user);
} catch (Exception e) {
throw new UnprocessableError("Cannot retrieve items:", e);
}
return itemList;
}
/**
* Set the status of a {@link List} of {@link Item} to released
*
* @param l
* @param user
* @throws ImejiException
*/
public void release(List<Item> l, User user) throws ImejiException {
Collection<Item> items = filterItemsByStatus(l, Status.PENDING);
for (Item item : items) {
prepareRelease(item, user);
}
updateBatch(items, user);
}
/**
* Make the Items private
*
* @param l
* @param user
* @throws ImejiException
*/
public void unRelease(List<Item> l, User user) throws ImejiException {
Collection<Item> items = filterItemsByStatus(l, Status.RELEASED);
for (Item item : items) {
item.setStatus(Status.PENDING);
}
updateBatch(items, user);
}
/**
* Set the status of a {@link List} of {@link Item} to withdraw and delete its files from the
* {@link Storage}
*
* @param l
* @param comment
* @throws ImejiException
*/
public void withdraw(List<Item> l, String comment, User user) throws ImejiException {
Collection<Item> items = filterItemsByStatus(l, Status.RELEASED);
for (Item item : items) {
prepareWithdraw(item, comment);
}
updateBatch(items, user);
for (Item item : items) {
removeFileFromStorage(item.getStorageId());
}
}
/**
* Return a new filtered List of only item with the requested {@link Status}
*
* @param items
* @param status
* @return
*/
private Collection<Item> filterItemsByStatus(List<Item> items, Status status) {
return new ArrayList<>(Collections2.filter(items, new Predicate<Item>() {
@Override
public boolean apply(Item item) {
return item.getStatus() == status;
}
}));
}
/**
* Remove a file from the current {@link Storage}
*
* @param id
*/
private void removeFileFromStorage(String id) {
StorageController storageController = new StorageController();
try {
storageController.delete(id);
} catch (Exception e) {
LOGGER.error("error deleting file", e);
}
}
/**
* Initialize the fulltext search value for all {@link Metadata} of an {@link Item}
*
* @param item
* @return
*/
private Item createFulltextForMetadata(Item item) {
for (MetadataSet mds : item.getMetadataSets()) {
for (Metadata md : mds.getMetadata()) {
md.asFulltext();
}
}
return item;
}
/**
* Return the external Url of the File
*
* @param fetchUrl
* @param referenceUrl
* @return
*/
private String getExternalFileUrl(String fetchUrl, String referenceUrl) {
return firstNonNullOrEmtpy(fetchUrl, referenceUrl);
}
private String firstNonNullOrEmtpy(String... strs) {
if (strs == null) {
return null;
}
for (String str : strs) {
if (str != null && !"".equals(str.trim())) {
return str;
}
}
return null;
}
/**
* True if the file must be download in imeji (i.e fetchurl is defined)
*
* @param fetchUrl
* @return
*/
private boolean downloadFile(String fetchUrl) {
return !isNullOrEmpty(fetchUrl);
}
/**
* Set the status of a {@link List} of {@link Item} to released
*
* @param l
* @param user
* @throws ImejiException
*/
public void updateItemsProfile(List<Item> l, User user, String profileUri) throws ImejiException {
for (Item item : l) {
item.getMetadataSet().setProfile(URI.create(profileUri));
item.getMetadataSet().getMetadata().clear();
}
updateBatch(l, user);
}
/**
* Throws an {@link Exception} if the file cannot be uploaded. The validation will only occur when
* the file has been stored locally)
*
* @throws ImejiException
* @throws UnprocessableError
*/
private void validateChecksum(URI collectionURI, File file, Boolean isUpdate)
throws UnprocessableError, ImejiException {
if (Imeji.isValidateChecksumInCollection()) {
if (checksumExistsInCollection(collectionURI, StorageUtils.calculateChecksum(file))) {
throw new UnprocessableError((!isUpdate)
? "Same file already exists in the collection (with same checksum). Please choose another file."
: "Same file already exists in the collection or you are trying to upload same file for the item (with same checksum). Please choose another file.");
}
}
}
/**
* True if the checksum already exists within another {@link Item} in this {@link CollectionImeji}
*
* @param filename
* @return
*/
private boolean checksumExistsInCollection(URI collectionId, String checksum) {
Search s = SearchFactory.create(SearchObjectTypes.ITEM, SEARCH_IMPLEMENTATIONS.JENA);
return s.searchString(JenaCustomQueries.selectItemByChecksum(collectionId, checksum), null,
null, 0, -1).getNumberOfRecords() > 0;
}
/**
* Clean the values of all {@link Metadata} of an {@link Item}
*
* @param l
*/
private void cleanMetadata(Collection<Item> l) {
for (Item item : l) {
for (Metadata md : item.getMetadataSet().getMetadata()) {
md.clean();
}
item.getMetadataSet().trim();
}
}
/**
* Read a file from its url
*
* @param tmp
* @param url
* @return
* @throws UnprocessableError
*/
private File readFile(String url) throws UnprocessableError {
try {
StorageController sController = new StorageController("external");
File tmp = TempFileUtil.createTempFile("createOrUploadWithExternalFile", null);
sController.read(url, new FileOutputStream(tmp), true);
return tmp;
} catch (Exception e) {
throw new UnprocessableError(e.getLocalizedMessage());
}
}
}