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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations