diff --git a/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageProvider.java b/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageProvider.java index f5f368fd0aa8..f1d65a483c91 100644 --- a/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageProvider.java +++ b/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageProvider.java @@ -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; @@ -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 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(), diff --git a/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageTemplates.java b/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageTemplates.java new file mode 100644 index 000000000000..410ff7af4ab3 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/core/ProvenanceMessageTemplates.java @@ -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; + } +} diff --git a/dspace-api/src/main/java/org/dspace/core/ProvenanceProvider.java b/dspace-api/src/main/java/org/dspace/core/ProvenanceProvider.java index 5472d03c77b3..9180cafb9fda 100644 --- a/dspace-api/src/main/java/org/dspace/core/ProvenanceProvider.java +++ b/dspace-api/src/main/java/org/dspace/core/ProvenanceProvider.java @@ -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); } } @@ -115,14 +115,16 @@ public void removeReadPolicies(Context context, DSpaceObject dso, List metadata = obj.getMetadata(); boolean contain = false; for (MetadataValue value : metadata) { if (!Objects.equals(value.getMetadataField().toString(), "dc_description_provenance")) { continue; } - if (provenanceMetadataModified(value.getValue()).contains(expectedSubStr)) { + if (provenanceMetadataModified(value.getValue()).contains(expectedMessage)) { contain = true; break; } } if (!contain) { - Assert.fail("Metadata provenance do not contain expected data: " + expectedSubStr); + Assert.fail("Metadata provenance do not contain expected data: " + expectedMessage); } } @@ -252,7 +246,7 @@ public void updateLicenseTest() throws Exception { getClient(token).perform(put("/api/core/items/" + item.getID() + "/bundles") .param("licenseID", clarinLicense2.getID().toString())) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "updateLicense"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.UPDATE_LICENSE.getTemplate()); deleteBitstream(bitstream); deleteClarinLicense(clarinLicense1); @@ -268,7 +262,7 @@ public void addLicenseTest() throws Exception { getClient(token).perform(put("/api/core/items/" + item.getID() + "/bundles") .param("licenseID", clarinLicense.getID().toString())) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "addLicense"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.ADD_LICENSE.getTemplate()); deleteClarinLicense(clarinLicense); } @@ -283,7 +277,7 @@ public void removeLicenseTest() throws Exception { getClient(token).perform(put("/api/core/items/" + item.getID() + "/bundles") .param("licenseID", "-1")) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "removeLicense"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.REMOVE_LICENSE.getTemplate()); deleteBitstream(bitstream); deleteClarinLicense(clarinLicense); @@ -306,7 +300,7 @@ public void makeDiscoverableTest() throws Exception { .andExpect(jsonPath("$.uuid", Matchers.is(item.getID().toString()))) .andExpect(jsonPath("$.discoverable", Matchers.is(true))); - objectCheck(itemService.find(context, item.getID()), "discoverable"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.DISCOVERABLE.getTemplate()); } @Test @@ -323,7 +317,7 @@ public void makeNonDiscoverableTest() throws Exception { .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "nonDiscoverable"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.NON_DISCOVERABLE.getTemplate()); } @Test @@ -338,7 +332,7 @@ public void addedToMappedCollTest() throws Exception { "https://localhost:8080/spring-rest/api/core/collections/" + coll.getID() + "\n" ) ); - objectCheck(itemService.find(context, item.getID()), "mappedCol"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.MAPPED_COL.getTemplate()); deleteCollection(coll.getID()); } @@ -355,7 +349,7 @@ public void addItemMetadataTest() throws Exception { .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "addItemMtd"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.ADD_ITEM_MTD.getTemplate()); } @Test @@ -371,7 +365,7 @@ public void replaceItemMetadataTest() throws Exception { .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "replaceItemMtd"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.REPLACE_ITEM_MTD.getTemplate()); } @Test @@ -387,7 +381,7 @@ public void removeItemMetadataTest() throws Exception { .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "removeItemMtd"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.REMOVE_ITEM_MTD.getTemplate()); } @Test @@ -403,7 +397,8 @@ public void removeBitstreamMetadataTest() throws Exception { .content(patchBody) .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "removeBitstreamMtd"); + objectCheck(itemService.find(context, item.getID()), + ProvenanceExpectedMessages.REMOVE_BITSTREAM_MTD.getTemplate()); deleteBitstream(bitstream); } @@ -421,7 +416,8 @@ public void addBitstreamMetadataTest() throws Exception { .content(patchBody) .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "removeBitstreamMtd"); + objectCheck(itemService.find(context, item.getID()), + ProvenanceExpectedMessages.REMOVE_BITSTREAM_MTD.getTemplate()); } @Test @@ -439,7 +435,8 @@ public void updateMetadataBitstreamTest() throws Exception { .content(patchBody) .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "replaceBitstreamMtd"); + objectCheck(itemService.find(context, item.getID()), + ProvenanceExpectedMessages.REPLACE_BITSTREAM_MTD.getTemplate()); deleteBitstream(bitstream); } @@ -456,7 +453,7 @@ public void removeBitstreamFromItemTest() throws Exception { getClient(adminToken).perform(patch("/api/core/bitstreams") .content(patchBody) .contentType(MediaType.APPLICATION_JSON_PATCH_JSON)); - objectCheck(itemService.find(context, item.getID()), "removeBitstream"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.REMOVE_BITSTREAM.getTemplate()); deleteBitstream(bitstream); } @@ -476,7 +473,7 @@ public void addBitstreamToItemTest() throws Exception { .perform(MockMvcRequestBuilders.multipart("/api/core/bundles/" + bundle.getID() + "/bitstreams") .file(file)) .andExpect(status().isCreated()); - objectCheck(itemService.find(context, item.getID()), "addBitstream"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.ADD_BITSTREAM.getTemplate()); deleteBundle(bundle.getID()); } @@ -493,7 +490,7 @@ public void moveItemColTest() throws Exception { "https://localhost:8080/spring-rest/api/core/collections/" + col.getID() )) .andExpect(status().isOk()); - objectCheck(itemService.find(context, item.getID()), "movedItemCol"); + objectCheck(itemService.find(context, item.getID()), ProvenanceExpectedMessages.MOVED_ITEM_COL.getTemplate()); deleteCollection(col.getID()); } diff --git a/dspace-server-webapp/src/test/resources/org/dspace/app/rest/provenance-test.json b/dspace-server-webapp/src/test/resources/org/dspace/app/rest/provenance-test.json deleted file mode 100644 index 268957768040..000000000000 --- a/dspace-server-webapp/src/test/resources/org/dspace/app/rest/provenance-test.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "discoverable": "Item was made discoverable by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0\nItem was in collections:\n", - "nonDiscoverable": "Item was made non-discoverable by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0\nItem was in collections:\n", - "mappedCol": "was mapped to collection", - "addItemMtd": "Item metadata (dc.title) was added by first (admin) last (admin) (admin@email.com) on", - "replaceItemMtd": "Item metadata (dc.title: Public item 1) was updated by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0", - "removeItemMtd": "Item metadata (dc.title: Public item 1) was deleted by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0", - "removeBitstreamMtd": "Item metadata (dc.description) was added by bitstream", - "replaceBitstreamMtd": "metadata (dc.title: test) was updated by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 1\n", - "removeBitstream": "was deleted bitstream", - "addBitstream": "Item was added bitstream to bundle", - "updateLicense": "License (Test 1) was updated by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 1\n", - "addLicense": "License (empty) was added by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0", - "removeLicense": "License (Test) was removed by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 1\n", - "movedItemCol": "Item was moved from collection " -} \ No newline at end of file