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;
}
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();
}
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;
}
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() {
}
};
}
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?
}
}
Aggregations