Search in sources :

Example 51 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class JaxRsJsonReaderTest method modelClass.

@Test
public void modelClass() throws IOException {
    FormClass formClass = new FormClass(ResourceId.valueOf("FORM"));
    formClass.setLabel("My Form");
    formClass.addField(ResourceId.valueOf("F1")).setLabel("My field").setRequired(true).setType(TextType.SIMPLE).setCode("MY");
    JaxRsJsonReader reader = new JaxRsJsonReader();
    assertTrue(reader.isReadable(FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    assertTrue(reader.isWriteable(FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    ByteArrayOutputStream entity = new ByteArrayOutputStream();
    reader.writeTo(formClass, formClass.getClass(), formClass.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
    JsonValue jsonObject = Json.parse(new String(entity.toByteArray(), Charsets.UTF_8));
    System.out.println(Json.stringify(jsonObject));
    FormClass reformClass = (FormClass) reader.readFrom((Class) FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 52 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class JaxRsJsonReaderTest method jsonValue.

@Test
public void jsonValue() throws IOException {
    JaxRsJsonReader reader = new JaxRsJsonReader();
    assertTrue(reader.isReadable(JsonValue.class, JsonValue.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    assertTrue(reader.isWriteable(JsonValue.class, JsonValue.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    JsonValue object = Json.createObject();
    object.put("b", true);
    object.put("s", "hello WOrld");
    object.put("i", 42);
    ByteArrayOutputStream entity = new ByteArrayOutputStream();
    reader.writeTo(object, object.getClass(), object.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
    JsonValue reobject = (JsonValue) reader.readFrom((Class) JsonValue.class, JsonValue.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
    assertThat(reobject.getBoolean("b"), equalTo(true));
    assertThat(reobject.getNumber("i"), equalTo(42.0));
    assertThat(reobject.getString("s"), equalTo("hello WOrld"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 53 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class JaxRsJsonReaderTest method jsTypeTest.

@Test
public void jsTypeTest() throws IOException {
    JaxRsJsonReader reader = new JaxRsJsonReader();
    assertTrue(reader.isReadable(DummyJsObject.class, DummyJsObject.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    assertTrue(reader.isWriteable(DummyJsObject.class, DummyJsObject.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
    DummyJsObject object = new DummyJsObject();
    object.b = true;
    object.s = "Hello world";
    object.i = 99;
    ByteArrayOutputStream entity = new ByteArrayOutputStream();
    reader.writeTo(object, object.getClass(), object.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
    JsonValue jsonValue = Json.parse(new String(entity.toByteArray(), Charsets.UTF_8));
    assertThat(jsonValue.getString("s"), equalTo("Hello world"));
    DummyJsObject reobject = (DummyJsObject) reader.readFrom((Class) DummyJsObject.class, DummyJsObject.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
    assertThat(reobject.b, equalTo(object.b));
    assertThat(reobject.d, equalTo(object.d));
    assertThat(reobject.s, equalTo(object.s));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 54 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class MySqlRecordHistoryBuilder method queryLabels.

private List<String> queryLabels(ReferenceType type, ReferenceValue value) {
    Map<ResourceId, String> labelMap = new HashMap<>();
    for (ResourceId formId : type.getRange()) {
        Optional<FormStorage> form = catalog.getForm(formId);
        if (form.isPresent()) {
            Optional<ResourceId> labelFieldId = findLabelField(form.get().getFormClass());
            for (RecordRef ref : value.getReferences()) {
                Optional<FormRecord> record = form.get().get(ref.getRecordId());
                if (record.isPresent()) {
                    JsonValue labelValue = Json.createNull();
                    if (labelFieldId.isPresent()) {
                        labelValue = record.get().getFields().get(labelFieldId.get().asString());
                    }
                    if (labelValue.isJsonNull()) {
                        labelValue = record.get().getFields().get(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD).asString());
                    }
                    if (labelValue.isJsonPrimitive()) {
                        labelMap.put(ref.getRecordId(), labelValue.asString());
                    }
                }
            }
        }
    }
    List<String> list = new ArrayList<>();
    for (RecordRef ref : value.getReferences()) {
        String label = labelMap.get(ref.getRecordId());
        if (label == null) {
            list.add(ref.toQualifiedString());
        } else {
            list.add(label);
        }
    }
    return list;
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) JsonValue(org.activityinfo.json.JsonValue) ResourceId(org.activityinfo.model.resource.ResourceId)

Example 55 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class FormConverter method fromPropertyValue.

public static JsonValue fromPropertyValue(Object propertyValue) {
    if (propertyValue == null) {
        return Json.createNull();
    } else if (propertyValue instanceof EmbeddedEntity) {
        return fromEmbeddedEntity(((EmbeddedEntity) propertyValue));
    } else if (propertyValue instanceof List) {
        List<Object> propertyValueList = (List<Object>) propertyValue;
        JsonValue convertedList = Json.createArray();
        for (Object propertyValueListItem : propertyValueList) {
            convertedList.add(fromPropertyValue(propertyValueListItem));
        }
        return convertedList;
    } else if (propertyValue instanceof String) {
        return Json.create((String) propertyValue);
    } else if (propertyValue instanceof Number) {
        return Json.create(((Number) propertyValue).doubleValue());
    } else if (propertyValue instanceof Boolean) {
        return Json.create((Boolean) propertyValue);
    } else {
        throw new UnsupportedOperationException("type: " + propertyValue.getClass().getName());
    }
}
Also used : JsonValue(org.activityinfo.json.JsonValue) List(java.util.List) EmbeddedEntity(com.google.appengine.api.datastore.EmbeddedEntity)

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