Skip to content

Commit

Permalink
Code formatting and styling
Browse files Browse the repository at this point in the history
  • Loading branch information
juanluisrp committed Nov 14, 2024
1 parent abed63e commit 73f8e48
Show file tree
Hide file tree
Showing 22 changed files with 280 additions and 211 deletions.
2 changes: 1 addition & 1 deletion auditable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.geonetwork-opensource</groupId>
<artifactId>geonetwork</artifactId>
<version>4.4.6-SNAPSHOT</version>
<version>4.4.7-SNAPSHOT</version>
</parent>

<!-- =========================================================== -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/

package org.fao.geonet.auditable;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -39,11 +38,13 @@
import java.util.*;

public abstract class BaseAuditableService<U> {
protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
protected static final String LINE_SEPARATOR = System.lineSeparator();

protected BaseAuditableRepository<U> repository;
protected SettingManager settingManager;

public abstract String getEntityType();

public void auditSave(U auditableEntity) {
if (!isAuditableEnabled()) return;

Expand Down Expand Up @@ -72,7 +73,6 @@ public List<RevisionInfo> getEntityHistory(Integer entityIdentifier) {
return retrieveRevisionHistory(revisions);
}

public abstract String getEntityType();

protected String retrieveRevisionHistoryAsString(Revisions<Integer, U> revisions, ResourceBundle messages) {
List<RevisionInfo> revisionInfoList = retrieveRevisionHistory(revisions);
Expand Down Expand Up @@ -123,7 +123,7 @@ protected List<RevisionInfo> retrieveRevisionHistory(Revisions<Integer, U> revis

// Initial revision
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> revisionMap = objectMapper.convertValue(initialRevision.getEntity(), Map.class);
Map<String, Object> revisionMap = objectMapper.convertValue(initialRevision.getEntity(), Map.class);
// Remove empty values and id
revisionMap.values().removeAll(Arrays.asList("", null));
revisionMap.remove(idFieldName);
Expand All @@ -136,13 +136,13 @@ protected List<RevisionInfo> retrieveRevisionHistory(Revisions<Integer, U> revis
revisionInfoList.add(initialRevisionInfo);

int i = 0;
while (i+1 < numRevisions) {
while (i + 1 < numRevisions) {
Revision<Integer, U> revision1 = revisionList.get(i);
Revision<Integer, U> revision2 = revisionList.get(i+1);
Revision<Integer, U> revision2 = revisionList.get(i + 1);

Map<String, Object> revision1Map = objectMapper.convertValue(revision1.getEntity(), Map.class);
Map<String, Object> revision1Map = objectMapper.convertValue(revision1.getEntity(), Map.class);
revision1Map.remove(idFieldName);
Map<String, Object> revision2Map = objectMapper.convertValue(revision2.getEntity(), Map.class);
Map<String, Object> revision2Map = objectMapper.convertValue(revision2.getEntity(), Map.class);
revision2Map.remove(idFieldName);

MapDifference<String, Object> diff = Maps.difference(revision1Map, revision2Map);
Expand All @@ -162,7 +162,6 @@ protected List<RevisionInfo> retrieveRevisionHistory(Revisions<Integer, U> revis
RevisionFieldChange revisionFieldChange = new RevisionFieldChange(key, oldValueAsString, newValueAsString);

revisionInfo.addChange(revisionFieldChange);

});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@

@Service
public class UserAuditableService extends BaseAuditableService<UserAuditable> {

public static final String ENTITY_TYPE = "user";

public UserAuditableService(SettingManager settingManager, UserAuditableRepository repository) {
this.settingManager = settingManager;
this.repository = repository;
}

@Override
public String getEntityType() {
return "user";
return ENTITY_TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
/*
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/
package org.fao.geonet.auditable.model;

/**
* This class represents a change in an entity field. It stores the field name,
* the previous and the new value.
*/
public class RevisionFieldChange {
private String name;
private String oldValue;

private String newValue;
private final String name;
private final String oldValue;
private final String newValue;

public RevisionFieldChange(String name, String oldValue, String newValue) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/

package org.fao.geonet.auditable.model;

import org.fao.geonet.domain.ISODate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.fao.geonet.domain.ISODate;

public class RevisionInfo {
private int revisionNumber;
private String user;
private String date;

private String value;

private List<RevisionFieldChange> changes;
private final int revisionNumber;
private final String user;
private final String date;
private final String value;
private final List<RevisionFieldChange> changes;

public RevisionInfo(int revisionNumber, String user, Date date, String value) {
this.revisionNumber = revisionNumber;
Expand All @@ -62,8 +59,11 @@ public String getValue() {
return value;
}

/**
* @return an unmodifiable view of the list of changes.
*/
public List<RevisionFieldChange> getChanges() {
return changes;
return Collections.unmodifiableList(changes);
}

public void addChange(RevisionFieldChange change) {
Expand Down
9 changes: 4 additions & 5 deletions auditable/src/main/resources/config-spring-geonetwork.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
-->

<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="org.fao.geonet.api"/>
<context:component-scan base-package="org.fao.geonet.auditable"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/

package org.fao.geonet.auditable.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.fao.geonet.domain.Group;
import org.fao.geonet.domain.Profile;
import org.fao.geonet.domain.User;
Expand All @@ -31,21 +33,16 @@
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UserAuditableTest {

@Test
public void testBuildUserAuditable() {
Group group = new Group()
.setId(1)
.setName("sample");
Group group = new Group().setId(1).setName("sample");
Group group2 = new Group().setId(2).setName("sampleGroup2");


User user = new User()
Expand All @@ -54,7 +51,7 @@ public void testBuildUserAuditable() {
.setSurname("surname")
.setUsername("username")
.setEnabled(true)
.setEmailAddresses(new HashSet<>(Arrays.asList("[email protected]")))
.setEmailAddresses(new HashSet<>(List.of("[email protected]")))
.setProfile(Profile.Reviewer);


Expand All @@ -65,7 +62,7 @@ public void testBuildUserAuditable() {
.setProfile(Profile.Editor);

UserGroup userGroup2 = new UserGroup()
.setGroup(group)
.setGroup(group2)
.setUser(user)
.setProfile(Profile.Reviewer);

Expand All @@ -81,12 +78,12 @@ public void testBuildUserAuditable() {
assertEquals(user.getUsername(), userAuditable.getUsername());
assertEquals(user.getEmailAddresses().toArray()[0], userAuditable.getEmailAddress());
assertEquals(user.getProfile().toString(), userAuditable.getProfile());
assertTrue(!StringUtils.hasLength(userAuditable.getGroupsRegisteredUser()));
assertFalse(StringUtils.hasLength(userAuditable.getGroupsRegisteredUser()));
assertTrue(userAuditable.getGroupsEditor().contains(group.getName()));
assertTrue(userAuditable.getGroupsReviewer().contains(group.getName()));
assertTrue(!StringUtils.hasLength(userAuditable.getGroupsUserAdmin()));
assertTrue(userAuditable.getGroupsReviewer().contains(group2.getName()));
assertFalse(StringUtils.hasLength(userAuditable.getGroupsUserAdmin()));

assertEquals(group.getName(), userAuditable.getGroupsEditor());
assertEquals(group.getName(), userAuditable.getGroupsReviewer());
assertEquals(group2.getName(), userAuditable.getGroupsReviewer());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2001-2016 Food and Agriculture Organization of the
# Copyright (C) 2001-2024 Food and Agriculture Organization of the
# United Nations (FAO-UN), United Nations World Food Programme (WFP)
# and United Nations Environment Programme (UNEP)
#
Expand All @@ -20,7 +20,6 @@
# Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
# Rome - Italy. email: [email protected]
#

mail_error=Failed to send email.
mail_config_test_subject=%s / Test / Mail configuration
mail_config_test_message=Test message from %s\n\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
# Rome - Italy. email: [email protected]
#

mail_error=Erreur lors de l'envoi du mail.
mail_config_test_subject=%s / Test / Configuration serveur de mail
mail_config_test_message=Message de test de %s\n\
Expand Down Expand Up @@ -237,8 +236,8 @@ api.metadata.status.errorSetStatusNotAllowed=Seul le propri\u00E9taire des m\u00

feedback_subject_userFeedback=Commentaire de l'utilisateur

audit.revision=Updated by %s on %s:\n\
audit.revision=Mise \u00E0 jour par %s le %s:\n\
%s
audit.revision.field.set=- Field '%s' set to '%s'
audit.revision.field.unset=- Field '%s' unset
audit.revision.field.updated=- Field '%s' changed from '%s' to '%s'
audit.revision.field.set=- Champ '%s' d\u00E9fini \u00E0 '%s'
audit.revision.field.unset=- Champ '%s' d\u00E9sactiv\u00E9
audit.revision.field.updated=- Champ '%s' modifi\u00E9 de '%s' \u00E0 '%s'
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/

package org.fao.geonet.auditable;

import java.util.Optional;
import org.fao.geonet.domain.User;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

/**
* Extracts the current user used, to store the information in the auditable entities.
*/
Expand Down
26 changes: 26 additions & 0 deletions domain/src/main/java/org/fao/geonet/auditable/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: [email protected]
*/
@NonNullApi
package org.fao.geonet.auditable;

import org.springframework.lang.NonNullApi;
Loading

0 comments on commit 73f8e48

Please sign in to comment.