Search in sources :

Example 1 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method init.

public void init(Properties p) {
    String awsAccessKeyId = p.getProperty("awsAccessKeyId");
    String awsSecretAccessKey = p.getProperty("awsSecretAccessKey");
    if (awsAccessKeyId == null || awsSecretAccessKey == null)
        throw new SienaException("Both awsAccessKeyId and awsSecretAccessKey properties must be set");
    prefix = p.getProperty("prefix");
    if (prefix == null)
        prefix = "";
    sdb = new AmazonSimpleDBClient(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey));
}
Also used : AmazonSimpleDBClient(com.amazonaws.services.simpledb.AmazonSimpleDBClient) SienaException(siena.SienaException) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 2 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method doFetchList.

protected <T> void doFetchList(Query<T> query, int limit, int offset, List<T> resList, int depth) {
    if (depth >= MAX_DEPTH) {
        throw new SienaException("Reached maximum depth of recursion when retrieving more data (" + MAX_DEPTH + ")");
    }
    preFetch(query, limit, offset, !resList.isEmpty());
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType) query.option(QueryOptionFetchType.ID);
    QueryOptionOffset off = (QueryOptionOffset) query.option(QueryOptionOffset.ID);
    // if previousPage has detected there is no more data, simply returns an empty list
    if (sdbCtx.noMoreDataBefore || sdbCtx.noMoreDataAfter) {
        return;
    }
    // manages cursor limitations for IN and != operators with offsets
    if (!sdbCtx.isActive()) {
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        SelectResult res = sdb.select(req);
        // activates the SdbCtx now that it is really initialised
        sdbCtx.activate();
        postFetch(query, res);
        // cursor not yet created
        switch(fetchType.fetchType) {
            case KEYS_ONLY:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
                }
                break;
            case NORMAL:
            default:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
                }
                // join management
                if (!query.getJoins().isEmpty() || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                    mapJoins(query, resList);
        }
        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    } else {
        // we prepare the query each time
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        // we can't use real asynchronous function with cursors
        // so the page is extracted at once and wrapped into a SienaFuture
        String token = sdbCtx.currentToken();
        if (token != null) {
            req.setNextToken(token);
        }
        SelectResult res = sdb.select(req);
        postFetch(query, res);
        switch(fetchType.fetchType) {
            case KEYS_ONLY:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
                }
                break;
            case NORMAL:
            default:
                if (off.isActive()) {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
                } else {
                    SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
                }
                // join management
                if (!query.getJoins().isEmpty() || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                    mapJoins(query, resList);
        }
        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    }
}
Also used : QueryOptionOffset(siena.core.options.QueryOptionOffset) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) SienaException(siena.SienaException) QueryOptionFetchType(siena.core.options.QueryOptionFetchType) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 3 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method count.

public <T> int count(Query<T> query) {
    StringBuffer domainBuf = new StringBuffer();
    SelectRequest req = SdbMappingUtils.buildCountQuery(query, prefix, domainBuf);
    try {
        checkDomain(domainBuf.toString());
        req.setConsistentRead(isReadConsistent());
        SelectResult res = sdb.select(req);
        return SdbMappingUtils.mapSelectResultToCount(res);
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) AmazonClientException(com.amazonaws.AmazonClientException) SienaException(siena.SienaException) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 4 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method insert.

public void insert(Object obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    String domain = SdbMappingUtils.getDomainName(clazz, prefix);
    try {
        checkDomain(domain);
        sdb.putAttributes(SdbMappingUtils.createPutRequest(domain, clazz, info, obj));
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 5 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method delete.

public void delete(Object obj) {
    Class<?> clazz = obj.getClass();
    String domain = SdbMappingUtils.getDomainName(clazz, prefix);
    try {
        checkDomain(domain);
        sdb.deleteAttributes(SdbMappingUtils.createDeleteRequest(domain, clazz, obj));
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) SienaException(siena.SienaException)

Aggregations

SienaException (siena.SienaException)214 Field (java.lang.reflect.Field)65 TransactionAccountFrom (siena.base.test.model.TransactionAccountFrom)52 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)40 SQLException (java.sql.SQLException)38 TransactionAccountTo (siena.base.test.model.TransactionAccountTo)34 SienaRestrictedApiException (siena.SienaRestrictedApiException)33 PreparedStatement (java.sql.PreparedStatement)25 ClassInfo (siena.ClassInfo)21 HashMap (java.util.HashMap)19 List (java.util.List)18 Discovery (siena.base.test.model.Discovery)18 Entity (com.google.appengine.api.datastore.Entity)15 Key (com.google.appengine.api.datastore.Key)14 ResultSet (java.sql.ResultSet)14 Id (siena.Id)14 AmazonClientException (com.amazonaws.AmazonClientException)12 Collection (java.util.Collection)10 DiscoveryStringId (siena.base.test.model.DiscoveryStringId)9