Search in sources :

Example 46 with RecordRef

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));
}
Also used : FormInputViewModel(org.activityinfo.ui.client.input.viewModel.FormInputViewModel) ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef)

Example 47 with RecordRef

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;
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) ArrayList(java.util.ArrayList) RecordRef(org.activityinfo.model.type.RecordRef)

Example 48 with RecordRef

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);
}
Also used : PeriodType(org.activityinfo.model.type.time.PeriodType) RecordRef(org.activityinfo.model.type.RecordRef)

Example 49 with RecordRef

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);
    }
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel)

Example 50 with RecordRef

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);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef)

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)66 Test (org.junit.Test)31 ResourceId (org.activityinfo.model.resource.ResourceId)26 ReferenceValue (org.activityinfo.model.type.ReferenceValue)26 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)14 FormInstance (org.activityinfo.model.form.FormInstance)7 FormTree (org.activityinfo.model.formTree.FormTree)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 Optional (com.google.common.base.Optional)5 FieldValue (org.activityinfo.model.type.FieldValue)5 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)5 FormClass (org.activityinfo.model.form.FormClass)4 ResultSet (java.sql.ResultSet)3 FormField (org.activityinfo.model.form.FormField)3 RecordTree (org.activityinfo.model.formTree.RecordTree)3 ColumnSet (org.activityinfo.model.query.ColumnSet)3 ColumnView (org.activityinfo.model.query.ColumnView)3 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)3