Search in sources :

Example 1 with DataServiceFault

use of org.wso2.carbon.dataservices.core.DataServiceFault in project wso2-dss-connectors by wso2-attic.

the class MongoDBDataSource method decodeQuery.

private Object[] decodeQuery(String query) throws DataServiceFault {
    int i1 = query.indexOf('.');
    if (i1 == -1) {
        throw new DataServiceFault("The MongoDB Collection not specified in the query '" + query + "'");
    }
    String collection = query.substring(0, i1).trim();
    int i2 = query.indexOf('(', i1);
    if (i2 == -1 || i2 - i1 <= 1) {
        throw new DataServiceFault("Invalid MongoDB operation in the query '" + query + "'");
    }
    String operation = query.substring(i1 + 1, i2).trim();
    int i3 = query.lastIndexOf(')');
    if (i3 == -1) {
        throw new DataServiceFault("Invalid MongoDB operation in the query '" + query + "'");
    }
    String opQuery = null;
    if (i3 - i2 > 1) {
        opQuery = query.substring(i2 + 1, i3).trim();
    }
    MongoOperation mongoOp = this.convertToMongoOp(operation);
    if (mongoOp == MongoOperation.UPDATE) {
        List<Object> result = new ArrayList<Object>();
        result.add(collection);
        result.add(mongoOp);
        result.addAll(parseInsertQuery(opQuery));
        return result.toArray();
    } else {
        return new Object[] { collection, mongoOp, this.checkAndCleanOpQuery(opQuery) };
    }
}
Also used : MongoOperation(org.wso2.dss.connectors.mongodb.MongoDBDSConstants.MongoOperation) DataServiceFault(org.wso2.carbon.dataservices.core.DataServiceFault) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject)

Example 2 with DataServiceFault

use of org.wso2.carbon.dataservices.core.DataServiceFault in project wso2-dss-connectors by wso2-attic.

the class MongoDBDataSource method init.

@Override
public void init(Map<String, String> params) throws DataServiceFault {
    String serversParam = params.get(MongoDBDSConstants.SERVERS);
    if (DBUtils.isEmptyString(serversParam)) {
        throw new DataServiceFault("The data source param '" + MongoDBDSConstants.SERVERS + "' is required");
    }
    String[] servers = serversParam.split(",");
    String database = params.get(MongoDBDSConstants.DATABASE);
    if (DBUtils.isEmptyString(database)) {
        throw new DataServiceFault("The data source param '" + MongoDBDSConstants.DATABASE + "' is required");
    }
    try {
        this.mongo = new Mongo(this.createServerAddresses(servers), this.extractMongoOptions(params));
        String writeConcern = params.get(MongoDBDSConstants.WRITE_CONCERN);
        if (!DBUtils.isEmptyString(writeConcern)) {
            this.getMongo().setWriteConcern(WriteConcern.valueOf(writeConcern));
        }
        String readPref = params.get(MongoDBDSConstants.READ_PREFERENCE);
        if (!DBUtils.isEmptyString(readPref)) {
            this.getMongo().setReadPreference(ReadPreference.valueOf(readPref));
        }
        this.jongo = new Jongo(this.getMongo().getDB(database));
    } catch (Exception e) {
        throw new DataServiceFault(e);
    }
}
Also used : Mongo(com.mongodb.Mongo) Jongo(org.jongo.Jongo) DataServiceFault(org.wso2.carbon.dataservices.core.DataServiceFault)

Aggregations

DataServiceFault (org.wso2.carbon.dataservices.core.DataServiceFault)2 DBObject (com.mongodb.DBObject)1 Mongo (com.mongodb.Mongo)1 ArrayList (java.util.ArrayList)1 Jongo (org.jongo.Jongo)1 MongoOperation (org.wso2.dss.connectors.mongodb.MongoDBDSConstants.MongoOperation)1