use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JSONEntityState method namedAssociationValueOf.
@Override
public NamedAssociationState namedAssociationValueOf(QualifiedName stateName) {
try {
JSONObject namedAssociations = state.getJSONObject(JSONKeys.NAMED_ASSOCIATIONS);
JSONObject jsonValues = namedAssociations.optJSONObject(stateName.name());
if (jsonValues == null) {
jsonValues = new JSONObject();
namedAssociations.put(stateName.name(), jsonValues);
}
return new JSONNamedAssociationState(this, jsonValues);
} catch (JSONException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class GaeEntityStoreMixin method applyChanges.
@Override
public void applyChanges(MapChanges changes) throws IOException {
final Transaction transaction = datastore.beginTransaction();
try {
changes.visitMap(new GaeMapChanger(transaction));
transaction.commit();
} catch (RuntimeException e) {
if (transaction.isActive()) {
transaction.rollback();
}
if (e instanceof EntityStoreException) {
throw (EntityStoreException) e;
} else {
throw new IOException(e);
}
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class FileEntityStoreMixin method get.
@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
try {
File f = getDataFile(entityReference);
if (!f.exists()) {
throw new EntityNotFoundException(entityReference);
}
byte[] serializedState = fetch(f);
return new StringReader(new String(serializedState, "UTF-8"));
} catch (IOException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.spi.entitystore.EntityStoreException 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.EntityStoreException in project qi4j-sdk by Qi4j.
the class SQLEntityStoreMixin method writeEntityState.
protected void writeEntityState(DefaultEntityState state, Writer writer, String version) throws EntityStoreException {
try {
JSONWriter json = new JSONWriter(writer);
JSONWriter properties = json.object().key(JSONKeys.IDENTITY).value(state.identity().identity()).key(JSONKeys.APPLICATION_VERSION).value(application.version()).key(JSONKeys.TYPE).value(first(state.entityDescriptor().types()).getName()).key(JSONKeys.VERSION).value(version).key(JSONKeys.MODIFIED).value(state.lastModified()).key(JSONKeys.PROPERTIES).object();
for (PropertyDescriptor persistentProperty : state.entityDescriptor().state().properties()) {
Object value = state.properties().get(persistentProperty.qualifiedName());
json.key(persistentProperty.qualifiedName().name());
if (value == null || ValueType.isPrimitiveValue(value)) {
json.value(value);
} else {
String serialized = valueSerialization.serialize(value);
if (serialized.startsWith("{")) {
json.value(new JSONObject(serialized));
} else if (serialized.startsWith("[")) {
json.value(new JSONArray(serialized));
} else {
json.value(serialized);
}
}
}
JSONWriter associations = properties.endObject().key(JSONKeys.ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations().entrySet()) {
EntityReference value = stateNameEntityReferenceEntry.getValue();
associations.key(stateNameEntityReferenceEntry.getKey().name()).value(value != null ? value.identity() : null);
}
JSONWriter manyAssociations = associations.endObject().key(JSONKeys.MANY_ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations().entrySet()) {
JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
for (EntityReference entityReference : stateNameListEntry.getValue()) {
assocs.value(entityReference.identity());
}
assocs.endArray();
}
JSONWriter namedAssociations = manyAssociations.endObject().key(JSONKeys.NAMED_ASSOCIATIONS).object();
for (Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameMapEntry : state.namedAssociations().entrySet()) {
JSONWriter assocs = namedAssociations.key(stateNameMapEntry.getKey().name()).object();
for (Map.Entry<String, EntityReference> entry : stateNameMapEntry.getValue().entrySet()) {
assocs.key(entry.getKey()).value(entry.getValue().identity());
}
assocs.endObject();
}
namedAssociations.endObject().endObject();
} catch (JSONException e) {
throw new EntityStoreException("Could not store EntityState", e);
}
}
Aggregations