Search in sources :

Example 56 with JsonValue

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));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FormField(org.activityinfo.model.form.FormField) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 57 with JsonValue

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);
}
Also used : JsonValue(org.activityinfo.json.JsonValue) Test(org.junit.Test)

Example 58 with JsonValue

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));
        }
    });
}
Also used : JsonValue(org.activityinfo.json.JsonValue) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) ExportFormJob(org.activityinfo.model.job.ExportFormJob) Response(javax.ws.rs.core.Response) JobStatus(org.activityinfo.model.job.JobStatus) JobRequest(org.activityinfo.model.job.JobRequest) VoidWork(com.googlecode.objectify.VoidWork) Queue(com.google.appengine.api.taskqueue.Queue) TableModel(org.activityinfo.model.analysis.TableModel) ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel) Test(org.junit.Test)

Example 59 with JsonValue

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;
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) JsonValue(org.activityinfo.json.JsonValue) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) JsonFormTreeBuilder(org.activityinfo.model.formTree.JsonFormTreeBuilder)

Example 60 with JsonValue

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;
}
Also used : HashMap(java.util.HashMap) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) JsonValue(org.activityinfo.json.JsonValue) ResourceId(org.activityinfo.model.resource.ResourceId) EnumType(org.activityinfo.model.type.enumerated.EnumType) FieldValue(org.activityinfo.model.type.FieldValue) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonValue (org.activityinfo.json.JsonValue)117 Test (org.junit.Test)24 ResourceId (org.activityinfo.model.resource.ResourceId)19 FormClass (org.activityinfo.model.form.FormClass)13 FormField (org.activityinfo.model.form.FormField)9 FieldValue (org.activityinfo.model.type.FieldValue)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 QuantityType (org.activityinfo.model.type.number.QuantityType)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3 JaxRsJsonReader (org.activityinfo.server.endpoint.rest.JaxRsJsonReader)3 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)3 EmbeddedEntity (com.google.appengine.api.datastore.EmbeddedEntity)2 URL (java.net.URL)2 JsonException (org.activityinfo.json.JsonException)2 JsonMappingException (org.activityinfo.json.JsonMappingException)2