Search in sources :

Example 1 with DELETE

use of org.hisp.dhis.common.AuditType.DELETE in project dhis2-core by dhis2.

the class HibernatePotentialDuplicateStore method moveEnrollments.

@Override
public void moveEnrollments(TrackedEntityInstance original, TrackedEntityInstance duplicate, List<String> enrollments) {
    List<ProgramInstance> pis = duplicate.getProgramInstances().stream().filter(e -> !e.isDeleted()).filter(e -> enrollments.contains(e.getUid())).collect(Collectors.toList());
    pis.forEach(duplicate.getProgramInstances()::remove);
    pis.forEach(e -> {
        e.setEntityInstance(original);
        e.setLastUpdatedBy(currentUserService.getCurrentUser());
        e.setLastUpdatedByUserInfo(UserInfoSnapshot.from(currentUserService.getCurrentUser()));
        e.setLastUpdated(new Date());
        getSession().update(e);
    });
    // Flush to update records before we delete duplicate, or else it might
    // be soft-deleted by hibernate.
    getSession().flush();
}
Also used : PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) PotentialDuplicateQuery(org.hisp.dhis.deduplication.PotentialDuplicateQuery) Arrays(java.util.Arrays) DeduplicationMergeParams(org.hisp.dhis.deduplication.DeduplicationMergeParams) DELETE(org.hisp.dhis.common.AuditType.DELETE) PotentialDuplicateStore(org.hisp.dhis.deduplication.PotentialDuplicateStore) Date(java.util.Date) AuditManager(org.hisp.dhis.artemis.audit.AuditManager) LocalDateTime(java.time.LocalDateTime) UPDATE(org.hisp.dhis.common.AuditType.UPDATE) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstanceStore(org.hisp.dhis.trackedentity.TrackedEntityInstanceStore) AuditType(org.hisp.dhis.audit.AuditType) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DeduplicationStatus(org.hisp.dhis.deduplication.DeduplicationStatus) TrackedEntityAttributeValueAuditService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAuditService) HibernateIdentifiableObjectStore(org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore) CREATE(org.hisp.dhis.common.AuditType.CREATE) Map(java.util.Map) NativeQuery(org.hibernate.query.NativeQuery) Query(org.hibernate.query.Query) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BigInteger(java.math.BigInteger) AuditScope(org.hisp.dhis.audit.AuditScope) Repository(org.springframework.stereotype.Repository) MergeObject(org.hisp.dhis.deduplication.MergeObject) CHANGELOG_TRACKER(org.hisp.dhis.external.conf.ConfigurationKey.CHANGELOG_TRACKER) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) TrackedEntityAttributeValueAudit(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) PotentialDuplicateConflictException(org.hisp.dhis.deduplication.PotentialDuplicateConflictException) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) SessionFactory(org.hibernate.SessionFactory) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Audit(org.hisp.dhis.artemis.audit.Audit) Collectors(java.util.stream.Collectors) List(java.util.List) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AclService(org.hisp.dhis.security.acl.AclService) Optional(java.util.Optional) Collections(java.util.Collections) AuditableEntity(org.hisp.dhis.artemis.audit.AuditableEntity) Relationship(org.hisp.dhis.relationship.Relationship) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Date(java.util.Date)

Example 2 with DELETE

use of org.hisp.dhis.common.AuditType.DELETE in project dhis2-core by dhis2.

the class HibernatePotentialDuplicateStore method moveTrackedEntityAttributeValues.

@Override
public void moveTrackedEntityAttributeValues(TrackedEntityInstance original, TrackedEntityInstance duplicate, List<String> trackedEntityAttributes) {
    // Collect existing teav from original for the tea list
    Map<String, TrackedEntityAttributeValue> originalAttributeValueMap = new HashMap<>();
    original.getTrackedEntityAttributeValues().forEach(oav -> {
        if (trackedEntityAttributes.contains(oav.getAttribute().getUid())) {
            originalAttributeValueMap.put(oav.getAttribute().getUid(), oav);
        }
    });
    duplicate.getTrackedEntityAttributeValues().stream().filter(av -> trackedEntityAttributes.contains(av.getAttribute().getUid())).forEach(av -> {
        TrackedEntityAttributeValue updatedTeav;
        org.hisp.dhis.common.AuditType auditType;
        if (originalAttributeValueMap.containsKey(av.getAttribute().getUid())) {
            // Teav exists in original, overwrite the value
            updatedTeav = originalAttributeValueMap.get(av.getAttribute().getUid());
            updatedTeav.setValue(av.getValue());
            auditType = UPDATE;
        } else {
            // teav does not exist in original, so create new and attach
            // it to original
            updatedTeav = new TrackedEntityAttributeValue();
            updatedTeav.setAttribute(av.getAttribute());
            updatedTeav.setEntityInstance(original);
            updatedTeav.setValue(av.getValue());
            auditType = CREATE;
        }
        getSession().delete(av);
        // We need to flush to make sure the previous teav is
        // deleted.
        // Or else we might end up breaking a
        // constraint, since hibernate does not respect order.
        getSession().flush();
        getSession().saveOrUpdate(updatedTeav);
        auditTeav(av, updatedTeav, auditType);
    });
}
Also used : PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) PotentialDuplicateQuery(org.hisp.dhis.deduplication.PotentialDuplicateQuery) Arrays(java.util.Arrays) DeduplicationMergeParams(org.hisp.dhis.deduplication.DeduplicationMergeParams) DELETE(org.hisp.dhis.common.AuditType.DELETE) PotentialDuplicateStore(org.hisp.dhis.deduplication.PotentialDuplicateStore) Date(java.util.Date) AuditManager(org.hisp.dhis.artemis.audit.AuditManager) LocalDateTime(java.time.LocalDateTime) UPDATE(org.hisp.dhis.common.AuditType.UPDATE) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstanceStore(org.hisp.dhis.trackedentity.TrackedEntityInstanceStore) AuditType(org.hisp.dhis.audit.AuditType) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DeduplicationStatus(org.hisp.dhis.deduplication.DeduplicationStatus) TrackedEntityAttributeValueAuditService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAuditService) HibernateIdentifiableObjectStore(org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore) CREATE(org.hisp.dhis.common.AuditType.CREATE) Map(java.util.Map) NativeQuery(org.hibernate.query.NativeQuery) Query(org.hibernate.query.Query) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) BigInteger(java.math.BigInteger) AuditScope(org.hisp.dhis.audit.AuditScope) Repository(org.springframework.stereotype.Repository) MergeObject(org.hisp.dhis.deduplication.MergeObject) CHANGELOG_TRACKER(org.hisp.dhis.external.conf.ConfigurationKey.CHANGELOG_TRACKER) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) TrackedEntityAttributeValueAudit(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) PotentialDuplicateConflictException(org.hisp.dhis.deduplication.PotentialDuplicateConflictException) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) SessionFactory(org.hibernate.SessionFactory) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Audit(org.hisp.dhis.artemis.audit.Audit) Collectors(java.util.stream.Collectors) List(java.util.List) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AclService(org.hisp.dhis.security.acl.AclService) Optional(java.util.Optional) Collections(java.util.Collections) AuditableEntity(org.hisp.dhis.artemis.audit.AuditableEntity) Relationship(org.hisp.dhis.relationship.Relationship) HashMap(java.util.HashMap) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Aggregations

BigInteger (java.math.BigInteger)2 LocalDateTime (java.time.LocalDateTime)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 SessionFactory (org.hibernate.SessionFactory)2 NativeQuery (org.hibernate.query.NativeQuery)2 Query (org.hibernate.query.Query)2 Audit (org.hisp.dhis.artemis.audit.Audit)2 AuditManager (org.hisp.dhis.artemis.audit.AuditManager)2 AuditableEntity (org.hisp.dhis.artemis.audit.AuditableEntity)2 AuditScope (org.hisp.dhis.audit.AuditScope)2 AuditType (org.hisp.dhis.audit.AuditType)2 CREATE (org.hisp.dhis.common.AuditType.CREATE)2 DELETE (org.hisp.dhis.common.AuditType.DELETE)2