Search in sources :

Example 1 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class ReferenceFieldWidget method init.

@Override
public void init(FieldValue value) {
    ReferenceValue referenceValue = (ReferenceValue) value;
    RecordRef recordRef = referenceValue.getOnlyReference();
    viewModel.setInitialSelection(referenceValue.getReferences());
}
Also used : ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef)

Example 2 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class ReferenceFieldWidget method onSelection.

private void onSelection(SelectionEvent<String> event) {
    LOGGER.info("onSelection: " + event.getSelectedItem());
    viewModel.getSelectedRecords().once().then(new AsyncCallback<Set<RecordRef>>() {

        @Override
        public void onFailure(Throwable caught) {
        }

        @Override
        public void onSuccess(Set<RecordRef> result) {
            if (result.isEmpty()) {
                fieldUpdater.update(FieldInput.EMPTY);
            } else {
                fieldUpdater.update(new FieldInput(new ReferenceValue(result)));
            }
        }
    });
}
Also used : Set(java.util.Set) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) FieldInput(org.activityinfo.ui.client.input.model.FieldInput)

Example 3 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class SubFormViewModelBuilder method computeActiveSubRecord.

private RecordRef computeActiveSubRecord(RecordRef parentRecordRef, FormInputModel inputModel) {
    // Has the user chosen a specific period?
    Optional<RecordRef> activeSubRecord = inputModel.getActiveSubRecord(fieldId);
    if (activeSubRecord.isPresent()) {
        return activeSubRecord.get();
    }
    // Otherwise choose the active record based on the user's previous choices
    // or the current date
    PeriodValue activePeriod = subFormKind.getPeriodType().containingDate(memory.getLastUsedDate());
    ResourceId recordId = ResourceId.periodSubRecordId(parentRecordRef, activePeriod);
    return new RecordRef(subFormId, recordId);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef) PeriodValue(org.activityinfo.model.type.time.PeriodValue)

Example 4 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class KeySelectionSet method normalize.

/**
 * When we have overlapping hierarchies, we don't include all complete selections as
 * the value for the field.
 *
 * For example, given a reference field that includes [PROVINCE, TERRITORY] in the range, we will
 * have two {@link KeySelection} for each of the forms in this range. Their key trees look like this:
 *
 * <pre>
 *
 *     PROVINCE               TERRITORY
 *
 *     k0: Province.Name      k0: Province.Name
 *                                 ^
 *                                 |
 *                            k1: Territory.Name
 *
 * </pre>
 *
 * Once the user has selected both the Province.Name (k0) and the Territory.Name (k1), then we have a valid
 * selection for both forms.
 *
 * However, in this case, we don't want to store _both_ the territory _and_ the province as this not really
 * the user's intent. We exclude the province selection because it is implied by the territory selection.
 */
private Set<RecordRef> normalize(List<Optional<RecordRef>> refs) {
    // Build a map from referenced form id -> selection
    Map<ResourceId, RecordRef> selectionMap = new HashMap<>();
    for (int i = 0; i < selections.size(); i++) {
        if (refs.get(i).isPresent()) {
            selectionMap.put(selections.get(i).getFormId(), refs.get(i).get());
        }
    }
    List<ResourceId> selectedFormIds = Lists.newArrayList(selectionMap.keySet());
    // Now remove redundant parents
    for (ResourceId formId : selectedFormIds) {
        for (ResourceId ancestorFormId : lookupKeySet.getAncestorForms(formId)) {
            selectionMap.remove(ancestorFormId);
        }
    }
    return new HashSet<>(selectionMap.values());
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef)

Example 5 with RecordRef

use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testSubFormInput.

@Test
public void testSubFormInput() {
    FormInputViewModelBuilder builder = builderFor(setup.getCatalog().getIncidentForm());
    // Start with empty input
    FormInputModel inputModel = new FormInputModel(new RecordRef(IncidentForm.FORM_ID, ResourceId.generateId()));
    // Should see one (empty) sub form record
    FormInputViewModel viewModel = builder.build(inputModel);
    SubFormViewModel referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(referralSubForm.getSubRecords(), hasSize(1));
    // We can update this sub record
    FormInputViewModel subRecord = referralSubForm.getSubRecords().get(0);
    inputModel = inputModel.update(subRecord.getRecordRef(), ReferralSubForm.ORGANIZATION_FIELD_ID, new FieldInput(TextValue.valueOf("CRS")));
    viewModel = builder.build(inputModel);
    referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(referralSubForm.getSubRecords(), hasSize(1));
    // Now add a second record
    inputModel = inputModel.addSubRecord(new RecordRef(ReferralSubForm.FORM_ID, ResourceId.generateId()));
    viewModel = builder.build(inputModel);
    referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
    assertThat(referralSubForm.getSubRecords(), hasSize(2));
    // Verify that the transaction is built is correctly
    RecordTransaction tx = viewModel.buildTransaction();
    RecordUpdate[] changes = tx.getChangeArray();
    assertThat(changes.length, equalTo(3));
    RecordUpdate parentChange = changes[0];
    RecordUpdate subFormChange = changes[1];
    assertThat(parentChange.getRecordRef(), equalTo(inputModel.getRecordRef()));
    assertThat(subFormChange.getParentRecordId(), equalTo(parentChange.getRecordId().asString()));
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

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