Search in sources :

Example 1 with DefaultEntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork in project qi4j-sdk by Qi4j.

the class MapEntityStoreMixin method entityStateOf.

@Override
public synchronized EntityState entityStateOf(EntityStoreUnitOfWork unitofwork, EntityReference identity) {
    DefaultEntityStoreUnitOfWork unitOfWork = (DefaultEntityStoreUnitOfWork) unitofwork;
    Reader in = mapEntityStore.get(identity);
    return readEntityState(unitOfWork, in);
}
Also used : DefaultEntityStoreUnitOfWork(org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork) Reader(java.io.Reader)

Example 2 with DefaultEntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork in project qi4j-sdk by Qi4j.

the class JSONMapEntityStoreMixin method applyChanges.

@Override
public StateCommitter applyChanges(final EntityStoreUnitOfWork unitOfWork, final Iterable<EntityState> state) throws EntityStoreException {
    return new StateCommitter() {

        @Override
        public void commit() {
            try {
                mapEntityStore.applyChanges(new MapEntityStore.MapChanges() {

                    @Override
                    public void visitMap(MapEntityStore.MapChanger changer) throws IOException {
                        DefaultEntityStoreUnitOfWork uow = (DefaultEntityStoreUnitOfWork) unitOfWork;
                        CacheOptions options = uow.usecase().metaInfo(CacheOptions.class);
                        if (options == null) {
                            options = CacheOptions.ALWAYS;
                        }
                        for (EntityState entityState : state) {
                            JSONEntityState state = (JSONEntityState) entityState;
                            if (state.status().equals(EntityStatus.NEW)) {
                                Writer writer = changer.newEntity(state.identity(), state.entityDescriptor());
                                writeEntityState(state, writer, unitOfWork.identity(), unitOfWork.currentTime());
                                writer.close();
                                if (options.cacheOnNew()) {
                                    cache.put(state.identity().identity(), new CacheState(state.state()));
                                }
                            } else if (state.status().equals(EntityStatus.UPDATED)) {
                                Writer writer = changer.updateEntity(state.identity(), state.entityDescriptor());
                                writeEntityState(state, writer, unitOfWork.identity(), unitOfWork.currentTime());
                                writer.close();
                                if (options.cacheOnWrite()) {
                                    cache.put(state.identity().identity(), new CacheState(state.state()));
                                }
                            } else if (state.status().equals(EntityStatus.REMOVED)) {
                                changer.removeEntity(state.identity(), state.entityDescriptor());
                                cache.remove(state.identity().identity());
                            }
                        }
                    }
                });
            } catch (IOException e) {
                throw new EntityStoreException(e);
            }
        }

        @Override
        public void cancel() {
        }
    };
}
Also used : IOException(java.io.IOException) EntityState(org.qi4j.spi.entity.EntityState) CacheOptions(org.qi4j.api.cache.CacheOptions) DefaultEntityStoreUnitOfWork(org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork) StateCommitter(org.qi4j.spi.entitystore.StateCommitter) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Writer(java.io.Writer)

Example 3 with DefaultEntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork in project qi4j-sdk by Qi4j.

the class SQLEntityStoreMixin method queryAllEntities.

private void queryAllEntities(Module module, EntityStatesVisitor entityStatesVisitor) {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    UsecaseBuilder builder = UsecaseBuilder.buildUsecase("qi4j.entitystore.sql.visit");
    Usecase usecase = builder.withMetaInfo(CacheOptions.NEVER).newUsecase();
    final DefaultEntityStoreUnitOfWork uow = new DefaultEntityStoreUnitOfWork(entityStoreSPI, newUnitOfWorkId(), module, usecase, System.currentTimeMillis());
    try {
        connection = database.getConnection();
        ps = database.prepareGetAllEntitiesStatement(connection);
        database.populateGetAllEntitiesStatement(ps);
        rs = ps.executeQuery();
        while (rs.next()) {
            DefaultEntityState entityState = readEntityState(uow, database.getEntityValue(rs).getReader());
            if (!entityStatesVisitor.visit(entityState)) {
                return;
            }
        }
    } catch (SQLException ex) {
        throw new EntityStoreException(ex);
    } finally {
        SQLUtil.closeQuietly(rs);
        SQLUtil.closeQuietly(ps);
        SQLUtil.closeQuietly(connection);
    }
}
Also used : DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) UsecaseBuilder(org.qi4j.api.usecase.UsecaseBuilder) DefaultEntityStoreUnitOfWork(org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork) PreparedStatement(java.sql.PreparedStatement) Usecase(org.qi4j.api.usecase.Usecase) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 4 with DefaultEntityStoreUnitOfWork

use of org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork 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);
        }
        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<QualifiedName, Object>();
        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, new NumberParser<Long>() {

                        @Override
                        public Long parse(String str) {
                            return Long.parseLong(str);
                        }
                    }));
                } else if (mainType.equals(Integer.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, new NumberParser<Integer>() {

                        @Override
                        public Integer parse(String str) {
                            return Integer.parseInt(str);
                        }
                    }));
                } else if (mainType.equals(Double.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, new NumberParser<Double>() {

                        @Override
                        public Double parse(String str) {
                            return Double.parseDouble(str);
                        }
                    }));
                } else if (mainType.equals(Float.class)) {
                    properties.put(persistentPropertyDescriptor.qualifiedName(), this.getNumber(propsPrefs, persistentPropertyDescriptor, new NumberParser<Float>() {

                        @Override
                        public Float parse(String str) {
                            return Float.parseFloat(str);
                        }
                    }));
                } 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<QualifiedName, EntityReference>();
        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<QualifiedName, List<EntityReference>>();
        Preferences manyAssocs = null;
        for (AssociationDescriptor manyAssociationType : entityDescriptor.state().manyAssociations()) {
            if (manyAssocs == null) {
                manyAssocs = entityPrefs.node("manyassociations");
            }
            List<EntityReference> references = new ArrayList<EntityReference>();
            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);
            }
        }
        return new DefaultEntityState(desuw, entityPrefs.get("version", ""), entityPrefs.getLong("modified", unitOfWork.currentTime()), identity, status, entityDescriptor, properties, associations, manyAssociations);
    } catch (ValueSerializationException e) {
        throw new EntityStoreException(e);
    } catch (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) ArrayList(java.util.ArrayList) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) MapType(org.qi4j.api.type.MapType) 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) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Aggregations

DefaultEntityStoreUnitOfWork (org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork)4 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)3 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 BackingStoreException (java.util.prefs.BackingStoreException)1 Preferences (java.util.prefs.Preferences)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 CacheOptions (org.qi4j.api.cache.CacheOptions)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 EntityReference (org.qi4j.api.entity.EntityReference)1