Search in sources :

Example 1 with SerialNumber

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

the class SerialNumberTest method testWithPrefix.

@Test
public void testWithPrefix() {
    TestDatabase database = harness.createDatabase();
    ActivityInfoClient client = harness.client();
    FormClass formClass = new FormClass(harness.newFormId());
    formClass.setLabel("Test Prefix");
    formClass.setDatabaseId(database.getId());
    FormField partnerField = CuidAdapter.partnerField(formClass);
    formClass.addElement(partnerField);
    FormField provinceField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
    FormField fileNumberField = formClass.addField(harness.newFieldId()).setCode("SN").setType(new SerialNumberType("PROVINCE", 5)).setLabel("Record Number");
    client.createForm(formClass);
    FormInstance kunduz1 = new FormInstance(harness.newRecordId(), formClass.getId());
    kunduz1.set(provinceField.getId(), "KUNDUZ");
    kunduz1.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(kunduz1);
    FormInstance kunduz2 = new FormInstance(harness.newRecordId(), formClass.getId());
    kunduz2.set(provinceField.getId(), "KUNDUZ");
    kunduz2.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(kunduz2);
    FormInstance takhar = new FormInstance(harness.newRecordId(), formClass.getId());
    takhar.set(provinceField.getId(), "TAKHAR");
    takhar.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(takhar);
    // Verify that the serial numbers have been assigned
    kunduz1 = client.getTypedRecord(formClass, kunduz1.getId());
    kunduz2 = client.getTypedRecord(formClass, kunduz2.getId());
    takhar = client.getTypedRecord(formClass, takhar.getId());
    assertThat(kunduz1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
    assertThat(kunduz2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 2)));
    assertThat(takhar.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("TAKHAR", 1)));
    // Now verify that we can query a table with serial numbers
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("SN");
    ColumnSet columnSet = client.queryTable(queryModel);
    ColumnView sn = columnSet.getColumnView("SN");
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ActivityInfoClient(org.activityinfo.client.ActivityInfoClient) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 2 with SerialNumber

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

the class SerialNumberTest method simpleTest.

@Test
public void simpleTest() {
    TestDatabase database = harness.createDatabase();
    ActivityInfoClient client = harness.client();
    FormClass formClass = new FormClass(harness.newFormId());
    formClass.setLabel("Test Form");
    formClass.setDatabaseId(database.getId());
    FormField partnerField = CuidAdapter.partnerField(formClass);
    formClass.addElement(partnerField);
    FormField fileNumberField = formClass.addField(harness.newFieldId()).setType(new SerialNumberType()).setLabel("Record Number");
    FormField nameField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Name");
    client.createForm(formClass);
    FormInstance record1 = new FormInstance(harness.newRecordId(), formClass.getId());
    record1.set(nameField.getId(), "Sue");
    record1.set(partnerField.getId(), database.getDefaultPartner());
    FormInstance record2 = new FormInstance(harness.newRecordId(), formClass.getId());
    record2.set(nameField.getId(), "Bob");
    record2.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(record1);
    client.createRecord(record2);
    record1 = client.getTypedRecord(formClass, record1.getId());
    record2 = client.getTypedRecord(formClass, record2.getId());
    assertThat(record1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber(1)));
    assertThat(record2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber(2)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ActivityInfoClient(org.activityinfo.client.ActivityInfoClient) FormClass(org.activityinfo.model.form.FormClass) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) Test(org.junit.Test)

Example 3 with SerialNumber

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

the class UpdaterTest method serialNumber.

@Test
public void serialNumber() throws JsonMappingException {
    FormClass formClass = new FormClass(ResourceId.valueOf("FORM1"));
    formClass.addField(ResourceId.valueOf("FIELD0")).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
    FormField serialNumberField = formClass.addField(ResourceId.valueOf("FIELD1")).setType(new SerialNumberType("PROVINCE", 5)).setRequired(true).setLabel("File Number").setCode("SN");
    JsonValue fields = Json.createObject();
    fields.put("PROVINCE", "KUNDUZ");
    JsonValue change = createObject();
    change.put("recordId", "A");
    change.put("formId", "FORM1");
    change.put("fields", fields);
    TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
    FormInstance effectiveRecord = updater.computeEffectiveRecord(formClass, Optional.<FormRecord>absent(), update);
    updater.generateSerialNumber(formClass, serialNumberField, effectiveRecord, update);
    FieldValue serialValue = update.getChangedFieldValues().get(serialNumberField.getId());
    assertThat(serialValue, equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 4 with SerialNumber

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

the class Updater method generateSerialNumber.

@VisibleForTesting
void generateSerialNumber(FormClass formClass, FormField formField, FormInstance effectiveRecord, TypedRecordUpdate update) {
    SerialNumberType type = (SerialNumberType) formField.getType();
    String prefix = computeSerialNumberPrefix(formClass, type, effectiveRecord);
    int serialNumber = serialNumberProvider.next(formClass.getId(), formField.getId(), prefix);
    update.set(formField.getId(), new SerialNumber(prefix, serialNumber));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) SerialNumberType(org.activityinfo.model.type.SerialNumberType) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with SerialNumber

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

the class MySqlUpdateTest method createFormWithSerialNumber.

@Test
public void createFormWithSerialNumber() {
    userId = 1;
    KeyGenerator generator = new KeyGenerator();
    int activityId = generator.generateInt();
    FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(1);
    formClass.setLabel("New Form");
    FormField serialNumField = new FormField(CuidAdapter.generateIndicatorId()).setType(new SerialNumberType()).setLabel("NUM").setRequired(true);
    formClass.addElement(serialNumField);
    FormField nameField = new FormField(CuidAdapter.generateIndicatorId()).setType(TextType.SIMPLE).setLabel("Name").setRequired(true);
    formClass.addElement(nameField);
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    ResourceId siteId = CuidAdapter.generateSiteCuid();
    // Create the record
    FormInstance creation = new FormInstance(siteId, formClass.getId());
    creation.set(nameField.getId(), TextValue.valueOf("Bob"));
    creation.set(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    executeUpdate(creation);
    newRequest();
    // Verify that the record has been created
    FormInstance created = FormInstance.toFormInstance(formClass, catalog.getForm(formClass.getId()).get().get(siteId).get());
    assertThat(created.get(nameField.getId()), equalTo((FieldValue) TextValue.valueOf("Bob")));
    assertThat(created.get(serialNumField.getId()), equalTo((FieldValue) new SerialNumber(1)));
    newRequest();
    // Now update the record's name
    JsonValue fieldValues = createObject();
    fieldValues.put(nameField.getName(), TextValue.valueOf("Sue").toJson());
    // the UI may send null values
    fieldValues.put(serialNumField.getName(), Json.createNull());
    JsonValue update = createObject();
    update.put("fieldValues", fieldValues);
    updater().execute(formClass.getId(), siteId, update);
    newRequest();
    // Finally verify that the serial number is unchanged
    FormInstance updated = FormInstance.toFormInstance(formClass, catalog.getForm(formClass.getId()).get().get(siteId).get());
    assertThat(updated.get(nameField.getId()), equalTo((FieldValue) TextValue.valueOf("Sue")));
    assertThat(updated.get(serialNumField.getId()), equalTo((FieldValue) new SerialNumber(1)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Aggregations

SerialNumber (org.activityinfo.model.type.SerialNumber)6 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 Test (org.junit.Test)5 FormClass (org.activityinfo.model.form.FormClass)4 FormField (org.activityinfo.model.form.FormField)4 FormInstance (org.activityinfo.model.form.FormInstance)4 FieldValue (org.activityinfo.model.type.FieldValue)4 ActivityInfoClient (org.activityinfo.client.ActivityInfoClient)2 JsonValue (org.activityinfo.json.JsonValue)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)1 ColumnSet (org.activityinfo.model.query.ColumnSet)1 ColumnView (org.activityinfo.model.query.ColumnView)1 QueryModel (org.activityinfo.model.query.QueryModel)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 RecordRef (org.activityinfo.model.type.RecordRef)1 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)1 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)1 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)1