Search in sources :

Example 6 with FormRecord

use of org.activityinfo.model.form.FormRecord in project activityinfo by bedatadriven.

the class MySqlUpdateTest method updateLocation.

@Test
public void updateLocation() throws SQLException {
    ResourceId recordId = CuidAdapter.cuid(LOCATION_DOMAIN, 1);
    ResourceId formId = CuidAdapter.locationFormClass(1);
    RecordUpdate update = new RecordUpdate();
    update.setFormId(formId);
    update.setRecordId(recordId);
    update.setFieldValue(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD), TextValue.valueOf("New Name"));
    Updater updater = updater();
    updater.executeChange(update);
    newRequest();
    FormStorage formStorage = catalog.getForm(formId).get();
    FormRecord record = formStorage.get(recordId).get();
    FormInstance typedRecord = FormInstance.toFormInstance(formStorage.getFormClass(), record);
    GeoPoint point = (GeoPoint) typedRecord.get(CuidAdapter.field(formId, CuidAdapter.GEOMETRY_FIELD));
    assertThat(point, not(nullValue()));
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) Updater(org.activityinfo.store.query.server.Updater) FormRecord(org.activityinfo.model.form.FormRecord) FormInstance(org.activityinfo.model.form.FormInstance) Test(org.junit.Test)

Example 7 with FormRecord

use of org.activityinfo.model.form.FormRecord in project activityinfo by bedatadriven.

the class MySqlUpdateTest method addNewAttributes.

@Test
public void addNewAttributes() {
    KeyGenerator generator = new KeyGenerator();
    int activityId = generator.generateInt();
    EnumType enumType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "A"), new EnumItem(EnumItem.generateId(), "B"));
    FormField selectField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS)).setType(enumType).setLabel("Select");
    FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(1);
    formClass.setLabel("New Form");
    formClass.addElement(selectField);
    catalog.createOrUpdateFormSchema(formClass);
    System.out.println("Created activity " + activityId);
    // Now change the enum items
    EnumType updatedType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "C"), new EnumItem(EnumItem.generateId(), "D"));
    selectField.setType(updatedType);
    newRequest();
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    // Now try to save a new instance with the value
    FieldValue valueC = new EnumValue(updatedType.getValues().get(0).getId());
    FormInstance newRecord = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
    newRecord.set(selectField.getId(), new EnumValue(updatedType.getValues().get(0).getId()));
    newRecord.set(CuidAdapter.partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    executeUpdate(newRecord);
    // Ensure that the select field has been saved
    FormRecord saved = catalog.getForm(formClass.getId()).get().get(newRecord.getId()).get();
    FormInstance savedInstance = FormInstance.toFormInstance(formClass, saved);
    assertThat(savedInstance.get(selectField.getId()), equalTo(valueC));
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) FieldValue(org.activityinfo.model.type.FieldValue) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormInstance(org.activityinfo.model.form.FormInstance) FormRecord(org.activityinfo.model.form.FormRecord) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 8 with FormRecord

use of org.activityinfo.model.form.FormRecord in project activityinfo by bedatadriven.

the class FormStoreTest method newRecordOffline.

@Test
public void newRecordOffline() {
    TestSetup setup = new TestSetup();
    Survey survey = setup.getSurveyForm();
    // Synchronize the survey form
    setup.setConnected(true);
    setup.getFormStore().setFormOffline(survey.getFormId(), true);
    setup.runScheduled();
    // Go offline...
    setup.setConnected(false);
    // Monitor the pending queue status
    Connection<PendingStatus> pendingStatus = setup.connect(setup.getOfflineStore().getPendingStatus());
    assertThat(pendingStatus.assertLoaded().isEmpty(), equalTo(true));
    // Create a new survey record
    FormInstance newRecordTyped = survey.getGenerator().get();
    RecordTransaction tx = RecordTransaction.builder().create(newRecordTyped).build();
    // Update a record...
    Promise<Void> updateResult = setup.getFormStore().updateRecords(tx);
    assertThat(updateResult.getState(), equalTo(Promise.State.FULFILLED));
    // Now query offline...
    Connection<Maybe<FormRecord>> recordView = setup.connect(setup.getFormStore().getRecord(newRecordTyped.getRef()));
    // It should be listed as a pending change...
    assertThat(pendingStatus.assertLoaded().getCount(), equalTo(1));
    Maybe<FormRecord> record = recordView.assertLoaded();
    assertThat(record.getState(), equalTo(Maybe.State.VISIBLE));
    assertThat(record.get().getRecordId(), equalTo(newRecordTyped.getId().asString()));
    // Finally go online and ensure that results are sent to the server
    setup.setConnected(true);
    setup.getOfflineStore().syncChanges();
    // Our queue should be empty again
    assertThat(pendingStatus.assertLoaded().isEmpty(), equalTo(true));
}
Also used : Maybe(org.activityinfo.promise.Maybe) FormInstance(org.activityinfo.model.form.FormInstance) FormRecord(org.activityinfo.model.form.FormRecord) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Aggregations

FormRecord (org.activityinfo.model.form.FormRecord)8 FormInstance (org.activityinfo.model.form.FormInstance)4 FormField (org.activityinfo.model.form.FormField)3 Test (org.junit.Test)3 FormClass (org.activityinfo.model.form.FormClass)2 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)2 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)1 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)1 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 FieldValue (org.activityinfo.model.type.FieldValue)1 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)1 EnumType (org.activityinfo.model.type.enumerated.EnumType)1 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)1 Maybe (org.activityinfo.promise.Maybe)1 FormRecordEntity (org.activityinfo.store.hrd.entity.FormRecordEntity)1 FormRecordSnapshotEntity (org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity)1 Updater (org.activityinfo.store.query.server.Updater)1 ColumnQueryBuilder (org.activityinfo.store.spi.ColumnQueryBuilder)1 FormStorage (org.activityinfo.store.spi.FormStorage)1