Search in sources :

Example 6 with EntityNotFoundException

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

the class JdbmEntityStoreMixin method applyChanges.

@WriteLock
@Override
public void applyChanges(MapChanges changes) throws IOException {
    try {
        changes.visitMap(new MapChanger() {

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

                    @Override
                    public void close() throws IOException {
                        super.close();
                        byte[] stateArray = toString().getBytes("UTF-8");
                        long stateIndex = recordManager.insert(stateArray, serializer);
                        String indexKey = ref.toString();
                        index.insert(indexKey.getBytes("UTF-8"), stateIndex, false);
                    }
                };
            }

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

                    @Override
                    public void close() throws IOException {
                        super.close();
                        Long stateIndex = getStateIndex(ref.toString());
                        byte[] stateArray = toString().getBytes("UTF-8");
                        recordManager.update(stateIndex, stateArray, serializer);
                    }
                };
            }

            @Override
            public void removeEntity(EntityReference ref, EntityDescriptor descriptor) throws EntityNotFoundException {
                try {
                    Long stateIndex = getStateIndex(ref.toString());
                    recordManager.delete(stateIndex);
                    index.remove(ref.toString().getBytes("UTF-8"));
                } catch (IOException e) {
                    throw new EntityStoreException(e);
                }
            }
        });
        recordManager.commit();
    } catch (Exception e) {
        e.printStackTrace();
        recordManager.rollback();
        if (e instanceof IOException) {
            throw (IOException) e;
        } else if (e instanceof EntityStoreException) {
            throw (EntityStoreException) e;
        } else {
            throw new IOException(e);
        }
    }
}
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) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) WriteLock(org.qi4j.library.locking.WriteLock)

Example 7 with EntityNotFoundException

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

the class MongoMapEntityStoreMixin method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    db.requestStart();
    final DBCollection entities = db.getCollection(collectionName);
    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();
                    DBObject bsonState = (DBObject) JSON.parse(jsonState);
                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.insert(entity, writeConcern);
                }
            };
        }

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

                @Override
                public void close() throws IOException {
                    super.close();
                    DBObject bsonState = (DBObject) JSON.parse(toString());
                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.update(byIdentity(ref), entity, false, false, writeConcern);
                }
            };
        }

        @Override
        public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
            DBObject entity = entities.findOne(byIdentity(ref));
            if (entity == null) {
                throw new EntityNotFoundException(ref);
            }
            entities.remove(entity, writeConcern);
        }
    });
    db.requestDone();
}
Also used : DBCollection(com.mongodb.DBCollection) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) BasicDBObject(com.mongodb.BasicDBObject) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 8 with EntityNotFoundException

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

the class MongoMapEntityStoreMixin method get.

@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
    db.requestStart();
    DBObject entity = db.getCollection(collectionName).findOne(byIdentity(entityReference));
    if (entity == null) {
        throw new EntityNotFoundException(entityReference);
    }
    DBObject bsonState = (DBObject) entity.get(STATE_COLUMN);
    db.requestDone();
    String jsonState = JSON.serialize(bsonState);
    return new StringReader(jsonState);
}
Also used : StringReader(java.io.StringReader) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 9 with EntityNotFoundException

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

the class GaeEntityStoreUnitOfWork method entityStateOf.

@Override
public EntityState entityStateOf(EntityReference reference) throws EntityStoreException, EntityNotFoundException {
    Key key = KeyFactory.createKey("qi4j-entity", reference.identity());
    try {
        Entity entity = datastore.get(key);
        GaeEntityState state = new GaeEntityState(this, valueSerialization, entity, module);
        states.add(state);
        return state;
    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
        throw new EntityNotFoundException(reference);
    }
}
Also used : com.google.appengine.api.datastore(com.google.appengine.api.datastore) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException)

Example 10 with EntityNotFoundException

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

the class JCloudsMapEntityStoreMixin method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    final BlobStore blobStore = storeContext.getBlobStore();
    changes.visitMap(new MapChanger() {

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

                @Override
                public void close() throws IOException {
                    super.close();
                    Blob blob = blobStore.blobBuilder(ref.identity()).payload(ByteSource.wrap(toString().getBytes(UTF_8))).build();
                    blobStore.putBlob(container, blob);
                }
            };
        }

        @Override
        public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
            if (!blobStore.blobExists(container, ref.identity())) {
                throw new EntityNotFoundException(ref);
            }
            return new StringWriter() {

                @Override
                public void close() throws IOException {
                    super.close();
                    Blob blob = blobStore.blobBuilder(ref.identity()).payload(ByteSource.wrap(toString().getBytes(UTF_8))).build();
                    blobStore.putBlob(container, blob);
                }
            };
        }

        @Override
        public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
            if (!blobStore.blobExists(container, ref.identity())) {
                throw new EntityNotFoundException(ref);
            }
            blobStore.removeBlob(container, ref.identity());
        }
    });
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) Blob(org.jclouds.blobstore.domain.Blob) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) EntityReference.parseEntityReference(org.qi4j.api.entity.EntityReference.parseEntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) BlobStore(org.jclouds.blobstore.BlobStore) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

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