Search in sources :

Example 11 with EntityNotFoundException

use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.

the class LevelDBEntityStoreMixin method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    final WriteBatch writeBatch = db.createWriteBatch();
    try {
        changes.visitMap(new MapChanger() {

            @Override
            public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        super.close();
                        String jsonState = toString();
                        writeBatch.put(ref.identity().getBytes(charset), jsonState.getBytes(charset));
                    }
                };
            }

            @Override
            public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        super.close();
                        String jsonState = toString();
                        writeBatch.put(ref.identity().getBytes(charset), jsonState.getBytes(charset));
                    }
                };
            }

            @Override
            public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
                writeBatch.delete(ref.identity().getBytes(charset));
            }
        });
        db.write(writeBatch);
    } finally {
        writeBatch.close();
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) WriteBatch(org.iq80.leveldb.WriteBatch) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 12 with EntityNotFoundException

use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.

the class SQLEntityStoreMixin method getValue.

protected EntityValueResult getValue(EntityReference ref) {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        connection = database.getConnection();
        ps = database.prepareGetEntityStatement(connection);
        database.populateGetEntityStatement(ps, ref);
        rs = ps.executeQuery();
        if (!rs.next()) {
            throw new EntityNotFoundException(ref);
        }
        EntityValueResult result = database.getEntityValue(rs);
        return result;
    } catch (SQLException sqle) {
        throw new EntityStoreException("Unable to get Entity " + ref, sqle);
    } finally {
        SQLUtil.closeQuietly(rs);
        SQLUtil.closeQuietly(ps);
        SQLUtil.closeQuietly(connection);
    }
}
Also used : EntityValueResult(org.qi4j.entitystore.sql.internal.DatabaseSQLService.EntityValueResult) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 13 with EntityNotFoundException

use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.

the class AbstractRiakMapEntityStore method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    try {
        final Bucket bucket = riakClient.fetchBucket(bucketKey).execute();
        changes.visitMap(new MapChanger() {

            @Override
            public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                            bucket.store(ref.identity(), toString()).execute();
                        } catch (RiakException ex) {
                            throw new EntityStoreException("Unable to apply entity change: newEntity", ex);
                        }
                    }
                };
            }

            @Override
            public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
                return new StringWriter(1000) {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                            IRiakObject entity = bucket.fetch(ref.identity()).execute();
                            if (entity == null) {
                                throw new EntityNotFoundException(ref);
                            }
                            bucket.store(ref.identity(), toString()).execute();
                        } catch (RiakException ex) {
                            throw new EntityStoreException("Unable to apply entity change: updateEntity", ex);
                        }
                    }
                };
            }

            @Override
            public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
                try {
                    IRiakObject entity = bucket.fetch(ref.identity()).execute();
                    if (entity == null) {
                        throw new EntityNotFoundException(ref);
                    }
                    bucket.delete(ref.identity()).execute();
                } catch (RiakException ex) {
                    throw new EntityStoreException("Unable to apply entity change: removeEntity", ex);
                }
            }
        });
    } catch (RiakRetryFailedException ex) {
        throw new EntityStoreException("Unable to apply entity changes.", ex);
    }
}
Also used : IRiakObject(com.basho.riak.client.IRiakObject) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) StringWriter(java.io.StringWriter) Bucket(com.basho.riak.client.bucket.Bucket) EntityReference(org.qi4j.api.entity.EntityReference) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) RiakRetryFailedException(com.basho.riak.client.RiakRetryFailedException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) RiakException(com.basho.riak.client.RiakException)

Example 14 with EntityNotFoundException

use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.

the class AbstractRiakMapEntityStore method get.

@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
    try {
        Bucket bucket = riakClient.fetchBucket(bucketKey).execute();
        IRiakObject entity = bucket.fetch(entityReference.identity()).execute();
        if (entity == null) {
            throw new EntityNotFoundException(entityReference);
        }
        String jsonState = entity.getValueAsString();
        return new StringReader(jsonState);
    } catch (RiakRetryFailedException ex) {
        throw new EntityStoreException("Unable to get Entity " + entityReference.identity(), ex);
    }
}
Also used : Bucket(com.basho.riak.client.bucket.Bucket) IRiakObject(com.basho.riak.client.IRiakObject) StringReader(java.io.StringReader) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) RiakRetryFailedException(com.basho.riak.client.RiakRetryFailedException)

Example 15 with EntityNotFoundException

use of org.qi4j.spi.entitystore.EntityNotFoundException in project qi4j-sdk by Qi4j.

the class VoldemortEntityStoreMixin method get.

@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
    try {
        Versioned<byte[]> versioned = client.get(entityReference.identity());
        if (versioned == null) {
            throw new EntityNotFoundException(entityReference);
        }
        byte[] serializedState = versioned.getValue();
        return new StringReader(new String(serializedState, "UTF-8"));
    } catch (IOException e) {
        throw new EntityStoreException(e);
    }
}
Also used : EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Aggregations

EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)18 IOException (java.io.IOException)9 EntityReference (org.qi4j.api.entity.EntityReference)8 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)8 StringReader (java.io.StringReader)7 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)6 StringWriter (java.io.StringWriter)5 Writer (java.io.Writer)5 EntityState (org.qi4j.spi.entity.EntityState)3 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)3 IRiakObject (com.basho.riak.client.IRiakObject)2 RiakRetryFailedException (com.basho.riak.client.RiakRetryFailedException)2 Bucket (com.basho.riak.client.bucket.Bucket)2 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 Blob (org.jclouds.blobstore.domain.Blob)2 Usecase (org.qi4j.api.usecase.Usecase)2 JSONEntityState (org.qi4j.spi.entitystore.helpers.JSONEntityState)2 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)2 ResourceException (org.restlet.resource.ResourceException)2