Example usage for com.mongodb DBCollection setObjectClass

List of usage examples for com.mongodb DBCollection setObjectClass

Introduction

In this page you can find the example usage for com.mongodb DBCollection setObjectClass.

Prototype

public void setObjectClass(final Class<? extends DBObject> aClass) 

Source Link

Document

Sets a default class for objects in this collection; null resets the class to nothing.

Usage

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.AlgorithmDao.java

License:Open Source License

/**
 * Creates a new algorithm's metadata./*w ww  .  jav  a 2  s.c  o m*/
 *
 * @param algorithm the new algorithm's metadata to be saved.
 * @return the Id of the new metadata object.
 */
public String createAlgorithm(Algorithm algorithm) {
    log.debug(MSG_DAO_CREATE);

    // Set collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok and retrieve the objectId
    ObjectId ownerId = checkOwner(algorithm, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        algorithm.setOwnerId(ownerId);
    } else {
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
    }

    // Check if the permissions field is ok and retrieve the list of permissions
    List<Permission> permissionsList = checkPermissions(algorithm, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        algorithm.setPermissions(permissionsList);
    } else {
        log.error(MSG_ERR_NOT_VALID_PERMISSION);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION);
    }

    algorithmsCollection = INSTANCE.getDatasource().getDbCollection(ALGORITHMS_COLLECTION_NAME);
    algorithmsCollection.setObjectClass(Algorithm.class);
    algorithmsCollection.insert(algorithm);

    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(algorithm);
        log.debug("New algorithm's metadata: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return algorithm.getId();
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.AlgorithmDao.java

License:Open Source License

/**
 * Updates the selected algorithm's metadata if exists, otherwise creates a new one.
 *
 * @param id        the Id of the selected algorithm's metadata to be updated.
 * @param algorihtm the algorithm's metadata object with the modifications (or the metadata to be
 *                  saved).//w  ww  .  jav  a 2 s  .c o  m
 * @return the updated metadata object.
 */
public Algorithm upsertAlgorithm(String id, Algorithm algorihtm) {
    log.debug(MSG_DAO_UPSERT + id);

    if (!ObjectId.isValid(id)) {
        log.error(MSG_ERR_NOT_VALID_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
    }

    if (algorihtm == null) {
        log.error(MSG_ERR_NOT_VALID_OBJ);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ);
    }

    // Intercept the possibility to change the Id
    if (algorihtm.containsField("_id")) {
        algorihtm.removeField("_id");
    }

    // Set collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok
    ObjectId ownerId = checkOwner(algorihtm, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        algorihtm.setOwnerId(ownerId);
    } else {
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
    }

    // Check if the permissions field is ok
    List<Permission> permissionsList = checkPermissions(algorihtm, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        algorihtm.setPermissions(permissionsList);
    } else {
        log.error(MSG_ERR_NOT_VALID_PERMISSION);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION);
    }

    algorithmsCollection = INSTANCE.getDatasource().getDbCollection(ALGORITHMS_COLLECTION_NAME);
    algorithmsCollection.setObjectClass(Algorithm.class);
    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(id));
    WriteResult wRes = algorithmsCollection.update(query, algorihtm, true, false); // upply upsert

    String numUpdates = String.valueOf(wRes.getN());
    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(algorihtm);
        log.debug(numUpdates + " algorithm updated: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return algorihtm;
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DatasetDao.java

License:Open Source License

/**
 * Creates a new dataset's metadata.//from w w  w.j  a v  a 2  s.c o  m
 *
 * @param dataset the new dataset's metadata to be saved.
 * @return the Id of the new metadata object.
 */
public String createDataset(Dataset dataset) {
    log.debug(MSG_DAO_CREATE);

    // Set collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok and retrieve the objectId
    ObjectId ownerId = checkOwner(dataset, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        dataset.setOwnerId(ownerId);
    } else {
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
    }

    // Check if the users field is ok and retrieve the objectIds list
    List<Permission> permissionsList = checkPermissions(dataset, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        dataset.setPermissions(permissionsList);
    } else {
        log.error(MSG_ERR_NOT_VALID_PERMISSION);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION);
    }

    datasetsCollection = INSTANCE.getDatasource().getDbCollection(DATASETS_COLLECTION_NAME);
    datasetsCollection.setObjectClass(Dataset.class);
    datasetsCollection.insert(dataset);

    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(dataset);
        log.debug("New dataset's metadata: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return dataset.getId();
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DatasetDao.java

License:Open Source License

/**
 * Updates the selected dataset's metadata if exists, otherwise creates a new one.
 *
 * @param id      the Id of the selected dataset's metadata to be updated.
 * @param dataset the dataset's metadata object with the modifications (or the metadata to be
 *                saved)// ww w. j a va  2 s . co m
 * @return the updated metadata object.
 */
public Dataset upsertDataset(String id, Dataset dataset) {
    log.debug(MSG_DAO_UPSERT);

    if (!ObjectId.isValid(id)) {
        log.error(MSG_ERR_NOT_VALID_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
    }

    if (dataset == null) {
        log.error(MSG_ERR_NOT_VALID_OBJ);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ);
    }

    // Intecept the possibility to change the Id
    if (dataset.containsField("_id")) {
        dataset.removeField("_id");
    }

    // Set collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok and retrieve the objectId
    ObjectId ownerId = checkOwner(dataset, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        dataset.setOwnerId(ownerId);
    } else {
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
    }

    // Check if the users field is ok and retrieve the objectIds list
    List<Permission> permissionsList = checkPermissions(dataset, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        dataset.setPermissions(permissionsList);
    } else {
        log.error(MSG_ERR_NOT_VALID_PERMISSION);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION);
    }

    datasetsCollection = INSTANCE.getDatasource().getDbCollection(DATASETS_COLLECTION_NAME);
    datasetsCollection.setObjectClass(Dataset.class);
    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(id));
    WriteResult wRes = datasetsCollection.update(query, dataset, true, false); // selection criteria, modifications to apply, upsert, multi-document

    String numUpdates = String.valueOf(wRes.getN());
    String jsonMsg;
    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(dataset);
        log.debug(numUpdates + " dataset updated: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return dataset;
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DataSourceDao.java

License:Open Source License

/**
 * Creates a new data-source's metadata object.
 *
 * @param datasource the new data-source's metadata to be stored.
 * @return the Id of the new data-source object.
 *///w  w  w  . ja v  a2  s  . c o  m
public String createDataSource(DataSource datasource) {
    log.debug(MSG_DAO_CREATE);

    // Set collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok and retrieve the objectId
    ObjectId ownerId = checkOwner(datasource, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        datasource.setOwnerId(ownerId);
    } else {
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
    }

    // Check if the permissions field is ok and retrieve the list of permissions
    List<Permission> permissionsList = checkPermissions(datasource, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        datasource.setPermissions(permissionsList);
    } else {
        log.error(MSG_ERR_NOT_VALID_PERMISSIONS);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSIONS);
    }

    datasourcesCollection = INSTANCE.getDatasource().getDbCollection(DATASOURCES_COLLECTION_NAME);
    datasourcesCollection.setObjectClass(DataSource.class);
    datasourcesCollection.insert(datasource);

    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(datasource);
        log.debug("New data source's metadata: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return datasource.getId();
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DataSourceDao.java

License:Open Source License

/**
 * Updates the selected data-source's metadata if exists, otherwise insert the new object.
 *
 * @param id         the Id of the selected data-source's metadata to be updated.
 * @param datasource the data-source's metadata object with the modifications (or the metadata
 *                   to be stored).//from  ww  w.  j  ava  2 s . c  om
 * @return the updates metadata object.
 */
public DataSource upsertDataSource(String id, DataSource datasource) {
    log.debug(MSG_DAO_UPSERT + id);

    if (!ObjectId.isValid(id)) {
        log.error(MSG_ERR_NOT_VALID_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
    }

    if (datasource == null) {
        log.error(MSG_ERR_NOT_VALID_OBJ);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ);
    }

    // Intercept the possibility to change the Id
    if (datasource.containsField("_id")) {
        datasource.removeField("_id");
    }

    // Set collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok
    ObjectId ownerId = checkOwner(datasource, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        datasource.setOwnerId(ownerId);
    } else { // catch the error
        log.error(MSG_ERR_NOT_VALID_OWNER_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID);
    }

    // Check if the permissions field is ok
    List<Permission> permissionsList = checkPermissions(datasource, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        datasource.setPermissions(permissionsList);
    } else { // catch the error
        log.error(MSG_ERR_NOT_VALID_PERMISSIONS);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSIONS);
    }

    datasourcesCollection = INSTANCE.getDatasource().getDbCollection(DATASOURCES_COLLECTION_NAME);
    datasourcesCollection.setObjectClass(DataSource.class);
    BasicDBObject query = new BasicDBObject("_id", new ObjectId(id));
    WriteResult wRes = datasourcesCollection.update(query, datasource, true, false); // upply upsert

    String numUpdates = String.valueOf(wRes.getN());
    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(datasource);
        log.debug(numUpdates + " datasource updated: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return datasource;
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DepartmentDao.java

License:Open Source License

/**
 * Create a new department./*  w  ww .  j  a  v a2s . c  o m*/
 *
 * @param department the new department to be saved
 * @return the Id of the new department.
 */
public String createDepartment(Department department) {
    log.debug(MSG_DAO_CREATE);

    if (department == null) {
        log.error(MSG_ERR_NOT_VALID_OBJ);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ);
    }

    BasicDBObject query = new BasicDBObject();

    if (department.getCompany() instanceof String) {
        String company_id = department.getCompany().toString();

        if (!ObjectId.isValid(company_id)) {
            log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
            throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
        }

        log.debug("Inserted company Id: " + company_id);

        query.put("_id", new ObjectId(company_id));
    } else if (department.getCompany() instanceof ObjectId) {
        ObjectId company_id = department.getCompanyId();

        log.debug("Inserted company Id: " + company_id.toString());

        query.put("_id", company_id);
    } else {
        log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
    }

    DBCollection companyCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    companyCollection.setObjectClass(Company.class);

    // Check if the company exists
    Company resCompany = (Company) companyCollection.findOne(query);

    if (resCompany == null) { // selected company doesn't exist
        log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
    } else {
        String debugMsg;
        try {
            debugMsg = INSTANCE.getObjectMapper().writeValueAsString(resCompany);
            log.debug("Related company: " + debugMsg); // print res in json format
        } catch (JsonProcessingException e) {
            log.error(e, e);
        }
    }

    department.setCompanyId(new ObjectId(resCompany.getId()));

    departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    departmentsCollection.setObjectClass(Department.class);
    try {
        departmentsCollection.insert(department);
    } catch (DuplicateKeyException e) {
        log.error(MSG_ERR_DUPLICATE_KEY, e);
        throw new BadRequestException(MSG_ERR_DUPLICATE_KEY);
    }

    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(department);
        log.debug("New department: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return department.getId();
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DepartmentDao.java

License:Open Source License

/**
 * Update the selected department if exists, otherwise create a new one.
 *
 * @param id         the Id of the selected department to be updated.
 * @param department the department object with the modifications (or the department to be
 *                   saved).//from   w w  w .ja v a 2 s .com
 * @return the updated department object.
 */
public Department upsertDepartment(String id, Department department) {
    log.debug(MSG_DAO_UPSERT + id + ".");

    // Intercept the possibility to change the Id
    if (department.containsField("_id")) {
        department.removeField("_id");
    }

    // Check if a department object is sent
    if (department == null) {
        log.error(MSG_ERR_NOT_VALID_OBJ);
        throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ);
    }

    // Setup MongoDB objects
    DBCollection companyCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    companyCollection.setObjectClass(Company.class);
    BasicDBObject query = new BasicDBObject();

    // Check if the inserted company Id is valid
    if (department.getCompany() instanceof ObjectId) { // type is already ObjectId
        ObjectId company_id = department.getCompanyId();

        log.debug("Inserted company Id: " + company_id.toString());

        query.put("_id", company_id); // setup query object
    } else if (department.getCompany() instanceof String) { // type is String
        String company_id = department.getCompany().toString();

        // Check if no Id is specified (empty string)
        if (company_id != null && company_id.isEmpty()) {
            log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
            throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
        } else {
            if (!ObjectId.isValid(company_id)) { // check if Id string is valid
                log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
                throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
            }

            log.debug("Inserted company Id: " + company_id);

            query.put("_id", new ObjectId(company_id)); // setup query object
        }
    } else {
        log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
    }

    Company resCompany = (Company) companyCollection.findOne(query);

    if (resCompany == null) { // selected company doesn't exist
        log.error(MSG_ERR_NOT_VALID_COMPANY_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID);
    } else {
        String debugMsg;
        try {
            debugMsg = INSTANCE.getObjectMapper().writeValueAsString(resCompany);
            log.debug("Related company: " + debugMsg); // print res in json format
        } catch (JsonProcessingException e) {
            log.error(e, e);
        }
    }

    // Associate companyId to department
    department.setCompanyId(new ObjectId(resCompany.getId()));

    departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    departmentsCollection.setObjectClass(Department.class);

    query.put("_id", new ObjectId(id)); // associate the Id
    WriteResult wRes = departmentsCollection.update(query, department, true, false); // upsert

    String numUpdates;
    String jsonMsg;

    try {
        numUpdates = String.valueOf(wRes.getN());
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(department);
        log.debug(numUpdates + " department updated: " + jsonMsg);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return department;
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DiscoverObjDao.java

License:Open Source License

/**
 * Discover the objects that can be used.
 *
 * The algorithm is the following: first check that requestorId is either a user, a department,
 * or a company, then build the query by following this assumptions: 1) if requestedId is user,
 * then extract from the user the department and the company; so build a query that includes
 * also objects usable by this department and this company. 2) if requestedId is department,
 * then extract from the department the company and so build a query that includes also object
 * usable by this company. 3) if requestedId is a company, than maintain the basic query with
 * this company./* w w w .j  a v a2 s  .  c om*/
 *
 * The query is built in this way: find the objects in which the field "permissions" contains an
 * object that has the field "referenceId" that is equal at least to one element from the array
 * of Ids.
 *
 * @param requestedId the Id of the user, department, or company that can use the objects.
 * @return the result of the query.
 */
public List discoverUsable(String requestedId) {
    log.debug(MSG_DAO_DISCV_USABLE + requestedId);

    if (!ObjectId.isValid(requestedId)) {
        log.error(MSG_ERR_NOT_VALID_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
    }

    User reqUser;
    DBCollection usersCollection;
    Department reqDepartment;
    DBCollection departmentsCollection;
    Company reqCompany;
    DBCollection companiesCollection;

    // Query for user, department, or company entities
    BasicDBObject query;

    // Query for objects (algorithms, datasets), containts field "status" specification ("public")
    List<BasicDBObject> queryObjects = new ArrayList<>();
    queryObjects.add(new BasicDBObject("status", "public")); // include public objects

    query = new BasicDBObject();
    query.put("_id", new ObjectId(requestedId));

    usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    reqUser = (User) usersCollection.findOne(query);
    if (reqUser != null) {
        // requestedId represents a User
        log.debug(MSG_DBG_IS_USER);
        List<ObjectId> iDs = new ArrayList<>();
        iDs.add(new ObjectId(reqUser.getId())); // search for user
        iDs.add(reqUser.getDepartmentId()); // search for user's department
        iDs.add(reqUser.getCompanyId()); // search for user's company
        // Build the query
        BasicDBObject innerQuery = new BasicDBObject("permissions", new BasicDBObject("$elemMatch",
                new BasicDBObject("referenceId", new BasicDBObject("$in", iDs))));
        // ...meaning: find in feald "permissions" if there is an object that has the field
        // "referenceId" that contains at least one element from the array "iDs"
        queryObjects.add(innerQuery); // include the inner query in objects query
    } else {
        departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
        departmentsCollection.setObjectClass(Department.class);
        reqDepartment = (Department) departmentsCollection.findOne(query);
        if (reqDepartment != null) {
            // requestedId represents a Department
            log.debug(MSG_DBG_IS_DEPARTMENT);
            List<ObjectId> iDs = new ArrayList();
            iDs.add(new ObjectId(reqDepartment.getId())); // search for department
            iDs.add(reqDepartment.getCompanyId()); // search for department's company
            // Build the query
            BasicDBObject innerQuery = new BasicDBObject("permissions", new BasicDBObject("$elemMatch",
                    new BasicDBObject("referenceId", new BasicDBObject("$in", iDs))));
            queryObjects.add(innerQuery); // include the inner query in objects query
        } else {
            companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
            companiesCollection.setObjectClass(Company.class);
            reqCompany = (Company) companiesCollection.findOne(query);
            if (reqCompany != null) {
                // requestedId represents a Company
                log.debug(MSG_DBG_IS_COMPANY);
                // Build the query
                BasicDBObject innerQuery = new BasicDBObject("permissions", new BasicDBObject("$elemMatch",
                        new BasicDBObject("referenceId", new ObjectId(requestedId)))); // search for company
                queryObjects.add(innerQuery); // include the inner query in objects query
            } else {
                // unrecognized Id
                log.error(MSG_ERR_NOT_VALID_ID);
                //throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
                // unrecognized Id - return only public objects
            }
        }
    }

    log.debug("Final query: " + queryObjects.toString());

    return makeQuery(queryObjects);
}

From source file:com.tilab.fiware.metaware.dao.impls.mongodb.ProcessDao.java

License:Open Source License

/**
 * Creates a new process' metadata object.
 *
 * @param process the new process' metadata object to be saved.
 * @return the Id of the new inserted process' metadata object.
 *///from   w w w. j  av  a 2  s. com
public String createProcess(Process process) {
    log.debug(MSG_DAO_CREATE);

    // Set the collections
    DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME);
    DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME);
    DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME);
    usersCollection.setObjectClass(User.class);
    departmentsCollection.setObjectClass(Department.class);
    companiesCollection.setObjectClass(Company.class);

    // Check if the owner field is ok and retrieve the ObjectId
    ObjectId ownerId = checkOwner(process, usersCollection, departmentsCollection, companiesCollection);
    if (ownerId != null) {
        process.setOwnerId(ownerId);
    } else {
        log.error(MSG_ERR_NOT_VALID_ID);
        throw new BadRequestException(MSG_ERR_NOT_VALID_ID);
    }

    // Check if the permissions field is ok and retrieve the list of permissions
    List<Permission> permissionsList = checkPermissions(process, usersCollection, departmentsCollection,
            companiesCollection);
    if (permissionsList != null) {
        process.setPermissions(permissionsList);
    } else {
        log.error(MSG_ERR_NOT_VALID_PERMISSION);
        throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION);
    }

    processesCollection = INSTANCE.getDatasource().getDbCollection(PROCESSES_COLLECTION_NAME);
    processesCollection.setObjectClass(Process.class);
    processesCollection.insert(process);

    String jsonMsg;

    try {
        jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(process);
    } catch (JsonProcessingException e) {
        log.error(e, e);
    }

    return process.getId();
}