Search in sources :

Example 21 with EntityStoreException

use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.

the class JSONMapEntityStoreMixin method fetchCachedState.

private EntityState fetchCachedState(EntityReference identity, DefaultEntityStoreUnitOfWork unitOfWork) {
    CacheState cacheState = cache.get(identity.identity());
    if (cacheState != null) {
        JSONObject data = cacheState.json;
        try {
            String type = data.getString(JSONKeys.TYPE);
            EntityDescriptor entityDescriptor = unitOfWork.module().entityDescriptor(type);
            return new JSONEntityState(unitOfWork, valueSerialization, identity, entityDescriptor, data);
        } catch (JSONException e) {
            // Should not be able to happen, unless internal error in the cache system.
            throw new EntityStoreException(e);
        }
    }
    return null;
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 22 with EntityStoreException

use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.

the class MapEntityStoreMixin method writeEntityState.

protected void writeEntityState(DefaultEntityState state, Writer writer, String version, long lastModified) throws EntityStoreException {
    try {
        JSONWriter json = new JSONWriter(writer);
        JSONWriter properties = json.object().key(JSONKeys.IDENTITY).value(state.identity().identity()).key(JSONKeys.APPLICATION_VERSION).value(application.version()).key(JSONKeys.TYPE).value(first(state.entityDescriptor().types()).getName()).key(JSONKeys.VERSION).value(version).key(JSONKeys.MODIFIED).value(lastModified).key(JSONKeys.PROPERTIES).object();
        EntityDescriptor entityType = state.entityDescriptor();
        for (PropertyDescriptor persistentProperty : entityType.state().properties()) {
            Object value = state.properties().get(persistentProperty.qualifiedName());
            json.key(persistentProperty.qualifiedName().name());
            if (value == null || ValueType.isPrimitiveValue(value)) {
                json.value(value);
            } else {
                String serialized = valueSerialization.serialize(value);
                if (serialized.startsWith("{")) {
                    json.value(new JSONObject(serialized));
                } else if (serialized.startsWith("[")) {
                    json.value(new JSONArray(serialized));
                } else {
                    json.value(serialized);
                }
            }
        }
        JSONWriter associations = properties.endObject().key(JSONKeys.ASSOCIATIONS).object();
        for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations().entrySet()) {
            EntityReference value = stateNameEntityReferenceEntry.getValue();
            associations.key(stateNameEntityReferenceEntry.getKey().name()).value(value != null ? value.identity() : null);
        }
        JSONWriter manyAssociations = associations.endObject().key(JSONKeys.MANY_ASSOCIATIONS).object();
        for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations().entrySet()) {
            JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
            for (EntityReference entityReference : stateNameListEntry.getValue()) {
                assocs.value(entityReference.identity());
            }
            assocs.endArray();
        }
        JSONWriter namedAssociations = manyAssociations.endObject().key(JSONKeys.NAMED_ASSOCIATIONS).object();
        for (Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry : state.namedAssociations().entrySet()) {
            JSONWriter assocs = namedAssociations.key(stateNameMapEntry.getKey().name()).object();
            for (Map.Entry<String, EntityReference> namedRef : stateNameMapEntry.getValue().entrySet()) {
                assocs.key(namedRef.getKey()).value(namedRef.getValue().identity());
            }
            assocs.endObject();
        }
        namedAssociations.endObject().endObject();
    } catch (JSONException e) {
        throw new EntityStoreException("Could not store EntityState", e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) JSONObject(org.json.JSONObject) EntityReference(org.qi4j.api.entity.EntityReference) JSONObject(org.json.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 23 with EntityStoreException

use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.

the class JSONEntityState method manyAssociationValueOf.

@Override
public ManyAssociationState manyAssociationValueOf(QualifiedName stateName) {
    try {
        JSONObject manyAssociations = state.getJSONObject(JSONKeys.MANY_ASSOCIATIONS);
        JSONArray jsonValues = manyAssociations.optJSONArray(stateName.name());
        if (jsonValues == null) {
            jsonValues = new JSONArray();
            manyAssociations.put(stateName.name(), jsonValues);
        }
        return new JSONManyAssociationState(this, jsonValues);
    } catch (JSONException e) {
        throw new EntityStoreException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 24 with EntityStoreException

use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.

the class JSONEntityState method setPropertyValue.

@Override
public void setPropertyValue(QualifiedName stateName, Object newValue) {
    try {
        Object jsonValue;
        if (newValue == null || ValueType.isPrimitiveValue(newValue)) {
            jsonValue = newValue;
        } else {
            String serialized = valueSerialization.serialize(newValue);
            if (serialized.startsWith("{")) {
                jsonValue = new JSONObject(serialized);
            } else if (serialized.startsWith("[")) {
                jsonValue = new JSONArray(serialized);
            } else {
                jsonValue = serialized;
            }
        }
        cloneStateIfGlobalStateLoaded();
        state.getJSONObject(JSONKeys.PROPERTIES).put(stateName.name(), jsonValue);
        markUpdated();
    } catch (ValueSerializationException | JSONException e) {
        throw new EntityStoreException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 25 with EntityStoreException

use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.

the class JSONEntityState method setAssociationValue.

@Override
public void setAssociationValue(QualifiedName stateName, EntityReference newEntity) {
    try {
        cloneStateIfGlobalStateLoaded();
        state.getJSONObject(JSONKeys.ASSOCIATIONS).put(stateName.name(), newEntity == null ? null : newEntity.identity());
        markUpdated();
    } catch (JSONException e) {
        throw new EntityStoreException(e);
    }
}
Also used : JSONException(org.json.JSONException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Aggregations

EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)35 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)13 EntityReference (org.qi4j.api.entity.EntityReference)9 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)9 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Map (java.util.Map)6 JSONArray (org.json.JSONArray)6 QualifiedName (org.qi4j.api.common.QualifiedName)6 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)5 StringReader (java.io.StringReader)4 Connection (java.sql.Connection)4 Module (org.qi4j.api.structure.Module)4