use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.
the class SchemaImporterV3 method processRows.
@Override
public boolean processRows() {
formMap.clear();
subFormMap.clear();
enumMap.clear();
refMap.clear();
fatalError = false;
for (SourceRow row : source.getRows()) {
try {
FormClass parentFormClass = getFormClass(row);
FormClass formClass = getSubFormClass(parentFormClass, row);
String type = formFieldType.get(row);
if (isEnum(type)) {
addChoice(formClass, row);
} else {
FieldType fieldType = parseFieldType(row);
FormField newField = addField(formClass, fieldType.getTypeClass(), row);
newField.setType(fieldType);
if (newField.getType() instanceof ReferenceType) {
refMap.put(formClass, newField);
}
}
} catch (UnableToParseRowException e) {
warnings.add(SafeHtmlUtils.fromString(e.getMessage()));
fatalError = true;
}
}
for (EnumBuilder enumBuilder : enumMap.values()) {
enumBuilder.formField.setType(new EnumType(enumBuilder.cardinality, enumBuilder.items));
}
if (validationCallback != null) {
validateReferences();
}
return !fatalError;
}
use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.
the class CurlExamplesGenerator method getFormSchema.
public static Example getFormSchema() {
FormClass exampleForm = new FormClass(ResourceId.generateId()).setDatabaseId(54).setLabel("NFI Distribution").setDescription("Form for collecting results of NFI distributions in North Kivu");
exampleForm.addField(ResourceId.generateId()).setLabel("Date of Distribution").setType(LocalDateType.INSTANCE).setRequired(true);
exampleForm.addField(CuidAdapter.partnerField(33)).setLabel("Partner").setType(ReferenceType.single(CuidAdapter.partnerFormId(54))).setRequired(true).setDescription("The implementing partner who conducted the distribution");
exampleForm.addField(ResourceId.generateId()).setLabel("Donor").setType(new EnumType(Cardinality.SINGLE, new EnumItem(ResourceId.generateId(), "USAID"), new EnumItem(ResourceId.generateId(), "DFID"), new EnumItem(ResourceId.generateId(), "ECHO"))).setRequired(true);
exampleForm.addField(ResourceId.generateId()).setLabel("Number of households receiving a kit").setType(new QuantityType("households")).setRequired(true);
return new Example("curl https://www.activityinfo.org/form/" + exampleForm.getId() + "/schema", exampleForm.toJson());
}
use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.
the class AttributeGroupDTO method asFormField.
@Override
public FormField asFormField() {
Cardinality cardinality = isMultipleAllowed() ? Cardinality.MULTIPLE : Cardinality.SINGLE;
List<EnumItem> values = Lists.newArrayList();
for (AttributeDTO attribute : getAttributes()) {
values.add(new EnumItem(CuidAdapter.attributeId(attribute.getId()), attribute.getName()));
}
return new FormField(CuidAdapter.attributeGroupField(getId())).setLabel(getName()).setType(new EnumType(cardinality, values)).setRequired(isMandatory());
}
use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.
the class AttributeDimBinding method findAttributeOrder.
private Map<String, Integer> findAttributeOrder(Optional<FormField> field) {
Map<String, Integer> map = new HashMap<>();
if (field.isPresent() && field.get().getType() instanceof EnumType) {
EnumType type = (EnumType) field.get().getType();
List<EnumItem> items = type.getValues();
for (int i = 0; i < items.size(); i++) {
map.put(items.get(i).getLabel(), i);
}
}
return map;
}
use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.
the class ActivityTest method orderIndicatorsActivities.
@Test
public void orderIndicatorsActivities() {
SchemaDTO schema = execute(new GetSchema());
UserDatabaseDTO db = schema.getDatabaseById(1);
LocationTypeDTO locType = schema.getCountryById(1).getLocationTypes().get(0);
ActivityFormDTO act = new ActivityFormDTO();
act.setName("Household Survey");
act.setLocationType(locType);
act.setReportingFrequency(ActivityFormDTO.REPORT_ONCE);
CreateResult createResult = execute(CreateEntity.Activity(db, act));
ResourceId classId = activityFormClass(createResult.getNewId());
FormClass formClass = assertResolves(locator.getFormClass(classId));
// create three new fields with an order that mixes "attributes" and "indicators"
FormField newField = new FormField(ResourceId.generateFieldId(QuantityType.TYPE_CLASS));
newField.setLabel("How old are you?");
newField.setType(new QuantityType().setUnits("years"));
formClass.addElement(newField);
FormField newGenderField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS));
newGenderField.setLabel("Gender");
EnumItem male = new EnumItem(EnumItem.generateId(), "Male");
EnumItem female = new EnumItem(EnumItem.generateId(), "Female");
newGenderField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(male, female)));
formClass.addElement(newGenderField);
FormField newTextField = new FormField(ResourceId.generateFieldId(TextType.TYPE_CLASS));
newTextField.setLabel("What is your name?");
newTextField.setType(TextType.SIMPLE);
formClass.addElement(newTextField);
assertResolves(locator.persist(formClass));
TFormClass reform = new TFormClass(assertResolves(locator.getFormClass(formClass.getId())));
System.out.println(Joiner.on("\n").join(reform.getFormClass().getFields()));
int a = reform.indexOfField("How old are you?");
int b = reform.indexOfField("Gender");
int c = reform.indexOfField("What is your name?");
assertTrue(a < b && b < c);
}
Aggregations