use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultPdfDataEntryFormService method insertTable_ProgramStageSections.
private void insertTable_ProgramStageSections(PdfPTable mainTable, Rectangle rectangle, PdfWriter writer, Collection<DataElement> dataElements) throws IOException, DocumentException {
boolean hasBorder = false;
// Add one to column count due to date entry + one hidden height set
// field.
int colCount = dataElements.size() + 1 + 1;
// Code 1
PdfPTable table = new PdfPTable(colCount);
float totalWidth = 800f;
float firstCellWidth_dateEntry = PdfDataEntryFormUtil.UNITSIZE_DEFAULT * 3;
float lastCellWidth_hidden = PdfDataEntryFormUtil.UNITSIZE_DEFAULT;
float dataElementCellWidth = (totalWidth - firstCellWidth_dateEntry - lastCellWidth_hidden) / dataElements.size();
// Create 2 types of Rectangles, one for Date field, one for data
// elements - to be used when rendering them.
Rectangle rectangleDate = new Rectangle(0, 0, PdfDataEntryFormUtil.UNITSIZE_DEFAULT * 2, PdfDataEntryFormUtil.UNITSIZE_DEFAULT);
Rectangle rectangleDataElement = new Rectangle(0, 0, dataElementCellWidth, PdfDataEntryFormUtil.UNITSIZE_DEFAULT);
// Cell Width Set
float[] cellWidths = new float[colCount];
// Date Field Settings
cellWidths[0] = firstCellWidth_dateEntry;
for (int i = 1; i < colCount - 1; i++) {
cellWidths[i] = dataElementCellWidth;
}
cellWidths[colCount - 1] = lastCellWidth_hidden;
table.setWidths(cellWidths);
// Create Header
addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), "Date", Element.ALIGN_CENTER);
// Add Program Data Elements Columns
for (DataElement dataElement : dataElements) {
addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), dataElement.getFormNameFallback(), Element.ALIGN_CENTER);
}
addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), TEXT_BLANK, Element.ALIGN_CENTER);
for (int rowNo = 1; rowNo <= PROGRAM_FORM_ROW_NUMBER; rowNo++) {
// Add Date Column
String strFieldDateLabel = PdfDataEntryFormUtil.LABELCODE_DATADATETEXTFIELD + Integer.toString(rowNo);
addCell_WithTextField(table, rectangleDate, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldDateLabel);
// Add Program Data Elements Columns
for (DataElement dataElement : dataElements) {
OptionSet optionSet = dataElement.getOptionSet();
String strFieldLabel = PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD + Integer.toString(dataElement.getId()) + // Integer.toString(rowNo);
"_" + Integer.toString(rowNo);
if (optionSet != null) {
// Get All Option
String query = "";
// TODO: This gets repeated <- Create an array of the
// options. and apply only once.
List<Option> options = optionService.getOptions(optionSet.getId(), query, MAX_OPTIONS_DISPLAYED);
addCell_WithDropDownListField(table, rectangleDataElement, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldLabel, options.toArray(new String[0]), options.toArray(new String[0]));
} else {
// NOTE: When Rendering for DataSet, DataElement's OptionSet
// does not get rendered.
// Only for events, it gets rendered as dropdown list.
addCell_WithTextField(table, rectangleDataElement, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldLabel);
}
}
addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), TEXT_BLANK, Element.ALIGN_LEFT);
}
PdfPCell cell_withInnerTable = new PdfPCell(table);
cell_withInnerTable.setBorder(Rectangle.NO_BORDER);
mainTable.addCell(cell_withInnerTable);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultPdfDataEntryFormService method insertTable_DataSetSections.
private void insertTable_DataSetSections(PdfPTable mainTable, PdfWriter writer, Rectangle rectangle, Collection<DataElement> dataElements, String sectionName) throws IOException, DocumentException {
boolean hasBorder = true;
// Add Section Name and Section Spacing
insertTable_TextRow(writer, mainTable, TEXT_BLANK);
if (sectionName != null && !sectionName.isEmpty()) {
insertTable_TextRow(writer, mainTable, sectionName, pdfFormFontSettings.getFont(PdfFormFontSettings.FONTTYPE_SECTIONHEADER));
}
// Create A Table To Add For Each Section
PdfPTable table = new PdfPTable(2);
table.setWidths(new int[] { 2, 1 });
table.setWidthPercentage(100.0f);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
// row.
for (DataElement dataElement : dataElements) {
for (DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getSortedCategoryOptionCombos()) {
String categoryOptionComboDisplayName = "";
// Hide Default category option combo name
if (!categoryOptionCombo.isDefault()) {
categoryOptionComboDisplayName = categoryOptionCombo.getDisplayName();
}
addCell_Text(table, PdfDataEntryFormUtil.getPdfPCell(hasBorder), dataElement.getFormNameFallback() + " " + categoryOptionComboDisplayName, Element.ALIGN_RIGHT);
String strFieldLabel = PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD + dataElement.getUid() + "_" + categoryOptionCombo.getUid();
ValueType valueType = dataElement.getValueType();
// Yes Only case - render as check-box
if (ValueType.TRUE_ONLY == valueType) {
addCell_WithCheckBox(table, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldLabel);
} else if (ValueType.BOOLEAN == valueType) {
// Create Yes - true, No - false, Select..
String[] optionList = new String[] { "[No Value]", "Yes", "No" };
String[] valueList = new String[] { "", "true", "false" };
// addCell_WithRadioButton(table, writer, strFieldLabel);
addCell_WithDropDownListField(table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldLabel, optionList, valueList);
} else if (valueType.isNumeric()) {
Rectangle rectNum = new Rectangle(TEXTBOXWIDTH_NUMBERTYPE, PdfDataEntryFormUtil.CONTENT_HEIGHT_DEFAULT);
addCell_WithTextField(table, rectNum, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldLabel, PdfFieldCell.TYPE_TEXT_NUMBER);
} else {
addCell_WithTextField(table, rectangle, writer, PdfDataEntryFormUtil.getPdfPCell(hasBorder), strFieldLabel);
}
}
}
PdfPCell cell_withInnerTable = new PdfPCell(table);
cell_withInnerTable.setBorder(Rectangle.NO_BORDER);
mainTable.addCell(cell_withInnerTable);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class ValidationUtilsTest method testDataValueIsZeroAndInsignificant.
@Test
public void testDataValueIsZeroAndInsignificant() {
DataElement de = new DataElement("DEA");
de.setValueType(ValueType.INTEGER);
de.setAggregationType(AggregationType.SUM);
assertTrue(dataValueIsZeroAndInsignificant("0", de));
de.setAggregationType(AggregationType.AVERAGE_SUM_ORG_UNIT);
assertFalse(dataValueIsZeroAndInsignificant("0", de));
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DhisConvenienceTest method createDataElement.
/**
* @param uniqueCharacter A unique character to identify the object.
* @param valueType The value type.
* @param aggregationType The aggregation type.
*/
public static DataElement createDataElement(char uniqueCharacter, ValueType valueType, AggregationType aggregationType) {
DataElement dataElement = createDataElement(uniqueCharacter);
dataElement.setValueType(valueType);
dataElement.setAggregationType(aggregationType);
return dataElement;
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DataValueController method saveDataValue.
// ---------------------------------------------------------------------
// POST
// ---------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void saveDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String value, @RequestParam(required = false) String comment, @RequestParam(required = false) boolean followUp, HttpServletResponse response) throws WebMessageException {
boolean strictPeriods = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
boolean strictCategoryOptionCombos = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
boolean strictOrgUnits = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
boolean requireCategoryOptionCombo = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = getAndValidateDataElement(de);
DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
Period period = getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
validateInvalidFuturePeriod(period, dataElement);
validateAttributeOptionComboWithOrgUnitAndPeriod(attributeOptionCombo, organisationUnit, period);
String valueValid = ValidationUtils.dataValueIsValid(value, dataElement);
if (valueValid != null) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid value: " + value + ", must match data element type: " + dataElement.getValueType()));
}
String commentValid = ValidationUtils.commentIsValid(comment);
if (commentValid != null) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid comment: " + comment));
}
OptionSet optionSet = dataElement.getOptionSet();
if (!Strings.isNullOrEmpty(value) && optionSet != null && !optionSet.getOptionCodesAsSet().contains(value)) {
throw new WebMessageException(WebMessageUtils.conflict("Data value is not a valid option of the data element option set: " + dataElement.getUid()));
}
if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
throw new WebMessageException(WebMessageUtils.conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
}
if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
throw new WebMessageException(WebMessageUtils.conflict("Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid()));
}
if (strictOrgUnits && !organisationUnit.hasDataElement(dataElement)) {
throw new WebMessageException(WebMessageUtils.conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
}
// ---------------------------------------------------------------------
// Locking validation
// ---------------------------------------------------------------------
validateDataSetNotLocked(dataElement, period, organisationUnit, attributeOptionCombo);
// ---------------------------------------------------------------------
// Period validation
// ---------------------------------------------------------------------
validateDataInputPeriodForDataElementAndPeriod(dataElement, period);
// ---------------------------------------------------------------------
// Assemble and save data value
// ---------------------------------------------------------------------
String storedBy = currentUserService.getCurrentUsername();
Date now = new Date();
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
FileResource fileResource = null;
if (dataValue == null) {
if (dataElement.getValueType() == ValueType.FILE_RESOURCE) {
if (value != null) {
fileResource = fileResourceService.getFileResource(value);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(WebMessageUtils.notFound(FileResource.class, value));
}
if (fileResource.isAssigned()) {
throw new WebMessageException(WebMessageUtils.conflict("File resource already assigned or linked to another data value"));
}
fileResource.setAssigned(true);
} else {
throw new WebMessageException(WebMessageUtils.conflict("Missing parameter 'value'"));
}
}
dataValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
dataValueService.addDataValue(dataValue);
} else {
if (value == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
if (comment == null) {
dataValueService.deleteDataValue(dataValue);
return;
} else {
value = "false";
}
}
if (dataElement.isFileType()) {
fileResourceService.deleteFileResource(dataValue.getValue());
}
if (value != null) {
dataValue.setValue(StringUtils.trimToNull(value));
}
if (comment != null) {
dataValue.setComment(StringUtils.trimToNull(comment));
}
if (followUp) {
dataValue.toggleFollowUp();
}
dataValue.setLastUpdated(now);
dataValue.setStoredBy(storedBy);
dataValueService.updateDataValue(dataValue);
}
if (fileResource != null) {
fileResourceService.updateFileResource(fileResource);
}
}
Aggregations