|
| 1 | +/** |
| 2 | + * The contents of this file are subject to the license and copyright |
| 3 | + * detailed in the LICENSE and NOTICE files at the root of the source |
| 4 | + * tree and available online at |
| 5 | + * |
| 6 | + * http://www.dspace.org/license/ |
| 7 | + */ |
| 8 | +package org.dspace.deletion.process; |
| 9 | + |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +import org.apache.commons.cli.ParseException; |
| 17 | +import org.dspace.authorize.AuthorizeException; |
| 18 | +import org.dspace.authorize.factory.AuthorizeServiceFactory; |
| 19 | +import org.dspace.authorize.service.AuthorizeService; |
| 20 | +import org.dspace.content.Collection; |
| 21 | +import org.dspace.content.Community; |
| 22 | +import org.dspace.content.DSpaceObject; |
| 23 | +import org.dspace.content.Item; |
| 24 | +import org.dspace.content.factory.ContentServiceFactory; |
| 25 | +import org.dspace.content.service.CollectionService; |
| 26 | +import org.dspace.content.service.CommunityService; |
| 27 | +import org.dspace.content.service.ItemService; |
| 28 | +import org.dspace.core.Constants; |
| 29 | +import org.dspace.core.Context; |
| 30 | +import org.dspace.deletion.process.strategies.DSpaceObjectDeletionStrategy; |
| 31 | +import org.dspace.eperson.EPerson; |
| 32 | +import org.dspace.eperson.factory.EPersonServiceFactory; |
| 33 | +import org.dspace.handle.factory.HandleServiceFactory; |
| 34 | +import org.dspace.handle.service.HandleService; |
| 35 | +import org.dspace.kernel.ServiceManager; |
| 36 | +import org.dspace.scripts.DSpaceRunnable; |
| 37 | +import org.dspace.utils.DSpace; |
| 38 | + |
| 39 | +/** |
| 40 | + * Batch process for deleting DSpace objects (Item, Collection, Community). |
| 41 | + * This class orchestrates the deletion process, delegating the actual deletion logic |
| 42 | + * to a strategy registry that selects the appropriate strategy for each DSpaceObject type. |
| 43 | + * |
| 44 | + * @author Mykhaylo Boychuk ([email protected]) |
| 45 | + */ |
| 46 | +public class DSpaceObjectDeletionProcess |
| 47 | + extends DSpaceRunnable<DSpaceObjectDeletionProcessScriptConfiguration<DSpaceObjectDeletionProcess>> { |
| 48 | + |
| 49 | + public static final String OBJECT_DELETION_SCRIPT = "object-deletion"; |
| 50 | + |
| 51 | + private ItemService itemService; |
| 52 | + private HandleService handleService; |
| 53 | + private CommunityService communityService; |
| 54 | + private AuthorizeService authorizeService; |
| 55 | + private CollectionService collectionService; |
| 56 | + |
| 57 | + private String id; |
| 58 | + private Context context; |
| 59 | + private String[] copyVirtualMetadata; |
| 60 | + private List<DSpaceObjectDeletionStrategy> deletionStrategies = new ArrayList<>(); |
| 61 | + |
| 62 | + @Override |
| 63 | + public void setup() throws ParseException { |
| 64 | + ServiceManager serviceManager = new DSpace().getServiceManager(); |
| 65 | + itemService = ContentServiceFactory.getInstance().getItemService(); |
| 66 | + handleService = HandleServiceFactory.getInstance().getHandleService(); |
| 67 | + communityService = ContentServiceFactory.getInstance().getCommunityService(); |
| 68 | + collectionService = ContentServiceFactory.getInstance().getCollectionService(); |
| 69 | + authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService(); |
| 70 | + deletionStrategies.addAll(serviceManager.getServicesByType(DSpaceObjectDeletionStrategy.class)); |
| 71 | + |
| 72 | + parseCommandLineOptions(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parses and validates command line options. |
| 77 | + */ |
| 78 | + private void parseCommandLineOptions() { |
| 79 | + this.id = commandLine.getOptionValue('i'); |
| 80 | + this.copyVirtualMetadata = commandLine.hasOption('c') ? parseCopyVirtualMetadataOption() : new String[0]; |
| 81 | + } |
| 82 | + |
| 83 | + private String[] parseCopyVirtualMetadataOption() { |
| 84 | + String value = commandLine.getOptionValue('c'); |
| 85 | + if (value.contains(",")) { |
| 86 | + return value.split(","); |
| 87 | + } |
| 88 | + return new String[] { value }; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void internalRun() throws Exception { |
| 93 | + assignCurrentUserInContext(); |
| 94 | + Optional<DSpaceObject> dSpaceObjectOptional = resolveDSpaceObject(this.id); |
| 95 | + |
| 96 | + if (dSpaceObjectOptional.isEmpty()) { |
| 97 | + var error = String.format("DSpaceObject for provided identifier:%s doesn't exist!", this.id); |
| 98 | + throw new IllegalArgumentException(error); |
| 99 | + } |
| 100 | + |
| 101 | + DSpaceObject dso = dSpaceObjectOptional.get(); |
| 102 | + |
| 103 | + if (!authorizeService.isAdmin(context, dso)) { |
| 104 | + throw new AuthorizeException("Current user is not eligible to execute script: " + OBJECT_DELETION_SCRIPT); |
| 105 | + } |
| 106 | + |
| 107 | + var info = "Performing deletion of DSpaceObject (and all child objects) for type=%s and uuid=%s"; |
| 108 | + handler.logInfo(String.format(info, Constants.typeText[dso.getType()], dso.getID().toString())); |
| 109 | + getStrategy(dso).delete(this.context, dso, this.copyVirtualMetadata); |
| 110 | + handler.logInfo("Deletion completed!"); |
| 111 | + } |
| 112 | + |
| 113 | + private DSpaceObjectDeletionStrategy getStrategy(DSpaceObject dso) { |
| 114 | + var error = "No strategy for type:" + dso.getType(); |
| 115 | + return deletionStrategies.stream() |
| 116 | + .filter(s -> s.supports(dso)) |
| 117 | + .findFirst() |
| 118 | + .orElseThrow(() -> new IllegalArgumentException(error)); |
| 119 | + } |
| 120 | + |
| 121 | + private void assignCurrentUserInContext() throws SQLException { |
| 122 | + this.context = new Context(); |
| 123 | + UUID uuid = getEpersonIdentifier(); |
| 124 | + if (uuid != null) { |
| 125 | + EPerson ePerson = EPersonServiceFactory.getInstance().getEPersonService().find(context, uuid); |
| 126 | + context.setCurrentUser(ePerson); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Resolves the identifier (Item, Collection, or Community). |
| 132 | + * |
| 133 | + * @param identifier The UUID or handle of the DSpace object. |
| 134 | + * @return An Optional containing the DSpaceObject if found. |
| 135 | + * @throws SQLException If database error occurs. |
| 136 | + */ |
| 137 | + private Optional<DSpaceObject> resolveDSpaceObject(String identifier) throws SQLException { |
| 138 | + UUID uuid = null; |
| 139 | + try { |
| 140 | + uuid = UUID.fromString(identifier); |
| 141 | + } catch (Exception e) { |
| 142 | + // It's not a UUID, proceed to treat it as a handle. |
| 143 | + } |
| 144 | + |
| 145 | + if (uuid != null) { |
| 146 | + Item item = itemService.find(context, uuid); |
| 147 | + if (item != null) { |
| 148 | + return Optional.of(item); |
| 149 | + } |
| 150 | + Community community = communityService.find(context, uuid); |
| 151 | + if (community != null) { |
| 152 | + return Optional.of(community); |
| 153 | + } |
| 154 | + Collection collection = collectionService.find(context, uuid); |
| 155 | + if (collection != null) { |
| 156 | + return Optional.of(collection); |
| 157 | + } |
| 158 | + } |
| 159 | + DSpaceObject dso = handleService.resolveToObject(context, identifier); |
| 160 | + return dso != null ? Optional.of(dso) : Optional.empty(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public DSpaceObjectDeletionProcessScriptConfiguration<DSpaceObjectDeletionProcess> getScriptConfiguration() { |
| 165 | + ServiceManager sm = new DSpace().getServiceManager(); |
| 166 | + return sm.getServiceByName(OBJECT_DELETION_SCRIPT, DSpaceObjectDeletionProcessScriptConfiguration.class); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments