Search in sources :

Example 1 with InvalidIdentifierReferenceException

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;
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Matcher(java.util.regex.Matcher) Constant(org.hisp.dhis.constant.Constant) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with InvalidIdentifierReferenceException

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;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Session(org.hibernate.Session)

Example 3 with InvalidIdentifierReferenceException

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;
}
Also used : DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) PdfReader(com.lowagie.text.pdf.PdfReader) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) AcroFields(com.lowagie.text.pdf.AcroFields) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)

Example 4 with InvalidIdentifierReferenceException

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);
}
Also used : DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) Calendar(java.util.Calendar) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType)

Aggregations

InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)4 AcroFields (com.lowagie.text.pdf.AcroFields)1 PdfReader (com.lowagie.text.pdf.PdfReader)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Matcher (java.util.regex.Matcher)1 Session (org.hibernate.Session)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 Constant (org.hisp.dhis.constant.Constant)1 DataValueSet (org.hisp.dhis.dxf2.datavalueset.DataValueSet)1 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)1 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)1 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)1 Period (org.hisp.dhis.period.Period)1 PeriodType (org.hisp.dhis.period.PeriodType)1 QuarterlyPeriodType (org.hisp.dhis.period.QuarterlyPeriodType)1 WeeklyPeriodType (org.hisp.dhis.period.WeeklyPeriodType)1 YearlyPeriodType (org.hisp.dhis.period.YearlyPeriodType)1 Transactional (org.springframework.transaction.annotation.Transactional)1