use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class UpdaterTest method missingValue.
@Test
public void missingValue() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = createObject();
fields.put("Q1", Json.createNull());
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertTrue(update.getChangedFieldValues().containsKey(fieldId));
}
use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class UpdaterTest method missingChangesProperty.
@Test(expected = InvalidUpdateException.class)
public void missingChangesProperty() {
JsonValue updateObject = createObject();
updater.execute(updateObject);
}
use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class JobResourceTest method startJob.
@Test
public void startJob() {
final org.activityinfo.json.JsonParser parser = new org.activityinfo.json.JsonParser();
final Queue queue = QueueFactory.getDefaultQueue();
final AuthenticatedUser user = new AuthenticatedUser("XYZ", 1, "akbertram@gmail.com");
final JobResource resource = new JobResource(Providers.of(user), queue);
// First request starts the job
final String jobId = ObjectifyService.run(new Work<String>() {
@Override
public String run() {
TableModel tableModel = ImmutableTableModel.builder().formId(ResourceId.valueOf("FORM1")).build();
ExportFormJob exportForm = new ExportFormJob(tableModel);
JobRequest request = new JobRequest(exportForm, "en");
Response response = resource.start(request.toJsonObject().toJson());
JsonValue resultObject = parser.parse((String) response.getEntity());
JobStatus result = JobStatus.fromJson(resultObject);
assertThat(result.getState(), equalTo(JobState.STARTED));
return result.getId();
}
});
// Second request retrieves status
ObjectifyService.run(new VoidWork() {
@Override
public void vrun() {
Response statusResponse = resource.get(jobId);
JsonValue statusObject = parser.parse(((String) statusResponse.getEntity()));
JobStatus status = JobStatus.fromJson(statusObject);
assertThat(status.getState(), equalTo(JobState.STARTED));
}
});
}
use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class MySqlCatalogTest method queryFormTree.
private FormTree queryFormTree(ResourceId classId) {
FormTreeBuilder builder = new FormTreeBuilder(catalog);
FormTree formTree = builder.queryTree(classId);
JsonValue formTreeObject = JsonFormTreeBuilder.toJson(formTree);
formTree = JsonFormTreeBuilder.fromJson(formTreeObject);
return formTree;
}
use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class SiteHistoryReader method parseChanges.
private Map<ResourceId, FieldValue> parseChanges(JsonValue jsonObject) {
Map<ResourceId, ResourceId> attributeToFieldMap = new HashMap<>();
for (ActivityField activityField : activity.getAttributeAndIndicatorFields()) {
if (activityField.getFormField().getType() instanceof EnumType) {
EnumType type = (EnumType) activityField.getFormField().getType();
for (EnumItem enumItem : type.getValues()) {
attributeToFieldMap.put(enumItem.getId(), activityField.getResourceId());
}
}
}
Map<ResourceId, FieldValue> valueMap = new HashMap<>();
Multimap<ResourceId, ResourceId> attributeValueMap = HashMultimap.create();
for (Map.Entry<String, JsonValue> jsonEntry : jsonObject.entrySet()) {
String fieldName = jsonEntry.getKey();
if (fieldName.equals("comments")) {
valueMap.put(fieldId(CuidAdapter.COMMENT_FIELD), NarrativeValue.valueOf(parseString(jsonEntry.getValue())));
} else if (fieldName.equals("date1")) {
valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
} else if (fieldName.equals("date2")) {
valueMap.put(fieldId(CuidAdapter.START_DATE_FIELD), parseDate(jsonEntry.getValue()));
} else if (fieldName.equals("partnerId")) {
valueMap.put(fieldId(CuidAdapter.PARTNER_FIELD), parseRef(jsonEntry.getValue(), activity.getPartnerFormClassId(), CuidAdapter.PARTNER_DOMAIN));
} else if (fieldName.equals("projectId")) {
valueMap.put(fieldId(CuidAdapter.PROJECT_FIELD), parseRef(jsonEntry.getValue(), activity.getProjectFormClassId(), CuidAdapter.PROJECT_DOMAIN));
} else if (fieldName.equals("locationId")) {
valueMap.put(fieldId(CuidAdapter.LOCATION_FIELD), parseRef(jsonEntry.getValue(), activity.getLocationFormClassId(), CuidAdapter.LOCATION_DOMAIN));
} else if (fieldName.startsWith("I")) {
int mIndex = fieldName.indexOf("M");
if (mIndex == -1) {
int indicatorId = Integer.parseInt(fieldName.substring(1));
ResourceId fieldId = CuidAdapter.indicatorField(indicatorId);
valueMap.put(fieldId, parseQuantity(jsonEntry.getValue()));
} else {
// old history
valueMap.put(ResourceId.valueOf(fieldName), parseQuantity(jsonEntry.getValue()));
}
} else if (fieldName.startsWith("ATTRIB")) {
if (parseBoolean(jsonEntry.getValue())) {
int attributeId = Integer.parseInt(fieldName.substring("ATTRIB".length()));
ResourceId attributeCuid = CuidAdapter.attributeId(attributeId);
ResourceId fieldId = attributeToFieldMap.get(attributeCuid);
if (fieldId != null) {
attributeValueMap.put(fieldId, attributeCuid);
}
}
}
}
for (ResourceId fieldId : attributeValueMap.keySet()) {
valueMap.put(fieldId, new EnumValue(attributeValueMap.get(fieldId)));
}
return valueMap;
}
Aggregations