Search in sources :

Example 1 with Index

use of org.estatio.module.index.dom.Index in project estatio by estatio.

the class IndexBuilder method execute.

@Override
protected void execute(final ExecutionContext executionContext) {
    checkParam("reference", executionContext, String.class);
    checkParam("name", executionContext, String.class);
    final Index index = createIndex(applicationTenancy, reference, name, executionContext);
    for (final Base base : bases) {
        final IndexBase indexBase = createIndexBase(index, base.year, base.factor, executionContext);
        for (final Value value : base.values) {
            createIndexValues(indexBase, value.year, value.values, executionContext);
        }
    }
    object = index;
}
Also used : IndexValue(org.estatio.module.index.dom.IndexValue) Index(org.estatio.module.index.dom.Index) IndexBase(org.estatio.module.index.dom.IndexBase) IndexBase(org.estatio.module.index.dom.IndexBase)

Example 2 with Index

use of org.estatio.module.index.dom.Index in project estatio by estatio.

the class IndexValueMaintLineItem method apply.

// //////////////////////////////////////
@MemberOrder(sequence = "2")
@ActionSemantics(Of.IDEMPOTENT)
@Bulk
public void apply() {
    if (bulkInteractionContext.isFirst()) {
        String error = check();
        if (error != null) {
            getContainer().raiseError(error);
            return;
        }
    }
    // only null on first pass, then populated
    ApplicationTenancy applicationTenancy = (ApplicationTenancy) scratchpad.get("applicationTenancy");
    if (applicationTenancy == null) {
        final String atPath = getAtPath();
        applicationTenancy = applicationTenancies.findTenancyByPath(atPath);
        scratchpad.put("applicationTenancy", applicationTenancy);
    }
    // only null on first pass, then populated
    Index index = (Index) scratchpad.get("index");
    if (index == null) {
        final String reference = getReference();
        index = indexRepository.newIndex(reference, reference, applicationTenancy);
        scratchpad.put("index", index);
        setIndex(index);
    }
    // only null on first pass, then populated, and only if !existingIndex
    IndexBase previousBase = (IndexBase) scratchpad.get("previousBase");
    final LocalDate baseStartDate = getBaseStartDate();
    final BigDecimal baseFactor = getBaseFactor();
    IndexBase indexBase = indexBaseRepository.findByIndexAndDate(index, baseStartDate);
    if (indexBase == null) {
        indexBase = indexBaseRepository.newIndexBase(index, previousBase, baseStartDate, baseFactor);
    }
    setIndexBase(indexBase);
    // for next time need to create
    scratchpad.put("previousBase", indexBase);
    final LocalDate valueStartDate = getValueStartDate();
    final BigDecimal value = getValue();
    IndexValue indexValue = indexValueRepository.findByIndexAndStartDate(index, valueStartDate);
    if (indexValue == null) {
        indexValue = indexValueRepository.findOrCreate(index, valueStartDate, value);
    } else {
        indexValue.setValue(value);
    }
    setIndexValue(indexValue);
    // belt-n-braces so that subsequent queries succeed...
    getContainer().flush();
}
Also used : Index(org.estatio.module.index.dom.Index) IndexBase(org.estatio.module.index.dom.IndexBase) IndexValue(org.estatio.module.index.dom.IndexValue) LocalDate(org.joda.time.LocalDate) ApplicationTenancy(org.isisaddons.module.security.dom.tenancy.ApplicationTenancy) BigDecimal(java.math.BigDecimal) ActionSemantics(org.apache.isis.applib.annotation.ActionSemantics) MemberOrder(org.apache.isis.applib.annotation.MemberOrder) Bulk(org.apache.isis.applib.annotation.Bulk)

Example 3 with Index

use of org.estatio.module.index.dom.Index in project estatio by estatio.

the class IndexValueMaintLineItem method check.

private String check() {
    @SuppressWarnings("rawtypes") List lineItemObjs = bulkInteractionContext.getDomainObjects();
    @SuppressWarnings("unchecked") List<IndexValueMaintLineItem> lineItems = lineItemObjs;
    // ensure items to process
    if (lineItems.isEmpty()) {
        return "No rows in spreadsheet";
    }
    // ensure all rows for a single index
    String reference = null;
    for (int i = 0; i < lineItems.size(); i++) {
        IndexValueMaintLineItem lineItem = lineItems.get(i);
        String eachReference = lineItem.getReference();
        if (reference == null) {
            reference = eachReference;
        } else {
            if (!Objects.equal(reference, eachReference)) {
                return "Row " + (i + 1) + ": all rows must be for same index reference";
            }
        }
    }
    // ensure valueStartDates are sequential
    LocalDate previousValueStartDate = null;
    LocalDate eachValueStartDate;
    for (int i = 0; i < lineItems.size(); i++) {
        IndexValueMaintLineItem lineItem = lineItems.get(i);
        eachValueStartDate = lineItem.getValueStartDate();
        if (previousValueStartDate != null) {
            if (!Objects.equal(eachValueStartDate.minusMonths(1), previousValueStartDate)) {
                return "Row " + (i + 1) + ": all rows must be for sequential; found " + previousValueStartDate.toString("yyyy/MM/dd") + " and " + eachValueStartDate.toString("yyyy/MM/dd");
            }
        }
        previousValueStartDate = eachValueStartDate;
    }
    // if existing index, ensure valueStartDate is:
    // * either for an existing month,
    // * or follows on from previous by no more than 1 month
    Index index = indexRepository.findByReference(reference);
    boolean existingIndex = index != null;
    scratchpad.put("index", index);
    if (existingIndex) {
        LocalDate firstValueStartDate = null;
        for (IndexValueMaintLineItem lineItem : lineItems) {
            firstValueStartDate = lineItem.getValueStartDate();
            scratchpad.put("firstValueStartDate", firstValueStartDate);
            break;
        }
        IndexValue existingValue = indexValueRepository.findByIndexAndStartDate(index, firstValueStartDate);
        if (existingValue == null) {
            LocalDate previousMonthValueStartDate = firstValueStartDate.minusMonths(1);
            IndexValue previousValue = indexValueRepository.findByIndexAndStartDate(index, previousMonthValueStartDate);
            if (previousValue == null) {
                IndexValue last = indexValueRepository.findLastByIndex(index);
                if (last != null) {
                    return "First row (" + firstValueStartDate.toString("yyyy/MM/dd") + ") must be an existing month or " + "for the 1 month after last (" + last.getStartDate().toString("yyyy/MM/dd") + ")";
                }
            } else {
                scratchpad.put("previousBase", previousValue.getIndexBase());
            }
        } else {
            scratchpad.put("previousBase", existingValue.getIndexBase());
        }
    }
    // ensure that baseStartDate and baseFactors change in step
    LocalDate previousBaseStartDate = null;
    BigDecimal previousBaseFactor = null;
    for (int i = 0; i < lineItems.size(); i++) {
        IndexValueMaintLineItem lineItem = lineItems.get(i);
        LocalDate eachBaseStartDate = lineItem.getBaseStartDate();
        BigDecimal eachBaseFactor = lineItem.getBaseFactor();
        if (previousBaseStartDate != null || previousBaseFactor != null) {
            if (Objects.equal(previousBaseStartDate, eachBaseStartDate) && !Objects.equal(previousBaseFactor, eachBaseFactor)) {
                return "Base factors can only change if base start date changes; " + "baseStartDate: " + eachBaseStartDate.toString("yyyy/MM/dd") + ", baseFactor: " + eachBaseFactor;
            }
        }
        previousBaseStartDate = eachBaseStartDate;
        previousBaseFactor = eachBaseFactor;
    }
    return null;
}
Also used : List(java.util.List) Index(org.estatio.module.index.dom.Index) IndexValue(org.estatio.module.index.dom.IndexValue) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 4 with Index

use of org.estatio.module.index.dom.Index in project estatio by estatio.

the class IndexationService method indexateToResult.

private IndexationResult indexateToResult(final Indexable input) {
    if (input.getIndex() == null || input.getBaseIndexStartDate() == null || input.getNextIndexStartDate() == null || input.getBaseIndexStartDate().compareTo(input.getNextIndexStartDate()) > 0) {
        return IndexationResult.NULL;
    }
    final Index index = input.getIndex();
    index.initialize(input);
    return IndexationCalculationMethod.calculate(input);
}
Also used : Index(org.estatio.module.index.dom.Index)

Aggregations

Index (org.estatio.module.index.dom.Index)4 IndexValue (org.estatio.module.index.dom.IndexValue)3 BigDecimal (java.math.BigDecimal)2 IndexBase (org.estatio.module.index.dom.IndexBase)2 LocalDate (org.joda.time.LocalDate)2 List (java.util.List)1 ActionSemantics (org.apache.isis.applib.annotation.ActionSemantics)1 Bulk (org.apache.isis.applib.annotation.Bulk)1 MemberOrder (org.apache.isis.applib.annotation.MemberOrder)1 ApplicationTenancy (org.isisaddons.module.security.dom.tenancy.ApplicationTenancy)1