forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCollectionServiceImpl.java
More file actions
1094 lines (926 loc) · 43.9 KB
/
CollectionServiceImpl.java
File metadata and controls
1094 lines (926 loc) · 43.9 KB
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.dspace.app.util.AuthorizeUtil;
import org.dspace.authorize.AuthorizeConfiguration;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.authorize.service.ResourcePolicyService;
import org.dspace.browse.ItemCountException;
import org.dspace.browse.ItemCounter;
import org.dspace.content.dao.CollectionDAO;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.I18nUtil;
import org.dspace.core.LogHelper;
import org.dspace.core.service.LicenseService;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.DiscoverQuery.SORT_ORDER;
import org.dspace.discovery.DiscoverResult;
import org.dspace.discovery.IndexableObject;
import org.dspace.discovery.SearchService;
import org.dspace.discovery.SearchServiceException;
import org.dspace.discovery.indexobject.IndexableCollection;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.GroupService;
import org.dspace.eperson.service.SubscribeService;
import org.dspace.event.Event;
import org.dspace.harvest.HarvestedCollection;
import org.dspace.harvest.service.HarvestedCollectionService;
import org.dspace.identifier.IdentifierException;
import org.dspace.identifier.service.IdentifierService;
import org.dspace.services.ConfigurationService;
import org.dspace.workflow.factory.WorkflowServiceFactory;
import org.dspace.xmlworkflow.WorkflowConfigurationException;
import org.dspace.xmlworkflow.factory.XmlWorkflowFactory;
import org.dspace.xmlworkflow.state.Workflow;
import org.dspace.xmlworkflow.storedcomponents.CollectionRole;
import org.dspace.xmlworkflow.storedcomponents.service.CollectionRoleService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Service implementation for the Collection object.
* This class is responsible for all business logic calls for the Collection object and is autowired by spring.
* This class should never be accessed directly.
*
* @author kevinvandevelde at atmire.com
*/
public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> implements CollectionService {
/**
* log4j category
*/
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(CollectionServiceImpl.class);
@Autowired(required = true)
protected CollectionDAO collectionDAO;
@Autowired(required = true)
protected AuthorizeService authorizeService;
@Autowired(required = true)
protected ResourcePolicyService resourcePolicyService;
@Autowired(required = true)
protected BitstreamService bitstreamService;
@Autowired(required = true)
protected ItemService itemService;
@Autowired(required = true)
protected CommunityService communityService;
@Autowired(required = true)
protected GroupService groupService;
@Autowired(required = true)
protected IdentifierService identifierService;
@Autowired(required = true)
protected LicenseService licenseService;
@Autowired(required = true)
protected SubscribeService subscribeService;
@Autowired(required = true)
protected WorkspaceItemService workspaceItemService;
@Autowired(required = true)
protected HarvestedCollectionService harvestedCollectionService;
@Autowired(required = true)
protected XmlWorkflowFactory workflowFactory;
@Autowired(required = true)
protected CollectionRoleService collectionRoleService;
@Autowired(required = true)
protected SearchService searchService;
@Autowired(required = true)
protected ConfigurationService configurationService;
protected CollectionServiceImpl() {
super();
}
@Override
public Collection create(Context context, Community community) throws SQLException, AuthorizeException {
return create(context, community, null);
}
@Override
public Collection create(Context context, Community community, String handle)
throws SQLException, AuthorizeException {
return create(context, community, handle, null);
}
@Override
public Collection create(Context context, Community community,
String handle, UUID uuid) throws SQLException, AuthorizeException {
if (community == null) {
throw new IllegalArgumentException("Community cannot be null when creating a new collection.");
}
Collection newCollection;
if (uuid != null) {
newCollection = collectionDAO.create(context, new Collection(uuid));
} else {
newCollection = collectionDAO.create(context, new Collection());
}
//Add our newly created collection to our community, authorization checks occur in THIS method
communityService.addCollection(context, community, newCollection);
// create the default authorization policy for collections
// of 'anonymous' READ
Group anonymousGroup = groupService.findByName(context, Group.ANONYMOUS);
authorizeService.createResourcePolicy(context, newCollection, anonymousGroup, null, Constants.READ, null);
// now create the default policies for submitted items
authorizeService
.createResourcePolicy(context, newCollection, anonymousGroup, null, Constants.DEFAULT_ITEM_READ, null);
authorizeService
.createResourcePolicy(context, newCollection, anonymousGroup, null,
Constants.DEFAULT_BITSTREAM_READ, null);
collectionDAO.save(context, newCollection);
//Update our collection so we have a collection identifier
try {
if (handle == null) {
identifierService.register(context, newCollection);
} else {
identifierService.register(context, newCollection, handle);
}
} catch (IllegalStateException | IdentifierException ex) {
throw new IllegalStateException(ex);
}
context.addEvent(new Event(Event.CREATE, Constants.COLLECTION,
newCollection.getID(), newCollection.getHandle(),
getIdentifiers(context, newCollection)));
log.info(LogHelper.getHeader(context, "create_collection",
"collection_id=" + newCollection.getID())
+ ",handle=" + newCollection.getHandle());
return newCollection;
}
@Override
public List<Collection> findAll(Context context) throws SQLException {
MetadataField nameField = metadataFieldService.findByElement(context, MetadataSchemaEnum.DC.getName(),
"title", null);
if (nameField == null) {
throw new IllegalArgumentException(
"Required metadata field '" + MetadataSchemaEnum.DC.getName() + ".title' doesn't exist!");
}
return collectionDAO.findAll(context, nameField);
}
@Override
public List<Collection> findAll(Context context, Integer limit, Integer offset) throws SQLException {
MetadataField nameField = metadataFieldService.findByElement(context, MetadataSchemaEnum.DC.getName(),
"title", null);
if (nameField == null) {
throw new IllegalArgumentException(
"Required metadata field '" + MetadataSchemaEnum.DC.getName() + ".title' doesn't exist!");
}
return collectionDAO.findAll(context, nameField, limit, offset);
}
@Override
public List<Collection> findAuthorizedOptimized(Context context, int actionID) throws SQLException {
if (!configurationService
.getBooleanProperty("org.dspace.content.Collection.findAuthorizedPerformanceOptimize", false)) {
// Fallback to legacy query if config says so. The rationale could be that a site found a bug.
return findAuthorized(context, null, actionID);
}
List<Collection> myResults = new ArrayList<>();
if (authorizeService.isAdmin(context)) {
return findAll(context);
}
//Check eperson->policy
List<Collection> directToCollection = findDirectMapped(context, actionID);
for (int i = 0; i < directToCollection.size(); i++) {
if (!myResults.contains(directToCollection.get(i))) {
myResults.add(directToCollection.get(i));
}
}
//Check eperson->groups->policy
List<Collection> groupToCollection = findGroupMapped(context, actionID);
for (Collection aGroupToCollection : groupToCollection) {
if (!myResults.contains(aGroupToCollection)) {
myResults.add(aGroupToCollection);
}
}
//Check eperson->groups->groups->policy->collection
//i.e. Malcolm Litchfield is a member of OSU_Press_Embargo,
// which is a member of: COLLECTION_24_ADMIN, COLLECTION_24_SUBMIT
List<Collection> group2GroupToCollection = findGroup2GroupMapped(context, actionID);
for (Collection aGroup2GroupToCollection : group2GroupToCollection) {
if (!myResults.contains(aGroup2GroupToCollection)) {
myResults.add(aGroup2GroupToCollection);
}
}
//TODO Check eperson->groups->groups->policy->community
//TODO Check eperson->groups->policy->community
// i.e. Typical Community Admin -- name.# > COMMUNITY_10_ADMIN > Ohio State University Press
//Check eperson->comm-admin
List<Collection> group2commCollections = findGroup2CommunityMapped(context);
for (Collection group2commCollection : group2commCollections) {
if (!myResults.contains(group2commCollection)) {
myResults.add(group2commCollection);
}
}
// Return the collections, sorted alphabetically
Collections.sort(myResults, new CollectionNameComparator());
return myResults;
}
@Override
public List<Collection> findDirectMapped(Context context, int actionID) throws SQLException {
return collectionDAO
.findAuthorized(context, context.getCurrentUser(), Arrays.asList(Constants.ADD, Constants.ADMIN));
}
@Override
public List<Collection> findGroup2CommunityMapped(Context context) throws SQLException {
List<Community> communities = communityService
.findAuthorizedGroupMapped(context, Arrays.asList(Constants.ADD, Constants.ADMIN));
List<Collection> collections = new ArrayList<>();
for (Community community : communities) {
collections.addAll(community.getCollections());
}
return collections;
}
@Override
public List<Collection> findGroup2GroupMapped(Context context, int actionID) throws SQLException {
return collectionDAO
.findAuthorizedByGroup(context, context.getCurrentUser(), Collections.singletonList(actionID));
}
@Override
public List<Collection> findGroupMapped(Context context, int actionID) throws SQLException {
List<Community> communities = communityService
.findAuthorized(context, Arrays.asList(Constants.ADD, Constants.ADMIN));
List<Collection> collections = new ArrayList<>();
for (Community community : communities) {
collections.addAll(community.getCollections());
}
return collections;
}
@Override
public Collection find(Context context, UUID id) throws SQLException {
return collectionDAO.findByID(context, Collection.class, id);
}
@Override
public void setMetadataSingleValue(Context context, Collection collection,
MetadataFieldName field, String language, String value)
throws MissingResourceException, SQLException {
if (field.equals(MD_NAME) && (value == null || value.trim().equals(""))) {
try {
value = I18nUtil.getMessage("org.dspace.content.untitled");
} catch (MissingResourceException e) {
value = "Untitled";
}
}
/*
* Set metadata field to null if null
* and trim strings to eliminate excess
* whitespace.
*/
if (value == null) {
clearMetadata(context, collection, field.schema, field.element, field.qualifier, Item.ANY);
collection.setMetadataModified();
} else {
super.setMetadataSingleValue(context, collection, field, null, value);
}
collection.addDetails(field.toString());
}
@Override
public Bitstream setLogo(Context context, Collection collection, InputStream is)
throws AuthorizeException, IOException, SQLException {
// Check authorisation
// authorized to remove the logo when DELETE rights
// authorized when canEdit
if (!((is == null) && authorizeService.authorizeActionBoolean(
context, collection, Constants.DELETE))) {
canEdit(context, collection, true);
}
// First, delete any existing logo
if (collection.getLogo() != null) {
bitstreamService.delete(context, collection.getLogo());
}
if (is == null) {
collection.setLogo(null);
log.info(LogHelper.getHeader(context, "remove_logo",
"collection_id=" + collection.getID()));
} else {
Bitstream newLogo = bitstreamService.create(context, is);
//added for data migration by Upgrade Dspace-Clarin
addLogo(context, collection, newLogo);
}
collection.setModified();
return collection.getLogo();
}
@Override
public void addLogo(Context context, Collection collection, Bitstream newLogo)
throws SQLException, AuthorizeException {
collection.setLogo(newLogo);
// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, collection, Constants.READ);
authorizeService.addPolicies(context, policies, newLogo);
log.info(LogHelper.getHeader(context, "set_logo",
"collection_id=" + collection.getID() + "logo_bitstream_id="
+ newLogo.getID()));
}
@Override
public Group createWorkflowGroup(Context context, Collection collection, int step)
throws SQLException, AuthorizeException {
// Check authorisation - Must be an Admin to create Workflow Group
AuthorizeUtil.authorizeManageWorkflowsGroup(context, collection);
if (getWorkflowGroup(context, collection, step) == null) {
//turn off authorization so that Collection Admins can create Collection Workflow Groups
context.turnOffAuthorisationSystem();
Group g = groupService.create(context);
groupService.setName(g,
"COLLECTION_" + collection.getID() + "_WORKFLOW_STEP_" + step);
groupService.update(context, g);
context.restoreAuthSystemState();
setWorkflowGroup(context, collection, step, g);
}
return getWorkflowGroup(context, collection, step);
}
@Override
public void setWorkflowGroup(Context context, Collection collection, int step, Group group)
throws SQLException {
Workflow workflow = null;
try {
workflow = workflowFactory.getWorkflow(collection);
} catch (WorkflowConfigurationException e) {
log.error(LogHelper.getHeader(context, "setWorkflowGroup",
"collection_id=" + collection.getID() + " " + e.getMessage()), e);
}
if (!StringUtils.equals(workflowFactory.getDefaultWorkflow().getID(), workflow.getID())) {
throw new IllegalArgumentException(
"setWorkflowGroup can be used only on collection with the default basic dspace workflow. "
+ "Instead, the collection: "
+ collection.getID() + " has the workflow: " + workflow.getID());
}
String roleId;
switch (step) {
case 1:
roleId = CollectionRoleService.LEGACY_WORKFLOW_STEP1_NAME;
break;
case 2:
roleId = CollectionRoleService.LEGACY_WORKFLOW_STEP2_NAME;
break;
case 3:
roleId = CollectionRoleService.LEGACY_WORKFLOW_STEP3_NAME;
break;
default:
throw new IllegalArgumentException("Illegal step count: " + step);
}
CollectionRole colRole = collectionRoleService.find(context, collection, roleId);
if (colRole == null) {
if (group != null) {
colRole = collectionRoleService.create(context, collection, roleId, group);
}
} else {
if (group != null) {
colRole.setGroup(group);
collectionRoleService.update(context, colRole);
} else {
collectionRoleService.delete(context, colRole);
}
}
collection.setModified();
}
@Override
public Group getWorkflowGroup(Context context, Collection collection, int step) {
String roleId;
switch (step) {
case 1:
roleId = CollectionRoleService.LEGACY_WORKFLOW_STEP1_NAME;
break;
case 2:
roleId = CollectionRoleService.LEGACY_WORKFLOW_STEP2_NAME;
break;
case 3:
roleId = CollectionRoleService.LEGACY_WORKFLOW_STEP3_NAME;
break;
default:
throw new IllegalArgumentException("Illegal step count: " + step);
}
CollectionRole colRole;
try {
colRole = collectionRoleService.find(context, collection, roleId);
if (colRole != null) {
return colRole.getGroup();
}
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Group createSubmitters(Context context, Collection collection) throws SQLException, AuthorizeException {
// Check authorisation - Must be an Admin to create Submitters Group
AuthorizeUtil.authorizeManageSubmittersGroup(context, collection);
Group submitters = collection.getSubmitters();
if (submitters == null) {
//turn off authorization so that Collection Admins can create Collection Submitters
context.turnOffAuthorisationSystem();
submitters = groupService.create(context);
context.restoreAuthSystemState();
groupService.setName(submitters,
"COLLECTION_" + collection.getID() + "_SUBMIT");
groupService.update(context, submitters);
}
// register this as the submitter group
collection.setSubmitters(submitters);
authorizeService.addPolicy(context, collection, Constants.ADD, submitters);
return submitters;
}
@Override
public void removeSubmitters(Context context, Collection collection) throws SQLException, AuthorizeException {
// Check authorisation - Must be an Admin to delete Submitters Group
AuthorizeUtil.authorizeManageSubmittersGroup(context, collection);
// just return if there is no administrative group.
if (collection.getSubmitters() == null) {
return;
}
// Remove the link to the collection table.
collection.setSubmitters(null);
}
@Override
public Group createAdministrators(Context context, Collection collection) throws SQLException, AuthorizeException {
// Check authorisation - Must be an Admin to create more Admins
AuthorizeUtil.authorizeManageAdminGroup(context, collection);
Group admins = collection.getAdministrators();
if (admins == null) {
//turn off authorization so that Community Admins can create Collection Admins
context.turnOffAuthorisationSystem();
admins = groupService.create(context);
context.restoreAuthSystemState();
groupService.setName(admins, "COLLECTION_" + collection.getID() + "_ADMIN");
groupService.update(context, admins);
}
authorizeService.addPolicy(context, collection,
Constants.ADMIN, admins);
// register this as the admin group
collection.setAdmins(admins);
context.addEvent(new Event(Event.MODIFY, Constants.COLLECTION, collection.getID(),
null, getIdentifiers(context, collection)));
return admins;
}
@Override
public void removeAdministrators(Context context, Collection collection) throws SQLException, AuthorizeException {
// Check authorisation - Must be an Admin of the parent community to delete Admin Group
AuthorizeUtil.authorizeRemoveAdminGroup(context, collection);
Group admins = collection.getAdministrators();
// just return if there is no administrative group.
if (admins == null) {
return;
}
// Remove the link to the collection table.
collection.setAdmins(null);
context.addEvent(new Event(Event.MODIFY, Constants.COLLECTION, collection.getID(),
null, getIdentifiers(context, collection)));
}
@Override
public String getLicense(Collection collection) {
String license = getMetadataFirstValue(collection, CollectionService.MD_LICENSE, Item.ANY);
if (license == null || license.trim().equals("")) {
// Fallback to site-wide default
license = licenseService.getDefaultSubmissionLicense();
}
return license;
}
@Override
public boolean hasCustomLicense(Collection collection) {
String license = collection.getLicenseCollection();
return StringUtils.isNotBlank(license);
}
@Override
public void createTemplateItem(Context context, Collection collection) throws SQLException, AuthorizeException {
// Check authorisation
AuthorizeUtil.authorizeManageTemplateItem(context, collection);
if (collection.getTemplateItem() == null) {
Item template = itemService.createTemplateItem(context, collection);
collection.setTemplateItem(template);
log.info(LogHelper.getHeader(context, "create_template_item",
"collection_id=" + collection.getID() + ",template_item_id="
+ template.getID()));
}
}
@Override
public void removeTemplateItem(Context context, Collection collection)
throws SQLException, AuthorizeException, IOException {
// Check authorisation
AuthorizeUtil.authorizeManageTemplateItem(context, collection);
Item template = collection.getTemplateItem();
if (template != null) {
log.info(LogHelper.getHeader(context, "remove_template_item",
"collection_id=" + collection.getID() + ",template_item_id="
+ template.getID()));
// temporarily turn off auth system, we have already checked the permission on the top of the method
// check it again will fail because we have already broken the relation between the collection and the item
context.turnOffAuthorisationSystem();
collection.setTemplateItem(null);
itemService.delete(context, template);
context.restoreAuthSystemState();
}
context.addEvent(new Event(Event.MODIFY, Constants.COLLECTION,
collection.getID(), "remove_template_item", getIdentifiers(context, collection)));
}
@Override
public void addItem(Context context, Collection collection, Item item) throws SQLException, AuthorizeException {
// Check authorisation
authorizeService.authorizeAction(context, collection, Constants.ADD);
log.info(LogHelper.getHeader(context, "add_item", "collection_id="
+ collection.getID() + ",item_id=" + item.getID()));
// Create mapping
// We do NOT add the item to the collection template since we would have to load in all our items
// Instead we add the collection to an item which works in the same way.
if (!item.getCollections().contains(collection)) {
item.addCollection(collection);
}
context.addEvent(new Event(Event.ADD, Constants.COLLECTION, collection.getID(),
Constants.ITEM, item.getID(), item.getHandle(),
getIdentifiers(context, collection)));
}
@Override
public void removeItem(Context context, Collection collection, Item item)
throws SQLException, AuthorizeException, IOException {
// Check authorisation
authorizeService.authorizeAction(context, collection, Constants.REMOVE);
//Check if we orphaned our poor item
if (item.getCollections().size() == 1) {
// Orphan; delete it
itemService.delete(context, item);
} else {
//Remove the item from the collection if we have multiple collections
item.removeCollection(collection);
}
context.addEvent(new Event(Event.REMOVE, Constants.COLLECTION,
collection.getID(), Constants.ITEM, item.getID(), item.getHandle(),
getIdentifiers(context, collection)));
}
@Override
public void update(Context context, Collection collection) throws SQLException, AuthorizeException {
// Check authorisation
canEdit(context, collection, true);
log.info(LogHelper.getHeader(context, "update_collection",
"collection_id=" + collection.getID()));
super.update(context, collection);
collectionDAO.save(context, collection);
if (collection.isModified()) {
context.addEvent(new Event(Event.MODIFY, Constants.COLLECTION,
collection.getID(), null, getIdentifiers(context, collection)));
collection.clearModified();
}
if (collection.isMetadataModified()) {
context.addEvent(new Event(Event.MODIFY_METADATA, Constants.COLLECTION, collection.getID(),
collection.getDetails(),getIdentifiers(context, collection)));
collection.clearModified();
}
collection.clearDetails();
}
@Override
public boolean canEditBoolean(Context context, Collection collection) throws SQLException {
return canEditBoolean(context, collection, true);
}
@Override
public boolean canEditBoolean(Context context, Collection collection, boolean useInheritance) throws SQLException {
try {
canEdit(context, collection, useInheritance);
return true;
} catch (AuthorizeException e) {
return false;
}
}
@Override
public void canEdit(Context context, Collection collection) throws SQLException, AuthorizeException {
canEdit(context, collection, true);
}
@Override
public void canEdit(Context context, Collection collection, boolean useInheritance)
throws SQLException, AuthorizeException {
List<Community> parents = communityService.getAllParents(context, collection);
for (Community parent : parents) {
if (authorizeService.authorizeActionBoolean(context, parent,
Constants.WRITE, useInheritance)) {
return;
}
if (authorizeService.authorizeActionBoolean(context, parent,
Constants.ADD, useInheritance)) {
return;
}
}
authorizeService.authorizeAction(context, collection, Constants.WRITE, useInheritance);
}
@Override
public void delete(Context context, Collection collection) throws SQLException, AuthorizeException, IOException {
log.info(LogHelper.getHeader(context, "delete_collection",
"collection_id=" + collection.getID()));
// remove harvested collections.
HarvestedCollection hc = harvestedCollectionService.find(context, collection);
if (hc != null) {
harvestedCollectionService.delete(context, hc);
}
context.addEvent(new Event(Event.DELETE, Constants.COLLECTION,
collection.getID(), collection.getHandle(), getIdentifiers(context, collection)));
// remove subscriptions - hmm, should this be in Subscription.java?
subscribeService.deleteByDspaceObject(context, collection);
// Remove Template Item
removeTemplateItem(context, collection);
// Remove items
// Remove items
Iterator<Item> items = itemService.findAllByCollection(context, collection);
while (items.hasNext()) {
Item item = items.next();
// items.remove();
if (itemService.isOwningCollection(item, collection)) {
// the collection to be deleted is the owning collection, thus remove
// the item from all collections it belongs to
itemService.delete(context, item);
} else {
// the item was only mapped to this collection, so just remove it
removeItem(context, collection, item);
}
}
// Delete bitstream logo
setLogo(context, collection, null);
Iterator<WorkspaceItem> workspaceItems = workspaceItemService.findByCollection(context, collection).iterator();
while (workspaceItems.hasNext()) {
WorkspaceItem workspaceItem = workspaceItems.next();
workspaceItems.remove();
workspaceItemService.deleteAll(context, workspaceItem);
}
WorkflowServiceFactory.getInstance().getWorkflowService().deleteCollection(context, collection);
WorkflowServiceFactory.getInstance().getWorkflowItemService().deleteByCollection(context, collection);
// get rid of the content count cache if it exists
// Remove any Handle
handleService.unbindHandle(context, collection);
// Remove any workflow roles
collectionRoleService.deleteByCollection(context, collection);
collection.getResourcePolicies().clear();
// Remove default administrators group
Group g = collection.getAdministrators();
if (g != null) {
collection.setAdmins(null);
groupService.delete(context, g);
}
// Remove default submitters group
g = collection.getSubmitters();
if (g != null) {
collection.setSubmitters(null);
groupService.delete(context, g);
}
Iterator<Community> owningCommunities = collection.getCommunities().iterator();
while (owningCommunities.hasNext()) {
Community owningCommunity = owningCommunities.next();
collection.removeCommunity(owningCommunity);
owningCommunity.removeCollection(collection);
}
collectionDAO.delete(context, collection);
}
@Override
public int getSupportsTypeConstant() {
return Constants.COLLECTION;
}
@Override
public List<Collection> findAuthorized(Context context, Community community, int actionID) throws SQLException {
List<Collection> myResults = new ArrayList<>();
List<Collection> myCollections;
if (community != null) {
myCollections = community.getCollections();
} else {
myCollections = findAll(context);
}
// now build a list of collections you have authorization for
for (Collection myCollection : myCollections) {
if (authorizeService.authorizeActionBoolean(context,
myCollection, actionID)) {
myResults.add(myCollection);
}
}
return myResults;
}
@Override
public Collection findByGroup(Context context, Group group) throws SQLException {
return collectionDAO.findByGroup(context, group);
}
@Override
public List<Collection> findCollectionsWithSubscribers(Context context) throws SQLException {
return collectionDAO.findCollectionsWithSubscribers(context);
}
@Override
public DSpaceObject getAdminObject(Context context, Collection collection, int action) throws SQLException {
DSpaceObject adminObject = null;
Community community = null;
List<Community> communities = collection.getCommunities();
if (CollectionUtils.isNotEmpty(communities)) {
community = communities.get(0);
}
switch (action) {
case Constants.REMOVE:
if (AuthorizeConfiguration.canCollectionAdminPerformItemDeletion()) {
adminObject = collection;
} else if (AuthorizeConfiguration.canCommunityAdminPerformItemDeletion()) {
adminObject = community;
}
break;
case Constants.DELETE:
if (AuthorizeConfiguration.canCommunityAdminPerformSubelementDeletion()) {
adminObject = community;
}
break;
default:
adminObject = collection;
break;
}
return adminObject;
}
@Override
public DSpaceObject getParentObject(Context context, Collection collection) throws SQLException {
List<Community> communities = collection.getCommunities();
if (CollectionUtils.isNotEmpty(communities)) {
return communities.get(0);
} else {
return null;
}
}
@Override
public void updateLastModified(Context context, Collection collection) throws SQLException, AuthorizeException {
//Also fire a modified event since the collection HAS been modified
context.addEvent(new Event(Event.MODIFY, Constants.COLLECTION,
collection.getID(), null, getIdentifiers(context, collection)));
}
@Override
public Collection findByIdOrLegacyId(Context context, String id) throws SQLException {
if (StringUtils.isNumeric(id)) {
return findByLegacyId(context, Integer.parseInt(id));
} else {
return find(context, UUID.fromString(id));
}
}
@Override
public Collection findByLegacyId(Context context, int id) throws SQLException {
return collectionDAO.findByLegacyId(context, id, Collection.class);
}
@Override
public int countTotal(Context context) throws SQLException {
return collectionDAO.countRows(context);
}
@Override
public List<Map.Entry<Collection, Long>> getCollectionsWithBitstreamSizesTotal(Context context)
throws SQLException {
return collectionDAO.getCollectionsWithBitstreamSizesTotal(context);
}
@Override
public Group createDefaultReadGroup(Context context, Collection collection, String typeOfGroupString,
int defaultRead)
throws SQLException, AuthorizeException {
Group role = groupService.create(context);
groupService.setName(role, getDefaultReadGroupName(collection, typeOfGroupString));
// Remove existing privileges from the anonymous group.
authorizeService.removePoliciesActionFilter(context, collection, defaultRead);
// Grant our new role the default privileges.
authorizeService.addPolicy(context, collection, defaultRead, role);
groupService.update(context, role);
return role;
}
@Override
public String getDefaultReadGroupName(Collection collection, String typeOfGroupString) {
return "COLLECTION_" + collection.getID().toString() + "_" + typeOfGroupString +
"_DEFAULT_READ";
}
@Override
public List<Collection> findCollectionsWithSubmit(String q, Context context, Community community,
int offset, int limit) throws SQLException, SearchServiceException {
List<Collection> collections = new ArrayList<>();
DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setDSpaceObjectFilter(IndexableCollection.TYPE);
discoverQuery.setStart(offset);
discoverQuery.setMaxResults(limit);
discoverQuery.setSortField(SOLR_SORT_FIELD, SORT_ORDER.asc);
DiscoverResult resp = retrieveCollectionsWithSubmit(context, discoverQuery, null, community, q);
for (IndexableObject solrCollections : resp.getIndexableObjects()) {
Collection c = ((IndexableCollection) solrCollections).getIndexedObject();
collections.add(c);
}
return collections;
}
@Override
public int countCollectionsWithSubmit(String q, Context context, Community community)
throws SQLException, SearchServiceException {
DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setMaxResults(0);
discoverQuery.setDSpaceObjectFilter(IndexableCollection.TYPE);
DiscoverResult resp = retrieveCollectionsWithSubmit(context, discoverQuery, null, community, q);
return (int)resp.getTotalSearchResults();
}
/**
* Finds all Indexed Collections where the current user has submit rights. If the user is an Admin,
* this is all Indexed Collections. Otherwise, it includes those collections where
* an indexed "submit" policy lists either the eperson or one of the eperson's groups
*
* @param context DSpace context
* @param discoverQuery
* @param entityType limit the returned collection to those related to given entity type
* @param community parent community, could be null
* @param q limit the returned collection to those with metadata values matching the query
* terms. The terms are used to make also a prefix query on SOLR
* so it can be used to implement an autosuggest feature over the collection name
* @return discovery search result objects
* @throws SQLException if something goes wrong
* @throws SearchServiceException if search error
*/
private DiscoverResult retrieveCollectionsWithSubmit(Context context, DiscoverQuery discoverQuery,
String entityType, Community community, String q)
throws SQLException, SearchServiceException {
StringBuilder query = new StringBuilder();