use of org.qi4j.api.value.ValueSerializationException 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.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class PreferencesEntityStoreMixin method entityStateOf.
@Override
public EntityState entityStateOf(EntityStoreUnitOfWork unitOfWork, EntityReference identity) {
try {
DefaultEntityStoreUnitOfWork desuw = (DefaultEntityStoreUnitOfWork) unitOfWork;
Module module = desuw.module();
if (!root.nodeExists(identity.identity())) {
throw new NoSuchEntityException(identity, UnknownType.class);
}
Preferences entityPrefs = root.node(identity.identity());
String type = entityPrefs.get("type", null);
EntityStatus status = EntityStatus.LOADED;
EntityDescriptor entityDescriptor = module.entityDescriptor(type);
if (entityDescriptor == null) {
throw new EntityTypeNotFoundException(type);
}
Map<QualifiedName, Object> properties = new HashMap<>();
Preferences propsPrefs = null;
for (PropertyDescriptor persistentPropertyDescriptor : entityDescriptor.state().properties()) {
if (persistentPropertyDescriptor.qualifiedName().name().equals("identity")) {
// Fake identity property
properties.put(persistentPropertyDescriptor.qualifiedName(), identity.identity());
continue;
}
if (propsPrefs == null) {
propsPrefs = entityPrefs.node("properties");
}
ValueType propertyType = persistentPropertyDescriptor.valueType();
Class<?> mainType = propertyType.mainType();
if (Number.class.isAssignableFrom(mainType)) {
if (mainType.equals(Long.class)) {
properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, LONG_PARSER));
} else if (mainType.equals(Integer.class)) {
properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, INT_PARSER));
} else if (mainType.equals(Double.class)) {
properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, DOUBLE_PARSER));
} else if (mainType.equals(Float.class)) {
properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, FLOAT_PARSER));
} else {
// Load as string even though it's a number
String json = propsPrefs.get(persistentPropertyDescriptor.qualifiedName().name(), null);
Object value;
if (json == null) {
value = null;
} else {
value = valueSerialization.deserialize(persistentPropertyDescriptor.valueType(), json);
}
properties.put(persistentPropertyDescriptor.qualifiedName(), value);
}
} else if (mainType.equals(Boolean.class)) {
Boolean initialValue = (Boolean) persistentPropertyDescriptor.initialValue(module);
properties.put(persistentPropertyDescriptor.qualifiedName(), propsPrefs.getBoolean(persistentPropertyDescriptor.qualifiedName().name(), initialValue == null ? false : initialValue));
} else if (propertyType instanceof ValueCompositeType || propertyType instanceof MapType || propertyType instanceof CollectionType || propertyType instanceof EnumType) {
String json = propsPrefs.get(persistentPropertyDescriptor.qualifiedName().name(), null);
Object value;
if (json == null) {
value = null;
} else {
value = valueSerialization.deserialize(persistentPropertyDescriptor.valueType(), json);
}
properties.put(persistentPropertyDescriptor.qualifiedName(), value);
} else {
String json = propsPrefs.get(persistentPropertyDescriptor.qualifiedName().name(), null);
if (json == null) {
if (persistentPropertyDescriptor.initialValue(module) != null) {
properties.put(persistentPropertyDescriptor.qualifiedName(), persistentPropertyDescriptor.initialValue(module));
} else {
properties.put(persistentPropertyDescriptor.qualifiedName(), null);
}
} else {
Object value = valueSerialization.deserialize(propertyType, json);
properties.put(persistentPropertyDescriptor.qualifiedName(), value);
}
}
}
// Associations
Map<QualifiedName, EntityReference> associations = new HashMap<>();
Preferences assocs = null;
for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
if (assocs == null) {
assocs = entityPrefs.node("associations");
}
String associatedEntity = assocs.get(associationType.qualifiedName().name(), null);
EntityReference value = associatedEntity == null ? null : EntityReference.parseEntityReference(associatedEntity);
associations.put(associationType.qualifiedName(), value);
}
// ManyAssociations
Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
Preferences manyAssocs = null;
for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
if (manyAssocs == null) {
manyAssocs = entityPrefs.node("manyassociations");
}
List<EntityReference> references = new ArrayList<>();
String entityReferences = manyAssocs.get(manyAssociationType.qualifiedName().name(), null);
if (entityReferences == null) {
// ManyAssociation not found, default to empty one
manyAssociations.put(manyAssociationType.qualifiedName(), references);
} else {
String[] refs = entityReferences.split("\n");
for (String ref : refs) {
EntityReference value = ref == null ? null : EntityReference.parseEntityReference(ref);
references.add(value);
}
manyAssociations.put(manyAssociationType.qualifiedName(), references);
}
}
// NamedAssociations
Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
Preferences namedAssocs = null;
for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
if (namedAssocs == null) {
namedAssocs = entityPrefs.node("namedassociations");
}
Map<String, EntityReference> references = new LinkedHashMap<>();
String entityReferences = namedAssocs.get(namedAssociationType.qualifiedName().name(), null);
if (entityReferences == null) {
// NamedAssociation not found, default to empty one
namedAssociations.put(namedAssociationType.qualifiedName(), references);
} else {
String[] namedRefs = entityReferences.split("\n");
if (namedRefs.length % 2 != 0) {
throw new EntityStoreException("Invalid NamedAssociation storage format");
}
for (int idx = 0; idx < namedRefs.length; idx += 2) {
String name = namedRefs[idx];
String ref = namedRefs[idx + 1];
references.put(name, EntityReference.parseEntityReference(ref));
}
namedAssociations.put(namedAssociationType.qualifiedName(), references);
}
}
return new DefaultEntityState(desuw, entityPrefs.get("version", ""), entityPrefs.getLong("modified", unitOfWork.currentTime()), identity, status, entityDescriptor, properties, associations, manyAssociations, namedAssociations);
} catch (ValueSerializationException | BackingStoreException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class NeoEntityState method propertyValueOf.
@Override
public Object propertyValueOf(QualifiedName stateName) {
try {
PropertyDescriptor persistentProperty = entityDescriptor().state().findPropertyModelByQualifiedName(stateName);
Object prop = underlyingNode.getProperty("prop::" + stateName.toString(), null);
if (prop == null) {
return null;
} else if (ValueType.isPrimitiveValueType(persistentProperty.valueType())) {
return prop;
} else {
return valueSerialization.deserialize(persistentProperty.valueType(), prop.toString());
}
} catch (ValueSerializationException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class NeoEntityState method setPropertyValue.
@Override
public void setPropertyValue(QualifiedName stateName, Object prop) {
try {
if (prop != null) {
PropertyDescriptor persistentProperty = entityDescriptor().state().findPropertyModelByQualifiedName(stateName);
if (ValueType.isPrimitiveValueType(persistentProperty.valueType())) {
underlyingNode.setProperty("prop::" + stateName.toString(), prop);
} else {
String jsonString = valueSerialization.serialize(prop);
underlyingNode.setProperty("prop::" + stateName.toString(), jsonString);
}
} else {
underlyingNode.removeProperty(stateName.toString());
}
setUpdated();
} catch (ValueSerializationException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.value.ValueSerializationException in project qi4j-sdk by Qi4j.
the class GaeEntityState method propertyValueOf.
@Override
public Object propertyValueOf(QualifiedName stateName) {
String uri = stateName.toURI();
Object value = entity.getProperty(uri);
if (value instanceof Text) {
value = ((Text) value).getValue();
}
ValueType type = valueTypes.get(stateName);
if (value != null && type != null) {
try {
value = valueSerialization.deserialize(type, value.toString());
} catch (ValueSerializationException e) {
String message = "\nqualifiedName: " + stateName + "\n stateName: " + stateName.name() + "\n uri: " + uri + "\n type: " + type + "\n value: " + value + "\n";
InternalError error = new InternalError(message);
error.initCause(e);
throw error;
}
}
System.out.println("getProperty( " + stateName + " ) --> " + uri + "=" + value);
return value;
}
Aggregations