List of usage examples for com.mongodb DBObject containsField
boolean containsField(String s);
From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java
License:Apache License
@Override public List<DBObject> findAllByQuery(String entityName, DBObject query) { if (query.containsField(EntityUtils.NAME)) { String name = (String) query.get(EntityUtils.NAME); query.put(EntityUtils.NAME,//from w w w . jav a 2 s . c o m new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i")); } DBCursor cursor = getCollection(entityName).find(query); List<DBObject> result = cursor.toArray(); return result; }
From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java
License:Apache License
/** * @param entityName//from w w w. ja v a 2 s .c om * @param query * @return list of all documents by query */ @Override public List<DBObject> findAllByQuery(String entityName, DBObject query, DBObject projection) { if (query.containsField(EntityUtils.NAME)) { String name = (String) query.get(EntityUtils.NAME); query.put(EntityUtils.NAME, new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i")); } final DBCursor cursor; if (projection != null && projection.keySet().size() > 0) { cursor = getCollection(entityName).find(query, projection); } else { cursor = getCollection(entityName).find(query); } List<DBObject> result = cursor.toArray(); return result; }
From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java
License:Apache License
@Override public List<DBObject> findAllByQuery(String entityName, DBObject query, Pageable page) { if (query.containsField(EntityUtils.NAME)) { String name = (String) query.get(EntityUtils.NAME); query.put(EntityUtils.NAME,/*w ww .j a v a2 s .c o m*/ new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i")); } DBCursor cursor = null; if (page.getSort() != null) { cursor = getCollection(entityName).find(query).sort(createSort(page)).skip(page.getOffset()) .limit(page.getPageSize()); } else { cursor = getCollection(entityName).find(query).skip(page.getOffset()).limit(page.getPageSize()); } List<DBObject> result = cursor.toArray(); return result; }
From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java
License:Apache License
/** * @param type/*from w ww .j av a 2s . com*/ * @param object * @return save result */ protected DBObject save(String type, DBObject object) { if (!object.containsField(EntityUtils.ID)) { object.put(EntityUtils.ID, EntityUtils.newId()); } if (!object.containsField(EntityUtils.CREATED_ON)) { object.put(EntityUtils.CREATED_ON, new Date()); } if (!object.containsField(EntityUtils.MODIFIED_ON)) { object.put(EntityUtils.MODIFIED_ON, new Date()); } if (!object.containsField(EntityUtils.ATTR_USER)) { DBObject currentUser = getLoggedUser(); if (currentUser != null) { object.put(EntityUtils.ATTR_USER, EntityUtils.createBaseUser(currentUser)); } } WriteResult result = getCollection(type).save(object); if (result.getError() != null) { return ErrorUtils.error(result.getError()); } DBObject success = ErrorUtils.success(HttpStatus.CREATED); success.put(EntityUtils.ID, object.get(EntityUtils.ID)); return success; }
From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java
License:Apache License
@SuppressWarnings({ "unchecked" }) @Override//from www . j a v a2s .com public DBObject update(String type, String id, DBObject object) { DBObject currentUser = getLoggedUser(); if (object.containsField("$set") && object.get("$set") != null) { if (Map.class.isInstance(object.get("$set"))) { Map<String, Object> setCommand = (Map<String, Object>) object.get("$set"); setCommand.put(EntityUtils.MODIFIED_ON, new Date()); setCommand.put(EntityUtils.ATTR_USER, EntityUtils.createBaseUser(currentUser)); } else { DBObject setCommand = (DBObject) object.get("$set"); setCommand.put(EntityUtils.MODIFIED_ON, new Date()); setCommand.put(EntityUtils.ATTR_USER, EntityUtils.createBaseUser(currentUser)); } } else { object.put(EntityUtils.MODIFIED_ON, new Date()); object.put(EntityUtils.ATTR_USER, EntityUtils.createBaseUser(currentUser)); } WriteResult result = getCollection(type).update(new BasicDBObject("_id", id), object); if (result.getError() != null) { return ErrorUtils.error(result.getError()); } return ErrorUtils.success(); }
From source file:com.mobileman.kuravis.core.services.fraud_report.impl.FraudReportServiceImpl.java
License:Apache License
/** * {@inheritDoc}//from w ww .j av a 2 s. co m */ @Override public DBObject reportEntity(String entityName, String entityId, DBObject data) { if (entityName == null || entityName.trim().length() == 0) { return ErrorUtils.error("entityName is empty", ErrorCodes.INCORRECT_PARAMETER); } Subject currentUser = SecurityUtils.getSubject(); if (currentUser == null || !currentUser.isAuthenticated()) { return ErrorUtils.error("Not authenticated: currentUser=" + currentUser, ErrorCodes.USER_NOT_AUTHENTICATED); } DBObject user = (DBObject) currentUser.getPrincipal(); if (user == null) { return ErrorUtils.error("Not authenticated: currentUser=" + currentUser, ErrorCodes.UNAUTHORIZED); } if (!data.containsField("fraudReportCategory")) { throw ErrorUtils.exception("Missing fraudReportCategory", ErrorCodes.INCORRECT_PARAMETER); } DBObject fraudReport = getCollection().findOne(new BasicDBObject(FraudReport.ATTR_ENTITY_NAME, entityName) .append(FraudReport.ATTR_ENTITY_ID, entityId)); if (fraudReport == null) { fraudReport = EntityUtils.createDBObjectId(EntityUtils.newId()); fraudReport.put(EntityUtils.CREATED_ON, new Date()); fraudReport.put(EntityUtils.MODIFIED_ON, fraudReport.get(EntityUtils.CREATED_ON)); fraudReport.put(FraudReport.ATTR_ENTITY_NAME, entityName); fraudReport.put(FraudReport.ATTR_ENTITY_ID, entityId); fraudReport.put("text", data.get("text")); fraudReport.put("sourceId", data.get("sourceId")); fraudReport.put(EntityUtils.ATTR_USER, data.get(EntityUtils.ATTR_USER)); getCollection().save(fraudReport); } DBObject fraudReportItem = EntityUtils.createDBObjectId(EntityUtils.newId()); fraudReportItem.put(EntityUtils.ATTR_USER, EntityUtils.createBaseUser(user)); fraudReportItem.put("fraudReportCategory", data.get("fraudReportCategory")); fraudReportItem.put("fraudReportId", fraudReport.get(EntityUtils.ID)); fraudReportItem.put(FraudReport.ATTR_ENTITY_NAME, entityName); fraudReportItem.put(FraudReport.ATTR_ENTITY_ID, entityId); fraudReportItem.put(EntityUtils.CREATED_ON, new Date()); fraudReportItem.put(EntityUtils.MODIFIED_ON, fraudReportItem.get(EntityUtils.CREATED_ON)); fraudReportItem.put("note", data == null ? null : data.get("note")); save(EntityUtils.FRAUD_REPORT_ITEM, fraudReportItem); return ErrorUtils.success(); }
From source file:com.mobileman.kuravis.core.services.treatment_review.impl.TreatmentReviewServiceImpl.java
License:Apache License
/** * {@inheritDoc}/*from w w w. j av a 2s .co m*/ * @see com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl#findAllByQuery(java.lang.String, com.mongodb.DBObject, org.springframework.data.domain.Pageable) */ @Override public List<DBObject> findAllByQuery(String entityName, DBObject query, Pageable page) { List<Order> orders = new ArrayList<>(); if (page.getSort() != null) { Iterator<Order> orderIter = page.getSort().iterator(); while (orderIter.hasNext()) { Order order = orderIter.next(); if (order.getProperty().equals("rank")) { order = new Order(order.getDirection(), "rating"); } else if (order.getProperty().equals("vote")) { order = new Order(order.getDirection(), "votesCount"); } else if (order.getProperty().equals("last_update")) { order = new Order(order.getDirection(), "modifiedOn"); } orders.add(order); } } final PageRequest newPage; if (orders.isEmpty()) { newPage = new PageRequest(page.getPageNumber(), page.getPageSize()); } else { newPage = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(orders)); } if (query.containsField("treatment." + EntityUtils.NAME)) { String name = (String) query.get("treatment." + EntityUtils.NAME); query.put("treatment." + EntityUtils.NAME, new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i")); } if (query.containsField("disease." + EntityUtils.NAME)) { String name = (String) query.get("disease." + EntityUtils.NAME); query.put("disease." + EntityUtils.NAME, new BasicDBObject("$regex", "^" + name.toLowerCase() + "$").append("$options", "i")); } Set<String> usersId = new HashSet<String>(); List<DBObject> result = super.findAllByQuery(getEntityName(), query, newPage); for (DBObject review : result) { String userId = EntityUtils.getEntityId(review.get("author")); usersId.add(userId); } if (!usersId.isEmpty()) { Map<String, DBObject> settingsMap = userService.findUsersData(usersId, "settings", User.ATTR_GENDER, User.ATTR_YEAR_OF_BIRTH); for (DBObject review : result) { DBObject author = (DBObject) review.get("author"); String userId = EntityUtils.getEntityId(author); if (settingsMap.containsKey(userId)) { author.put("settings", settingsMap.get(userId).get("settings")); author.put(User.ATTR_GENDER, settingsMap.get(userId).get(User.ATTR_GENDER)); author.put(User.ATTR_YEAR_OF_BIRTH, settingsMap.get(userId).get(User.ATTR_YEAR_OF_BIRTH)); } } } return result; }
From source file:com.mobileman.kuravis.core.services.treatment_review.impl.TreatmentReviewServiceImpl.java
License:Apache License
/** * {@inheritDoc}// ww w . ja v a2 s . co m * @see com.mobileman.kuravis.core.services.treatment_review.TreatmentReviewService#createTreatmentReviewForSubscription(com.mongodb.DBObject) */ @SuppressWarnings("unchecked") @Override public DBObject createTreatmentReviewForSubscription(DBObject data) { if (data == null) { throw ErrorUtils.exception("data are nil", ErrorCodes.INCORRECT_PARAMETER); } if (!data.containsField("treatmentReview")) { throw ErrorUtils.exception("treatmentReview is missing", ErrorCodes.INCORRECT_PARAMETER); } if (!data.containsField("email")) { throw ErrorUtils.exception("email is missing", ErrorCodes.INCORRECT_PARAMETER); } return createTempTreatmentReviewForSubscriber((Map<String, Object>) data.get("treatmentReview"), (String) data.get("email")); }
From source file:com.mobileman.kuravis.core.services.treatment_review.impl.TreatmentReviewServiceImpl.java
License:Apache License
@SuppressWarnings({ "unchecked" }) @Override/* w w w.j a va 2 s. c o m*/ public DBObject createTreatmentReview(DBObject treatmentReview) { if (treatmentReview == null) { return ErrorUtils.error("treatmentReview si nil", ErrorCodes.INCORRECT_PARAMETER); } Subject userSubject = SecurityUtils.getSubject(); if (userSubject == null || !userSubject.isAuthenticated()) { return ErrorUtils.error("Not authenticated: currentUser=" + userSubject, ErrorCodes.USER_NOT_AUTHENTICATED); } DBObject loggedUser = (DBObject) userSubject.getPrincipal(); DBObject disease = diseaseService .createOrFindByName((Map<String, Object>) treatmentReview.get(TreatmentReview.DISEASE)); DBObject treatment = treatmentService .createOrFindByName((Map<String, Object>) treatmentReview.get(TreatmentReview.TREATMENT)); if (this.reviewAlreadyExistsForUser((String) disease.get(EntityUtils.ID), (String) treatment.get(EntityUtils.ID))) { throw ErrorUtils.exception(ErrorCodes.REVIEW_ALREADY_EXISTS); } if (RoleUtils.isNonverifiedUser(loggedUser)) { return createTempTreatmentReview(treatmentReview, loggedUser); } final CRUDAction action = CRUDAction.CREATE; List<DBObject> trSideEffects = processSideEffectsOnCreateOrUpdate(treatmentReview); Number rating = processRatingOnCreateOrUpdate(treatmentReview); processUserOnCreateOrUpdate(treatmentReview, loggedUser); EntityUtils.setBasePropertiesOnCreate(treatmentReview); treatmentReview.put(TreatmentReview.DISEASE, disease); treatmentReview.put(TreatmentReview.TREATMENT, treatment); if (rating != null) { treatmentReview.put(TreatmentReview.RATING, rating); } DBObject reviewAuthor = null; if (!treatmentReview.containsField(TreatmentReview.AUTHOR)) { treatmentReview.put(TreatmentReview.AUTHOR, EntityUtils.createBaseUser(loggedUser)); } else { reviewAuthor = userService .findById(EntityUtils.getEntityId(treatmentReview.get(TreatmentReview.AUTHOR))); treatmentReview.put(TreatmentReview.AUTHOR, EntityUtils.createBaseUser(reviewAuthor)); } WriteResult wresult = getCollection().save(treatmentReview); if (ErrorUtils.isError(wresult)) { return ErrorUtils.error(wresult); } processTreatmentReviewSummary(treatmentReview, null, action, reviewAuthor != null ? reviewAuthor : loggedUser, disease, treatment, trSideEffects); getCollection(Disease.ENTITY_NAME).update(new BasicDBObject(EntityUtils.ID, disease.get(EntityUtils.ID)), new BasicDBObject("$inc", new BasicDBObject(Disease.TREATMENT_REVIEWS_COUNT, 1))); getCollection(EntityUtils.USER).update(new BasicDBObject(EntityUtils.ID, loggedUser.get(EntityUtils.ID)), new BasicDBObject("$inc", new BasicDBObject("invitationCount", 1))); try { Message<DBObject> twitterUpdate = new GenericMessage<DBObject>(treatmentReview); reviewCreatedInChannel.send(twitterUpdate); } catch (Exception e) { } this.eventService.createReviewEvent(treatmentReview); DBObject result = ErrorUtils.success(); result.put(EntityUtils.ID, treatmentReview.get(EntityUtils.ID)); return result; }
From source file:com.mobileman.kuravis.core.services.treatment_review.impl.TreatmentReviewServiceImpl.java
License:Apache License
/** * @param treatmentReview//w w w. ja v a2 s .c om * @param user */ private void processUserOnCreateOrUpdate(DBObject treatmentReview, DBObject user) { DBObject newUserData = new BasicDBObject(); for (String userProperty : new String[] { User.ATTR_YEAR_OF_BIRTH, User.ATTR_GENDER }) { if (treatmentReview.containsField(userProperty)) { newUserData.put(userProperty, treatmentReview.get(userProperty)); } } if (newUserData.toMap().size() > 0) { this.userService.updateUser((String) user.get(EntityUtils.ID), newUserData); } treatmentReview.removeField(User.ATTR_YEAR_OF_BIRTH); treatmentReview.removeField(User.ATTR_GENDER); }