Search in sources :

Example 6 with EntityStoreException

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);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 7 with EntityStoreException

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);
        }
    }
}
Also used : Transaction(com.google.appengine.api.datastore.Transaction) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) IOException(java.io.IOException)

Example 8 with EntityStoreException

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);
    }
}
Also used : StringReader(java.io.StringReader) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) IOException(java.io.IOException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) File(java.io.File)

Example 9 with EntityStoreException

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);
    }
}
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 10 with EntityStoreException

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);
    }
}
Also used : JSONWriter(org.json.JSONWriter) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) EntityReference(org.qi4j.api.entity.EntityReference) JSONObject(org.json.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)35 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)13 EntityReference (org.qi4j.api.entity.EntityReference)9 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)9 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Map (java.util.Map)6 JSONArray (org.json.JSONArray)6 QualifiedName (org.qi4j.api.common.QualifiedName)6 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)5 StringReader (java.io.StringReader)4 Connection (java.sql.Connection)4 Module (org.qi4j.api.structure.Module)4