use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class RepeatingSubFormPanel method addRecordHandler.
private void addRecordHandler(SelectEvent event) {
// If we have a placeholder, then add it first, otherwise it will
// disappear
Optional<FormInputViewModel> placeholder = viewModel.getPlaceholder();
if (placeholder.isPresent()) {
inputHandler.addSubRecord(placeholder.get().getRecordRef());
}
// Add a new sub record with unique ID
ResourceId subFormId = subTree.getRootFormId();
ResourceId newSubRecordId = ResourceId.generateId();
inputHandler.addSubRecord(new RecordRef(subFormId, newSubRecordId));
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class SubFormViewModel method buildUpdates.
public List<RecordUpdate> buildUpdates(RecordRef parentRef) {
List<RecordUpdate> updates = new ArrayList<>();
for (FormInputViewModel subRecord : subRecords) {
if (!subRecord.isPlaceholder()) {
updates.add(subRecord.buildUpdate(Optional.of(parentRef)));
}
}
for (RecordRef deletedRecord : deletedRecords) {
RecordUpdate update = new RecordUpdate();
update.setFormId(deletedRecord.getFormId());
update.setRecordId(deletedRecord.getRecordId());
update.setDeleted(true);
updates.add(update);
}
return updates;
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class SubFormViewModel method getActivePeriod.
public PeriodValue getActivePeriod() {
RecordRef ref = getActiveRecordRef();
PeriodType periodType = subFormKind.getPeriodType();
return periodType.fromSubFormKey(ref);
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class SubFormViewModelBuilder method build.
public SubFormViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingParentRecord) {
List<FormInputViewModel> subRecordViews = new ArrayList<>();
// First do existing records
Set<RecordRef> existingRefs = new HashSet<>();
List<RecordTree> existingSubTrees;
if (existingParentRecord.isVisible()) {
existingSubTrees = existingParentRecord.get().buildSubTrees(inputModel.getRecordRef(), subTree);
} else {
existingSubTrees = Collections.emptyList();
}
Set<RecordRef> deletedRecords = new HashSet<>();
for (RecordTree existingSubRecord : existingSubTrees) {
RecordRef ref = existingSubRecord.getRootRef();
if (inputModel.isDeleted(ref)) {
deletedRecords.add(ref);
} else {
FormInputModel subInput = inputModel.getSubRecord(ref).orElse(new FormInputModel(ref));
FormInputViewModel subViewModel = formBuilder.build(subInput, Maybe.of(existingSubRecord));
existingRefs.add(ref);
subRecordViews.add(subViewModel);
}
}
// Now add sub records newly added by the user
for (FormInputModel subInput : inputModel.getSubRecords()) {
if (!existingRefs.contains(subInput.getRecordRef())) {
if (subInput.getRecordRef().getFormId().equals(subFormId)) {
FormInputViewModel subViewModel = formBuilder.build(subInput, Maybe.notFound());
subRecordViews.add(subViewModel);
}
}
}
if (keyField.isPresent()) {
// Keyed/Period subforms have a single active record
RecordRef activeRecord = computeActiveSubRecord(inputModel.getRecordRef(), inputModel);
FormInputViewModel activeRecordViewModel = find(activeRecord, subRecordViews);
return new SubFormViewModel(fieldId, subFormKind, subRecordViews, activeRecordViewModel, deletedRecords);
} else {
if (subRecordViews.isEmpty()) {
RecordRef placeholderId = placeholderRecordRef();
if (!inputModel.isDeleted(placeholderId)) {
subRecordViews.add(formBuilder.placeholder(placeholderId));
}
}
return new SubFormViewModel(fieldId, subRecordViews, deletedRecords);
}
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class FormInputModel method updateActiveSubRecord.
public FormInputModel updateActiveSubRecord(ResourceId fieldId, RecordRef newActiveRef) {
Map<ResourceId, RecordRef> updatedMap = Maps.newHashMap(this.activeSubRecords);
RecordRef oldRef = updatedMap.put(fieldId, newActiveRef);
if (Objects.equals(oldRef, newActiveRef)) {
return this;
}
return new FormInputModel(recordRef, fieldInputs, subRecords, updatedMap, deletedSubRecords);
}
Aggregations