use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.
the class AbstractCollectionSerializationTest method givenListOfMapStringStringAndNullElementWhenSerializingAndDeserializingExpectEquals.
@Test
public void givenListOfMapStringStringAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
String output = valueSerialization.serialize(stringListOfMaps());
ValueType stringType = new ValueType(String.class);
CollectionType collectionType = new CollectionType(List.class, new MapType(Map.class, stringType, stringType));
List<Map<String, String>> value = valueSerialization.deserialize(collectionType, output);
assertEquals(stringListOfMaps(), value);
}
use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.
the class PropertyInstance method prepareToBuild.
@SuppressWarnings({ "raw", "unchecked" })
public void prepareToBuild(PropertyModel propertyDescriptor) {
// Check if state has to be modified
model = propertyDescriptor.getBuilderInfo();
if (propertyDescriptor.valueType() instanceof ValueCompositeType) {
Object value = get();
if (value != null) {
ValueInstance.valueInstanceOf((ValueComposite) value).prepareToBuild();
}
} else if (propertyDescriptor.valueType() instanceof CollectionType) {
Object value = get();
if (value != null) {
if (value instanceof List) {
value = new ArrayList((Collection) value);
} else if (value instanceof Set) {
value = new LinkedHashSet((Collection) value);
}
// Check if items are Values
CollectionType collection = (CollectionType) propertyDescriptor.valueType();
if (collection.collectedType() instanceof ValueCompositeType) {
Collection coll = (Collection) value;
for (Object instance : coll) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
}
}
set((T) value);
}
} else if (propertyDescriptor.valueType() instanceof MapType) {
Object value = get();
if (value != null) {
Map map = new LinkedHashMap((Map) value);
// Check if keys/values are Values
MapType mapType = (MapType) propertyDescriptor.valueType();
if (mapType.keyType() instanceof ValueCompositeType) {
for (Object instance : map.keySet()) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
}
}
if (mapType.valueType() instanceof ValueCompositeType) {
for (Object instance : map.values()) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
}
}
set((T) value);
}
}
}
use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.
the class PropertyInstance method prepareBuilderState.
@SuppressWarnings({ "raw", "unchecked" })
public void prepareBuilderState(PropertyModel propertyDescriptor) {
// Check if state has to be modified
if (propertyDescriptor.valueType() instanceof ValueCompositeType) {
Object value = get();
if (value != null) {
ValueInstance.valueInstanceOf((ValueComposite) value).prepareBuilderState();
}
} else if (propertyDescriptor.valueType() instanceof CollectionType) {
T value = get();
if (value != null) {
if (propertyDescriptor.isImmutable()) {
if (value instanceof List) {
value = (T) Collections.unmodifiableList((List<? extends Object>) value);
} else if (value instanceof Set) {
value = (T) Collections.unmodifiableSet((Set<? extends Object>) value);
} else {
value = (T) Collections.unmodifiableCollection((Collection<? extends Object>) value);
}
this.value = value;
}
CollectionType collection = (CollectionType) propertyDescriptor.valueType();
if (collection.collectedType() instanceof ValueCompositeType) {
Collection coll = (Collection) value;
for (Object instance : coll) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
}
}
}
} else if (propertyDescriptor.valueType() instanceof MapType) {
T value = get();
if (value != null) {
MapType mapType = (MapType) propertyDescriptor.valueType();
if (mapType.keyType() instanceof ValueCompositeType) {
Map map = (Map) value;
for (Object instance : map.keySet()) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
}
}
if (mapType.valueType() instanceof ValueCompositeType) {
Map map = (Map) value;
for (Object instance : map.values()) {
ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
}
}
if (propertyDescriptor.isImmutable()) {
value = (T) Collections.unmodifiableMap((Map<?, ?>) value);
}
this.value = value;
}
}
model = propertyDescriptor;
}
use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.
the class ValueDeserializerAdapter method deserializeValueComposite.
@SuppressWarnings("unchecked")
private <T> T deserializeValueComposite(ValueCompositeType valueCompositeType, Class<?> valueBuilderType, InputNodeType inputNode) throws Exception {
final Map<String, Object> stateMap = new HashMap<>();
// Properties
for (PropertyDescriptor property : valueCompositeType.properties()) {
String propertyName = property.qualifiedName().name();
Object value;
if (objectHasField(inputNode, propertyName)) {
value = getObjectFieldValue(inputNode, propertyName, buildDeserializeInputNodeFunction(property.valueType()));
TREE_PARSING_LOG.trace("In deserializeValueComposite(), getObjectFieldValue( {} ) for key {} returned '{}' of class {}", inputNode, propertyName, value, value == null ? "N/A" : value.getClass());
if (property.isImmutable()) {
if (value instanceof Set) {
value = Collections.unmodifiableSet((Set<?>) value);
} else if (value instanceof List) {
value = Collections.unmodifiableList((List<?>) value);
} else if (value instanceof Map) {
value = Collections.unmodifiableMap((Map<?, ?>) value);
}
}
TREE_PARSING_LOG.trace("Property {}#{}( {} ) deserialized value is '{}' of class {}", property.qualifiedName().type(), property.qualifiedName().name(), property.valueType(), value, value == null ? "N/A" : value.getClass());
} else {
// Serialized object does not contain the field, try to default it
value = property.initialValue(valuesModule());
TREE_PARSING_LOG.trace("Property {} was not defined in serialized object and has been defaulted to '{}'", property.qualifiedName(), value);
}
stateMap.put(propertyName, value);
}
// Associations
for (AssociationDescriptor association : valueCompositeType.associations()) {
String associationName = association.qualifiedName().name();
if (objectHasField(inputNode, associationName)) {
Object value = getObjectFieldValue(inputNode, associationName, buildDeserializeInputNodeFunction(new ValueType(EntityReference.class)));
stateMap.put(associationName, value);
}
}
// ManyAssociations
for (AssociationDescriptor manyAssociation : valueCompositeType.manyAssociations()) {
String manyAssociationName = manyAssociation.qualifiedName().name();
if (objectHasField(inputNode, manyAssociationName)) {
Object value = getObjectFieldValue(inputNode, manyAssociationName, buildDeserializeInputNodeFunction(new CollectionType(Collection.class, new ValueType(EntityReference.class))));
stateMap.put(manyAssociationName, value);
}
}
// NamedAssociations
for (AssociationDescriptor namedAssociation : valueCompositeType.namedAssociations()) {
String namedAssociationName = namedAssociation.qualifiedName().name();
if (objectHasField(inputNode, namedAssociationName)) {
Object value = getObjectFieldValue(inputNode, namedAssociationName, buildDeserializeInputNodeFunction(MapType.of(String.class, EntityReference.class)));
stateMap.put(namedAssociationName, value);
}
}
ValueBuilder<?> valueBuilder = buildNewValueBuilderWithState(valueBuilderType, stateMap);
// Unchecked cast because the builder could use a type != T
return (T) valueBuilder.newInstance();
}
use of org.qi4j.api.type.CollectionType 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);
}
}
Aggregations