use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class DefaultFieldFilterService method buildNode.
private AbstractNode buildNode(FieldMap fieldMap, Class<?> klass, Object object, String nodeName) {
Schema schema = schemaService.getDynamicSchema(klass);
ComplexNode complexNode = new ComplexNode(nodeName);
complexNode.setNamespace(schema.getNamespace());
if (object == null) {
return new SimpleNode(schema.getName(), null);
}
updateFields(fieldMap, schema.getKlass());
for (String fieldKey : fieldMap.keySet()) {
AbstractNode child;
Property property = schema.getProperty(fieldKey);
if (property == null || !property.isReadable()) {
// throw new FieldFilterException( fieldKey, schema );
log.debug("Unknown field property `" + fieldKey + "`, available fields are " + schema.getPropertyMap().keySet());
continue;
}
Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
Schema propertySchema = schemaService.getDynamicSchema(property.getKlass());
FieldMap fieldValue = fieldMap.get(fieldKey);
if (returnValue == null && property.isCollection()) {
continue;
}
if (property.isCollection()) {
updateFields(fieldValue, property.getItemKlass());
} else {
updateFields(fieldValue, property.getKlass());
}
if (fieldValue.isEmpty()) {
List<String> fields = Preset.defaultAssociationPreset().getFields();
if (property.isCollection()) {
Collection<?> collection = (Collection<?>) returnValue;
child = new CollectionNode(property.getCollectionName());
child.setNamespace(property.getNamespace());
if (property.isIdentifiableObject() && isProperIdObject(property.getItemKlass())) {
for (Object collectionObject : collection) {
child.addChild(getProperties(property, collectionObject, fields));
}
} else if (!property.isSimple()) {
FieldMap map = getFullFieldMap(schemaService.getDynamicSchema(property.getItemKlass()));
for (Object collectionObject : collection) {
Node node = buildNode(map, property.getItemKlass(), collectionObject);
if (!node.getChildren().isEmpty()) {
child.addChild(node);
}
}
} else {
if (collection != null) {
for (Object collectionObject : collection) {
SimpleNode simpleNode = child.addChild(new SimpleNode(property.getName(), collectionObject));
simpleNode.setProperty(property);
}
}
}
} else if (property.isIdentifiableObject() && isProperIdObject(property.getKlass())) {
child = getProperties(property, returnValue, fields);
} else {
if (propertySchema.getProperties().isEmpty()) {
SimpleNode simpleNode = new SimpleNode(fieldKey, returnValue);
simpleNode.setAttribute(property.isAttribute());
simpleNode.setNamespace(property.getNamespace());
child = simpleNode;
} else {
child = buildNode(getFullFieldMap(propertySchema), property.getKlass(), returnValue);
}
}
} else {
if (property.isCollection()) {
child = new CollectionNode(property.getCollectionName());
child.setNamespace(property.getNamespace());
for (Object collectionObject : (Collection<?>) returnValue) {
Node node = buildNode(fieldValue, property.getItemKlass(), collectionObject, property.getName());
if (!node.getChildren().isEmpty()) {
child.addChild(node);
}
}
} else {
child = buildNode(fieldValue, property.getKlass(), returnValue);
}
}
if (child != null) {
child.setName(fieldKey);
child.setProperty(property);
// TODO fix ugly hack, will be replaced by custom field serializer/deserializer
if (child.isSimple() && PeriodType.class.isInstance((((SimpleNode) child).getValue()))) {
child = new SimpleNode(child.getName(), ((PeriodType) ((SimpleNode) child).getValue()).getName());
}
complexNode.addChild(fieldValue.getPipeline().process(child));
}
}
return complexNode;
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class FacilityReportingServiceImpl method getMobileDataSetsForUnit.
// -------------------------------------------------------------------------
// Service methods
// -------------------------------------------------------------------------
@Override
public List<DataSet> getMobileDataSetsForUnit(OrganisationUnit unit, String localeString) {
List<DataSet> datasets = new ArrayList<>();
Locale locale = LocaleUtil.getLocale(localeString);
if (DEBUG)
log.debug("Getting data sets for unit " + unit.getName());
for (org.hisp.dhis.dataset.DataSet dataSet : dataSetService.getDataSetsForMobile(unit)) {
PeriodType periodType = dataSet.getPeriodType();
if (periodType instanceof DailyPeriodType || periodType instanceof WeeklyPeriodType || periodType instanceof MonthlyPeriodType || periodType instanceof YearlyPeriodType || periodType instanceof QuarterlyPeriodType) {
if (DEBUG)
log.debug("Found data set " + dataSet.getName());
datasets.add(getDataSetForLocale(dataSet.getId(), locale));
} else {
log.warn("Dataset '" + dataSet.getName() + "' set to be reported from mobile, but not of a supported period type: " + periodType.getName());
}
}
return datasets;
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class AbstractEventService method validateExpiryDays.
private void validateExpiryDays(Event event, Program program, ProgramStageInstance programStageInstance) {
if (program != null) {
if (program.getCompleteEventsExpiryDays() > 0) {
if (event.getStatus() == EventStatus.COMPLETED || programStageInstance != null && programStageInstance.getStatus() == EventStatus.COMPLETED) {
Date referenceDate = null;
if (programStageInstance != null) {
referenceDate = programStageInstance.getCompletedDate();
} else {
if (event.getCompletedDate() != null) {
referenceDate = DateUtils.parseDate(event.getCompletedDate());
}
}
if (referenceDate == null) {
throw new IllegalQueryException("Event needs to have completed date.");
}
if ((new Date()).after(DateUtils.getDateAfterAddition(referenceDate, program.getCompleteEventsExpiryDays()))) {
throw new IllegalQueryException("The event's completness date has expired. Not possible to make changes to this event");
}
}
}
PeriodType periodType = program.getExpiryPeriodType();
if (periodType != null && program.getExpiryDays() > 0) {
if (programStageInstance != null) {
Date today = new Date();
if (programStageInstance.getExecutionDate() == null) {
throw new IllegalQueryException("Event needs to have event date.");
}
Period period = periodType.createPeriod(programStageInstance.getExecutionDate());
if (today.after(DateUtils.getDateAfterAddition(period.getEndDate(), program.getExpiryDays()))) {
throw new IllegalQueryException("The program's expiry date has passed. It is not possible to make changes to this event.");
}
} else {
String referenceDate = event.getEventDate() != null ? event.getEventDate() : event.getDueDate() != null ? event.getDueDate() : null;
if (referenceDate == null) {
throw new IllegalQueryException("Event needs to have at least one (event or schedule) date. ");
}
Period period = periodType.createPeriod(new Date());
if (DateUtils.parseDate(referenceDate).before(period.getStartDate())) {
throw new IllegalQueryException("The event's date belongs to an expired period. It is not possble to create such event.");
}
}
}
}
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class DefaultPredictorService method getSamplePeriodsMap.
/**
* Creates a map relating each output period to a list of sample periods
* from which the sample data is to be drawn.
*
* @param outputPeriods the output periods
* @param predictor the predictor
* @return map from output periods to sample periods
*/
private ListMap<Period, Period> getSamplePeriodsMap(List<Period> outputPeriods, Predictor predictor) {
int sequentialCount = predictor.getSequentialSampleCount();
int annualCount = predictor.getAnnualSampleCount();
int skipCount = firstNonNull(predictor.getSequentialSkipCount(), 0);
PeriodType periodType = predictor.getPeriodType();
ListMap<Period, Period> samplePeriodsMap = new ListMap<Period, Period>();
for (Period outputPeriod : outputPeriods) {
samplePeriodsMap.put(outputPeriod, new ArrayList<Period>());
Period p = periodType.getPreviousPeriod(outputPeriod, skipCount);
for (int i = skipCount; i < sequentialCount; i++) {
p = periodType.getPreviousPeriod(p);
addPeriod(samplePeriodsMap, outputPeriod, p);
}
for (int year = 1; year <= annualCount; year++) {
Period pPrev = periodType.getPreviousYearsPeriod(outputPeriod, year);
Period pNext = pPrev;
addPeriod(samplePeriodsMap, outputPeriod, pPrev);
for (int i = 0; i < sequentialCount; i++) {
pPrev = periodType.getPreviousPeriod(pPrev);
pNext = periodType.getNextPeriod(pNext);
addPeriod(samplePeriodsMap, outputPeriod, pPrev);
addPeriod(samplePeriodsMap, outputPeriod, pNext);
}
}
}
return samplePeriodsMap;
}
use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.
the class AddProgramStageAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
minDaysFromStart = (minDaysFromStart == null) ? 0 : minDaysFromStart;
repeatable = (repeatable == null) ? false : repeatable;
autoGenerateEvent = (autoGenerateEvent == null) ? false : autoGenerateEvent;
validCompleteOnly = (validCompleteOnly == null) ? false : validCompleteOnly;
displayGenerateEventBox = (displayGenerateEventBox == null) ? false : displayGenerateEventBox;
captureCoordinates = (captureCoordinates == null) ? false : captureCoordinates;
relatedEntityInstance = (relatedEntityInstance == null) ? false : relatedEntityInstance;
generatedByEnrollmentDate = (generatedByEnrollmentDate == null) ? false : generatedByEnrollmentDate;
blockEntryForm = (blockEntryForm == null) ? false : blockEntryForm;
remindCompleted = (remindCompleted == null) ? false : remindCompleted;
allowGenerateNextVisit = (allowGenerateNextVisit == null) ? false : allowGenerateNextVisit;
openAfterEnrollment = (openAfterEnrollment == null) ? false : openAfterEnrollment;
preGenerateUID = (preGenerateUID == null) ? false : preGenerateUID;
hideDueDate = (hideDueDate == null) ? false : hideDueDate;
ProgramStage programStage = new ProgramStage();
Program program = programService.getProgram(id);
programStage.setName(StringUtils.trimToNull(name));
programStage.setDescription(StringUtils.trimToNull(description));
programStage.setProgram(program);
programStage.setStandardInterval(standardInterval);
programStage.setExecutionDateLabel(StringUtils.trimToNull(excecutionDateLabel));
programStage.setRepeatable(repeatable);
programStage.setMinDaysFromStart(minDaysFromStart);
programStage.setDisplayGenerateEventBox(displayGenerateEventBox);
programStage.setValidCompleteOnly(validCompleteOnly);
if (!hideDueDate) {
programStage.setDueDateLabel(StringUtils.trimToNull(dueDateLabel));
}
periodTypeName = StringUtils.trimToNull(periodTypeName);
if (periodTypeName != null) {
PeriodType periodType = PeriodType.getPeriodTypeByName(periodTypeName);
programStage.setPeriodType(periodService.getPeriodTypeByClass(periodType.getClass()));
} else {
programStage.setPeriodType(null);
}
if (program.isWithoutRegistration()) {
programStage.setAutoGenerateEvent(true);
} else {
programStage.setAutoGenerateEvent(autoGenerateEvent);
}
programStage.setCaptureCoordinates(captureCoordinates);
programStage.setBlockEntryForm(blockEntryForm);
programStage.setRemindCompleted(remindCompleted);
programStage.setGeneratedByEnrollmentDate(generatedByEnrollmentDate);
programStage.setAllowGenerateNextVisit(allowGenerateNextVisit);
programStage.setOpenAfterEnrollment(openAfterEnrollment);
programStage.setReportDateToUse(reportDateToUse);
programStage.setPreGenerateUID(preGenerateUID);
programStage.setSortOrder(program.getProgramStages().size() + 1);
programStage.setHideDueDate(hideDueDate);
program.getProgramStages().add(programStage);
if (jsonAttributeValues != null) {
attributeService.updateAttributeValues(programStage, jsonAttributeValues);
}
programStageService.saveProgramStage(programStage);
for (int i = 0; i < this.selectedDataElementsValidator.size(); i++) {
DataElement dataElement = dataElementService.getDataElement(selectedDataElementsValidator.get(i));
Boolean allowed = allowProvidedElsewhere.get(i) == null ? false : allowProvidedElsewhere.get(i);
Boolean displayInReport = displayInReports.get(i) == null ? false : displayInReports.get(i);
Boolean allowDate = allowFutureDates.get(i) == null ? false : allowFutureDates.get(i);
ProgramStageDataElement programStageDataElement = new ProgramStageDataElement(programStage, dataElement, this.compulsories.get(i), new Integer(i));
programStageDataElement.setAllowProvidedElsewhere(allowed);
programStageDataElement.setDisplayInReports(displayInReport);
programStageDataElement.setAllowFutureDate(allowDate);
programStageDataElementService.addProgramStageDataElement(programStageDataElement);
}
return SUCCESS;
}
Aggregations