use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method saveProgramStage.
@Override
public String saveProgramStage(org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage, int patientId, int orgUnitId) throws NotAllowedException {
if (mobileProgramStage.isSingleEvent()) {
TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
ProgramStageInstance prStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
ProgramStage programStage = programStageService.getProgramStage(prStageInstance.getProgramStage().getId());
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
// ---------------------------------------------------------------------
// Add a new program-instance
//
// ---------------------------------------------------------------------
ProgramInstance programInstance = new ProgramInstance();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setProgram(programStage.getProgram());
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstance.setEntityInstance(patient);
programInstanceService.addProgramInstance(programInstance);
// ---------------------------------------------------------------------
// Add a new program-stage-instance
//
// ---------------------------------------------------------------------
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setProgramStage(programStage);
programStageInstance.setDueDate(new Date());
programStageInstance.setExecutionDate(new Date());
programStageInstance.setOrganisationUnit(organisationUnit);
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstanceService.addProgramStageInstance(programStageInstance);
// ---------------------------------------------------------------------
// Save value
//
// ---------------------------------------------------------------------
List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
String value = dataElement1.getValue();
if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
value = PeriodUtil.convertDateFormat(value);
}
TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue();
patientDataValue.setDataElement(dataElement);
patientDataValue.setValue(value);
patientDataValue.setProgramStageInstance(programStageInstance);
patientDataValue.setLastUpdated(new Date());
dataValueService.saveTrackedEntityDataValue(patientDataValue);
}
return SINGLE_EVENT_UPLOADED;
} else {
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
try {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
programStageInstance.setOrganisationUnit(organisationUnit);
} catch (Exception e) {
programStageInstance.setOrganisationUnit(null);
}
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
String value = dataElement1.getValue();
if (value != null) {
if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
value = PeriodUtil.convertDateFormat(value);
}
TrackedEntityDataValue previousPatientDataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
if (previousPatientDataValue == null) {
TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
dataValueService.saveTrackedEntityDataValue(patientDataValue);
} else {
previousPatientDataValue.setValue(value);
previousPatientDataValue.setLastUpdated(new Date());
previousPatientDataValue.setProvidedElsewhere(false);
dataValueService.updateTrackedEntityDataValue(previousPatientDataValue);
}
}
}
if (DateUtils.getMediumDate(mobileProgramStage.getReportDate()) != null) {
programStageInstance.setExecutionDate(DateUtils.getMediumDate(mobileProgramStage.getReportDate()));
} else {
programStageInstance.setExecutionDate(new Date());
}
if (programStageInstance.getProgramStage().getProgramStageDataElements().size() > dataElements.size()) {
programStageInstanceService.updateProgramStageInstance(programStageInstance);
return PROGRAM_STAGE_SECTION_UPLOADED;
} else {
if (mobileProgramStage.isCompleted()) {
programStageInstance.setStatus(EventStatus.COMPLETED);
}
programStageInstanceService.updateProgramStageInstance(programStageInstance);
// check if all belonged program stage are completed
if (!mobileProgramStage.isRepeatable() && isAllProgramStageFinished(programStageInstance) == true) {
ProgramInstance programInstance = programStageInstance.getProgramInstance();
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstanceService.updateProgramInstance(programInstance);
}
if (mobileProgramStage.isRepeatable()) {
Date nextDate = DateUtils.getDateAfterAddition(new Date(), mobileProgramStage.getStandardInterval());
return PROGRAM_STAGE_UPLOADED + "$" + DateUtils.getMediumDateString(nextDate);
} else {
return PROGRAM_STAGE_UPLOADED;
}
}
}
}
use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class PdfDataEntryFormImportUtil method insertValueProgramStageDataElement.
private void insertValueProgramStageDataElement(ProgramStageInstance programStageInstance, int dataElementId, String value) throws Exception {
DataElement dataElement = dataElementService.getDataElement(dataElementId);
TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
value = StringUtils.trimToNull(value);
if (programStageInstance.getExecutionDate() == null) {
programStageInstance.setExecutionDate(new Date());
programStageInstanceService.updateProgramStageInstance(programStageInstance);
}
// providedElsewhere = (providedElsewhere == null) ? false :
// providedElsewhere;
String storedBy = currentUserService.getCurrentUsername();
if (dataValue == null && value != null) {
// LOG.debug( "Adding TrackedEntityDataValue, value added" );
dataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
dataValue.setStoredBy(storedBy);
// dataValue.setProvidedElsewhere( providedElsewhere );
dataValueService.saveTrackedEntityDataValue(dataValue);
}
if (dataValue != null && value == null) {
dataValueService.deleteTrackedEntityDataValue(dataValue);
} else if (dataValue != null) {
dataValue.setValue(value);
dataValue.setLastUpdated(new Date());
dataValue.setStoredBy(storedBy);
dataValueService.updateTrackedEntityDataValue(dataValue);
}
}
use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class DefaultProgramDataEntryService method prepareDataEntryFormForAdd.
@Override
public String prepareDataEntryFormForAdd(String htmlCode, I18n i18n, ProgramStage programStage) {
// ---------------------------------------------------------------------
// Inline Javascript to add to HTML before outputting
// ---------------------------------------------------------------------
final String jQueryCalendar = "<script>$DATAPICKER-METHOD(\"$PROGRAMSTAGEID-$DATAELEMENTID-val\", false);</script>";
StringBuffer sb = new StringBuffer();
// ---------------------------------------------------------------------
// Pattern to match data elements in the HTML code
// ---------------------------------------------------------------------
Matcher dataElementMatcher = INPUT_PATTERN.matcher(htmlCode);
int tabindex = 0;
// ---------------------------------------------------------------------
// Iterate through all matching data element fields
// ---------------------------------------------------------------------
Map<String, ProgramStageDataElement> programStageDataElementMap = getProgramStageDataElementMap(programStage);
while (dataElementMatcher.find()) {
// -----------------------------------------------------------------
// Get HTML input field code
// -----------------------------------------------------------------
String compulsory = "null";
boolean allowProvidedElsewhere = false;
String dateMethod = "datePickerValid";
String inputHTML = dataElementMatcher.group(1);
Matcher identifierMatcher = IDENTIFIER_PATTERN_FIELD.matcher(inputHTML);
if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
// -------------------------------------------------------------
// Get ID of data element
// -------------------------------------------------------------
String programStageUid = identifierMatcher.group(1);
String dataElementUid = identifierMatcher.group(2);
DataElement dataElement = null;
String programStageName = programStage.getDisplayName();
if (!programStageUid.equals(programStage.getUid())) {
dataElement = dataElementService.getDataElement(dataElementUid);
ProgramStage otherProgramStage = programStageService.getProgramStage(programStageUid);
programStageName = otherProgramStage != null ? otherProgramStage.getDisplayName() : "N/A";
} else {
ProgramStageDataElement psde = programStageDataElementMap.get(dataElementUid);
if (psde == null) {
return i18n.getString("some_data_element_not_exist");
}
dataElement = psde.getDataElement();
compulsory = BooleanUtils.toStringTrueFalse(psde.isCompulsory());
allowProvidedElsewhere = psde.getAllowProvidedElsewhere();
if (psde.getAllowFutureDate()) {
dateMethod = "datePicker";
}
}
if (dataElement == null) {
continue;
}
// -------------------------------------------------------------
// Find existing value of data element in data set
// -------------------------------------------------------------
TrackedEntityDataValue entityInstanceDataValue = null;
String dataElementValue = EMPTY;
if (inputHTML.contains("title=\"\"")) {
inputHTML = inputHTML.replace("title=\"\"", "title=\"[ " + dataElement.getUid() + " - " + dataElement.getName() + " - " + dataElement.getValueType() + " ]\" ");
} else {
inputHTML += "title=\"[ " + dataElement.getUid() + " - " + dataElement.getName() + " - " + dataElement.getValueType() + " ]\" ";
}
// -------------------------------------------------------------
// Set field for dataElement
// -------------------------------------------------------------
tabindex++;
ValueType valueType = dataElement.getValueType();
if (valueType.isText() || valueType.isNumeric() || ValueType.USERNAME == valueType) {
inputHTML = populateCustomDataEntryForTextBox(dataElement, inputHTML, dataElementValue);
} else if (ValueType.DATE == valueType || ValueType.DATETIME == valueType) {
inputHTML = populateCustomDataEntryForDate(inputHTML, dataElementValue);
} else if (ValueType.TRUE_ONLY == valueType) {
inputHTML = populateCustomDataEntryForTrueOnly(dataElement, inputHTML, dataElementValue);
} else if (ValueType.BOOLEAN == valueType) {
inputHTML = populateCustomDataEntryForBoolean(dataElement, inputHTML, dataElementValue, i18n);
}
// -----------------------------------------------------------
// Check if this dataElement is from another programStage then
// disable
// If programStagsInstance is completed then disabled it
// -----------------------------------------------------------
String disabled = "";
if (!programStageUid.equals(programStage.getUid())) {
disabled = "disabled=\"\"";
} else {
if (ValueType.DATE == valueType || ValueType.DATETIME == valueType) {
inputHTML += jQueryCalendar;
}
if (allowProvidedElsewhere) {
// Add ProvidedByOtherFacility checkbox
inputHTML = addProvidedElsewherCheckbox(inputHTML, entityInstanceDataValue, programStage);
}
}
inputHTML = inputHTML.replace("$DATAELEMENTID", String.valueOf(dataElementUid));
inputHTML = inputHTML.replace("$VALUE", dataElementValue);
inputHTML = inputHTML.replace("$PROGRAMSTAGEID", String.valueOf(programStageUid));
inputHTML = inputHTML.replace("$PROGRAMSTAGENAME", programStageName);
inputHTML = inputHTML.replace("$DATAELEMENTNAME", dataElement.getName());
inputHTML = inputHTML.replace("$DATAELEMENTTYPE", dataElement.getValueType().toString());
inputHTML = inputHTML.replace("$DISABLED", disabled);
inputHTML = inputHTML.replace("$COMPULSORY", compulsory);
inputHTML = inputHTML.replace("$SAVEMODE", "false");
inputHTML = inputHTML.replace("$TABINDEX", tabindex + "");
inputHTML = inputHTML.replace("$DATAPICKER-METHOD", dateMethod);
inputHTML = inputHTML.replaceAll("\\$", "\\\\\\$");
dataElementMatcher.appendReplacement(sb, inputHTML);
}
}
dataElementMatcher.appendTail(sb);
return populateI18nStrings(sb.toString(), i18n);
}
use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class AbstractEventService method convertProgramStageInstance.
private Event convertProgramStageInstance(ProgramStageInstance programStageInstance) {
if (programStageInstance == null) {
return null;
}
Event event = new Event();
event.setEvent(programStageInstance.getUid());
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
event.setFollowup(programStageInstance.getProgramInstance().getFollowup());
event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(programStageInstance.getProgramInstance().getStatus()));
event.setStatus(programStageInstance.getStatus());
event.setEventDate(DateUtils.getIso8601NoTz(programStageInstance.getExecutionDate()));
event.setDueDate(DateUtils.getIso8601NoTz(programStageInstance.getDueDate()));
event.setStoredBy(programStageInstance.getStoredBy());
event.setCompletedBy(programStageInstance.getCompletedBy());
event.setCompletedDate(DateUtils.getIso8601NoTz(programStageInstance.getCompletedDate()));
event.setCreated(DateUtils.getIso8601NoTz(programStageInstance.getCreated()));
event.setCreatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getCreatedAtClient()));
event.setLastUpdated(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdated()));
event.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programStageInstance.getLastUpdatedAtClient()));
UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
OrganisationUnit ou = programStageInstance.getOrganisationUnit();
if (ou != null) {
if (!organisationUnitService.isInUserHierarchy(ou)) {
if (!userCredentials.isSuper() && !userCredentials.isAuthorized("F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS")) {
throw new IllegalQueryException("User has no access to organisation unit: " + ou.getUid());
}
}
event.setOrgUnit(ou.getUid());
event.setOrgUnitName(ou.getName());
}
Program program = programStageInstance.getProgramInstance().getProgram();
if (!userCredentials.isSuper() && !userCredentials.getAllPrograms().contains(program)) {
throw new IllegalQueryException("User has no access to program: " + program.getUid());
}
event.setProgram(program.getUid());
event.setEnrollment(programStageInstance.getProgramInstance().getUid());
event.setProgramStage(programStageInstance.getProgramStage().getUid());
event.setAttributeOptionCombo(programStageInstance.getAttributeOptionCombo().getUid());
event.setAttributeCategoryOptions(String.join(";", programStageInstance.getAttributeOptionCombo().getCategoryOptions().stream().map(DataElementCategoryOption::getUid).collect(Collectors.toList())));
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
event.setTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance().getUid());
}
if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
Coordinate coordinate = null;
if (programStageInstance.getLongitude() != null && programStageInstance.getLatitude() != null) {
coordinate = new Coordinate(programStageInstance.getLongitude(), programStageInstance.getLatitude());
try {
List<Double> list = OBJECT_MAPPER.readValue(coordinate.getCoordinateString(), new TypeReference<List<Double>>() {
});
coordinate.setLongitude(list.get(0));
coordinate.setLatitude(list.get(1));
} catch (IOException ignored) {
}
}
if (coordinate != null && coordinate.isValid()) {
event.setCoordinate(coordinate);
}
}
Collection<TrackedEntityDataValue> dataValues = dataValueService.getTrackedEntityDataValues(programStageInstance);
for (TrackedEntityDataValue dataValue : dataValues) {
DataValue value = new DataValue();
value.setCreated(DateUtils.getIso8601NoTz(dataValue.getCreated()));
value.setLastUpdated(DateUtils.getIso8601NoTz(dataValue.getLastUpdated()));
value.setDataElement(dataValue.getDataElement().getUid());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
event.getDataValues().add(value);
}
List<TrackedEntityComment> comments = programStageInstance.getComments();
for (TrackedEntityComment comment : comments) {
Note note = new Note();
note.setValue(comment.getCommentText());
note.setStoredBy(comment.getCreator());
if (comment.getCreatedDate() != null) {
note.setStoredDate(DateUtils.getIso8601NoTz(comment.getCreatedDate()));
}
event.getNotes().add(note);
}
return event;
}
use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method saveSingleEventWithoutRegistration.
@Override
public String saveSingleEventWithoutRegistration(org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage, int orgUnitId) throws NotAllowedException {
ProgramStage programStage = programStageService.getProgramStage(mobileProgramStage.getId());
Program program = programStage.getProgram();
ProgramInstance programInstance = new ProgramInstance();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setProgram(program);
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstanceService.addProgramInstance(programInstance);
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setProgramStage(programStage);
programStageInstance.setDueDate(new Date());
programStageInstance.setExecutionDate(new Date());
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstance.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
programStageInstanceService.addProgramStageInstance(programStageInstance);
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement mobileDataElement : mobileProgramStage.getDataElements()) {
TrackedEntityDataValue trackedEntityDataValue = new TrackedEntityDataValue();
trackedEntityDataValue.setDataElement(dataElementService.getDataElement(mobileDataElement.getId()));
String value = mobileDataElement.getValue();
if (value != null && !value.trim().equals("")) {
trackedEntityDataValue.setValue(value);
trackedEntityDataValue.setProgramStageInstance(programStageInstance);
trackedEntityDataValue.setProvidedElsewhere(false);
trackedEntityDataValue.setLastUpdated(new Date());
dataValueService.saveTrackedEntityDataValue(trackedEntityDataValue);
}
}
return SINGLE_EVENT_WITHOUT_REGISTRATION_UPLOADED;
}
Aggregations