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