Skip to content

Commit

Permalink
The message templates are loaded from the Enum instead of json file.
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Dec 12, 2024
1 parent 130d78f commit 419aff3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
*/
package org.dspace.core;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Bitstream;
Expand All @@ -36,51 +32,28 @@
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public class ProvenanceMessageProvider {
private static final String PROVENANCE_MSG_JSON = "provenance_messages.json";
private static final Logger log = LoggerFactory.getLogger(ProvenanceMessageProvider.class);
private Map<String, String> messageTemplates;
private InstallItemService installItemService = ContentServiceFactory.getInstance().getInstallItemService();

public ProvenanceMessageProvider() {
loadMessageTemplates();
}

private void loadMessageTemplates() {
ObjectMapper mapper = new ObjectMapper();
String msg;
try (InputStream inputStream = getClass().getResourceAsStream(PROVENANCE_MSG_JSON)) {
if (inputStream == null) {
msg = "Failed to find message templates file.";
log.error(msg);
throw new RuntimeException(msg);
}
messageTemplates = mapper.readValue(inputStream, Map.class);
} catch (IOException e) {
msg = "Failed to load message templates.";
log.error(msg);
throw new RuntimeException(msg, e);
}
}
public ProvenanceMessageProvider() {}

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

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);
public String validateMessageTemplate(String messageTemplate, Object... args) {
if (messageTemplate == null) {
throw new IllegalArgumentException("The message template is null!");
}
return String.format(template, args);
return String.format(messageTemplate, args);
}

public String getMessage(Context context, String templateKey, Object... args) {
public String getMessage(Context context, String messageTemplate, Object... args) {
EPerson currentUser = context.getCurrentUser();
String timestamp = DCDate.getCurrent().toString();
String details = getMessage(templateKey, args);
String details = validateMessageTemplate(messageTemplate, args);
return String.format("%s by %s (%s) on %s",
details,
currentUser.getFullName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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;

public enum ProvenanceMessageTemplates {
ACCESS_CONDITION("Access condition (%s) was added to %s (%s)"),
RESOURCE_POLICIES_REMOVED("Resource policies (%s) of %s (%s) were removed"),
BUNDLE_ADDED("Item was added bitstream to bundle (%s)"),
EDIT_LICENSE("License (%s) was %s"),
MOVE_ITEM("Item was moved from collection (%s) to different collection"),
MAPPED_ITEM("Item was mapped to collection (%s)"),
DELETED_ITEM_FROM_MAPPED("Item was deleted from mapped collection (%s)"),
EDIT_BITSTREAM("Item (%s) was deleted bitstream (%s)"),
ITEM_METADATA("Item metadata (%s) was %s"),
BITSTREAM_METADATA("Item metadata (%s) was %s bitstream (%s)"),
ITEM_REPLACE_SINGLE_METADATA("Item bitstream (%s) metadata (%s) was updated"),
DISCOVERABLE("Item was made %sdiscoverable");

private final String template;

ProvenanceMessageTemplates(String template) {
this.template = template;
}

public String getTemplate() {
return template;
}
}
53 changes: 31 additions & 22 deletions dspace-api/src/main/java/org/dspace/core/ProvenanceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public void setItemPolicies(Context context, Item item, BulkAccessControlInput a
throws SQLException, AuthorizeException {
String resPoliciesStr = extractAccessConditions(accessControl.getItem().getAccessConditions());
if (StringUtils.isNotBlank(resPoliciesStr)) {
String msg = messageProvider.getMessage(context,"accessCondition", resPoliciesStr,
"item", item.getID());
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.ACCESS_CONDITION.getTemplate(),
resPoliciesStr, "item", item.getID());
addProvenanceMetadata(context, item, msg);
}
}
Expand All @@ -115,14 +115,16 @@ public void removeReadPolicies(Context context, DSpaceObject dso, List<ResourceP
String resPoliciesStr = messageProvider.getResourcePoliciesMessage(resPolicies);
if (dso.getType() == Constants.ITEM) {
Item item = (Item) dso;
String msg = messageProvider.getMessage(context,"resourcePoliciesRemoved",
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.RESOURCE_POLICIES_REMOVED.getTemplate(),
resPoliciesStr.isEmpty() ? "empty" : resPoliciesStr, "item", item.getID());
addProvenanceMetadata(context, item, msg);
} else if (dso.getType() == Constants.BITSTREAM) {
Bitstream bitstream = (Bitstream) dso;
Item item = getItem(context, bitstream);
if (Objects.nonNull(item)) {
String msg = messageProvider.getMessage(context,"resourcePoliciesRemoved",
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.RESOURCE_POLICIES_REMOVED.getTemplate(),
resPoliciesStr.isEmpty() ? "empty" : resPoliciesStr, "bitstream", bitstream.getID());
addProvenanceMetadata(context, item, msg);
}
Expand All @@ -133,8 +135,8 @@ public void setBitstreamPolicies(Context context, Bitstream bitstream, Item item
BulkAccessControlInput accessControl) throws SQLException, AuthorizeException {
String accConditionsStr = extractAccessConditions(accessControl.getBitstream().getAccessConditions());
if (StringUtils.isNotBlank(accConditionsStr)) {
String msg = messageProvider.getMessage(context,"accessCondition", accConditionsStr,
"bitstream", bitstream.getID());
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.ACCESS_CONDITION.getTemplate(),
accConditionsStr, "bitstream", bitstream.getID());
addProvenanceMetadata(context, item, msg);
}
}
Expand All @@ -146,14 +148,15 @@ public void editLicense(Context context, Item item, boolean newLicense) throws S
oldLicense = findLicenseInBundles(item, Constants.CONTENT_BUNDLE_NAME, oldLicense, context);
}

String msg = messageProvider.getMessage(context, "editLicense", item,
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);
}

public void moveItem(Context context, Item item, Collection collection) throws SQLException, AuthorizeException {
String msg = messageProvider.getMessage(context, "moveItem", item, collection.getID());
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.MOVE_ITEM.getTemplate(),
item, collection.getID());
// Update item in DB
// Because a user can move an item without authorization turn off authorization
context.turnOffAuthorisationSystem();
Expand All @@ -162,29 +165,31 @@ public void moveItem(Context context, Item item, Collection collection) throws S
}

public void mappedItem(Context context, Item item, Collection collection) throws SQLException, AuthorizeException {
String msg = messageProvider.getMessage(context, "mappedItem", collection.getID());
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.MAPPED_ITEM.getTemplate(),
collection.getID());
addProvenanceMetadata(context, item, msg);
}

public void deletedItemFromMapped(Context context, Item item, Collection collection)
throws SQLException, AuthorizeException {
String msg = messageProvider.getMessage(context, "deletedItemFromMapped", collection.getID());
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.DELETED_ITEM_FROM_MAPPED.getTemplate(), collection.getID());
addProvenanceMetadata(context, item, msg);
}

public void deleteBitstream(Context context,Bitstream bitstream) throws SQLException, AuthorizeException {
Item item = getItem(context, bitstream);
if (Objects.nonNull(item)) {
String msg = messageProvider.getMessage(context, "editBitstream", item,
item.getID(), messageProvider.getBitstreamMessage(bitstream));
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.EDIT_BITSTREAM.getTemplate(),
item, item.getID(), messageProvider.getBitstreamMessage(bitstream));
addProvenanceMetadata(context, item, msg);
}
}

public void addMetadata(Context context, DSpaceObject dso, MetadataField metadataField)
throws SQLException, AuthorizeException {
if (Constants.ITEM == dso.getType()) {
String msg = messageProvider.getMessage(context, "itemMetadata",
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.ITEM_METADATA.getTemplate(),
messageProvider.getMetadataField(metadataField), "added");
addProvenanceMetadata(context, (Item) dso, msg);
}
Expand All @@ -193,7 +198,8 @@ public void addMetadata(Context context, DSpaceObject dso, MetadataField metadat
Bitstream bitstream = (Bitstream) dso;
Item item = getItem(context, bitstream);
if (Objects.nonNull(item)) {
String msg = messageProvider.getMessage(context, "bitstreamMetadata", item,
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.BITSTREAM_METADATA.getTemplate(), item,
messageProvider.getMetadataField(metadataField), "added by",
messageProvider.getBitstreamMessage(bitstream));
addProvenanceMetadata(context, item, msg);
Expand All @@ -218,7 +224,8 @@ public void removeMetadata(Context context, DSpaceObject dso, MetadataField meta
Bitstream bitstream = (Bitstream) dso;
Item item = getItem(context, bitstream);
if (Objects.nonNull(item)) {
String msg = messageProvider.getMessage(context, "bitstreamMetadata", item,
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.BITSTREAM_METADATA.getTemplate(), item,
messageProvider.getMetadata(messageProvider.getMetadataField(oldMtdKey), oldMtdValue),
"deleted from", messageProvider.getBitstreamMessage(bitstream));
addProvenanceMetadata(context, item, msg);
Expand All @@ -233,7 +240,7 @@ public void removeMetadataAtIndex(Context context, DSpaceObject dso, List<Metada
// Remember removed mtd
String oldMtdKey = messageProvider.getMetadataField(metadataValues.get(indexInt).getMetadataField());
String oldMtdValue = metadataValues.get(indexInt).getValue();
String msg = messageProvider.getMessage(context, "itemMetadata",
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.ITEM_METADATA.getTemplate(),
(Item) dso, messageProvider.getMetadata(oldMtdKey, oldMtdValue), "deleted");
addProvenanceMetadata(context, (Item) dso, msg);
}
Expand All @@ -243,9 +250,9 @@ public void replaceMetadata(Context context, DSpaceObject dso, MetadataField met
if (dso.getType() != Constants.ITEM) {
return;
}
String msg = messageProvider.getMessage(context, "itemMetadata",(Item) dso,
messageProvider.getMetadata(messageProvider.getMetadataField(metadataField), oldMtdVal),
"updated");
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.ITEM_METADATA.getTemplate(),
(Item) dso,messageProvider.getMetadata(messageProvider.getMetadataField(metadataField),
oldMtdVal), "updated");
addProvenanceMetadata(context, (Item) dso, msg);
}

Expand All @@ -258,7 +265,8 @@ public void replaceMetadataSingle(Context context, DSpaceObject dso, MetadataFie
Bitstream bitstream = (Bitstream) dso;
Item item = getItem(context, bitstream);
if (Objects.nonNull(item)) {
String msg = messageProvider.getMessage(context, "itemReplaceSingleMetadata", item,
String msg = messageProvider.getMessage(context,
ProvenanceMessageTemplates.ITEM_REPLACE_SINGLE_METADATA.getTemplate(), item,
messageProvider.getBitstreamMessage(bitstream),
messageProvider.getMetadata(messageProvider.getMetadataField(metadataField), oldMtdVal));
addProvenanceMetadata(context, item, msg);;
Expand All @@ -267,14 +275,15 @@ public void replaceMetadataSingle(Context context, DSpaceObject dso, MetadataFie

public void makeDiscoverable(Context context, Item item, boolean discoverable)
throws SQLException, AuthorizeException {
String msg = messageProvider.getMessage(context, "discoverable",
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.DISCOVERABLE.getTemplate(),
item, discoverable ? "" : "non-") + messageProvider.addCollectionsToMessage(item);
addProvenanceMetadata(context, item, msg);
}

public void uploadBitstream(Context context, Bundle bundle) throws SQLException, AuthorizeException {
Item item = bundle.getItems().get(0);
String msg = messageProvider.getMessage(context, "bundleAdded", item, bundle.getID());
String msg = messageProvider.getMessage(context, ProvenanceMessageTemplates.BUNDLE_ADDED.getTemplate(),
item, bundle.getID());
addProvenanceMetadata(context,item, msg);
itemService.update(context, item);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.app.rest;

public enum ProvenanceExpectedMessages {
DISCOVERABLE("Item was made discoverable by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 0\nItem was in collections:\n"),
NON_DISCOVERABLE("Item was made non-discoverable by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 0\nItem was in collections:\n"),
MAPPED_COL("was mapped to collection"),
ADD_ITEM_MTD("Item metadata (dc.title) was added by first (admin) last (admin) ([email protected]) on"),
REPLACE_ITEM_MTD("Item metadata (dc.title: Public item 1) was updated by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 0"),
REMOVE_ITEM_MTD("Item metadata (dc.title: Public item 1) was deleted by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 0"),
REMOVE_BITSTREAM_MTD("Item metadata (dc.description) was added by bitstream"),
REPLACE_BITSTREAM_MTD("metadata (dc.title: test) was updated by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 1\n"),
REMOVE_BITSTREAM("was deleted bitstream"),
ADD_BITSTREAM("Item was added bitstream to bundle"),
UPDATE_LICENSE("License (Test 1) was updated by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 1\n"),
ADD_LICENSE("License (empty) was added by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 0"),
REMOVE_LICENSE("License (Test) was removed by first (admin) last (admin) ([email protected]) on \nNo. of bitstreams: 1\n"),
MOVED_ITEM_COL("Item was moved from collection ");

private final String template;

// Constructor to initialize enum with the template string
ProvenanceExpectedMessages(String template) {
this.template = template;
}

// Method to retrieve the template string
public String getTemplate() {
return template;
}
}
Loading

0 comments on commit 419aff3

Please sign in to comment.