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");
}
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)));
}
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)));
}
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));
}
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)));
}
Aggregations