Search in sources :

Example 6 with SerialNumberType

use of org.activityinfo.model.type.SerialNumberType 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 7 with SerialNumberType

use of org.activityinfo.model.type.SerialNumberType 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

SerialNumberType (org.activityinfo.model.type.SerialNumberType)7 SerialNumber (org.activityinfo.model.type.SerialNumber)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 Test (org.junit.Test)4 ActivityInfoClient (org.activityinfo.client.ActivityInfoClient)2 JsonValue (org.activityinfo.json.JsonValue)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 UiHandler (com.google.gwt.uibinder.client.UiHandler)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 FieldType (org.activityinfo.model.type.FieldType)1 NarrativeType (org.activityinfo.model.type.NarrativeType)1 ReferenceType (org.activityinfo.model.type.ReferenceType)1 AttachmentType (org.activityinfo.model.type.attachment.AttachmentType)1