Search in sources :

Example 1 with MergeObject

use of org.hisp.dhis.deduplication.MergeObject in project dhis2-core by dhis2.

the class DeduplicationController method mergePotentialDuplicate.

@PostMapping(value = "/{id}/merge")
@ResponseStatus(value = HttpStatus.OK)
public void mergePotentialDuplicate(@PathVariable String id, @RequestParam(defaultValue = "AUTO") MergeStrategy mergeStrategy, @RequestBody(required = false) MergeObject mergeObject) throws NotFoundException, PotentialDuplicateConflictException, PotentialDuplicateForbiddenException {
    PotentialDuplicate potentialDuplicate = getPotentialDuplicateBy(id);
    if (potentialDuplicate.getOriginal() == null || potentialDuplicate.getDuplicate() == null) {
        throw new PotentialDuplicateConflictException("PotentialDuplicate is missing references and cannot be merged.");
    }
    TrackedEntityInstance original = getTei(potentialDuplicate.getOriginal());
    TrackedEntityInstance duplicate = getTei(potentialDuplicate.getDuplicate());
    if (mergeObject == null) {
        mergeObject = new MergeObject();
    }
    DeduplicationMergeParams params = DeduplicationMergeParams.builder().potentialDuplicate(potentialDuplicate).mergeObject(mergeObject).original(original).duplicate(duplicate).build();
    if (MergeStrategy.MANUAL.equals(mergeStrategy)) {
        deduplicationService.manualMerge(params);
    } else {
        deduplicationService.autoMerge(params);
    }
}
Also used : PotentialDuplicateConflictException(org.hisp.dhis.deduplication.PotentialDuplicateConflictException) MergeObject(org.hisp.dhis.deduplication.MergeObject) PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) DeduplicationMergeParams(org.hisp.dhis.deduplication.DeduplicationMergeParams) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 2 with MergeObject

use of org.hisp.dhis.deduplication.MergeObject in project dhis2-core by dhis2.

the class DeduplicationControllerMvcTest method shouldMergePotentialDuplicate.

@Test
void shouldMergePotentialDuplicate() throws Exception {
    String uid = "uid";
    PotentialDuplicate potentialDuplicate = new PotentialDuplicate(teiA, teiB);
    when(deduplicationService.getPotentialDuplicateByUid(uid)).thenReturn(potentialDuplicate);
    MergeObject mergeObject = MergeObject.builder().build();
    mockMvc.perform(post(ENDPOINT + "/" + uid + "/merge").content(objectMapper.writeValueAsString(mergeObject)).param("mergeStrategy", MergeStrategy.AUTO.name()).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    verify(deduplicationService).autoMerge(deduplicationMergeParams);
    verify(deduplicationService, times(0)).manualMerge(deduplicationMergeParams);
}
Also used : MergeObject(org.hisp.dhis.deduplication.MergeObject) PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) Test(org.junit.jupiter.api.Test)

Example 3 with MergeObject

use of org.hisp.dhis.deduplication.MergeObject in project dhis2-core by dhis2.

the class DeduplicationControllerMvcTest method shouldManualMergePotentialDuplicate.

@Test
void shouldManualMergePotentialDuplicate() throws Exception {
    String uid = "uid";
    PotentialDuplicate potentialDuplicate = new PotentialDuplicate(teiA, teiB);
    when(deduplicationService.getPotentialDuplicateByUid(uid)).thenReturn(potentialDuplicate);
    MergeObject mergeObject = MergeObject.builder().build();
    mockMvc.perform(post(ENDPOINT + "/" + uid + "/merge").content(objectMapper.writeValueAsString(mergeObject)).param("mergeStrategy", MergeStrategy.MANUAL.name()).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    verify(deduplicationService, times(0)).autoMerge(deduplicationMergeParams);
    verify(deduplicationService).manualMerge(deduplicationMergeParams);
}
Also used : MergeObject(org.hisp.dhis.deduplication.MergeObject) PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) Test(org.junit.jupiter.api.Test)

Example 4 with MergeObject

use of org.hisp.dhis.deduplication.MergeObject in project dhis2-core by dhis2.

the class DeduplicationControllerMvcTest method shouldThrowAutoMergeForbiddenException.

@Test
void shouldThrowAutoMergeForbiddenException() throws Exception {
    String uid = "uid";
    PotentialDuplicate potentialDuplicate = new PotentialDuplicate(teiA, teiB);
    when(deduplicationService.getPotentialDuplicateByUid(uid)).thenReturn(potentialDuplicate);
    doThrow(new PotentialDuplicateForbiddenException("Forbidden")).when(deduplicationService).autoMerge(deduplicationMergeParams);
    MergeObject mergeObject = MergeObject.builder().build();
    mockMvc.perform(post(ENDPOINT + "/" + uid + "/merge").content(objectMapper.writeValueAsString(mergeObject)).param("mergeStrategy", MergeStrategy.AUTO.name()).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andExpect(result -> assertTrue(result.getResolvedException() instanceof PotentialDuplicateForbiddenException));
    verify(deduplicationService).autoMerge(deduplicationMergeParams);
    verify(deduplicationService, times(0)).manualMerge(deduplicationMergeParams);
}
Also used : PotentialDuplicateForbiddenException(org.hisp.dhis.deduplication.PotentialDuplicateForbiddenException) MergeObject(org.hisp.dhis.deduplication.MergeObject) PotentialDuplicate(org.hisp.dhis.deduplication.PotentialDuplicate) Test(org.junit.jupiter.api.Test)

Example 5 with MergeObject

use of org.hisp.dhis.deduplication.MergeObject in project dhis2-core by dhis2.

the class HibernatePotentialDuplicateStore method auditMerge.

@Override
public void auditMerge(DeduplicationMergeParams params) {
    TrackedEntityInstance duplicate = params.getDuplicate();
    MergeObject mergeObject = params.getMergeObject();
    mergeObject.getRelationships().forEach(rel -> {
        duplicate.getRelationshipItems().stream().map(RelationshipItem::getRelationship).filter(r -> r.getUid().equals(rel)).findAny().ifPresent(relationship -> auditManager.send(Audit.builder().auditScope(AuditScope.TRACKER).auditType(AuditType.UPDATE).createdAt(LocalDateTime.now()).object(relationship).klass(HibernateProxyUtils.getRealClass(relationship).getCanonicalName()).uid(rel).auditableEntity(new AuditableEntity(Relationship.class, relationship)).build()));
    });
}
Also used : AuditableEntity(org.hisp.dhis.artemis.audit.AuditableEntity) MergeObject(org.hisp.dhis.deduplication.MergeObject) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Aggregations

MergeObject (org.hisp.dhis.deduplication.MergeObject)6 PotentialDuplicate (org.hisp.dhis.deduplication.PotentialDuplicate)5 Test (org.junit.jupiter.api.Test)4 PotentialDuplicateConflictException (org.hisp.dhis.deduplication.PotentialDuplicateConflictException)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 AuditableEntity (org.hisp.dhis.artemis.audit.AuditableEntity)1 DeduplicationMergeParams (org.hisp.dhis.deduplication.DeduplicationMergeParams)1 PotentialDuplicateForbiddenException (org.hisp.dhis.deduplication.PotentialDuplicateForbiddenException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1