use of org.qi4j.api.property.PropertyDescriptor 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.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class SQLEntityStoreMixin method readEntityState.
protected DefaultEntityState readEntityState(DefaultEntityStoreUnitOfWork unitOfWork, Reader entityState) throws EntityStoreException {
try {
Module module = unitOfWork.module();
JSONObject jsonObject = new JSONObject(new JSONTokener(entityState));
EntityStatus status = EntityStatus.LOADED;
String version = jsonObject.getString(JSONKeys.VERSION);
long modified = jsonObject.getLong(JSONKeys.MODIFIED);
String identity = jsonObject.getString(JSONKeys.IDENTITY);
// Check if version is correct
String currentAppVersion = jsonObject.optString(JSONKeys.APPLICATION_VERSION, "0.0");
if (!currentAppVersion.equals(application.version())) {
if (migration != null) {
migration.migrate(jsonObject, application.version(), this);
} else {
// Do nothing - set version to be correct
jsonObject.put(JSONKeys.APPLICATION_VERSION, application.version());
}
LOGGER.trace("Updated version nr on {} from {} to {}", identity, currentAppVersion, application.version());
// State changed
status = EntityStatus.UPDATED;
}
String type = jsonObject.getString(JSONKeys.TYPE);
EntityDescriptor entityDescriptor = module.entityDescriptor(type);
if (entityDescriptor == null) {
throw new EntityTypeNotFoundException(type);
}
Map<QualifiedName, Object> properties = new HashMap<>();
JSONObject props = jsonObject.getJSONObject(JSONKeys.PROPERTIES);
for (PropertyDescriptor propertyDescriptor : entityDescriptor.state().properties()) {
Object jsonValue;
try {
jsonValue = props.get(propertyDescriptor.qualifiedName().name());
} catch (JSONException e) {
// Value not found, default it
Object initialValue = propertyDescriptor.initialValue(module);
properties.put(propertyDescriptor.qualifiedName(), initialValue);
status = EntityStatus.UPDATED;
continue;
}
if (JSONObject.NULL.equals(jsonValue)) {
properties.put(propertyDescriptor.qualifiedName(), null);
} else {
Object value = valueSerialization.deserialize(propertyDescriptor.valueType(), jsonValue.toString());
properties.put(propertyDescriptor.qualifiedName(), value);
}
}
Map<QualifiedName, EntityReference> associations = new HashMap<>();
JSONObject assocs = jsonObject.getJSONObject(JSONKeys.ASSOCIATIONS);
for (AssociationDescriptor associationType : entityDescriptor.state().associations()) {
try {
Object jsonValue = assocs.get(associationType.qualifiedName().name());
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
associations.put(associationType.qualifiedName(), value);
} catch (JSONException e) {
// Association not found, default it to null
associations.put(associationType.qualifiedName(), null);
status = EntityStatus.UPDATED;
}
}
JSONObject manyAssocs = jsonObject.getJSONObject(JSONKeys.MANY_ASSOCIATIONS);
Map<QualifiedName, List<EntityReference>> manyAssociations = new HashMap<>();
for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
List<EntityReference> references = new ArrayList<>();
try {
JSONArray jsonValues = manyAssocs.getJSONArray(manyAssociationType.qualifiedName().name());
for (int i = 0; i < jsonValues.length(); i++) {
Object jsonValue = jsonValues.getString(i);
EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue);
references.add(value);
}
manyAssociations.put(manyAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// ManyAssociation not found, default to empty one
manyAssociations.put(manyAssociationType.qualifiedName(), references);
}
}
JSONObject namedAssocs = jsonObject.has(JSONKeys.NAMED_ASSOCIATIONS) ? jsonObject.getJSONObject(JSONKeys.NAMED_ASSOCIATIONS) : new JSONObject();
Map<QualifiedName, Map<String, EntityReference>> namedAssociations = new HashMap<>();
for (AssociationDescriptor namedAssociationType : entityDescriptor.state().namedAssociations()) {
Map<String, EntityReference> references = new LinkedHashMap<>();
try {
JSONObject jsonValues = namedAssocs.getJSONObject(namedAssociationType.qualifiedName().name());
JSONArray names = jsonValues.names();
if (names != null) {
for (int idx = 0; idx < names.length(); idx++) {
String name = names.getString(idx);
String jsonValue = jsonValues.getString(name);
references.put(name, EntityReference.parseEntityReference(jsonValue));
}
}
namedAssociations.put(namedAssociationType.qualifiedName(), references);
} catch (JSONException e) {
// NamedAssociation not found, default to empty one
namedAssociations.put(namedAssociationType.qualifiedName(), references);
}
}
return new DefaultEntityState(unitOfWork, version, modified, EntityReference.parseEntityReference(identity), status, entityDescriptor, properties, associations, manyAssociations, namedAssociations);
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.api.property.PropertyDescriptor 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);
}
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method doUnQualifiedUpdate.
private void doUnQualifiedUpdate(AssociationStateHolder eState, AssociationStateDescriptor eStateDesc, AssociationStateHolder vState, AssociationStateDescriptor vStateDesc) {
for (PropertyDescriptor ePropDesc : eStateDesc.properties()) {
if (IDENTITY_STATE_NAME.equals(ePropDesc.qualifiedName())) {
// Ignore Identity, could be logged
continue;
}
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(ePropDesc.qualifiedName().name());
eState.propertyFor(ePropDesc.accessor()).set(vState.propertyFor(vPropDesc.accessor()).get());
} catch (IllegalArgumentException propNotFoundOnValue) {
// Property not found on Value, do nothing
}
}
for (AssociationDescriptor eAssocDesc : eStateDesc.associations()) {
Association<Object> eAssoc = eState.associationFor(eAssocDesc.accessor());
try {
AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName(eAssocDesc.qualifiedName().name());
eAssoc.set(vState.associationFor(vAssocDesc.accessor()).get());
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Association not found on Value, find Property<String> and load associated Entity
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(eAssocDesc.qualifiedName().name());
if (STRING_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
String assocId = (String) vState.propertyFor(vPropDesc.accessor()).get();
if (assocId != null) {
eAssoc.set(module.currentUnitOfWork().get((Class) eAssocDesc.type(), assocId));
} else {
eAssoc.set(null);
}
}
} catch (IllegalArgumentException propNotFoundOnValue) {
// Do nothing
}
}
}
for (AssociationDescriptor eAssocDesc : eStateDesc.manyAssociations()) {
ManyAssociation<Object> eManyAssoc = eState.manyAssociationFor(eAssocDesc.accessor());
try {
AssociationDescriptor vAssDesc = vStateDesc.getManyAssociationByName(eAssocDesc.qualifiedName().name());
ManyAssociation<Object> vManyAss = vState.manyAssociationFor(vAssDesc.accessor());
for (Object ass : eManyAssoc.toList()) {
eManyAssoc.remove(ass);
}
for (Object ass : vManyAss.toList()) {
eManyAssoc.add(ass);
}
} catch (IllegalArgumentException assNotFoundOnValue) {
// ManyAssociation not found on Value, find Property<List<String>> and load associated Entities
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(eAssocDesc.qualifiedName().name());
if (STRING_COLLECTION_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Collection<String> vAssocState = (Collection) vState.propertyFor(vPropDesc.accessor()).get();
for (Object ass : eManyAssoc.toList()) {
eManyAssoc.remove(ass);
}
if (vAssocState != null) {
for (String eachAssoc : vAssocState) {
eManyAssoc.add(module.currentUnitOfWork().get((Class) eAssocDesc.type(), eachAssoc));
}
}
}
} catch (IllegalArgumentException propNotFoundOnValue) {
// Do nothing
}
}
}
for (AssociationDescriptor eAssocDesc : eStateDesc.namedAssociations()) {
NamedAssociation<Object> eNamedAssoc = eState.namedAssociationFor(eAssocDesc.accessor());
try {
AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName(eAssocDesc.qualifiedName().name());
NamedAssociation<Object> vNamedAssoc = vState.namedAssociationFor(vAssocDesc.accessor());
for (String assocName : Iterables.toList(eNamedAssoc)) {
eNamedAssoc.remove(assocName);
}
for (Map.Entry<String, Object> assocEntry : vNamedAssoc.toMap().entrySet()) {
eNamedAssoc.put(assocEntry.getKey(), assocEntry.getValue());
}
} catch (IllegalArgumentException assocNotFoundOnValue) {
// NamedAssociation not found on Value, find Property<Map<String,String>> and load associated Entities
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(eAssocDesc.qualifiedName().name());
if (STRING_MAP_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Map<String, String> vAssocState = (Map) vState.propertyFor(vPropDesc.accessor()).get();
for (String assocName : Iterables.toList(eNamedAssoc)) {
eNamedAssoc.remove(assocName);
}
if (vAssocState != null) {
for (Map.Entry<String, String> assocEntry : vAssocState.entrySet()) {
eNamedAssoc.put(assocEntry.getKey(), module.currentUnitOfWork().get((Class) eAssocDesc.type(), assocEntry.getValue()));
}
}
}
} catch (IllegalArgumentException propNotFoundOnValue) {
// Do nothing
}
}
}
}
use of org.qi4j.api.property.PropertyDescriptor in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method doUnqualifiedConversion.
private <T> EntityBuilder<?> doUnqualifiedConversion(Class<T> entityType, String identity, final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc) {
Function<PropertyDescriptor, Object> props = new Function<PropertyDescriptor, Object>() {
@Override
public Object map(PropertyDescriptor ePropDesc) {
String propName = ePropDesc.qualifiedName().name();
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(propName);
return vState.propertyFor(vPropDesc.accessor()).get();
} catch (IllegalArgumentException propNotFoundOnValue) {
// Property not found on Value
return null;
}
}
};
Function<AssociationDescriptor, EntityReference> assocs = new Function<AssociationDescriptor, EntityReference>() {
@Override
public EntityReference map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName(assocName);
Object assocEntity = vState.associationFor(vAssocDesc.accessor()).get();
return assocEntity == null ? null : EntityReference.entityReferenceFor(assocEntity);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Association not found on Value, find Property<String> and convert to Association
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
String assocId = (String) vState.propertyFor(vPropDesc.accessor()).get();
return assocId == null ? null : EntityReference.parseEntityReference(assocId);
}
return null;
} catch (IllegalArgumentException propNotFoundOnValue) {
return null;
}
}
}
};
Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = new Function<AssociationDescriptor, Iterable<EntityReference>>() {
@Override
public Iterable<EntityReference> map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByName(assocName);
ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor(vAssocDesc.accessor());
return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.map(vManyAssoc);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// ManyAssociation not found on Value, find List<String> and convert to ManyAssociation
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_COLLECTION_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Collection<String> vAssocState = (Collection) vState.propertyFor(vPropDesc.accessor()).get();
return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.map(vAssocState);
}
return Iterables.empty();
} catch (IllegalArgumentException propNotFoundOnValue) {
return Iterables.empty();
}
}
}
};
Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = new Function<AssociationDescriptor, Map<String, EntityReference>>() {
@Override
public Map<String, EntityReference> map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName(assocName);
NamedAssociation<Object> vAssocState = vState.namedAssociationFor(vAssocDesc.accessor());
return NAMED_ASSOC_TO_ENTITY_REF_MAP.map(vAssocState);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Find Map<String,String> Property and convert to NamedAssociation
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_MAP_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Map<String, String> vAssocState = (Map) vState.propertyFor(vPropDesc.accessor()).get();
return STRING_MAP_TO_ENTITY_REF_MAP.map(vAssocState);
}
return Collections.EMPTY_MAP;
} catch (IllegalArgumentException propNotFoundOnValue) {
return Collections.EMPTY_MAP;
}
}
}
};
return module.currentUnitOfWork().newEntityBuilderWithState(entityType, identity, props, assocs, manyAssocs, namedAssocs);
}
Aggregations