Skip to content

Commit

Permalink
chore: Move Changelog service to tracker module [DHIS2-17835] (#18425)
Browse files Browse the repository at this point in the history
* chore: Move Changelog service to tracker module [DHIS2-17835]

* chore: Fix sonar issues [DHIS2-17835]

* chore: Fix sonar issues [DHIS2-17835]

* chore: Fix sonar issues [DHIS2-17835]

* chore: Fix h2 jsonB test [DHIS2-17835]

* chore: Rename package [DHIS2-17835]
  • Loading branch information
muilpp authored Aug 26, 2024
1 parent 034c4c8 commit 7970a48
Show file tree
Hide file tree
Showing 24 changed files with 352 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.trackedentity;

import org.hisp.dhis.changelog.ChangeLogType;

/**
* @author Abyot Asalefew Gizaw [email protected]
*/
public interface ApiTrackedEntityAuditService {

String ID = ApiTrackedEntityAuditService.class.getName();

void addTrackedEntityAudit(
TrackedEntity trackedEntity, String username, ChangeLogType changeLogType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.trackedentity;

/**
* @author Abyot Asalefew Gizaw [email protected]
*/
public interface ApiTrackedEntityAuditStore {
String ID = ApiTrackedEntityAuditStore.class.getName();

/**
* Adds the given tracked entity audit.
*
* @param trackedEntityAudit the {@link TrackedEntityAudit} to add.
*/
void addTrackedEntityAudit(TrackedEntityAudit trackedEntityAudit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @author Abyot Asalefew Gizaw [email protected]
*/
@JacksonXmlRootElement(localName = "trackedEntityInstanceAudit", namespace = DxfNamespaces.DXF_2_0)
public class TrackedEntityChangeLog implements Serializable {
public class TrackedEntityAudit implements Serializable {
private static final long serialVersionUID = 4260110537887403524L;

private long id;
Expand All @@ -61,16 +61,16 @@ public class TrackedEntityChangeLog implements Serializable {
// Constructors
// -------------------------------------------------------------------------

public TrackedEntityChangeLog() {}
public TrackedEntityAudit() {}

public TrackedEntityChangeLog(String trackedEntity, String accessedBy, ChangeLogType auditType) {
public TrackedEntityAudit(String trackedEntity, String accessedBy, ChangeLogType auditType) {
this.trackedEntity = trackedEntity;
this.accessedBy = accessedBy;
this.created = new Date();
this.auditType = auditType;
}

public TrackedEntityChangeLog(
public TrackedEntityAudit(
String trackedEntity,
String comment,
Date created,
Expand All @@ -96,7 +96,7 @@ public boolean equals(Object obj) {
return false;
}

final TrackedEntityChangeLog other = (TrackedEntityChangeLog) obj;
final TrackedEntityAudit other = (TrackedEntityAudit) obj;

return Objects.equals(this.trackedEntity, other.trackedEntity)
&& Objects.equals(this.comment, other.comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
@Data
@Accessors(chain = true)
public class TrackedEntityChangeLogQueryParams {
public class TrackedEntityAuditQueryParams {
private List<String> trackedEntities = new ArrayList<>();

private List<String> users = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void addTrackedEntityAttributeValueChangLog(
TrackedEntityAttributeValueChangeLog trackedEntityAttributeValueChangeLog);

/**
* @deprecated use TrackedEntityChangeLogService.getEventChangeLog(UID) instead
* @deprecated use TrackedEntityChangeLogService.getTrackedEntityChangeLog(UID) instead
*/
@Deprecated(since = "2.41")
List<TrackedEntityAttributeValueChangeLog> getTrackedEntityAttributeValueChangeLogs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.DeliveryChannel;
import org.hisp.dhis.common.IdentifiableObject;
Expand All @@ -51,8 +50,8 @@
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.security.acl.AclService;
import org.hisp.dhis.trackedentity.ApiTrackedEntityAuditService;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackedEntityChangeLogService;
import org.hisp.dhis.user.CurrentUserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -80,7 +79,7 @@ public class DefaultProgramMessageService implements ProgramMessageService {

private final ProgramMessageOperationParamMapper operationParamMapper;

private final TrackedEntityChangeLogService trackedEntityChangeLogService;
private final ApiTrackedEntityAuditService apiTrackedEntityAuditService;

// -------------------------------------------------------------------------
// Implementation methods
Expand Down Expand Up @@ -174,8 +173,7 @@ public void validatePayload(ProgramMessage message) {
violations.add("Tracked entity does not exist");
}

trackedEntityChangeLogService.addTrackedEntityChangeLog(
trackedEntity, getCurrentUsername(), READ);
apiTrackedEntityAuditService.addTrackedEntityAudit(trackedEntity, getCurrentUsername(), READ);
}

if (recipients.getOrganisationUnit() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.trackedentity.ApiTrackedEntityAuditService;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackedEntityChangeLogService;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -50,7 +50,7 @@ public abstract class DeliveryChannelStrategy {

@Autowired private IdentifiableObjectManager manager;

@Autowired private TrackedEntityChangeLogService trackedEntityChangeLogService;
@Autowired private ApiTrackedEntityAuditService apiTrackedEntityAuditService;

// -------------------------------------------------------------------------
// Abstract methods
Expand Down Expand Up @@ -97,8 +97,7 @@ protected TrackedEntity getTrackedEntity(ProgramMessage message) {

TrackedEntity trackedEntity = manager.get(TrackedEntity.class, uid);

trackedEntityChangeLogService.addTrackedEntityChangeLog(
trackedEntity, getCurrentUsername(), READ);
apiTrackedEntityAuditService.addTrackedEntityAudit(trackedEntity, getCurrentUsername(), READ);

message.getRecipients().setTrackedEntity(trackedEntity);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.trackedentity;

import lombok.RequiredArgsConstructor;
import org.hisp.dhis.changelog.ChangeLogType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Abyot Asalefew Gizaw [email protected]
*/
@RequiredArgsConstructor
@Service("org.hisp.dhis.trackedentity.ApiTrackedEntityAuditService")
public class DefaultApiTrackedEntityAuditService implements ApiTrackedEntityAuditService {
private final ApiTrackedEntityAuditStore apiTrackedEntityAuditStore;

@Override
@Async
@Transactional
public void addTrackedEntityAudit(
TrackedEntity trackedEntity, String username, ChangeLogType changeLogType) {
if (username != null
&& trackedEntity != null
&& trackedEntity.getTrackedEntityType() != null
&& trackedEntity.getTrackedEntityType().isAllowAuditLog()) {
TrackedEntityAudit trackedEntityAudit =
new TrackedEntityAudit(trackedEntity.getUid(), username, changeLogType);
apiTrackedEntityAuditStore.addTrackedEntityAudit(trackedEntityAudit);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.trackedentity.hibernate;

import javax.persistence.EntityManager;
import org.hisp.dhis.hibernate.HibernateGenericStore;
import org.hisp.dhis.trackedentity.ApiTrackedEntityAuditStore;
import org.hisp.dhis.trackedentity.TrackedEntityAudit;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
* @author Abyot Asalefew Gizaw [email protected]
*/
@Repository("org.hisp.dhis.trackedentity.ApiTrackedEntityAuditStore")
public class HibernateApiTrackedEntityAuditStore extends HibernateGenericStore<TrackedEntityAudit>
implements ApiTrackedEntityAuditStore {

public HibernateApiTrackedEntityAuditStore(
EntityManager entityManager, JdbcTemplate jdbcTemplate, ApplicationEventPublisher publisher) {
super(entityManager, jdbcTemplate, publisher, TrackedEntityAudit.class, false);
}

// -------------------------------------------------------------------------
// ApiTrackedEntityAuditService implementation
// -------------------------------------------------------------------------

@Override
public void addTrackedEntityAudit(TrackedEntityAudit trackedEntityAudit) {
getSession().save(trackedEntityAudit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="org.hisp.dhis.trackedentity.TrackedEntityChangeLog" table="trackedentityaudit">
<class name="org.hisp.dhis.trackedentity.TrackedEntityAudit" table="trackedentityaudit">
<id name="id" column="trackedentityauditid">
<generator class="sequence">
<param name="sequence_name">trackedentityinstanceaudit_sequence</param>
Expand Down
Loading

0 comments on commit 7970a48

Please sign in to comment.