List of usage examples for com.mongodb BasicDBObject BasicDBObject
public BasicDBObject(final String key, final Object value)
From source file:cfel.service.PortalImportTask.java
License:Open Source License
private void fixThumbnailImages() { DBObject query = new BasicDBObject("card_image_url", new BasicDBObject("$exists", false)); for (Iterator<DBObject> it = mPhotoCollection.find(query); it.hasNext();) { DBObject photo = it.next();// w ww.jav a 2s .c o m if (!photo.containsField("portal_image_url")) { System.err.println("No portal_image_url in " + photo); continue; } String portal_image_url = photo.get("portal_image_url").toString(); if (!portal_image_url.matches("^https?://.*")) { portal_image_url = Config.ALBUM_ROOT + portal_image_url; } try { URL url = new URL(portal_image_url); System.err.println("Missing thumbnail images for " + url); BufferedImage[] images = ImageUtil.convertImage(url); if (images != null) { DBObject updates = new BasicDBObject(); saveThumbnailImages(updates, images, url.getPath().replace(".", "_") + ".png"); mPhotoCollection.update(photo, new BasicDBObject("$set", updates)); } else { System.err.println(url + " corrupted"); } } catch (MalformedURLException e) { System.err.println("Unknown image URL" + portal_image_url); continue; } } }
From source file:cfel.service.PortalImportTask.java
License:Open Source License
private void fixActivities(Set<String> imageSet) { DBObject query = new BasicDBObject("$and", new DBObject[] { new BasicDBObject("portal_image_url", new BasicDBObject("$exists", true)), new BasicDBObject("uploader_user_id", new BasicDBObject("$exists", true)) }); for (Iterator<DBObject> it = mPhotoCollection.find(query); it.hasNext();) { DBObject photo = it.next();/*ww w.jav a 2 s . c o m*/ boolean temporary_removed = !imageSet.contains(photo.get("portal_image_url").toString()); boolean removed = Boolean.TRUE.equals(photo.get("permanently_removed")) || temporary_removed; if (temporary_removed != Boolean.TRUE.equals(photo.get("temporary_removed"))) { mPhotoCollection.update(photo, new BasicDBObject("$set", new BasicDBObject("temporary_removed", temporary_removed))); } if (removed != Boolean.TRUE.equals(photo.get("removed"))) { mPhotoCollection.update(photo, new BasicDBObject("$set", new BasicDBObject("removed", removed))); System.out.println("removed:" + removed + " " + photo); } } }
From source file:cfel.servlet.ImportCsvServlet.java
License:Open Source License
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response)//from w w w . ja va 2 s . c o m */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBCollection collection = mDS.getCollection("photo"); if (collection == null) { response.sendError(404, "No photo collection"); return; } InputStream is = null; try { is = request.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); // Ignore header while ((line = br.readLine()) != null) { String[] values = getStrings(line); if (values.length == 2) { String portal_image_url = values[0]; String class_id = values[1]; System.out.println("url=" + portal_image_url + ", season=" + class_id); DBObject query = new BasicDBObject("portal_image_url", portal_image_url); DBObject photo = collection.findOne(query); if (photo == null) { continue; } Object guessObj = photo.get("guess"); if (guessObj instanceof List<?>) { System.out.println(guessObj); List<?> guess = (List<?>) guessObj; Object lastGuess = guess.get(guess.size() - 1); if (lastGuess instanceof DBObject && class_id.equals(((DBObject) lastGuess).get("class_id"))) { continue; } } DBObject newGuess = new BasicDBObject(); newGuess.put("updated", new Date()); newGuess.put("class_id", class_id); DBObject update = new BasicDBObject("$push", new BasicDBObject("guess", newGuess)); collection.update(photo, update, true, false); } else { System.err.println("Unknown data: " + line); } } } finally { if (is != null) { is.close(); } } response.setStatus(200); }
From source file:cfel.servlet.ServiceServlet.java
License:Open Source License
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response)//from w w w .ja v a2s .c om * * Insert a new resource */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("type"); String id = request.getParameter("id"); String data = request.getParameter("data"); System.out.println("doPost: type=" + type + " id=" + id + " data=" + data); if ("file".equals(type)) { // Save a file with id doPut(request, response); return; } DBCollection collection = mDS.getCollection(type); if (collection == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unknown collection %s", type)); return; } String action = request.getParameter("action"); if (action != null) { // Manipulate database if ("update".equals(action)) { String query = request.getParameter("query"); String update = request.getParameter("update"); String upsert = request.getParameter("upsert"); String multi = request.getParameter("multi"); DBObject queryObj = null; if (id != null) { queryObj = new BasicDBObject("_id", new ObjectId(id)); } else if (query != null) { queryObj = (DBObject) JSON.parse(query); } DBObject updateObj = update != null ? (DBObject) JSON.parse(update) : null; if (queryObj == null || updateObj == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No query or update parameters"); return; } sendJSON(collection.update(queryObj, updateObj, "true".equals(upsert), "true".equals(multi)), response); } else { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unknown action %s", action)); } return; } if (data == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No data specified"); return; } // Insert a document DBObject dataObject = (DBObject) JSON.parse(data); Object dataID = dataObject.get("_id"); if (dataID != null && collection.findOne(dataID) != null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Duplicated id"); return; } collection.insert(dataObject); sendJSON(dataObject, response); }
From source file:cfel.servlet.ServiceServlet.java
License:Open Source License
/** * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse) * //from w w w . j a va2s. c om * Update a resource with id */ protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("type"); String id = request.getParameter("id"); String data = request.getParameter("data"); System.out.println("doPut: type=" + type + " id=" + id); if ("file".equals(type)) { // Save a file if (id == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No file id specified"); return; } InputStream is = null; try { mDS.saveFile(id, is = request.getInputStream(), request.getContentType()); } finally { if (is != null) { is.close(); } } return; } DBCollection collection = mDS.getCollection(type); if (collection == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unknown collection %s", type)); return; } if (data == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No data specified"); return; } // Update a document DBObject dataObject = (DBObject) JSON.parse(data); Object idObj = dataObject.get("_id"); // = id in source if (idObj == null && id != null) { idObj = new ObjectId(id); // = id in URI } if (idObj == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No document id specified"); return; } collection.update(new BasicDBObject("_id", idObj), dataObject, true, false); sendJSON(dataObject, response); }
From source file:ch.windmobile.server.mongo.MongoDataSource.java
License:Open Source License
@Override public List<StationInfo> getStationInfoList(boolean allStation) throws DataSourceException { try {/*w ww . j a v a 2 s .co m*/ DBCollection stations = database.getCollection(getStationsCollectionName()); List<String> list = new ArrayList<String>(); if (allStation == true) { list.add(Status.RED.value()); list.add(Status.ORANGE.value()); list.add(Status.GREEN.value()); } else { list.add(Status.GREEN.value()); } DBObject query = BasicDBObjectBuilder.start("prov", getProvider()) .add("status", new BasicDBObject("$in", list)).get(); DBCursor cursor = stations.find(query); List<StationInfo> stationInfoList = new ArrayList<StationInfo>(); while (cursor.hasNext()) { try { BasicDBObject stationJson = (BasicDBObject) cursor.next(); if (getStationsFilter() != null) { String stationId = stationJson.getString("_id"); if (getStationsFilter().contains(stationId)) { stationInfoList.add(createStationInfo(stationJson)); } } else { stationInfoList.add(createStationInfo(stationJson)); } } catch (Exception e) { log.warn("Station was ignored because:", e); } } return stationInfoList; } catch (Exception e) { throw exceptionHandler(e); } }
From source file:ch.windmobile.server.social.mongodb.AuthenticationServiceImpl.java
License:Open Source License
@Override public String authenticate(final String email, final Object password) throws AuthenticationServiceException { if (password == null) { throw new IllegalArgumentException("Password cannot be null"); }/*from w w w . j a v a 2s . co m*/ DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS); // Search user by email DBObject user = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email)); if (user != null) { String b64 = (String) user.get(MongoDBConstants.USER_PROP_SHA1); try { boolean ok = AuthenticationServiceUtil.validateSHA1(email, password.toString(), b64); if (ok) { return (String) user.get(MongoDBConstants.USER_PROP_ROLE); } else { throw new AuthenticationService.AuthenticationServiceException("Invalid password"); } } catch (NoSuchAlgorithmException e) { throw new AuthenticationService.AuthenticationServiceException( "Unexcepted error : " + e.getMessage()); } } throw new AuthenticationService.AuthenticationServiceException("User not found"); }
From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java
License:Open Source License
@Override public Messages findMessages(String chatRoomId, int maxCount) { if (maxCount <= 0) { throw new IllegalArgumentException("maxCount arg[1] must be greater than 0"); }//from w w w . ja va 2 s . c om final String collectionName = computeCollectionName(chatRoomId); final DBObject fields = BasicDBObjectBuilder.start("_id", 1).add(MongoDBConstants.CHAT_PROP_USER, 1) .add(MongoDBConstants.CHAT_PROP_TEXT, 1).add(MongoDBConstants.CHAT_PROP_TIME, 1) .add(MongoDBConstants.CHAT_PROP_EMAIL_HASH, 1).get(); DBCursor result = db.getCollection(collectionName).find(null, fields) .sort(new BasicDBObject("$natural", -1)).limit(maxCount); List<DBObject> all = result.toArray(); Messages messages = new Messages(); for (DBObject dbObject : all) { Message message = new Message(); message.setId(dbObject.get("_id").toString()); DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime((String) dbObject.get("time")); message.setDate(dateTime); message.setPseudo((String) dbObject.get(MongoDBConstants.CHAT_PROP_USER)); message.setText((String) dbObject.get(MongoDBConstants.CHAT_PROP_TEXT)); message.setEmailHash((String) dbObject.get(MongoDBConstants.CHAT_PROP_EMAIL_HASH)); messages.getMessages().add(message); } return messages; }
From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java
License:Open Source License
@Override public Long getLastMessageId(String chatRoomId) { try {/* w w w . j ava2 s . co m*/ Double value = (Double) db.getCollection(MongoDBConstants.COLLECTION_CHATROOMS) .findOne(new BasicDBObject("_id", chatRoomId)).get("counter"); return value.longValue(); } catch (Exception e) { return -1L; } }
From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java
License:Open Source License
@Override public boolean allowAnonymousMessages(String chatRoomId) { try {/*from ww w . ja v a 2 s .c om*/ Boolean refuseAnonymous = (Boolean) db.getCollection(MongoDBConstants.COLLECTION_CHATROOMS) .findOne(new BasicDBObject("_id", chatRoomId)).get(MongoDBConstants.CHATROOM_REFUSE_ANONYMOUS); return !refuseAnonymous; } catch (Exception e) { return true; } }