use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method getTrackedEntityInstance.
public org.hisp.dhis.trackedentity.TrackedEntityInstance getTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions, ImportSummary importSummary) {
if (StringUtils.isEmpty(trackedEntityInstance.getOrgUnit())) {
importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "No org unit ID in tracked entity instance object."));
return null;
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = new org.hisp.dhis.trackedentity.TrackedEntityInstance();
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), trackedEntityInstance.getOrgUnit());
if (organisationUnit == null) {
importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "Invalid org unit ID: " + trackedEntityInstance.getOrgUnit()));
return null;
}
entityInstance.setOrganisationUnit(organisationUnit);
TrackedEntity trackedEntity = getTrackedEntity(importOptions.getIdSchemes(), trackedEntityInstance.getTrackedEntity());
if (trackedEntity == null) {
importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "Invalid tracked entity ID: " + trackedEntityInstance.getTrackedEntity()));
return null;
}
entityInstance.setTrackedEntity(trackedEntity);
entityInstance.setUid(CodeGenerator.isValidUid(trackedEntityInstance.getTrackedEntityInstance()) ? trackedEntityInstance.getTrackedEntityInstance() : CodeGenerator.generateUid());
return entityInstance;
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addTrackedEntityInstances.
// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries addTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummaries importSummaries = new ImportSummaries();
int counter = 0;
for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
importSummaries.addImportSummary(addTrackedEntityInstance(trackedEntityInstance, importOptions));
if (counter % FLUSH_FREQUENCY == 0) {
clearSession();
}
counter++;
}
return importSummaries;
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.
@Override
public ImportSummary updateTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
trackedEntityInstance.trimValuesToNull();
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkRelationships(trackedEntityInstance));
importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, trackedEntityInstance.getTrackedEntityInstance());
if (entityInstance == null) {
importConflicts.add(new ImportConflict("TrackedEntityInstance", "trackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " does not point to valid trackedEntityInstance"));
}
OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, trackedEntityInstance.getOrgUnit());
if (organisationUnit == null) {
importConflicts.add(new ImportConflict("OrganisationUnit", "orgUnit " + trackedEntityInstance.getOrgUnit() + " does not point to valid organisation unit"));
}
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
entityInstance.setInactive(trackedEntityInstance.isInactive());
removeRelationships(entityInstance);
removeAttributeValues(entityInstance);
teiService.updateTrackedEntityInstance(entityInstance);
updateRelationships(trackedEntityInstance, entityInstance);
updateAttributeValues(trackedEntityInstance, entityInstance);
updateDateFields(trackedEntityInstance, entityInstance);
teiService.updateTrackedEntityInstance(entityInstance);
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setReference(entityInstance.getUid());
importSummary.getImportCount().incrementUpdated();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
return importSummary;
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class JacksonTrackedEntityInstanceService method addTrackedEntityInstanceList.
private ImportSummaries addTrackedEntityInstanceList(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
List<TrackedEntityInstance> create = new ArrayList<>();
List<TrackedEntityInstance> update = new ArrayList<>();
List<String> delete = new ArrayList<>();
if (importOptions.getImportStrategy().isCreate()) {
create.addAll(trackedEntityInstances);
} else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
if (StringUtils.isEmpty(trackedEntityInstance.getTrackedEntityInstance())) {
create.add(trackedEntityInstance);
} else {
if (!teiService.trackedEntityInstanceExists(trackedEntityInstance.getTrackedEntityInstance())) {
create.add(trackedEntityInstance);
} else {
update.add(trackedEntityInstance);
}
}
}
} else if (importOptions.getImportStrategy().isUpdate()) {
update.addAll(trackedEntityInstances);
} else if (importOptions.getImportStrategy().isDelete()) {
delete.addAll(trackedEntityInstances.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
}
importSummaries.addImportSummaries(addTrackedEntityInstances(create, importOptions));
importSummaries.addImportSummaries(updateTrackedEntityInstances(update, importOptions));
importSummaries.addImportSummaries(deleteTrackedEntityInstances(delete));
return importSummaries;
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractEventService method addEvent.
protected ImportSummary addEvent(Event event, User user, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
ProgramStage programStage = getProgramStage(importOptions.getIdSchemes().getProgramStageIdScheme(), event.getProgramStage());
ProgramInstance programInstance;
ProgramStageInstance programStageInstance = null;
if (program == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.program does not point to a valid program: " + event.getProgram()).incrementIgnored();
}
if (programStage == null && program.isRegistration()) {
return new ImportSummary(ImportStatus.ERROR, "Event.programStage does not point to a valid programStage, and program is multi stage: " + event.getProgramStage()).incrementIgnored();
} else if (programStage == null) {
programStage = program.getProgramStageByStage(1);
}
Assert.notNull(programStage, "Program stage cannot be null");
if (!canAccess(program, user)) {
return new ImportSummary(ImportStatus.ERROR, "Current user does not have permission to access this program").incrementIgnored();
}
if (program.isRegistration()) {
if (event.getTrackedEntityInstance() == null) {
return new ImportSummary(ImportStatus.ERROR, "No Event.trackedEntityInstance was provided for registration based program").incrementIgnored();
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(event.getTrackedEntityInstance());
if (entityInstance == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.trackedEntityInstance does not point to a valid tracked entity instance: " + event.getTrackedEntityInstance()).incrementIgnored();
}
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(entityInstance, program, ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " is not enrolled in program: " + program.getUid()).incrementIgnored();
} else if (programInstances.size() > 1) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " have multiple active enrollments in program: " + program.getUid()).incrementIgnored();
}
programInstance = programInstances.get(0);
if (!programStage.getRepeatable()) {
programStageInstance = programStageInstanceService.getProgramStageInstance(programInstance, programStage);
if (programStageInstance != null && !programStageInstance.getUid().equals(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").incrementIgnored();
}
} else {
if (event.getEvent() != null) {
programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
if (programStageInstance == null) {
if (!CodeGenerator.isValidUid(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
}
}
}
}
} else {
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(program, ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
// Create PI if it doesn't exist (should only be one)
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(program);
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
return new ImportSummary(ImportStatus.ERROR, "Multiple active program instances exists for program: " + program.getUid()).incrementIgnored();
}
programInstance = programInstances.get(0);
if (event.getEvent() != null) {
programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
if (programStageInstance == null) {
if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.UID) && !CodeGenerator.isValidUid(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
}
}
}
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
program = programInstance.getProgram();
if (programStageInstance != null) {
programStage = programStageInstance.getProgramStage();
}
if (organisationUnit == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit: " + event.getOrgUnit()).incrementIgnored();
}
if (!programInstance.getProgram().hasOrganisationUnit(organisationUnit)) {
return new ImportSummary(ImportStatus.ERROR, "Program is not assigned to this organisation unit: " + event.getOrgUnit()).incrementIgnored();
}
validateExpiryDays(event, program, null);
return saveEvent(program, programInstance, programStage, programStageInstance, organisationUnit, event, user, importOptions);
}
Aggregations