Skip to content

Commit

Permalink
feat: Migrate TEA change logs into new table [DHIS2-18474] (#19321)
Browse files Browse the repository at this point in the history
* feat: Migrate TEA change logs into new table [DHIS2-18474]

* feat: Fix sonar issues [DHIS2-18474]

* feat: Import one object in test[DHIS2-18474]

* feat: Lowercase sql script  [DHIS2-18474]

* feat: Address PR comments [DHIS2-18474]

* feat: Removed unused handle method [DHIS2-18474]

* feat: Make constructor private [DHIS2-18474]

* feat: Address PR comments [DHIS2-18474]
  • Loading branch information
muilpp authored Dec 3, 2024
1 parent c1fc423 commit c02f941
Show file tree
Hide file tree
Showing 27 changed files with 1,064 additions and 1,648 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public int deleteSoftDeletedTrackedEntities() {
"delete from relationshipitem where trackedentityid in " + teSelect,
"delete from trackedentityattributevalue where trackedentityid in " + teSelect,
"delete from trackedentityattributevalueaudit where trackedentityid in " + teSelect,
"delete from trackedentitychangelog where trackedentityid in " + teSelect,
"delete from trackedentityprogramowner where trackedentityid in " + teSelect,
"delete from programtempowner where trackedentityid in " + teSelect,
"delete from programtempownershipaudit where trackedentityid in " + teSelect,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="org.hisp.dhis.tracker.export.trackedentity.TrackedEntityChangeLog"
table="trackedentitychangelog">

<id name="id" column="trackedentitychangelogid">
<generator class="sequence">
<param name="sequence_name">trackedentitychangelog_sequence</param>
</generator>
</id>

<many-to-one name="trackedEntity" class="org.hisp.dhis.trackedentity.TrackedEntity"
column="trackedentityid"
foreign-key="fk_trackedentitychangelog_trackedentityinstanceid" not-null="true"/>

<many-to-one name="trackedEntityAttribute" class="org.hisp.dhis.trackedentity.TrackedEntityAttribute"
column="trackedentityattributeid"
foreign-key="fk_trackedentitychangelog_trackedentityattributeid" not-null="true"/>

<property name="previousValue" column="previousvalue" access="property" length="50000"/>

<property name="currentValue" column="currentvalue" access="property" length="50000"/>

<property name="changeLogType" column="changelogtype" length="100" not-null="true">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">org.hisp.dhis.changelog.ChangeLogType</param>
<param name="useNamed">true</param>
<param name="type">12</param>
</type>
</property>

<property name="created" type="timestamp" not-null="true"/>

<property name="createdByUsername" column="createdby" type="string"/>

<many-to-one name="createdBy" class="org.hisp.dhis.user.User" column="createdby" insert="false"
update="false" property-ref="username"/>

<many-to-one name="programAttribute" class="org.hisp.dhis.program.ProgramTrackedEntityAttribute"
column="trackedentityattributeid" insert="false" update="false" access="field" property-ref="attribute" />
</class>
</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public List<String> canRead(@Nonnull UserDetails user, TrackedEntity trackedEnti
private List<String> canRead(
UserDetails user, TrackedEntity trackedEntity, List<Program> programs) {

if (null == trackedEntity) {
if (trackedEntity == null) {
return List.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
package org.hisp.dhis.tracker.export.trackedentity;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.changelog.ChangeLogType;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.feedback.BadRequestException;
Expand Down Expand Up @@ -63,17 +66,40 @@ public class DefaultTrackedEntityChangeLogService implements TrackedEntityChange

private final TrackerAccessManager trackerAccessManager;

private final JdbcTrackedEntityChangeLogStore jdbcTrackedEntityChangeLogStore;

private final TrackedEntityAttributeValueChangeLogStore attributeValueChangeLogStore;

private final HibernateTrackedEntityChangeLogStore hibernateTrackedEntityChangeLogStore;

@Override
@Transactional
public void addTrackedEntityAttributeValueChangeLog(
TrackedEntityAttributeValueChangeLog attributeValueChangeLog) {
attributeValueChangeLogStore.addTrackedEntityAttributeValueChangeLog(attributeValueChangeLog);
}

@Transactional
@Override
public void addTrackedEntityChangeLog(
@Nonnull TrackedEntity trackedEntity,
@Nonnull TrackedEntityAttribute trackedEntityAttribute,
@Nullable String previousValue,
@Nullable String currentValue,
@Nonnull ChangeLogType changeLogType,
@Nonnull String username) {

TrackedEntityChangeLog trackedEntityChangeLog =
new TrackedEntityChangeLog(
trackedEntity,
trackedEntityAttribute,
previousValue,
currentValue,
changeLogType,
new Date(),
username);

hibernateTrackedEntityChangeLogStore.addTrackedEntityChangeLog(trackedEntityChangeLog);
}

@Override
@Transactional(readOnly = true)
public List<TrackedEntityAttributeValueChangeLog> getTrackedEntityAttributeValueChangeLogs(
Expand Down Expand Up @@ -110,25 +136,25 @@ public int countTrackedEntityAttributeValueChangeLogs(

@Override
@Transactional
public void deleteTrackedEntityAttributeValueChangeLogs(TrackedEntity trackedEntity) {
attributeValueChangeLogStore.deleteTrackedEntityAttributeValueChangeLogs(trackedEntity);
public void deleteTrackedEntityChangeLogs(TrackedEntity trackedEntity) {
hibernateTrackedEntityChangeLogStore.deleteTrackedEntityChangeLogs(trackedEntity);
}

@Override
@Transactional(readOnly = true)
public Page<TrackedEntityChangeLog> getTrackedEntityChangeLog(
UID trackedEntityUid,
UID programUid,
TrackedEntityChangeLogOperationParams operationParams,
PageParams pageParams)
@Nonnull UID trackedEntityUid,
@Nullable UID programUid,
@Nonnull TrackedEntityChangeLogOperationParams operationParams,
@Nonnull PageParams pageParams)
throws NotFoundException, ForbiddenException, BadRequestException {
TrackedEntity trackedEntity = trackedEntityService.getTrackedEntity(trackedEntityUid);
if (trackedEntity == null) {
throw new NotFoundException(TrackedEntity.class, trackedEntityUid.getValue());
}

UserDetails currentUser = CurrentUserUtil.getCurrentUserDetails();
Set<String> trackedEntityAttributes = Collections.emptySet();
Set<UID> trackedEntityAttributes = Collections.emptySet();
if (programUid != null) {
Program program = validateProgram(programUid.getValue());
validateOwnership(currentUser, trackedEntity, program);
Expand All @@ -137,18 +163,14 @@ public Page<TrackedEntityChangeLog> getTrackedEntityChangeLog(
trackedEntityAttributes = validateTrackedEntityAttributes(trackedEntity);
}

return jdbcTrackedEntityChangeLogStore.getTrackedEntityChangeLog(
trackedEntityUid,
trackedEntityAttributes,
programUid,
operationParams.getOrder(),
pageParams);
return hibernateTrackedEntityChangeLogStore.getTrackedEntityChangeLogs(
trackedEntityUid, programUid, trackedEntityAttributes, operationParams, pageParams);
}

@Override
@Transactional(readOnly = true)
public Set<String> getOrderableFields() {
return jdbcTrackedEntityChangeLogStore.getOrderableFields();
return hibernateTrackedEntityChangeLogStore.getOrderableFields();
}

private Program validateProgram(String programUid) throws NotFoundException {
Expand Down Expand Up @@ -177,7 +199,7 @@ private void validateTrackedEntity(UserDetails currentUser, TrackedEntity tracke
}
}

private Set<String> validateTrackedEntityAttributes(TrackedEntity trackedEntity)
private Set<UID> validateTrackedEntityAttributes(TrackedEntity trackedEntity)
throws NotFoundException {
Set<TrackedEntityAttribute> attributes =
trackedEntityAttributeService.getTrackedEntityTypeAttributes(
Expand All @@ -187,6 +209,6 @@ private Set<String> validateTrackedEntityAttributes(TrackedEntity trackedEntity)
throw new NotFoundException(TrackedEntity.class, trackedEntity.getUid());
}

return attributes.stream().map(BaseIdentifiableObject::getUid).collect(Collectors.toSet());
return attributes.stream().map(a -> UID.of(a.getUid())).collect(Collectors.toSet());
}
}
Loading

0 comments on commit c02f941

Please sign in to comment.