Search in sources :

Example 6 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType 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);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ValueComposite(org.qi4j.api.value.ValueComposite) MapType(org.qi4j.api.type.MapType) LinkedHashMap(java.util.LinkedHashMap) CollectionType(org.qi4j.api.type.CollectionType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 7 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType 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;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) CollectionType(org.qi4j.api.type.CollectionType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ValueComposite(org.qi4j.api.value.ValueComposite) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) MapType(org.qi4j.api.type.MapType)

Example 8 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType 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);
    }
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) MapType(org.qi4j.api.type.MapType) LinkedHashMap(java.util.LinkedHashMap) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) EntityReference(org.qi4j.api.entity.EntityReference) DefaultEntityStoreUnitOfWork(org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork) EntityStatus(org.qi4j.spi.entity.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Preferences(java.util.prefs.Preferences) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueType(org.qi4j.api.type.ValueType) QualifiedName(org.qi4j.api.common.QualifiedName) BackingStoreException(java.util.prefs.BackingStoreException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) Module(org.qi4j.api.structure.Module) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 9 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType 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);
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueType(org.qi4j.api.type.ValueType) QualifiedName(org.qi4j.api.common.QualifiedName) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) MapType(org.qi4j.api.type.MapType) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) EntityReference(org.qi4j.api.entity.EntityReference) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Preferences(java.util.prefs.Preferences) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Example 10 with ValueCompositeType

use of org.qi4j.api.type.ValueCompositeType in project qi4j-sdk by Qi4j.

the class EntityStateSerializer method serializeProperty.

private void serializeProperty(PropertyDescriptor persistentProperty, Object property, Resource subject, Graph graph, boolean includeNonQueryable) {
    if (!(includeNonQueryable || persistentProperty.queryable())) {
        // Skip non-queryable
        return;
    }
    ValueType valueType = persistentProperty.valueType();
    final ValueFactory valueFactory = graph.getValueFactory();
    String propertyURI = persistentProperty.qualifiedName().toURI();
    URI predicate = valueFactory.createURI(propertyURI);
    String baseURI = propertyURI.substring(0, propertyURI.indexOf('#')) + "/";
    if (valueType instanceof ValueCompositeType) {
        serializeValueComposite(subject, predicate, (ValueComposite) property, valueType, graph, baseURI, includeNonQueryable);
    } else {
        String stringProperty = valueSerializer.serialize(new Options().withoutTypeInfo(), property);
        final Literal object = valueFactory.createLiteral(stringProperty);
        graph.add(subject, predicate, object);
    }
}
Also used : Options(org.qi4j.api.value.ValueSerializer.Options) ValueType(org.qi4j.api.type.ValueType) Literal(org.openrdf.model.Literal) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Aggregations

ValueCompositeType (org.qi4j.api.type.ValueCompositeType)11 ValueType (org.qi4j.api.type.ValueType)7 CollectionType (org.qi4j.api.type.CollectionType)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 Map (java.util.Map)4 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)4 MapType (org.qi4j.api.type.MapType)4 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)4 HashMap (java.util.HashMap)3 QualifiedName (org.qi4j.api.common.QualifiedName)3 ValueComposite (org.qi4j.api.value.ValueComposite)3 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Preferences (java.util.prefs.Preferences)2 URI (org.openrdf.model.URI)2 ValueFactory (org.openrdf.model.ValueFactory)2 EntityReference (org.qi4j.api.entity.EntityReference)2