List of usage examples for com.mongodb BasicDBObjectBuilder isEmpty
public boolean isEmpty()
From source file:com.mingo.mongo.aggregation.AggregationUtils.java
License:Apache License
/** * Added condition for created field./*from w w w.j av a 2 s . c o m*/ * * @param builder {@link BasicDBObjectBuilder} * @param field field * @param parameters parameters */ public static void addBetweenCriteria(BasicDBObjectBuilder builder, String field, Map<String, String> parameters) { BasicDBObjectBuilder createdBuilder = start(); Date startRange = parameters.containsKey(START_DATE) ? convertToDate(parameters.get(START_DATE)) : null; Date endRange = parameters.containsKey(END_DATE) ? addDays(convertToDate(parameters.get(END_DATE)), 1) : null; if (startRange != null) { createdBuilder.add(GT.getMongoName(), startRange); } if (endRange != null) { createdBuilder.add(LT.getMongoName(), endRange); } if (!createdBuilder.isEmpty()) { builder.add(field, createdBuilder.get()); } }
From source file:org.mongoj.samples.service.persistence.CarPersistenceImpl.java
License:Open Source License
protected Car updateImpl(org.mongoj.samples.model.Car car) throws UpdateException, SystemException { DBCollection collection = getDB().getCollection(CarImpl.COLLECTION_NAME); if (car.isNew()) { car.setNew(false);// w w w .j a v a 2 s. c o m CarImpl carImpl = (CarImpl) car; carImpl.addMap.clear(); carImpl.appendMap.clear(); carImpl.removeMap.clear(); carImpl.setMap.clear(); WriteResult writeResult = collection.insert(getDBObject(car)); String err = writeResult.getError(); if (err != null) { throw new UpdateException(err); } } else { DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(car.getId())).get(); CarImpl carImpl = (CarImpl) car; BasicDBObjectBuilder updateBuilder = BasicDBObjectBuilder.start(); BasicDBObjectBuilder setUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder pushUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder pushAllUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder addUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder removeUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder removeAllUpdates = BasicDBObjectBuilder.start(); for (String field : carImpl.setMap.keySet()) { setUpdates = setUpdates.add(field, carImpl.setMap.get(field)); } if (!setUpdates.isEmpty()) { updateBuilder.add(SET_OPERATOR, setUpdates.get()); } for (String field : carImpl.appendMap.keySet()) { List<Object> list = (List<Object>) carImpl.appendMap.get(field); if (!list.isEmpty()) { if (list.size() == 1) { pushUpdates = pushUpdates.add(field, ((List) carImpl.appendMap.get(field)).get(0)); } else { pushAllUpdates = pushAllUpdates.add(field, carImpl.appendMap.get(field)); } } } if (!pushUpdates.isEmpty()) { updateBuilder.add(PUSH_OPERATOR, pushUpdates.get()); } if (!pushAllUpdates.isEmpty()) { updateBuilder.add(PUSH_ALL_OPERATOR, pushAllUpdates.get()); } for (String field : carImpl.addMap.keySet()) { List<Object> list = (List<Object>) carImpl.addMap.get(field); if (!list.isEmpty()) { if (list.size() == 1) { addUpdates = addUpdates.add(field, ((List) carImpl.addMap.get(field)).get(0)); } else { DBObject each = BasicDBObjectBuilder.start() .add(EACH_OPERATOR, ((List) carImpl.addMap.get(field)).toArray()).get(); addUpdates = addUpdates.add(field, each); } } } if (!addUpdates.isEmpty()) { updateBuilder.add(ADD_TO_SET_OPERATOR, addUpdates.get()); } for (String field : carImpl.removeMap.keySet()) { List<Object> list = (List<Object>) carImpl.removeMap.get(field); if (!list.isEmpty()) { if (list.size() == 1) { removeUpdates = removeUpdates.add(field, ((List) carImpl.removeMap.get(field)).get(0)); } else { removeAllUpdates = removeAllUpdates.add(field, carImpl.removeMap.get(field)); } } } if (!removeUpdates.isEmpty()) { updateBuilder.add(PULL_OPERATOR, removeUpdates.get()); } if (!removeAllUpdates.isEmpty()) { updateBuilder.add(PULL_ALL_OPERATOR, removeAllUpdates.get()); } if (!updateBuilder.isEmpty()) { DBObject update = updateBuilder.get(); _log.debug("Update query = {}", update); WriteResult writeResult = collection.update(criteria, update); String err = writeResult.getError(); if (err != null) { throw new UpdateException(err); } } } return car; }
From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java
License:Open Source License
protected User updateImpl(org.mongoj.samples.model.User user) throws UpdateException, SystemException { DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME); if (user.isNew()) { user.setNew(false);//from ww w . j a v a2s . c om UserImpl userImpl = (UserImpl) user; userImpl.addMap.clear(); userImpl.appendMap.clear(); userImpl.removeMap.clear(); userImpl.setMap.clear(); WriteResult writeResult = collection.insert(getDBObject(user)); String err = writeResult.getError(); if (err != null) { throw new UpdateException(err); } } else { DBObject criteria = new QueryBuilder().put("_id").is(new ObjectId(user.getId())).get(); UserImpl userImpl = (UserImpl) user; BasicDBObjectBuilder updateBuilder = BasicDBObjectBuilder.start(); BasicDBObjectBuilder setUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder pushUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder pushAllUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder addUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder removeUpdates = BasicDBObjectBuilder.start(); BasicDBObjectBuilder removeAllUpdates = BasicDBObjectBuilder.start(); for (String field : userImpl.setMap.keySet()) { setUpdates = setUpdates.add(field, userImpl.setMap.get(field)); } if (!setUpdates.isEmpty()) { updateBuilder.add(SET_OPERATOR, setUpdates.get()); } for (String field : userImpl.appendMap.keySet()) { List<Object> list = (List<Object>) userImpl.appendMap.get(field); if (!list.isEmpty()) { if (list.size() == 1) { pushUpdates = pushUpdates.add(field, ((List) userImpl.appendMap.get(field)).get(0)); } else { pushAllUpdates = pushAllUpdates.add(field, userImpl.appendMap.get(field)); } } } if (!pushUpdates.isEmpty()) { updateBuilder.add(PUSH_OPERATOR, pushUpdates.get()); } if (!pushAllUpdates.isEmpty()) { updateBuilder.add(PUSH_ALL_OPERATOR, pushAllUpdates.get()); } for (String field : userImpl.addMap.keySet()) { List<Object> list = (List<Object>) userImpl.addMap.get(field); if (!list.isEmpty()) { if (list.size() == 1) { addUpdates = addUpdates.add(field, ((List) userImpl.addMap.get(field)).get(0)); } else { DBObject each = BasicDBObjectBuilder.start() .add(EACH_OPERATOR, ((List) userImpl.addMap.get(field)).toArray()).get(); addUpdates = addUpdates.add(field, each); } } } if (!addUpdates.isEmpty()) { updateBuilder.add(ADD_TO_SET_OPERATOR, addUpdates.get()); } for (String field : userImpl.removeMap.keySet()) { List<Object> list = (List<Object>) userImpl.removeMap.get(field); if (!list.isEmpty()) { if (list.size() == 1) { removeUpdates = removeUpdates.add(field, ((List) userImpl.removeMap.get(field)).get(0)); } else { removeAllUpdates = removeAllUpdates.add(field, userImpl.removeMap.get(field)); } } } if (!removeUpdates.isEmpty()) { updateBuilder.add(PULL_OPERATOR, removeUpdates.get()); } if (!removeAllUpdates.isEmpty()) { updateBuilder.add(PULL_ALL_OPERATOR, removeAllUpdates.get()); } if (!updateBuilder.isEmpty()) { DBObject update = updateBuilder.get(); _log.debug("Update query = {}", update); WriteResult writeResult = collection.update(criteria, update); String err = writeResult.getError(); if (err != null) { throw new UpdateException(err); } } } return user; }