use of org.estatio.module.index.dom.IndexValue in project estatio by estatio.
the class IndexBuilder method createIndexValues.
private void createIndexValues(final IndexBase indexBase, final int year, final double[] values, final ExecutionContext executionContext) {
int i = 0;
for (final double value : values) {
final IndexValue indexValue = indexBase.newIndexValue(ld(year, i + 1, 1), BigDecimal.valueOf(value));
executionContext.addResult(this, indexValue);
i++;
}
}
use of org.estatio.module.index.dom.IndexValue in project estatio by estatio.
the class IndexImport method importData.
@Programmatic
@Override
public List<Object> importData(final Object previousRow) {
final ApplicationTenancy applicationTenancy = applicationTenancyRepository.findByPath(getAtPath());
// Creator and implement IndexCreator, IndexBaseCreator, IndexValueCreator
final IndexValue indexValue = indexRepository.findOrCreateIndex(applicationTenancy, indexReference, indexName).findOrCreateBase(indexBaseStartDate, indexBaseFactor).newIndexValue(startDate, value);
return Lists.newArrayList(indexValue);
}
use of org.estatio.module.index.dom.IndexValue 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();
}
use of org.estatio.module.index.dom.IndexValue 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;
}
Aggregations