Search in sources :

Example 21 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class JacksonValueDeserializer method readArrayInCollection.

@Override
protected <T> Collection<T> readArrayInCollection(JsonParser input, Function<JsonParser, T> deserializer, Collection<T> collection) throws Exception {
    JsonToken token = input.getCurrentToken();
    if (token == JsonToken.VALUE_NULL) {
        return null;
    }
    if (token != JsonToken.START_ARRAY) {
        token = input.nextToken();
    }
    if (token == JsonToken.VALUE_NULL) {
        return null;
    }
    if (token != JsonToken.START_ARRAY) {
        throw new ValueSerializationException("Expected an array start at " + input.getCurrentLocation().toString());
    }
    while (input.nextToken() != JsonToken.END_ARRAY) {
        T element = deserializer.map(input);
        collection.add(element);
    }
    return collection;
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 22 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class StaxValueDeserializer method putArrayNodeInCollection.

@Override
protected <T> void putArrayNodeInCollection(Node inputNode, Function<Node, T> deserializer, Collection<T> collection) throws Exception {
    if (inputNode == null) {
        return;
    }
    if (!(inputNode instanceof Element)) {
        throw new ValueSerializationException("Expected an Element but got " + inputNode);
    }
    NodeList arrayValues = inputNode.getChildNodes();
    for (int arrayValuesIndex = 0; arrayValuesIndex < arrayValues.getLength(); arrayValuesIndex++) {
        Node arrayValue = arrayValues.item(arrayValuesIndex);
        T value = deserializer.map(arrayValue.getFirstChild());
        collection.add(value);
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Example 23 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class StaxValueDeserializer method readPlainValue.

@Override
protected Object readPlainValue(XMLEventReader input) throws Exception {
    if (!input.hasNext()) {
        return null;
    }
    XMLEvent nextEvent = input.nextEvent();
    if (nextEvent.getEventType() == XMLEvent.START_ELEMENT && "null".equals(nextEvent.asStartElement().getName().getLocalPart())) {
        input.nextTag();
        return null;
    }
    if (nextEvent.getEventType() != XMLEvent.CHARACTERS) {
        throw new ValueSerializationException("Expected characters but got: " + nextEvent);
    }
    String stringValue = nextEvent.asCharacters().getData();
    return detectAndConvertStringValue(stringValue);
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Example 24 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class StaxValueDeserializer method getObjectFieldValue.

@Override
@SuppressWarnings("unchecked")
protected <T> T getObjectFieldValue(Node inputNode, String key, Function<Node, T> valueDeserializer) throws Exception {
    if (inputNode == null) {
        return null;
    }
    Node objectNode;
    if ("value".equals(inputNode.getLocalName())) {
        objectNode = getDirectChildNode(inputNode, "object");
    } else {
        objectNode = inputNode;
    }
    if (objectNode == null) {
        return null;
    }
    if (!"object".equals(objectNode.getLocalName())) {
        throw new ValueSerializationException("Expected an object value but got: " + objectNode);
    }
    Node fieldNode = getObjectFieldNode(objectNode, key);
    if (fieldNode == null) {
        return null;
    }
    Node valueElement = getDirectChildNode(fieldNode, "value");
    Node valueNode = valueElement.getFirstChild();
    if (valueNode == null) {
        return (T) "";
    }
    if (valueNode.getNodeType() == Node.ELEMENT_NODE && "null".equals(valueNode.getLocalName())) {
        return null;
    }
    T value = valueDeserializer.map(valueNode);
    return value;
}
Also used : Node(org.w3c.dom.Node) ValueSerializationException(org.qi4j.api.value.ValueSerializationException)

Example 25 with ValueSerializationException

use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.

the class EntityResource method post.

@Override
public Representation post(Representation entityRepresentation, Variant variant) throws ResourceException {
    Usecase usecase = UsecaseBuilder.newUsecase("Update entity");
    EntityStoreUnitOfWork unitOfWork = entityStore.newUnitOfWork(usecase, module, System.currentTimeMillis());
    EntityState entity = getEntityState(unitOfWork);
    Form form = new Form(entityRepresentation);
    try {
        final EntityDescriptor descriptor = entity.entityDescriptor();
        // Parse JSON into properties
        for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
            if (!persistentProperty.isImmutable()) {
                String formValue = form.getFirstValue(persistentProperty.qualifiedName().name(), null);
                if (formValue == null) {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), null);
                } else {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), valueSerialization.deserialize(persistentProperty.valueType(), formValue));
                }
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().associations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            if (newStringAssociation == null || newStringAssociation.isEmpty()) {
                entity.setAssociationValue(associationType.qualifiedName(), null);
            } else {
                entity.setAssociationValue(associationType.qualifiedName(), EntityReference.parseEntityReference(newStringAssociation));
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            ManyAssociationState manyAssociation = entity.manyAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (EntityReference entityReference : manyAssociation) {
                    manyAssociation.remove(entityReference);
                }
                continue;
            }
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String identity;
            try {
                // Synchronize old and new association
                int index = 0;
                while ((identity = bufferedReader.readLine()) != null) {
                    EntityReference reference = new EntityReference(identity);
                    if (manyAssociation.count() < index && manyAssociation.get(index).equals(reference)) {
                        continue;
                    }
                    try {
                        unitOfWork.entityStateOf(reference);
                        manyAssociation.remove(reference);
                        manyAssociation.add(index++, reference);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                while (manyAssociation.count() > index) {
                    manyAssociation.remove(manyAssociation.get(index));
                }
            } catch (IOException e) {
            // Ignore
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            NamedAssociationState namedAssociation = entity.namedAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (String name : namedAssociation) {
                    namedAssociation.remove(name);
                }
                continue;
            }
            Set<String> names = new HashSet<>();
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    String name = line;
                    line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    String identity = line;
                    EntityReference reference = new EntityReference(identity);
                    try {
                        unitOfWork.entityStateOf(reference);
                        namedAssociation.remove(name);
                        namedAssociation.put(name, reference);
                        names.add(name);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                for (String assocName : Iterables.toList(namedAssociation)) {
                    if (!names.contains(assocName)) {
                        namedAssociation.remove(assocName);
                    }
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    } catch (ValueSerializationException | IllegalArgumentException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    try {
        unitOfWork.applyChanges().commit();
    } catch (ConcurrentEntityStateModificationException e) {
        throw new ResourceException(Status.CLIENT_ERROR_CONFLICT);
    } catch (EntityNotFoundException e) {
        throw new ResourceException(Status.CLIENT_ERROR_GONE);
    }
    getResponse().setStatus(Status.SUCCESS_RESET_CONTENT);
    return new EmptyRepresentation();
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) Form(org.restlet.data.Form) NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) EntityState(org.qi4j.spi.entity.EntityState) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) IOException(java.io.IOException) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) EntityReference(org.qi4j.api.entity.EntityReference) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ResourceException(org.restlet.resource.ResourceException) Usecase(org.qi4j.api.usecase.Usecase) HashSet(java.util.HashSet)

Aggregations

ValueSerializationException (org.qi4j.api.value.ValueSerializationException)25 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)6 ValueType (org.qi4j.api.type.ValueType)5 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)5 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 Node (org.w3c.dom.Node)4 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 EntityReference (org.qi4j.api.entity.EntityReference)3 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Text (com.google.appengine.api.datastore.Text)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2