use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method saveDataValues.
private void saveDataValues(ActivityValue activityValue, ProgramStageInstance programStageInstance, Map<Integer, DataElement> dataElementMap) {
org.hisp.dhis.dataelement.DataElement dataElement;
String value;
for (DataValue dv : activityValue.getDataValues()) {
value = dv.getValue();
if (value != null && value.trim().length() == 0) {
value = null;
}
if (value != null) {
value = value.trim();
}
dataElement = dataElementMap.get(dv.getId());
TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
if (dataValue == null) {
if (value != null) {
if (programStageInstance.getExecutionDate() == null) {
programStageInstance.setExecutionDate(new Date());
programStageInstanceService.updateProgramStageInstance(programStageInstance);
}
dataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
dataValueService.saveTrackedEntityDataValue(dataValue);
}
} else {
if (programStageInstance.getExecutionDate() == null) {
programStageInstance.setExecutionDate(new Date());
programStageInstanceService.updateProgramStageInstance(programStageInstance);
}
dataValue.setValue(value);
dataValue.setLastUpdated(new Date());
dataValueService.updateTrackedEntityDataValue(dataValue);
}
}
}
use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method getDataElementsForMobile.
private List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> getDataElementsForMobile(ProgramStage programStage, ProgramStageInstance programStageInstance) {
List<ProgramStageDataElement> programStageDataElements = new ArrayList<>(programStage.getProgramStageDataElements());
List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> mobileDataElements = new ArrayList<>();
for (ProgramStageDataElement programStageDataElement : programStageDataElements) {
org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement mobileDataElement = new org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement();
mobileDataElement.setId(programStageDataElement.getDataElement().getId());
String dataElementName;
if (programStageDataElement.getDataElement().getFormName() != null && !programStageDataElement.getDataElement().getFormName().trim().equals("")) {
dataElementName = programStageDataElement.getDataElement().getFormName();
} else {
dataElementName = programStageDataElement.getDataElement().getName();
}
mobileDataElement.setName(dataElementName);
mobileDataElement.setType(programStageDataElement.getDataElement().getValueType());
// problem
mobileDataElement.setCompulsory(programStageDataElement.isCompulsory());
// mobileDataElement.setNumberType(
// programStageDataElement.getDataElement().getNumberType() );
// Value
TrackedEntityDataValue patientDataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, programStageDataElement.getDataElement());
if (patientDataValue != null) {
// Convert to standard date format before send to client
if (ValueType.DATE == programStageDataElement.getDataElement().getValueType()) {
mobileDataElement.setValue(PeriodUtil.convertDateFormat(patientDataValue.getValue()));
} else {
mobileDataElement.setValue(patientDataValue.getValue());
}
} else {
mobileDataElement.setValue(null);
}
// Option set
if (programStageDataElement.getDataElement().getOptionSet() != null) {
mobileDataElement.setOptionSet(ModelMapping.getOptionSet(programStageDataElement.getDataElement()));
} else {
mobileDataElement.setOptionSet(null);
}
// Category Option Combo
if (programStageDataElement.getDataElement().getCategoryCombos() != null) {
mobileDataElement.setCategoryOptionCombos(ModelMapping.getCategoryOptionCombos(programStageDataElement.getDataElement()));
} else {
mobileDataElement.setCategoryOptionCombos(null);
}
mobileDataElements.add(mobileDataElement);
}
return mobileDataElements;
}
use of org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue in project dhis2-core by dhis2.
the class DefaultProgramDataEntryService method prepareDataEntryFormForEntry.
// -------------------------------------------------------------------------
// Implementation methods
// -------------------------------------------------------------------------
@Override
public String prepareDataEntryFormForEntry(String htmlCode, Collection<TrackedEntityDataValue> dataValues, I18n i18n, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit) {
Map<String, Collection<TrackedEntityDataValue>> mapDataValue = new HashMap<>();
// ---------------------------------------------------------------------
// Inline Javascript to add to HTML before outputting
// ---------------------------------------------------------------------
final String jQueryCalendar = "<script>$DATAPICKER-METHOD(\"$PROGRAMSTAGEID-$DATAELEMENTID-val\", false, 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 data element ID of data element
// -------------------------------------------------------------
String programStageUid = identifierMatcher.group(1);
String dataElementUid = identifierMatcher.group(2);
DataElement dataElement = null;
ProgramStageDataElement programStageDataElement = 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 = programStageDataElementMap.get(dataElementUid);
dataElement = programStageDataElement.getDataElement();
if (dataElement == null) {
return i18n.getString("some_data_element_not_exist");
}
compulsory = BooleanUtils.toStringTrueFalse(programStageDataElement.isCompulsory());
allowProvidedElsewhere = programStageDataElement.getAllowProvidedElsewhere();
if (programStageDataElement.getAllowFutureDate()) {
dateMethod = "datePicker";
}
}
if (dataElement == null) {
continue;
}
// -------------------------------------------------------------
// Find existing value of data element in data set
// -------------------------------------------------------------
TrackedEntityDataValue entityInstanceDataValue = null;
String dataElementValue = EMPTY;
if (!programStageUid.equals(programStage.getUid())) {
Collection<TrackedEntityDataValue> entityInstanceDataValues = mapDataValue.get(programStageUid);
if (entityInstanceDataValues == null && programStageInstance != null) {
ProgramStage otherProgramStage = programStageService.getProgramStage(programStageUid);
ProgramStageInstance otherProgramStageInstance = programStageInstanceService.getProgramStageInstance(programStageInstance.getProgramInstance(), otherProgramStage);
entityInstanceDataValues = dataValueService.getTrackedEntityDataValues(otherProgramStageInstance);
mapDataValue.put(programStageUid, entityInstanceDataValues);
}
entityInstanceDataValue = getValue(entityInstanceDataValues, dataElementUid);
dataElementValue = entityInstanceDataValue != null ? entityInstanceDataValue.getValue() : dataElementValue;
} else {
entityInstanceDataValue = getValue(dataValues, dataElementUid);
dataElementValue = entityInstanceDataValue != null ? entityInstanceDataValue.getValue() : dataElementValue;
}
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 (dataElement.getOptionSet() != null && dataElement.getOptionSet().getOptions().size() < 7) {
if (programStageDataElement != null && programStageDataElement.getRenderOptionsAsRadio()) {
String idField = programStageUid + "-" + dataElementUid + "-val";
inputHTML = populateCustomDataEntryForOptionSet(dataElement, idField, entityInstanceDataValue, i18n);
}
} else 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.getFormNameFallback());
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 SingleEventListener method registerEvent.
// -------------------------------------------------------------------------
// Supportive Methods
// -------------------------------------------------------------------------
private void registerEvent(Map<String, String> commandValuePairs, SMSCommand smsCommand, IncomingSms sms) {
OrganisationUnit orgUnit = getOrganisationUnits(sms).iterator().next();
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(smsCommand.getProgram(), ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(smsCommand.getProgram());
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
update(sms, SmsMessageStatus.FAILED, false);
sendFeedback("Multiple active program instances exists for program: " + smsCommand.getProgram().getUid(), sms.getOriginator(), ERROR);
return;
}
ProgramInstance programInstance = null;
programInstance = programInstances.get(0);
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setOrganisationUnit(orgUnit);
programStageInstance.setProgramStage(smsCommand.getProgramStage());
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setExecutionDate(sms.getSentDate());
programStageInstance.setDueDate(sms.getSentDate());
programStageInstance.setAttributeOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
programStageInstance.setCompletedBy("DHIS 2");
programStageInstanceService.addProgramStageInstance(programStageInstance);
for (SMSCode smsCode : smsCommand.getCodes()) {
TrackedEntityDataValue dataValue = new TrackedEntityDataValue();
dataValue.setAutoFields();
dataValue.setDataElement(smsCode.getDataElement());
dataValue.setProgramStageInstance(programStageInstance);
dataValue.setValue(commandValuePairs.get(smsCode.getCode()));
trackedEntityDataValueService.saveTrackedEntityDataValue(dataValue);
}
update(sms, SmsMessageStatus.PROCESSED, true);
sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getSuccessMessage(), EVENT_REGISTERED), sms.getOriginator(), INFO);
}
Aggregations