use of org.hisp.dhis.common.exception.InvalidIdentifierReferenceException in project dhis2-core by dhis2.
the class DefaultExpressionService method getExpressionDescription.
@Override
@Transactional
public String getExpressionDescription(String expression) {
if (expression == null || expression.isEmpty()) {
return null;
}
// ---------------------------------------------------------------------
// Operands
// ---------------------------------------------------------------------
StringBuffer sb = new StringBuffer();
Matcher matcher = VARIABLE_PATTERN.matcher(expression);
while (matcher.find()) {
String dimensionItem = matcher.group(GROUP_ID);
DimensionalItemObject dimensionItemObject = dimensionService.getDataDimensionalItemObject(dimensionItem);
if (dimensionItemObject == null) {
throw new InvalidIdentifierReferenceException("Identifier does not reference a dimensional item object: " + dimensionItem);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(dimensionItemObject.getDisplayName()));
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = CONSTANT_PATTERN.matcher(expression);
while (matcher.find()) {
String co = matcher.group(GROUP_ID);
Constant constant = constantService.getConstant(co);
if (constant == null) {
throw new InvalidIdentifierReferenceException("Identifier does not reference a constant: " + co);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(constant.getDisplayName()));
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Org unit groups
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = OU_GROUP_PATTERN.matcher(expression);
while (matcher.find()) {
String oug = matcher.group(GROUP_ID);
OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup(oug);
if (group == null) {
throw new InvalidIdentifierReferenceException("Identifier does not reference an organisation unit group: " + oug);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(group.getDisplayName()));
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Days
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = DAYS_PATTERN.matcher(expression);
while (matcher.find()) {
matcher.appendReplacement(sb, DAYS_DESCRIPTION);
}
expression = TextUtils.appendTail(matcher, sb);
return expression;
}
use of org.hisp.dhis.common.exception.InvalidIdentifierReferenceException in project dhis2-core by dhis2.
the class HibernatePeriodStore method reloadPeriodType.
@Override
public PeriodType reloadPeriodType(PeriodType periodType) {
Session session = sessionFactory.getCurrentSession();
if (periodType == null || session.contains(periodType)) {
return periodType;
}
PeriodType reloadedPeriodType = getPeriodType(periodType.getClass());
if (reloadedPeriodType == null) {
throw new InvalidIdentifierReferenceException("The PeriodType referenced by the Period is not in database: " + periodType.getName());
}
return reloadedPeriodType;
}
use of org.hisp.dhis.common.exception.InvalidIdentifierReferenceException in project dhis2-core by dhis2.
the class PdfDataEntryFormUtil method getDataValueSet.
/**
* Creates data value set from Input Stream (PDF) for PDF data import
*/
public static DataValueSet getDataValueSet(InputStream in) {
PdfReader reader = null;
DataValueSet dataValueSet = new DataValueSet();
List<org.hisp.dhis.dxf2.datavalue.DataValue> dataValueList = new ArrayList<>();
try {
reader = new PdfReader(in);
AcroFields form = reader.getAcroFields();
if (form != null) {
// Process OrgUnitUID and PeriodID from the PDF Form
String orgUnitUid = form.getField(PdfDataEntryFormUtil.LABELCODE_ORGID).trim();
String periodId = form.getField(PdfDataEntryFormUtil.LABELCODE_PERIODID).trim();
if (periodId == null || periodId.isEmpty()) {
throw new InvalidIdentifierReferenceException(ERROR_EMPTY_PERIOD);
}
if (orgUnitUid == null || orgUnitUid.isEmpty()) {
throw new InvalidIdentifierReferenceException(ERROR_EMPTY_ORG_UNIT);
}
Period period = PeriodType.getPeriodFromIsoString(periodId);
if (period == null) {
throw new InvalidIdentifierReferenceException(ERROR_INVALID_PERIOD + periodId);
}
// Loop Through the Fields and get data.
@SuppressWarnings("unchecked") Set<String> fldNames = form.getFields().keySet();
for (String fldName : fldNames) {
if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD)) {
String[] strArrFldName = fldName.split("_");
org.hisp.dhis.dxf2.datavalue.DataValue dataValue = new org.hisp.dhis.dxf2.datavalue.DataValue();
dataValue.setDataElement(strArrFldName[1]);
dataValue.setCategoryOptionCombo(strArrFldName[2]);
dataValue.setOrgUnit(orgUnitUid);
dataValue.setPeriod(period.getIsoDate());
dataValue.setValue(fieldValueFormat(strArrFldName, form.getField(fldName)));
dataValue.setStoredBy(DATAVALUE_IMPORT_STOREBY);
dataValue.setComment(DATAVALUE_IMPORT_COMMENT);
dataValue.setFollowup(false);
dataValue.setLastUpdated(DateUtils.getMediumDateString());
dataValueList.add(dataValue);
}
}
dataValueSet.setDataValues(dataValueList);
} else {
throw new RuntimeException("Could not generate PDF AcroFields form from input");
}
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (reader != null) {
reader.close();
}
}
return dataValueSet;
}
use of org.hisp.dhis.common.exception.InvalidIdentifierReferenceException in project dhis2-core by dhis2.
the class PeriodUtil method getPeriod.
public static Period getPeriod(String periodName, PeriodType periodType) throws InvalidIdentifierReferenceException {
if (periodType instanceof DailyPeriodType) {
return periodType.createPeriod(DateUtils.getMediumDate(periodName));
}
if (periodType instanceof WeeklyPeriodType) {
return periodType.createPeriod(DateUtils.getMediumDate(periodName));
}
if (periodType instanceof MonthlyPeriodType) {
int dashIndex = periodName.indexOf('-');
if (dashIndex < 0) {
return null;
}
int month = Integer.parseInt(periodName.substring(0, dashIndex));
int year = Integer.parseInt(periodName.substring(dashIndex + 1, periodName.length()));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
return periodType.createPeriod(cal.getTime());
}
if (periodType instanceof YearlyPeriodType) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(periodName));
return periodType.createPeriod(cal.getTime());
}
if (periodType instanceof QuarterlyPeriodType) {
Calendar cal = Calendar.getInstance();
int month = 0;
if (periodName.substring(0, periodName.indexOf(" ")).equals("Jan")) {
month = 1;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Apr")) {
month = 4;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Jul")) {
month = 6;
} else if (periodName.substring(0, periodName.indexOf(" ")).equals("Oct")) {
month = 10;
}
int year = Integer.parseInt(periodName.substring(periodName.lastIndexOf(" ") + 1));
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
if (month != 0) {
return periodType.createPeriod(cal.getTime());
}
}
throw new InvalidIdentifierReferenceException("Couldn't make a period of type " + periodType.getName() + " and name " + periodName);
}
Aggregations