Search in sources :

Example 1 with FindOne

use of org.jongo.FindOne in project profile by craftercms.

the class ProfileRepositoryImpl method findOneByQuery.

@Override
public Profile findOneByQuery(String query, String... attributesToReturn) throws MongoDataException {
    try {
        FindOne findOne = getCollection().findOne(query);
        addProjection(findOne, attributesToReturn);
        return findOne.as(Profile.class);
    } catch (MongoException ex) {
        String msg = "Unable to find profile by query '" + query + "'";
        logger.error(msg, ex);
        throw new MongoDataException(msg, ex);
    }
}
Also used : FindOne(org.jongo.FindOne) MongoException(com.mongodb.MongoException) MongoDataException(org.craftercms.commons.mongo.MongoDataException)

Example 2 with FindOne

use of org.jongo.FindOne in project profile by craftercms.

the class ProfileRepositoryImpl method findById.

@Override
public Profile findById(String id, String... attributesToReturn) throws MongoDataException {
    try {
        FindOne findOne = getCollection().findOne(new ObjectId(id));
        addProjection(findOne, attributesToReturn);
        return findOne.as(Profile.class);
    } catch (MongoException ex) {
        String msg = "Unable to find profile by id '" + id + "'";
        logger.error(msg, ex);
        throw new MongoDataException(msg, ex);
    } catch (IllegalArgumentException ex) {
        String msg = "Given id '" + id + "' can't be converted to an ObjectId";
        logger.error(msg, ex);
        throw new MongoDataException(msg, ex);
    }
}
Also used : FindOne(org.jongo.FindOne) MongoException(com.mongodb.MongoException) ObjectId(org.bson.types.ObjectId) MongoDataException(org.craftercms.commons.mongo.MongoDataException)

Example 3 with FindOne

use of org.jongo.FindOne in project profile by craftercms.

the class ProfileRepositoryImpl method findByTenantAndUsername.

@Override
public Profile findByTenantAndUsername(String tenantName, String username, String... attributesToReturn) throws MongoDataException {
    try {
        String query = getQueryFor(KEY_FIND_BY_TENANT_AND_USERNAME_QUERY);
        FindOne findOne = getCollection().findOne(query, tenantName, username);
        if (ArrayUtils.isNotEmpty(attributesToReturn)) {
            findOne = findOne.projection(buildProjectionWithAttributes(attributesToReturn));
        }
        return findOne.as(Profile.class);
    } catch (MongoException ex) {
        String msg = "Unable to find profile by tenant name '" + tenantName + "' and username '" + username + "'";
        logger.error(msg, ex);
        throw new MongoDataException(msg, ex);
    }
}
Also used : FindOne(org.jongo.FindOne) MongoException(com.mongodb.MongoException) MongoDataException(org.craftercms.commons.mongo.MongoDataException)

Aggregations

MongoException (com.mongodb.MongoException)3 MongoDataException (org.craftercms.commons.mongo.MongoDataException)3 FindOne (org.jongo.FindOne)3 ObjectId (org.bson.types.ObjectId)1