List of usage examples for com.mongodb WriteResult getN
public int getN()
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DatasetDao.java
License:Open Source License
/** * Removes the selected dataset's metadata. * * @param id the Id of the selected dataset's metadata. *///from w ww .j a va2 s . c o m public void deleteDataset(String id) { log.debug(MSG_DAO_DELETE); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } datasetsCollection = INSTANCE.getDatasource().getDbCollection(DATASETS_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = datasetsCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }
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).//w w w . jav a2s . c o m * @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.DataSourceDao.java
License:Open Source License
/** * Removes the selected data-source's metadata object. * * @param id the Id of the selected data-source's metadata object. *///from w w w . j a va 2s .c om public void deleteDataSource(String id) { log.debug(MSG_DAO_DELETE + id); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } datasourcesCollection = INSTANCE.getDatasource().getDbCollection(DATASOURCES_COLLECTION_NAME); BasicDBObject query = new BasicDBObject("_id", new ObjectId(id)); WriteResult wRes = datasourcesCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }
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 .j av a 2s. c om*/ * @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.DepartmentDao.java
License:Open Source License
/** * Remove the selected department.//from w w w .ja va 2 s . c o m * * @param id the Id of the selected department. */ public void deleteDepartment(String id) { log.debug(MSG_DAO_DELETE + id + "."); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = departmentsCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.ProcessDao.java
License:Open Source License
/** * Updates the selected process' metadata object if exists, otherwise creates a new one. * * @param id the Id of the selected process' metadata object to be updated. * @param process the process' metadata object with the modifications. * @return the updated process' metadata. *//*from w w w .j a v a 2s.c o m*/ public Process upsertProcess(String id, Process process) { log.debug(MSG_DAO_UPSERT + id); // Initial checks if (!ObjectId.isValid(id)) { // check id string log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } if (process == null) { // check if process param exists log.error(MSG_ERR_NOT_VALID_OBJ); throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ); } if (process.containsField("_id")) { // intercept the possibility to change the Id process.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(process, usersCollection, departmentsCollection, companiesCollection); if (ownerId != null) { process.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(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); } // Perform the update processesCollection = INSTANCE.getDatasource().getDbCollection(PROCESSES_COLLECTION_NAME); processesCollection.setObjectClass(Process.class); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = processesCollection.update(query, process, true, false); // selection criteria, modifications to apply, upsert, multi-document // Output production String numUpdates = String.valueOf(wRes.getN()); // wRes.getN returns the number of updated objects String jsonMsg; try { jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(process); log.debug(numUpdates + " processes updated: " + process); } catch (JsonProcessingException e) { log.error(e, e); } return process; }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.ProcessDao.java
License:Open Source License
/** * Removes the selected process' metadata object. * * @param id the Id of the selected process' metadata object. *///from w w w . j a va 2 s.co m public void deleteProcess(String id) { log.debug(MSG_DAO_DELETE + id); // Check passed Id if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } processesCollection = INSTANCE.getDatasource().getDbCollection(PROCESSES_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = processesCollection.remove(query); // Check the number of deleted objects if (wRes.getN() == 0) { // if 0 then the query found nothing log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(MSG_ERR_NOT_FOUND); } }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.TemplateDao.java
License:Open Source License
/** * Remove the metadata structure template of the specified object. * * @param name the name of the object./*from w ww. j a v a2 s.com*/ */ public void deleteTemplate(String name) { log.debug(MSG_DAO_DELETE + name); templatesCollection = INSTANCE.getDatasource().getDbCollection(TEMPLATES_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("name", name); WriteResult wRes = templatesCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.UserDao.java
License:Open Source License
/** * Update the selected user if exists, otherwise create a new one. * * @param id the Id of the selected user to be updated. * @param user the user object with the modifications (or the new user to be saved). * @return the updated user object./*from w w w.java2 s . com*/ */ public User upsertUser(String id, User user) { log.debug(MSG_DAO_UPSERT + id + "."); // Intercept the possibility to change the Id if (user.containsField("_id")) { user.removeField("_id"); } // Check if the user object is sent if (user == null) { log.error(MSG_ERR_NOT_VALID_OBJ); throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ); } // Setup MongoDB query BasicDBObject query = new BasicDBObject(); // Check if the inserted company Id is valid if (user.getCompany() instanceof ObjectId) { // already an ObjectId ObjectId company_id = user.getCompanyId(); log.debug("Inserted company id: " + company_id.toHexString()); // Setup query object query.put("_id", company_id); // Execute the query and associate the final companyId user.setCompanyId(new ObjectId(retrieveCompany(query).getId())); } else if (user.getCompany() instanceof String) { // stored as a String String company_id = user.getCompany().toString(); if (company_id != null && company_id.isEmpty()) { // empty string log.debug(MSG_WARN_NO_COMPANY); // ok but warning } else { // Check if the inserted Id is valid if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } log.debug("Inserted company id: " + company_id); // Setup query object query.put("_id", new ObjectId(company_id)); // Execute the query and associate the final companyId user.setCompanyId(new ObjectId(retrieveCompany(query).getId())); } } else { log.error(MSG_ERR_NOT_VALID_COMPANY_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_COMPANY_ID); } // Check if the inserted department Id is valid if (user.getDepartment() instanceof ObjectId) { // already an ObjectId ObjectId department_id = user.getDepartmentId(); log.debug("Inserted department id: " + department_id.toHexString()); // Setup query object query.put("_id", department_id); // Execute the query and associate the final departmentId user.setDepartmentId(new ObjectId(retrieveDepartment(query).getId())); } else if (user.getDepartment() instanceof String) { // stored as a string String department_id = user.getDepartment().toString(); if (department_id != null && department_id.isEmpty()) { // empty string log.debug(MSG_WARN_NO_DEPARTMENT); // ok but warn } else { if (!ObjectId.isValid(department_id)) { log.error(MSG_ERR_NOT_VALID_DEPARTMENT_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_DEPARTMENT_ID); } log.debug("Inserted department id: " + department_id); // Setup query object query.put("_id", new ObjectId(department_id)); // Execute the query and associate the final departmentId user.setDepartmentId(new ObjectId(retrieveDepartment(query).getId())); } } else { log.error(MSG_ERR_NOT_VALID_DEPARTMENT_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_DEPARTMENT_ID); } // Perform the upsert usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME); usersCollection.setObjectClass(User.class); query.put("_id", new ObjectId(id)); User storedUser = (User) usersCollection.findOne(query); // Force to maintain the same role if not admin - to change normal user to admin, a new user has to be created if (!storedUser.getRole().equals("admin")) { user.setRole(storedUser.getRole()); } WriteResult wRes = usersCollection.update(query, user, true, false); // selection criteria, modifications to apply, upsert, multi-document update String numUpdates; String jsonMsg; try { numUpdates = String.valueOf(wRes.getN()); jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(user); log.debug(numUpdates + " user updated: " + jsonMsg); // print res in json format } catch (JsonProcessingException e) { log.error(e, e); } return user; }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.UserDao.java
License:Open Source License
/** * Remove the selected user./* w w w . j a va2 s .co m*/ * * @param id the Id of the selected user. */ public void deleteUser(String id) { log.debug(MSG_DAO_DELETE + id + "."); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = usersCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }