Search in sources :

Example 1 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class EntityBuilderInstance method newInstance.

@Override
@SuppressWarnings("unchecked")
public T newInstance() throws LifecycleException {
    checkValid();
    String identity;
    // Figure out whether to use given or generated identity
    identity = (String) entityState.propertyValueOf(IDENTITY_STATE_NAME);
    EntityState newEntityState = model.model().newEntityState(store, EntityReference.parseEntityReference(identity));
    prototypeInstance.invokeCreate();
    // Check constraints
    prototypeInstance.checkConstraints();
    entityState.copyTo(newEntityState);
    EntityInstance instance = model.model().newInstance(uow, model.module(), newEntityState);
    Object proxy = instance.proxy();
    // Add entity in UOW
    uow.addEntity(instance);
    // Invalidate builder
    this.identity = null;
    return (T) proxy;
}
Also used : EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState)

Example 2 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method get.

public <T> T get(EntityReference identity, ModuleUnitOfWork uow, Iterable<ModelModule<EntityModel>> potentialModels, Class<T> mixinType) throws EntityTypeNotFoundException, NoSuchEntityException {
    checkOpen();
    EntityInstance entityInstance = instanceCache.get(identity);
    if (entityInstance == null) {
        // Not yet in cache
        // Check if this is a root UoW, or if no parent UoW knows about this entity
        EntityState entityState = null;
        EntityModel model = null;
        ModuleInstance module = null;
        // Figure out what EntityStore to use
        for (ModelModule<EntityModel> potentialModel : potentialModels) {
            EntityStore store = potentialModel.module().entityStore();
            EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork(store, potentialModel.module());
            try {
                entityState = storeUow.entityStateOf(identity);
            } catch (EntityNotFoundException e) {
                continue;
            }
            // Get the selected model
            model = (EntityModel) entityState.entityDescriptor();
            module = potentialModel.module();
        }
        // Check if model was found
        if (model == null) {
            // Check if state was found
            if (entityState == null) {
                throw new NoSuchEntityException(identity, mixinType);
            } else {
                throw new EntityTypeNotFoundException(mixinType.getName());
            }
        }
        // Create instance
        entityInstance = new EntityInstance(uow, module, model, entityState);
        instanceCache.put(identity, entityInstance);
    } else {
        // Check if it has been removed
        if (entityInstance.status() == EntityStatus.REMOVED) {
            throw new NoSuchEntityException(identity, mixinType);
        }
    }
    return entityInstance.proxy();
}
Also used : EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EntityModel(org.qi4j.runtime.entity.EntityModel) EntityInstance(org.qi4j.runtime.entity.EntityInstance) EntityState(org.qi4j.spi.entity.EntityState) EntityStore(org.qi4j.spi.entitystore.EntityStore) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ModuleInstance(org.qi4j.runtime.structure.ModuleInstance) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException)

Example 3 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class DefaultEntityStoreUnitOfWork method newEntityState.

// EntityStore
@Override
public EntityState newEntityState(EntityReference anIdentity, EntityDescriptor descriptor) throws EntityStoreException {
    EntityState entityState = states.get(anIdentity);
    if (entityState != null) {
        throw new EntityAlreadyExistsException(anIdentity);
    }
    EntityState state = entityStoreSPI.newEntityState(this, anIdentity, descriptor);
    states.put(anIdentity, state);
    return state;
}
Also used : EntityState(org.qi4j.spi.entity.EntityState)

Example 4 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class PreferencesEntityStoreMixin method applyChanges.

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

        @Override
        public void commit() {
            try {
                synchronized (root) {
                    for (EntityState entityState : state) {
                        DefaultEntityState state = (DefaultEntityState) entityState;
                        if (state.status().equals(EntityStatus.NEW)) {
                            Preferences entityPrefs = root.node(state.identity().identity());
                            writeEntityState(state, entityPrefs, unitofwork.identity(), unitofwork.currentTime());
                        } else if (state.status().equals(EntityStatus.UPDATED)) {
                            Preferences entityPrefs = root.node(state.identity().identity());
                            writeEntityState(state, entityPrefs, unitofwork.identity(), unitofwork.currentTime());
                        } else if (state.status().equals(EntityStatus.REMOVED)) {
                            root.node(state.identity().identity()).removeNode();
                        }
                    }
                    root.flush();
                }
            } catch (BackingStoreException e) {
                throw new EntityStoreException(e);
            }
        }

        @Override
        public void cancel() {
        }
    };
}
Also used : DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) BackingStoreException(java.util.prefs.BackingStoreException) StateCommitter(org.qi4j.spi.entitystore.StateCommitter) EntityState(org.qi4j.spi.entity.EntityState) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Preferences(java.util.prefs.Preferences)

Example 5 with EntityState

use of org.qi4j.spi.entity.EntityState in project qi4j-sdk by Qi4j.

the class SolrEntityIndexerMixin method notifyChanges.

@Override
public void notifyChanges(Iterable<EntityState> entityStates) {
    try {
        try {
            // Figure out what to update
            List<String> deleted = null;
            List<SolrInputDocument> added = new ArrayList<SolrInputDocument>();
            for (EntityState entityState : entityStates) {
                if (entityState.entityDescriptor().queryable()) {
                    if (entityState.status().equals(EntityStatus.REMOVED)) {
                        if (deleted == null)
                            deleted = new ArrayList<String>();
                        deleted.add(entityState.identity().identity());
                    } else if (entityState.status().equals(EntityStatus.UPDATED)) {
                        added.add(indexEntityState(entityState, server));
                    } else if (entityState.status().equals(EntityStatus.NEW)) {
                        added.add(indexEntityState(entityState, server));
                    }
                }
            }
            // Send changes to Solr
            if (deleted != null)
                server.deleteById(deleted);
            if (!added.isEmpty())
                server.add(added);
        } finally {
            if (server != null) {
                server.commit(false, false);
            }
        }
    } catch (Throwable e) {
        logger.error("Could not update Solr", e);
    //TODO What shall we do with the exception?
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ArrayList(java.util.ArrayList) EntityState(org.qi4j.spi.entity.EntityState)

Aggregations

EntityState (org.qi4j.spi.entity.EntityState)17 ResourceException (org.restlet.resource.ResourceException)4 Date (java.util.Date)3 EntityReference (org.qi4j.api.entity.EntityReference)3 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)3 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)3 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)3 JSONEntityState (org.qi4j.spi.entitystore.helpers.JSONEntityState)3 PrintWriter (java.io.PrintWriter)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 EntityStatus (org.qi4j.spi.entity.EntityStatus)2 EntityStore (org.qi4j.spi.entitystore.EntityStore)2 StateCommitter (org.qi4j.spi.entitystore.StateCommitter)2 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2