Skip to content

Commit 0c8e2fd

Browse files
author
Piaget Bouaka Donfack
committed
[DURACOM-317] AUDIT feature community
1 parent 926cc0b commit 0c8e2fd

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

dspace-api/src/main/java/org/dspace/app/audit/AuditSolrServiceImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ private boolean isProcessableEvent(Event event) {
175175
* @throws SQLException if a database access error occurs
176176
*/
177177
private boolean isAuditableItem(Context context, Event event) throws SQLException {
178-
if (event.getSubjectType() != Constants.ITEM && event.getSubjectType() != Constants.BITSTREAM
179-
&& event.getSubjectType() != Constants.BUNDLE) {
178+
if ((event.getSubjectType() != Constants.ITEM && event.getSubjectType() != Constants.BITSTREAM
179+
&& event.getSubjectType() != Constants.BUNDLE) || event.getEventType() == Event.CREATE) {
180180
return true;
181181
}
182182

@@ -333,6 +333,10 @@ public List<AuditEvent> getAuditEventsFromEvent(Context context, Event event) {
333333

334334
for (MetadataEvent metadataEvent : metadataEvents) {
335335
AuditEvent audit = buildBasicAuditEvent(context, event);
336+
if (event.getEventType() == Event.CREATE ) {
337+
audit.setEventType("MODIFY_METADATA");
338+
}
339+
336340
audit.setMetadataField(metadataEvent.getMetadataField());
337341
audit.setValue(metadataEvent.getValue());
338342
audit.setAuthority(metadataEvent.getAuthority());

dspace-api/src/main/java/org/dspace/app/audit/MetadataEvent.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
package org.dspace.app.audit;
99

10+
import java.util.Objects;
11+
1012
import org.dspace.content.MetadataValue;
1113

1214
/**
@@ -99,4 +101,19 @@ public void setAction(String action) {
99101
this.action = action;
100102
}
101103

104+
@Override
105+
public boolean equals(Object o) {
106+
if (o == null || getClass() != o.getClass()) {
107+
return false;
108+
}
109+
MetadataEvent that = (MetadataEvent) o;
110+
return place == that.place && Objects.equals(metadataField, that.metadataField) &&
111+
Objects.equals(value, that.value) && Objects.equals(authority, that.authority) &&
112+
Objects.equals(confidence, that.confidence) && Objects.equals(action, that.action);
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
return Objects.hash(metadataField, value, authority, confidence, place, action);
118+
}
102119
}

dspace-api/src/main/java/org/dspace/content/DSpaceObject.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
import java.io.Serializable;
1111
import java.util.ArrayList;
12+
import java.util.HashSet;
1213
import java.util.List;
14+
import java.util.Set;
1315
import java.util.UUID;
1416

1517
import jakarta.persistence.CascadeType;
@@ -53,7 +55,7 @@ public abstract class DSpaceObject implements Serializable, ReloadableEntity<jav
5355
// accumulate information to be stored in the Audit system
5456
// data is stored in a structured way, so no need to concatenate
5557
@Transient
56-
private ArrayList<MetadataEvent> metadataEventDetails;
58+
private Set<MetadataEvent> metadataEventDetails;
5759

5860
/**
5961
* The same order should be applied inside this comparator
@@ -146,7 +148,7 @@ public String getDetails() {
146148
*/
147149
public void addMetadataEventDetails(MetadataEvent event) {
148150
if (metadataEventDetails == null) {
149-
metadataEventDetails = new ArrayList<>();
151+
metadataEventDetails = new HashSet<>();
150152
}
151153
metadataEventDetails.add(event);
152154
}
@@ -155,7 +157,16 @@ public void addMetadataEventDetails(MetadataEvent event) {
155157
* @return list of metadata event details, or empty list if there is none.
156158
*/
157159
public List<MetadataEvent> getMetadataEventDetails() {
158-
return metadataEventDetails == null ? List.of() : metadataEventDetails;
160+
return metadataEventDetails == null ? List.of() : metadataEventDetails.stream().toList();
161+
}
162+
163+
/**
164+
* Clear the list of metadata event details to avoid duplication in audit logs.
165+
*/
166+
public void clearMetadataEventDetails() {
167+
if (metadataEventDetails != null) {
168+
metadataEventDetails.clear();
169+
}
159170
}
160171

161172
/**

dspace-api/src/main/java/org/dspace/content/InstallItemServiceImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ protected void populateMetadata(Context c, Item item)
168168
// NOTE: As of DSpace 4.0, DSpace no longer sets an issue date by default
169169
List<MetadataValue> currentDateIssued = itemService
170170
.getMetadata(item, MetadataSchemaEnum.DC.getName(), "date", "issued", Item.ANY);
171-
itemService.clearMetadata(c, item, MetadataSchemaEnum.DC.getName(), "date", "issued", Item.ANY);
172-
for (MetadataValue dcv : currentDateIssued) {
173-
if (dcv.getValue() != null && dcv.getValue().equalsIgnoreCase("today")) {
174-
DCDate issued = new DCDate(now.getYear(), now.getMonth(), now.getDay(), -1, -1, -1);
175-
itemService.addMetadata(c, item, dcv.getMetadataField(), dcv.getLanguage(), issued.toString());
176-
} else if (dcv.getValue() != null) {
177-
itemService.addMetadata(c, item, dcv.getMetadataField(), dcv.getLanguage(), dcv.getValue());
178-
}
171+
172+
List<MetadataValue> currentDateIssuedToRemove = currentDateIssued.stream()
173+
.filter(mdv -> mdv.getValue() != null && mdv.getValue().equalsIgnoreCase("today")).toList();
174+
175+
itemService.removeMetadataValues(c, item, currentDateIssuedToRemove);
176+
for (MetadataValue dcv : currentDateIssuedToRemove) {
177+
DCDate issued = new DCDate(now.getYear(), now.getMonth(), now.getDay(), -1, -1, -1);
178+
itemService.addMetadata(c, item, dcv.getMetadataField(), dcv.getLanguage(), issued.toString());
179179
}
180180

181181
String provDescription = "Made available in DSpace on " + now

dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ protected Item createItem(Context context, UUID uuid) throws SQLException, Autho
567567
update(context, item);
568568
context.restoreAuthSystemState();
569569

570-
context.addEvent(new Event(Event.CREATE, Constants.ITEM, item.getID(),
571-
null, getIdentifiers(context, item)));
570+
context.addEvent(new Event(Event.CREATE, Constants.ITEM, item.getID(), null,
571+
DetailType.DSO_SUMMARY, getIdentifiers(context, item)));
572572

573573
log.info(LogHelper.getHeader(context, "create_item", "item_id=" + item.getID()));
574574

@@ -586,8 +586,8 @@ protected Item createItem(Context context) throws SQLException, AuthorizeExcepti
586586
update(context, item);
587587
context.restoreAuthSystemState();
588588

589-
context.addEvent(new Event(Event.CREATE, Constants.ITEM, item.getID(),
590-
null, getIdentifiers(context, item)));
589+
context.addEvent(new Event(Event.CREATE, Constants.ITEM, item.getID(), null,
590+
DetailType.DSO_SUMMARY, getIdentifiers(context, item)));
591591

592592
log.info(LogHelper.getHeader(context, "create_item", "item_id=" + item.getID()));
593593

@@ -693,6 +693,7 @@ public void update(Context context, Item item) throws SQLException, AuthorizeExc
693693
context.addEvent(new Event(Event.MODIFY_METADATA, item.getType(), item.getID(),
694694
item.getMetadataEventDetails(), DetailType.DSO_SUMMARY,
695695
getIdentifiers(context, item)));
696+
item.clearMetadataEventDetails();
696697
}
697698

698699
context.addEvent(new Event(Event.MODIFY, Constants.ITEM, item.getID(),

dspace-server-webapp/src/test/java/org/dspace/app/rest/AuditEventRestRepositoryIT.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* Integration Tests against the /api/system/auditevents endpoint
4848
*/
4949
public class AuditEventRestRepositoryIT extends AbstractControllerIntegrationTest {
50-
private final int TOTAL_ELEMENT = 71;
50+
private final int TOTAL_ELEMENT = 18;
5151
private Collection collection;
5252

5353
private Item item;
@@ -206,21 +206,21 @@ public void findAllPaginationTest() throws Exception {
206206
Matchers.containsString("page=0"), Matchers.containsString("size=1"))))
207207
.andExpect(jsonPath("$.page.size", is(1)))
208208
.andExpect(jsonPath("$.page.totalElements", is(TOTAL_ELEMENT)));
209-
getClient(adminToken).perform(get("/api/system/auditevents").param("size", "50").param("page", "2"))
209+
getClient(adminToken).perform(get("/api/system/auditevents").param("size", "10").param("page", "2"))
210210
.andExpect(status().isOk()).andExpect(content().contentType(contentType))
211211
.andExpect(
212212
jsonPath("$._links.self.href", Matchers.containsString("/api/system/auditevents")))
213213
.andExpect(jsonPath("$._links.next.href").doesNotExist())
214214
.andExpect(jsonPath("$._links.last.href",
215215
Matchers.allOf(Matchers.containsString("/api/system/auditevents?"),
216-
Matchers.containsString("page=1"), Matchers.containsString("size=50"))))
216+
Matchers.containsString("page=1"), Matchers.containsString("size=10"))))
217217
.andExpect(jsonPath("$._links.first.href",
218218
Matchers.allOf(Matchers.containsString("/api/system/auditevents?"),
219-
Matchers.containsString("page=0"), Matchers.containsString("size=50"))))
219+
Matchers.containsString("page=0"), Matchers.containsString("size=10"))))
220220
.andExpect(jsonPath("$._links.prev.href",
221221
Matchers.allOf(Matchers.containsString("/api/system/auditevents?"),
222-
Matchers.containsString("page=1"), Matchers.containsString("size=50"))))
223-
.andExpect(jsonPath("$.page.size", is(50)))
222+
Matchers.containsString("page=1"), Matchers.containsString("size=10"))))
223+
.andExpect(jsonPath("$.page.size", is(10)))
224224
.andExpect(jsonPath("$.page.totalElements", is(TOTAL_ELEMENT)));
225225
}
226226

@@ -406,14 +406,14 @@ public void findByObjectPaginationTest() throws Exception {
406406
.andExpect(jsonPath("$._links.last.href",
407407
Matchers.allOf(
408408
Matchers.containsString("/api/system/auditevents/search/findByObject?"),
409-
Matchers.containsString("page=57"), Matchers.containsString("size=1"))))
409+
Matchers.containsString("page=8"), Matchers.containsString("size=1"))))
410410
.andExpect(jsonPath("$._links.first.href",
411411
Matchers.allOf(
412412
Matchers.containsString("/api/system/auditevents/search/findByObject?"),
413413
Matchers.containsString("page=0"), Matchers.containsString("size=1"))))
414414
.andExpect(jsonPath("$._links.prev.href").doesNotExist())
415415
.andExpect(jsonPath("$.page.size", is(1)))
416-
.andExpect(jsonPath("$.page.totalElements", is(58)));
416+
.andExpect(jsonPath("$.page.totalElements", is(9)));
417417

418418
getClient(adminToken).perform(get("/api/system/auditevents/search/findByObject")
419419
.param("object", item.getID().toString())
@@ -437,13 +437,13 @@ public void findByObjectPaginationTest() throws Exception {
437437
.andExpect(jsonPath("$._links.last.href",
438438
Matchers.allOf(
439439
Matchers.containsString("/api/system/auditevents/search/findByObject?"),
440-
Matchers.containsString("page=57"), Matchers.containsString("size=1"))))
440+
Matchers.containsString("page=8"), Matchers.containsString("size=1"))))
441441
.andExpect(jsonPath("$._links.first.href",
442442
Matchers.allOf(
443443
Matchers.containsString("/api/system/auditevents/search/findByObject?"),
444444
Matchers.containsString("page=0"), Matchers.containsString("size=1"))))
445445
.andExpect(jsonPath("$.page.size", is(1)))
446-
.andExpect(jsonPath("$.page.totalElements", is(58)));
446+
.andExpect(jsonPath("$.page.totalElements", is(9)));
447447
}
448448

449449
@Test

0 commit comments

Comments
 (0)