Skip to content

Commit

Permalink
create class for provenance management
Browse files Browse the repository at this point in the history
  • Loading branch information
Paurikova2 committed Sep 27, 2024
1 parent aae82cc commit 0b3c0d7
Show file tree
Hide file tree
Showing 18 changed files with 647 additions and 415 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,6 @@ private void updateItemPolicies(Item item, BulkAccessControlInput accessControl)
*/
private void setItemPolicies(Item item, BulkAccessControlInput accessControl)
throws SQLException, AuthorizeException {

//String resPoliciesStr = extractAccessConditions(accessControl.getItem().getAccessConditions());

accessControl
.getItem()
.getAccessConditions()
Expand All @@ -477,10 +474,6 @@ private void setItemPolicies(Item item, BulkAccessControlInput accessControl)

itemService.adjustItemPolicies(context, item, item.getOwningCollection(), false);

// if (resPoliciesStr.isEmpty()) {
// String msg = "Access condition (" + resPoliciesStr + ") was added to item";
// addProvenanceMetadata(context, item, msg);
// }
provenanceService.setItemPolicies(context, item, accessControl);
}

Expand Down Expand Up @@ -600,7 +593,7 @@ private void setBitstreamPolicies(Bitstream bitstream, Item item, BulkAccessCont

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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 org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Bitstream;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;

import java.sql.SQLException;
import java.util.List;

public interface ProvenanceMessageProvider {
public String getMessage(Context context, String templateKey, Object... args) throws SQLException, AuthorizeException;
public String getMessage(Context context, String templateKey, Item item, Object... args) throws SQLException, AuthorizeException;
public String getMessage(String templateKey, Object... args);
public String addCollectionsToMessage(Item item) throws SQLException, AuthorizeException;
public String getBitstreamMessage(Bitstream bitstream);
public String getResourcePoliciesMessage(List<ResourcePolicy> resPolicies);
public String getMetadata(String oldMtdKey, String oldMtdValue);
public String getMetadataField(MetadataField metadataField);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.DCDate;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.InstallItemService;
import org.dspace.eperson.EPerson;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ProvenanceMessageProviderImpl implements ProvenanceMessageProvider {
private Map<String, String> messageTemplates;

@Autowired
private InstallItemService installItemService = ContentServiceFactory.getInstance().getInstallItemService();

@Autowired
public ProvenanceMessageProviderImpl() {
loadMessageTemplates();
}

private void loadMessageTemplates() {
ObjectMapper mapper = new ObjectMapper();
try {
messageTemplates = mapper.readValue(Files.readAllBytes(Paths.get("C:\\workspace\\DSpace\\dspace-api\\src\\main\\java\\org\\dspace\\core\\provenance_messages.json")), Map.class);
} catch (IOException e) {
throw new RuntimeException("Failed to load message templates", e);
}
}

@Override
public String getMessage(Context context, String templateKey, Item item, Object... args) throws SQLException, AuthorizeException {
String msg = getMessage(context, templateKey, args);
msg = msg + "\n" + installItemService.getBitstreamProvenanceMessage(context, item);
return msg;
}

@Override
public String getMessage(String templateKey, Object... args) {
String template = messageTemplates.get(templateKey);
if (template == null) {
throw new IllegalArgumentException("No message template found for key: " + templateKey);
}
return String.format(template, args);
}

@Override
public String getMessage(Context context, String templateKey, Object... args) {
EPerson currentUser = context.getCurrentUser();
String timestamp = DCDate.getCurrent().toString();
String details = getMessage(templateKey, args);
return String.format("%s by %s (%s) on %s",
details,
currentUser.getFullName(),
currentUser.getEmail(),
timestamp);
}

@Override
public String addCollectionsToMessage(Item item) throws SQLException, AuthorizeException {
String msg = "Item was in collections:\n";
List<Collection> collsList = item.getCollections();
for (Collection coll : collsList) {
msg = msg + coll.getName() + " (ID: " + coll.getID() + ")\n";
}
return msg;
}

@Override
public String getBitstreamMessage(Bitstream bitstream) {
// values of deleted bitstream
String msg = bitstream.getName() + ": " +
bitstream.getSizeBytes() + " bytes, checksum: " +
bitstream.getChecksum() + " (" +
bitstream.getChecksumAlgorithm() + ")\n";
return msg;
}

@Override
public String getResourcePoliciesMessage(List<ResourcePolicy> resPolicies) {
return resPolicies.stream()
.filter(rp -> rp.getAction() == Constants.READ)
.map(rp -> String.format("[%s, %s, %d, %s, %s, %s, %s]",
rp.getRpName(), rp.getRpType(), rp.getAction(),
rp.getEPerson() != null ? rp.getEPerson().getEmail() : null,
rp.getGroup() != null ? rp.getGroup().getName() : null,
rp.getStartDate() != null ? rp.getStartDate().toString() : null,
rp.getEndDate() != null ? rp.getEndDate().toString() : null))
.collect(Collectors.joining(";"));
}

@Override
public String getMetadata(String oldMtdKey, String oldMtdValue) {
return oldMtdKey + ": " + oldMtdValue;
}

@Override
public String getMetadataField(MetadataField metadataField) {
return metadataField.toString()
.replace('_', '.');
}
}
28 changes: 27 additions & 1 deletion dspace-api/src/main/java/org/dspace/core/ProvenanceService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
/**
* 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 org.dspace.app.bulkaccesscontrol.model.BulkAccessControlInput;
import org.dspace.authorize.AuthorizeException;
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;

import java.sql.SQLException;
import java.util.List;

public interface ProvenanceService {
public void setBitstreaPolicies(Context context, Bitstream bitstream, Item item, BulkAccessControlInput accessControl) throws SQLException, AuthorizeException;
public void setBitstreamPolicies(Context context, Bitstream bitstream, Item item, BulkAccessControlInput accessControl) throws SQLException, AuthorizeException;

public void setItemPolicies(Context context, Item item, BulkAccessControlInput accessControl) throws SQLException, AuthorizeException;

public String removedReadPolicies(Context context, DSpaceObject dso, String type) throws SQLException, AuthorizeException;
public void uploadBitstream(Context context, Bundle bundle) throws SQLException, AuthorizeException;
public void editLicense(Context context, Item item, boolean newLicense) throws SQLException, AuthorizeException;

public void moveItem(Context context, Item item, Collection collection) throws SQLException, AuthorizeException;
public void mappedItem(Context context, Item item, Collection collection) throws SQLException, AuthorizeException;
public void deletedItemFromMapped(Context context, Item item, Collection collection) throws SQLException, AuthorizeException;
public void deleteBitstream(Context context,Bitstream bitstream) throws SQLException, AuthorizeException;
public void addMetadata(Context context, DSpaceObject dso, MetadataField metadataField) throws SQLException, AuthorizeException;
public void removeMetadata(Context context, DSpaceObject dso, MetadataField metadataField) throws SQLException, AuthorizeException;
public void removeMetadataAtIndex(Context context, DSpaceObject dso, List<MetadataValue> metadataValues,
int indexInt) throws SQLException, AuthorizeException;
public void replaceMetadata(Context context, DSpaceObject dso, MetadataField metadataField, String oldMtdVal) throws SQLException, AuthorizeException;
public void replaceMetadataSingle(Context context, DSpaceObject dso, MetadataField metadataField, String oldMtdVal) throws SQLException, AuthorizeException;
public void makeDiscoverable(Context context, Item item, boolean discoverable) throws SQLException, AuthorizeException;
}
Loading

0 comments on commit 0b3c0d7

Please sign in to comment.