Skip to content

Commit

Permalink
Renamed ProvenanceProvider to ProvenanceService and refactored it to …
Browse files Browse the repository at this point in the history
…be used as Spring Service.
  • Loading branch information
milanmajchrak committed Dec 13, 2024
1 parent 51302fa commit 46ab18c
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.ProvenanceProvider;
import org.dspace.core.ProvenanceServiceImpl;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.SearchService;
import org.dspace.discovery.SearchServiceException;
Expand All @@ -70,6 +70,7 @@
import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.submit.model.AccessConditionOption;
import org.dspace.utils.DSpace;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Implementation of {@link DSpaceRunnable} to perform a bulk access control via json file.
Expand Down Expand Up @@ -113,7 +114,8 @@ public class BulkAccessControl extends DSpaceRunnable<BulkAccessControlScriptCon

protected String eperson = null;

protected ProvenanceProvider provenanceProvider = new ProvenanceProvider();
@Autowired
protected ProvenanceServiceImpl provenanceService;

@Override
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -469,7 +471,7 @@ private void setItemPolicies(Item item, BulkAccessControlInput accessControl)
itemAccessConditions.get(accessCondition.getName())));

itemService.adjustItemPolicies(context, item, item.getOwningCollection(), false);
provenanceProvider.setItemPolicies(context, item, accessControl);
provenanceService.setItemPolicies(context, item, accessControl);
}

/**
Expand Down Expand Up @@ -560,7 +562,7 @@ private void removeReadPolicies(DSpaceObject dso, String type) {
// Get all read policies of the dso before removing them
List<ResourcePolicy> resPolicies = resourcePolicyService.find(context, dso, type);
resourcePolicyService.removePolicies(context, dso, type, Constants.READ);
provenanceProvider.removeReadPolicies(context, dso, resPolicies);
provenanceService.removeReadPolicies(context, dso, resPolicies);
} catch (SQLException | AuthorizeException e) {
throw new BulkAccessControlException(e);
}
Expand Down Expand Up @@ -588,7 +590,7 @@ private void setBitstreamPolicies(Bitstream bitstream, Item item, BulkAccessCont

itemService.adjustBitstreamPolicies(context, item, item.getOwningCollection(), bitstream);
mediaFilterService.updatePoliciesOfDerivativeBitstreams(context, item, bitstream);
provenanceProvider.setBitstreamPolicies(context, bitstream, item, accessControl);
provenanceService.setBitstreamPolicies(context, bitstream, item, accessControl);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public class ProvenanceMessageProvider {
public class ProvenanceMessageFormatter {
private InstallItemService installItemService = ContentServiceFactory.getInstance().getInstallItemService();

public ProvenanceMessageProvider() {}
public ProvenanceMessageFormatter() {}

public String getMessage(Context context, String messageTemplate, Item item, Object... args)
throws SQLException, AuthorizeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
package org.dspace.core;

/**
* The ProvenanceMessageTemplates enum provides message templates for provenance messages.
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public enum ProvenanceMessageTemplates {
ACCESS_CONDITION("Access condition (%s) was added to %s (%s)"),
RESOURCE_POLICIES_REMOVED("Resource policies (%s) of %s (%s) were removed"),
Expand Down
178 changes: 178 additions & 0 deletions dspace-api/src/main/java/org/dspace/core/ProvenanceService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* 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.core;

import java.util.List;

import org.dspace.app.bulkaccesscontrol.model.BulkAccessControlInput;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataValue;

/**
* The ProvenanceService is responsible for creating provenance metadata for items based on the actions performed.
*
* @author Milan Majchrak (dspace at dataquest.sk)
*/
public interface ProvenanceService {
/**
* Add a provenance message to the item when a new access condition is added
*
* @param context DSpace context object
* @param item item to which the access condition is added
* @param accessControl the access control input
*/
void setItemPolicies(Context context, Item item, BulkAccessControlInput accessControl);

/**
* Add a provenance message to the item when a read policy is removed
*
* @param context DSpace context object
* @param dso DSpace object from which the read policy is removed
* @param resPolicies list of resource policies that are removed
*/
void removeReadPolicies(Context context, DSpaceObject dso, List<ResourcePolicy> resPolicies);

/**
* Add a provenance message to the item when a bitstream policy is set
*
* @param context DSpace context object
* @param bitstream bitstream to which the policy is set
* @param item item to which the bitstream belongs
* @param accessControl the access control input
*/
void setBitstreamPolicies(Context context, Bitstream bitstream, Item item,
BulkAccessControlInput accessControl);

/**
* Add a provenance message to the item when an item's license is edited
*
* @param context DSpace context object
* @param item item to which the license is edited
* @param newLicense true if the license is new, false if it's edited
*/
void editLicense(Context context, Item item, boolean newLicense);

/**
* Add a provenance message to the item when it's moved to a collection
*
* @param context DSpace context object
* @param item item that is moved
* @param collection collection to which the item is moved
*/
void moveItem(Context context, Item item, Collection collection);

/**
* Add a provenance message to the item when it's mapped to a collection
*
* @param context DSpace context object
* @param item item that is mapped
* @param collection collection to which the item is mapped
*/
void mappedItem(Context context, Item item, Collection collection);

/**
* Add a provenance message to the item when it's deleted from a mapped collection
*
* @param context DSpace context object
* @param item item that is deleted from a mapped collection
* @param collection collection from which the item is deleted
*/
void deletedItemFromMapped(Context context, Item item, Collection collection);

/**
* Add a provenance message to the item when it's bitstream is deleted
*
* @param context DSpace context object
* @param bitstream deleted bitstream
* @param item item from which the bitstream is deleted
*/
void deleteBitstream(Context context, Bitstream bitstream, Item item);

/**
* Add a provenance message to the item when metadata is added
*
* @param context DSpace context object
* @param dso DSpace object to which the metadata is added
* @param metadataField metadata field that is added
*/
void addMetadata(Context context, DSpaceObject dso, MetadataField metadataField);

/**
* Add a provenance message to the item when metadata is removed
*
* @param context DSpace context object
* @param dso DSpace object from which the metadata is removed
* @param metadataField metadata field that is removed
*/
void removeMetadata(Context context, DSpaceObject dso, MetadataField metadataField);

/**
* Add a provenance message to the item when metadata is removed at a given index
*
* @param context DSpace context object
* @param dso DSpace object from which the metadata is removed
* @param metadataValues list of metadata values
* @param indexInt index at which the metadata is removed
*/
void removeMetadataAtIndex(Context context, DSpaceObject dso, List<MetadataValue> metadataValues,
int indexInt);

/**
* Add a provenance message to the item when metadata is replaced
*
* @param context DSpace context object
* @param dso DSpace object to which the metadata is replaced
* @param metadataField metadata field that is replaced
* @param oldMtdVal old metadata value
*/
void replaceMetadata(Context context, DSpaceObject dso, MetadataField metadataField, String oldMtdVal);

/**
* Add a provenance message to the item when metadata is replaced
*
* @param context DSpace context object
* @param dso DSpace object to which the metadata is replaced
* @param metadataField metadata field that is replaced
* @param oldMtdVal old metadata value
*/
void replaceMetadataSingle(Context context, DSpaceObject dso, MetadataField metadataField,
String oldMtdVal);

/**
* Add a provenance message to the item when metadata is updated
*
* @param context DSpace context object
* @param item item to which the metadata is updated
* @param discoverable true if the item is discoverable, false if it's not
*/
void makeDiscoverable(Context context, Item item, boolean discoverable);

/**
* Add a provenance message to the item when a bitstream is uploaded
*
* @param context DSpace context object
* @param bundle bundle to which the bitstream is uploaded
*/
void uploadBitstream(Context context, Bundle bundle);

/**
* Fetch an Item object using a service and return the first Item object from the list.
* Log an error if the list is empty or if there is an SQL error
*
* @param context DSpace context object
* @param bitstream bitstream to which the item is fetched
*/
Item findItemByBitstream(Context context, Bitstream bitstream);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.dspace.app.bulkaccesscontrol.model.BulkAccessControlInput;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.ResourcePolicyService;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
Expand All @@ -31,36 +29,41 @@
import org.dspace.content.MetadataSchemaEnum;
import org.dspace.content.MetadataValue;
import org.dspace.content.clarin.ClarinLicenseResourceMapping;
import org.dspace.content.factory.ClarinServiceFactory;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.clarin.ClarinItemService;
import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService;
import org.springframework.beans.factory.annotation.Autowired;

/**
* ProvenanceProviderrest is responsible for creating provenance metadata for items based on the actions performed.
* ProvenanceServiceImpl is an implementation of ProvenanceService.
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public class ProvenanceProvider {
private static final Logger log = LogManager.getLogger(ProvenanceProvider.class);
public class ProvenanceServiceImpl implements ProvenanceService {
private static final Logger log = LogManager.getLogger(ProvenanceServiceImpl.class);

private ItemService itemService = ContentServiceFactory.getInstance().getItemService();
private ClarinItemService clarinItemService = ClarinServiceFactory.getInstance().getClarinItemService();
private ClarinLicenseResourceMappingService clarinResourceMappingService =
ClarinServiceFactory.getInstance().getClarinLicenseResourceMappingService();
private BitstreamService bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
@Autowired
private ItemService itemService;
@Autowired
private ClarinItemService clarinItemService;
@Autowired
private ClarinLicenseResourceMappingService clarinResourceMappingService;
@Autowired
private BitstreamService bitstreamService;

private final ProvenanceMessageProvider messageProvider = new ProvenanceMessageProvider();
private final ProvenanceMessageFormatter messageProvider = new ProvenanceMessageFormatter();

public void setItemPolicies(Context context, Item item, BulkAccessControlInput accessControl)
throws SQLException, AuthorizeException {
public void setItemPolicies(Context context, Item item, BulkAccessControlInput accessControl) {
String resPoliciesStr = extractAccessConditions(accessControl.getItem().getAccessConditions());
if (StringUtils.isNotBlank(resPoliciesStr)) {
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.ACCESS_CONDITION.getTemplate(),
resPoliciesStr, "item", item.getID());
addProvenanceMetadata(context, item, msg);
try {
addProvenanceMetadata(context, item, msg);
} catch (SQLException | AuthorizeException e) {
log.error("Unable to add new provenance metadata when setting item policies.", e);
}
}
}

Expand Down Expand Up @@ -114,8 +117,8 @@ public void editLicense(Context context, Item item, boolean newLicense) {
oldLicense = findLicenseInBundles(item, Constants.CONTENT_BUNDLE_NAME, oldLicense, context);
}

String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.EDIT_LICENSE.getTemplate(), item,
Objects.isNull(oldLicense) ? "empty" : oldLicense,
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.EDIT_LICENSE.getTemplate(),
item, Objects.isNull(oldLicense) ? "empty" : oldLicense,
!newLicense ? "removed" : Objects.isNull(oldLicense) ? "added" : "updated");
addProvenanceMetadata(context, item, msg);
} catch (SQLException | AuthorizeException e) {
Expand Down Expand Up @@ -160,12 +163,12 @@ public void deletedItemFromMapped(Context context, Item item, Collection collect
}
}

public void deleteBitstream(Context context,Bitstream bitstream) {
public void deleteBitstream(Context context, Bitstream bitstream, Item item) {
try {
Item item = findItemByBitstream(context, bitstream);
if (Objects.nonNull(item)) {
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.EDIT_BITSTREAM.getTemplate(),
item, item.getID(), messageProvider.getMessage(bitstream));
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.EDIT_BITSTREAM.getTemplate(), item, item.getID(),
messageProvider.getMessage(bitstream));
addProvenanceMetadata(context, item, msg);
}
} catch (SQLException | AuthorizeException e) {
Expand Down Expand Up @@ -315,8 +318,14 @@ private String extractAccessConditions(List<AccessCondition> accessConditions) {
.collect(Collectors.joining(";"));
}

private Item findItemByBitstream(Context context, Bitstream bitstream) throws SQLException {
List<Item> items = clarinItemService.findByBitstreamUUID(context, bitstream.getID());
public Item findItemByBitstream(Context context, Bitstream bitstream) {
List<Item> items = null;
try {
items = clarinItemService.findByBitstreamUUID(context, bitstream.getID());
} catch (SQLException e) {
log.error("Unable to find item by bitstream (" + bitstream.getID() + " ).", e);
return null;
}
if (items.isEmpty()) {
log.warn("Bitstream (" + bitstream.getID() + ") is not assigned to any item.");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
import org.dspace.app.rest.repository.BundleRestRepository;
import org.dspace.app.rest.utils.ContextUtil;
import org.dspace.app.rest.utils.Utils;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bundle;
import org.dspace.content.service.BundleService;
import org.dspace.core.Context;
import org.dspace.core.ProvenanceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.ControllerUtils;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
Expand Down Expand Up @@ -79,8 +77,6 @@ public class BundleUploadBitstreamController {
@Autowired
private ConverterService converter;

private ProvenanceProvider provenanceProvider = new ProvenanceProvider();

/**
* Method to upload a Bitstream to a Bundle with the given UUID in the URL. This will create a Bitstream with the
* file provided in the request and attach this to the Item that matches the UUID in the URL.
Expand Down Expand Up @@ -117,7 +113,6 @@ public ResponseEntity<RepresentationModel<?>> uploadBitstream(
BitstreamRest bitstreamRest = bundleRestRepository.uploadBitstream(
context, bundle, uploadfile.getOriginalFilename(), fileInputStream, properties);
BitstreamResource bitstreamResource = converter.toResource(bitstreamRest);
provenanceProvider.uploadBitstream(context, bundle);

return ControllerUtils.toResponseEntity(HttpStatus.CREATED, new HttpHeaders(), bitstreamResource);
}
Expand Down
Loading

0 comments on commit 46ab18c

Please sign in to comment.