use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormField method toJsonObject.
@Override
public JsonValue toJsonObject() {
JsonValue object = createObject();
object.put("id", id.asString());
object.put("code", code);
object.put("label", label);
object.put("description", description);
object.put("relevanceCondition", relevanceConditionExpression);
object.put("visible", visible);
object.put("required", required);
object.put("type", type.getTypeClass().getId());
if (key) {
object.put("key", true);
}
if (!superProperties.isEmpty()) {
JsonValue superPropertiesArray = Json.createArray();
for (ResourceId superProperty : superProperties) {
superPropertiesArray.add(Json.createFromNullable(superProperty.asString()));
}
object.put("superProperties", superPropertiesArray);
}
if (type instanceof ParametrizedFieldType) {
object.put("typeParameters", ((ParametrizedFieldType) type).getParametersAsJson());
}
return object;
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormInstance method toJsonObject.
public JsonValue toJsonObject() {
JsonValue fields = createObject();
for (Map.Entry<ResourceId, FieldValue> entry : fieldMap.entrySet()) {
if (entry.getValue() != null) {
fields.put(entry.getKey().asString(), entry.getValue().toJson());
}
}
JsonValue object = createObject();
object.put("formId", getFormId().asString());
object.put("recordId", getId().asString());
object.put("fieldValues", fields);
return object;
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class XlsFormBuilderTest method test.
@Test
public void test() throws IOException {
final FormClass formClass = new FormClass(ResourceId.valueOf("F1"));
formClass.addField(ResourceId.valueOf("X1")).setLabel("What is your name?").setRequired(true).setCode("RespName").setType(TextType.SIMPLE);
formClass.addField(ResourceId.valueOf("X2")).setLabel("How old are you?").setRequired(true).setCode("Age").setType(new QuantityType("years"));
formClass.addField(ResourceId.valueOf("X3")).setLabel("Age in dog years").setRequired(true).setCode("AgeDogYears").setType(new CalculatedFieldType("Age*7"));
formClass.addField(ResourceId.valueOf("X4")).setLabel("Date of birth").setRequired(false).setCode("DOB").setType(LocalDateType.INSTANCE);
formClass.addField(ResourceId.valueOf("X5")).setLabel("What is your favorite color?").setRequired(false).setCode("Color").setType(new EnumType(Cardinality.SINGLE, Lists.newArrayList(new EnumItem(ResourceId.valueOf("Z1"), "Blue"), new EnumItem(ResourceId.valueOf("Z2"), "Red"), new EnumItem(ResourceId.valueOf("Z3"), "Green"))));
formClass.addField(ResourceId.valueOf("X6")).setLabel("Who are your brothers?").setCode("Brothers").setType(new SubFormReferenceType(ResourceId.valueOf("F2")));
formClass.addField(ResourceId.valueOf("X7")).setLabel("Likes any color?").setCode("Colorful").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("containsAny(X5,Z1,Z2,Z3)");
formClass.addField(ResourceId.valueOf("X8")).setLabel("Likes the color blue?").setCode("BlueLover").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("X5==Z1");
formClass.addField(ResourceId.valueOf("X9")).setLabel("Likes the color red?").setCode("RedLover").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("X5==\'Z2\'");
final FormClass subFormClass = new FormClass(ResourceId.valueOf("F2"));
subFormClass.setLabel("Sub Form");
subFormClass.addField(ResourceId.valueOf("Y1")).setLabel("What is his name?").setType(TextType.SIMPLE).setCode("Name");
FormClassProvider provider = new FormClassProvider() {
@Override
public FormClass getFormClass(ResourceId formId) {
if (formId.equals(formClass.getId())) {
return formClass;
} else if (formId.equals(subFormClass.getId())) {
return subFormClass;
} else {
throw new IllegalArgumentException();
}
}
};
XlsFormBuilder writer = new XlsFormBuilder(provider);
writer.build(formClass.getId());
try (FileOutputStream fos = new FileOutputStream("test.xls")) {
writer.write(fos);
}
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormClass method fromJson.
public static FormClass fromJson(JsonValue object) {
// Deal with previous encoding
ResourceId id;
if (object.hasKey("@id")) {
id = ResourceId.valueOf(object.get("@id").asString());
} else {
id = ResourceId.valueOf(object.get("id").asString());
}
FormClass formClass = new FormClass(id);
if (object.hasKey("schemaVersion")) {
formClass.setSchemaVersion(object.get("schemaVersion").asLong());
}
if (object.hasKey("databaseId")) {
formClass.setDatabaseId(ResourceId.valueOf(object.get("databaseId").asString()));
}
if (object.hasKey("_class_label")) {
formClass.setLabel(JsonParsing.toNullableString(object.get("_class_label")));
} else {
formClass.setLabel(JsonParsing.toNullableString(object.get("label")));
}
if (object.hasKey("subFormKind")) {
formClass.setSubFormKind(SubFormKind.valueOf(object.get("subFormKind").asString().toUpperCase()));
formClass.setParentFormId(ResourceId.valueOf(object.get("parentFormId").asString()));
}
if (object.hasKey("elements")) {
JsonValue elements = object.get("elements");
if (elements.isJsonArray()) {
JsonValue elementsArray = elements;
formClass.elements.addAll(fromJsonArray(elementsArray));
}
}
return formClass;
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class QuantityTypeTest method deserialization.
/**
* Ensure QuantityType is parsed correctly from JSON.
* In particular, ensure that the "aggregation" field is correctly initialised from a legacy schema with no
* aggregation field within Quantity JSON element.
*
* @throws IOException
*/
@Test
public void deserialization() throws IOException {
FormClass formClass = parseResource();
ResourceId quantityFieldId = ResourceId.valueOf("i0000006090");
FormField quantityField = formClass.getField(quantityFieldId);
// Quantity Checks
assertThat(quantityField.getLabel(), equalTo("Number of water points constructed"));
assertThat(quantityField.isRequired(), is(false));
assertThat(quantityField.isVisible(), is(true));
// QuantityType Checks
assertThat(quantityField.getType(), instanceOf(QuantityType.class));
QuantityType quantityType = (QuantityType) quantityField.getType();
assertThat(quantityType.getUnits(), equalTo("Waterpoints"));
assertThat(quantityType.getAggregation(), is(notNullValue()));
assertThat(quantityType.getAggregation(), equalTo(QuantityType.Aggregation.SUM));
assertThat(quantityType.getAggregation().ordinal(), equalTo(0));
}
Aggregations