Search in sources :

Example 1 with EntityNotFoundException

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

the class JCloudsMapEntityStoreMixin method get.

@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
    InputStreamMap isMap = storeContext.createInputStreamMap(container);
    InputStream input = isMap.get(entityReference.identity());
    if (input == null) {
        throw new EntityNotFoundException(entityReference);
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Inputs.byteBuffer(input, 4096).transferTo(Outputs.byteBuffer(baos));
        return new StringReader(baos.toString("UTF-8"));
    } catch (IOException ex) {
        throw new EntityStoreException("Unable to read entity state for: " + entityReference, ex);
    } finally {
        try {
            input.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : InputStreamMap(org.jclouds.blobstore.InputStreamMap) InputStream(java.io.InputStream) StringReader(java.io.StringReader) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 2 with EntityNotFoundException

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

the class JdbmEntityStoreMixin method get.

@ReadLock
@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
    try {
        Long stateIndex = getStateIndex(entityReference.identity());
        if (stateIndex == null) {
            throw new EntityNotFoundException(entityReference);
        }
        byte[] serializedState = (byte[]) recordManager.fetch(stateIndex, serializer);
        if (serializedState == null) {
            throw new EntityNotFoundException(entityReference);
        }
        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) ReadLock(org.qi4j.library.locking.ReadLock)

Example 3 with EntityNotFoundException

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

the class GaeEntityStoreMixin method get.

@Override
public Reader get(EntityReference ref) throws EntityStoreException {
    try {
        Key key = KeyFactory.createKey(entityKind, ref.toURI());
        Entity entity = datastore.get(key);
        Text serializedState = (Text) entity.getProperty("value");
        if (serializedState == null) {
            throw new EntityNotFoundException(ref);
        }
        return new StringReader(serializedState.getValue());
    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
        e.printStackTrace();
        throw new EntityNotFoundException(ref);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) StringReader(java.io.StringReader) Text(com.google.appengine.api.datastore.Text) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) Key(com.google.appengine.api.datastore.Key)

Example 4 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();
                    System.out.println("############################################");
                    try {
                        System.out.println(new JSONObject(jsonState).toString(2));
                    } catch (JSONException ex) {
                        ex.printStackTrace();
                    }
                    System.out.println("############################################");
                    DBObject bsonState = (DBObject) JSON.parse(jsonState);
                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.save(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, true, 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 : JSONException(org.json.JSONException) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) BasicDBObject(com.mongodb.BasicDBObject) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) EntityReference(org.qi4j.api.entity.EntityReference) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 5 with EntityNotFoundException

use of org.qi4j.spi.entitystore.EntityNotFoundException 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)

Aggregations

EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)18 IOException (java.io.IOException)8 EntityReference (org.qi4j.api.entity.EntityReference)8 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)8 StringReader (java.io.StringReader)6 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 Usecase (org.qi4j.api.usecase.Usecase)2 JSONEntityState (org.qi4j.spi.entitystore.helpers.JSONEntityState)2 ResourceException (org.restlet.resource.ResourceException)2 RiakException (com.basho.riak.client.RiakException)1 com.google.appengine.api.datastore (com.google.appengine.api.datastore)1