Search in sources :

Example 16 with ValueSerializationException

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

the class GaeEntityState method setPropertyValue.

@Override
public void setPropertyValue(QualifiedName stateName, Object newValue) {
    System.out.println("setProperty( " + stateName + ", " + newValue + " )");
    Object value = null;
    if (newValue == null || ValueType.isPrimitiveValue(newValue)) {
        value = newValue;
    } else {
        try {
            value = valueSerialization.serialize(newValue);
        } catch (ValueSerializationException e) {
            String message = "\nqualifiedName: " + stateName + "\n    stateName: " + stateName.name() + "\n        class: " + newValue.getClass() + "\n        value: " + value + "\n";
            InternalError error = new InternalError(message);
            error.initCause(e);
            throw error;
        }
    }
    if (value instanceof String) {
        value = new Text((String) value);
    }
    entity.setUnindexedProperty(stateName.toURI(), value);
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) Text(com.google.appengine.api.datastore.Text)

Example 17 with ValueSerializationException

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

the class PreferencesEntityStoreMixin method writeEntityState.

protected void writeEntityState(DefaultEntityState state, Preferences entityPrefs, String identity, long lastModified) throws EntityStoreException {
    try {
        // Store into Preferences API
        entityPrefs.put("type", first(state.entityDescriptor().types()).getName());
        entityPrefs.put("version", identity);
        entityPrefs.putLong("modified", lastModified);
        // Properties
        Preferences propsPrefs = entityPrefs.node("properties");
        for (PropertyDescriptor persistentProperty : state.entityDescriptor().state().properties()) {
            if (persistentProperty.qualifiedName().name().equals("identity")) {
                // Skip Identity.identity()
                continue;
            }
            Object value = state.properties().get(persistentProperty.qualifiedName());
            if (value == null) {
                propsPrefs.remove(persistentProperty.qualifiedName().name());
            } else {
                ValueType valueType = persistentProperty.valueType();
                Class<?> mainType = valueType.mainType();
                if (Number.class.isAssignableFrom(mainType)) {
                    if (mainType.equals(Long.class)) {
                        propsPrefs.putLong(persistentProperty.qualifiedName().name(), (Long) value);
                    } else if (mainType.equals(Integer.class)) {
                        propsPrefs.putInt(persistentProperty.qualifiedName().name(), (Integer) value);
                    } else if (mainType.equals(Double.class)) {
                        propsPrefs.putDouble(persistentProperty.qualifiedName().name(), (Double) value);
                    } else if (mainType.equals(Float.class)) {
                        propsPrefs.putFloat(persistentProperty.qualifiedName().name(), (Float) value);
                    } else {
                        // Store as string even though it's a number
                        String jsonString = valueSerialization.serialize(value);
                        propsPrefs.put(persistentProperty.qualifiedName().name(), jsonString);
                    }
                } else if (mainType.equals(Boolean.class)) {
                    propsPrefs.putBoolean(persistentProperty.qualifiedName().name(), (Boolean) value);
                } else if (valueType instanceof ValueCompositeType || valueType instanceof MapType || valueType instanceof CollectionType || valueType instanceof EnumType) {
                    String jsonString = valueSerialization.serialize(value);
                    propsPrefs.put(persistentProperty.qualifiedName().name(), jsonString);
                } else {
                    String jsonString = valueSerialization.serialize(value);
                    propsPrefs.put(persistentProperty.qualifiedName().name(), jsonString);
                }
            }
        }
        // Associations
        if (!state.associations().isEmpty()) {
            Preferences assocsPrefs = entityPrefs.node("associations");
            for (Map.Entry<QualifiedName, EntityReference> association : state.associations().entrySet()) {
                if (association.getValue() == null) {
                    assocsPrefs.remove(association.getKey().name());
                } else {
                    assocsPrefs.put(association.getKey().name(), association.getValue().identity());
                }
            }
        }
        // ManyAssociations
        if (!state.manyAssociations().isEmpty()) {
            Preferences manyAssocsPrefs = entityPrefs.node("manyassociations");
            for (Map.Entry<QualifiedName, List<EntityReference>> manyAssociation : state.manyAssociations().entrySet()) {
                StringBuilder manyAssocs = new StringBuilder();
                for (EntityReference entityReference : manyAssociation.getValue()) {
                    if (manyAssocs.length() > 0) {
                        manyAssocs.append("\n");
                    }
                    manyAssocs.append(entityReference.identity());
                }
                if (manyAssocs.length() > 0) {
                    manyAssocsPrefs.put(manyAssociation.getKey().name(), manyAssocs.toString());
                }
            }
        }
        // NamedAssociations
        if (!state.namedAssociations().isEmpty()) {
            Preferences namedAssocsPrefs = entityPrefs.node("namedassociations");
            for (Map.Entry<QualifiedName, Map<String, EntityReference>> namedAssociation : state.namedAssociations().entrySet()) {
                StringBuilder namedAssocs = new StringBuilder();
                for (Map.Entry<String, EntityReference> namedRef : namedAssociation.getValue().entrySet()) {
                    if (namedAssocs.length() > 0) {
                        namedAssocs.append("\n");
                    }
                    namedAssocs.append(namedRef.getKey()).append("\n").append(namedRef.getValue().identity());
                }
                if (namedAssocs.length() > 0) {
                    namedAssocsPrefs.put(namedAssociation.getKey().name(), namedAssocs.toString());
                }
            }
        }
    } catch (ValueSerializationException e) {
        throw new EntityStoreException("Could not store EntityState", e);
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueType(org.qi4j.api.type.ValueType) QualifiedName(org.qi4j.api.common.QualifiedName) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) MapType(org.qi4j.api.type.MapType) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) EntityReference(org.qi4j.api.entity.EntityReference) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Preferences(java.util.prefs.Preferences) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 18 with ValueSerializationException

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

the class JacksonValueDeserializer method putArrayNodeInMap.

@Override
protected <K, V> void putArrayNodeInMap(JsonNode inputNode, Function<JsonNode, K> keyDeserializer, Function<JsonNode, V> valueDeserializer, Map<K, V> map) throws Exception {
    if (isNullOrMissing(inputNode)) {
        return;
    }
    if (!inputNode.isArray()) {
        throw new ValueSerializationException("Expected an array but got " + inputNode);
    }
    ArrayNode array = (ArrayNode) inputNode;
    for (JsonNode item : array) {
        if (!item.isObject()) {
            throw new ValueSerializationException("Expected an object but got " + inputNode);
        }
        JsonNode keyNode = item.get("key");
        JsonNode valueNode = item.get("value");
        K key = keyDeserializer.map(keyNode);
        V value = valueDeserializer.map(valueNode);
        if (key != null) {
            map.put(key, value);
        }
    }
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 19 with ValueSerializationException

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

the class JacksonValueDeserializer method putArrayNodeInCollection.

@Override
protected <T> void putArrayNodeInCollection(JsonNode inputNode, Function<JsonNode, T> deserializer, Collection<T> collection) throws Exception {
    if (isNullOrMissing(inputNode)) {
        return;
    }
    if (!inputNode.isArray()) {
        throw new ValueSerializationException("Expected an array but got " + inputNode);
    }
    ArrayNode array = (ArrayNode) inputNode;
    for (JsonNode item : array) {
        T value = deserializer.map(item);
        collection.add(value);
    }
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 20 with ValueSerializationException

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

the class JacksonValueDeserializer method readMapInMap.

@Override
protected <K, V> Map<K, V> readMapInMap(JsonParser input, Function<JsonParser, K> keyDeserializer, Function<JsonParser, V> valueDeserializer, Map<K, V> map) 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());
    }
    JsonToken currentToken = input.nextToken();
    while (currentToken != JsonToken.END_ARRAY) {
        if (currentToken != JsonToken.START_OBJECT) {
            throw new ValueSerializationException("Expected an object start at " + input.getCurrentLocation().toString());
        }
        currentToken = input.nextToken();
        K key = null;
        V value = null;
        while (currentToken != JsonToken.END_OBJECT) {
            String objectKey = input.getCurrentName();
            input.nextToken();
            if ("key".equals(objectKey)) {
                key = keyDeserializer.map(input);
            } else if ("value".equals(objectKey)) {
                value = valueDeserializer.map(input);
            } else {
                //input.nextToken();
                input.skipChildren();
            }
            currentToken = input.nextToken();
        }
        if (key != null) {
            map.put(key, value);
        }
        currentToken = input.nextToken();
    }
    return map;
}
Also used : ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JsonToken(com.fasterxml.jackson.core.JsonToken)

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