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