Search in sources :

Example 16 with KeyGenerator

use of org.activityinfo.model.legacy.KeyGenerator 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)

Example 17 with KeyGenerator

use of org.activityinfo.model.legacy.KeyGenerator 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 18 with KeyGenerator

use of org.activityinfo.model.legacy.KeyGenerator in project activityinfo by bedatadriven.

the class MySqlUpdateTest method createFormWithCalculationAndRelevance.

@Test
public void createFormWithCalculationAndRelevance() {
    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 numberField = new FormField(CuidAdapter.generateIndicatorId()).setType(new QuantityType("widgets")).setLabel("NUM").setRequired(true);
    formClass.addElement(numberField);
    FormField calculatedField = new FormField(CuidAdapter.generateIndicatorId()).setType(new CalculatedFieldType("1")).setLabel("Calculation").setRelevanceConditionExpression("NUM>42").setRequired(true);
    formClass.addElement(calculatedField);
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    // Create two records
    RecordTransactionBuilder tx = new RecordTransactionBuilder();
    RecordUpdate site1 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
    site1.setFieldValue(numberField.getId(), new Quantity(10));
    site1.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    RecordUpdate site2 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
    site2.setFieldValue(numberField.getId(), new Quantity(60));
    site2.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    updater().execute(tx.build());
    newRequest();
    // Query results
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId();
    queryModel.selectExpr("Num").as("num");
    queryModel.selectExpr("Calculation").as("calc");
    query(queryModel);
    ColumnView num = columnSet.getColumnView("num");
    ColumnView calculation = columnSet.getColumnView("calc");
    assertThat(calculation.isMissing(0), equalTo(num.getDouble(0) <= 42));
    assertThat(calculation.isMissing(1), equalTo(num.getDouble(1) <= 42));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Quantity(org.activityinfo.model.type.number.Quantity) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Aggregations

KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)18 Test (org.junit.Test)9 FormClass (org.activityinfo.model.form.FormClass)6 FormField (org.activityinfo.model.form.FormField)6 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)6 FormInstance (org.activityinfo.model.form.FormInstance)5 ResourceId (org.activityinfo.model.resource.ResourceId)4 GregorianCalendar (java.util.GregorianCalendar)3 CreateSite (org.activityinfo.legacy.shared.command.CreateSite)3 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)3 FieldValue (org.activityinfo.model.type.FieldValue)3 LocalDate (org.activityinfo.model.type.time.LocalDate)3 OnDataSet (org.activityinfo.server.database.OnDataSet)3 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)2 RecordTransactionBuilder (org.activityinfo.model.resource.RecordTransactionBuilder)2 AttachmentValue (org.activityinfo.model.type.attachment.AttachmentValue)2 Quantity (org.activityinfo.model.type.number.Quantity)2 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)2 Optional (com.google.common.base.Optional)1