List of usage examples for com.mongodb BasicDBObject append
@Override public BasicDBObject append(final String key, final Object val)
From source file:com.andiandy.m101j.week3.hw2_3.BlogPostDAO.java
License:Apache License
public void addPostComment(final String name, final String email, final String body, final String permalink) { // XXX HW 3.3, Work Here // Hints:/* w w w . j a va 2 s . co m*/ // - email is optional and may come in NULL. Check for that. // - best solution uses an update command to the database and a suitable // operator to append the comment on to any existing list of comments DBObject query = QueryBuilder.start("permalink").is(permalink).get(); DBObject post = postsCollection.find(query).next(); BasicDBObject comment = new BasicDBObject(); comment.append("author", name); if (email != null) { comment.append("email", email); } comment.append("body", body); post.put("comments", comment); postsCollection.update(query, new BasicDBObject("$push", new BasicDBObject("comments", comment))); }
From source file:com.andiandy.m101j.week3.hw2_3.SessionDAO.java
License:Apache License
public String startSession(String username) { String sessionID = generateSessionID(); // build the BSON object BasicDBObject session = new BasicDBObject("username", username); session.append("_id", sessionID); sessionsCollection.insert(session);/* ww w . j a v a 2 s. c om*/ return session.getString("_id"); }
From source file:com.andiandy.m101j.week3.hw2_3.UserDAO.java
License:Apache License
public boolean addUser(String username, String password, String email) { String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt())); BasicDBObject user = new BasicDBObject(); user.append("_id", username).append("password", passwordHash); if (email != null && !email.equals("")) { // the provided email address user.append("email", email); }//from www . ja va 2 s .com try { usersCollection.insert(user); return true; } catch (MongoException.DuplicateKey e) { System.out.println("Username already in use: " + username); return false; } }
From source file:com.andiandy.m101j.week4.course.BlogPostDAO.java
License:Apache License
public String addPost(String title, String body, List<String> tags, String username) { System.out.println("inserting blog entry " + title + " " + body); String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _ permalink = permalink.replaceAll("\\W", ""); // get rid of non alphanumeric permalink = permalink.toLowerCase(); BasicDBObject post = new BasicDBObject("title", title); post.append("author", username); post.append("body", body); post.append("permalink", permalink); post.append("tags", tags); post.append("comments", new java.util.ArrayList<String>()); post.append("date", new java.util.Date()); try {/*from www .j av a 2 s. c o m*/ postsCollection.insert(post); System.out.println("Inserting blog post with permalink " + permalink); } catch (Exception e) { System.out.println("Error inserting post"); return null; } return permalink; }
From source file:com.andiandy.m101j.week4.course.BlogPostDAO.java
License:Apache License
public void addPostComment(final String name, final String email, final String body, final String permalink) { BasicDBObject comment = new BasicDBObject("author", name).append("body", body); if (email != null && !email.equals("")) { comment.append("email", email); }/*ww w.j av a 2 s. c o m*/ @SuppressWarnings("unused") WriteResult result = postsCollection.update(new BasicDBObject("permalink", permalink), new BasicDBObject("$push", new BasicDBObject("comments", comment)), false, false); }
From source file:com.andiandy.m101j.week4.course.SessionDAO.java
License:Apache License
public String startSession(String username) { String sessionID = getSessionID(); // build the BSON object BasicDBObject session = new BasicDBObject("username", username); session.append("_id", sessionID); sessionsCollection.insert(session);// w w w .j a v a 2 s .c om return session.getString("_id"); }
From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java
License:Open Source License
public Resource getRootFolder(String username) { DBCollection files = mongo.getDataBase().getCollection("files"); DBObject filter = new BasicDBObject(); filter.put("user", username); filter.put("root", true); DBObject root = files.findOne(filter); if (root == null) { BasicDBObject newRoot = new BasicDBObject(); newRoot.append("type", Resource.ResourceType.FOLDER.toString()); newRoot.append("name", username); newRoot.append("root", true); newRoot.append("user", username); newRoot.append("creationDate", new Date()); newRoot.append("modificationDate", new Date()); files.insert(newRoot);/* w w w . ja v a 2 s .c o m*/ root = newRoot; } Resource res = buildResource(root); return res; }
From source file:com.app.mongoDao.MongoBookDao.java
@Override public String deleteBook(String book) { String status = ""; try {/*from w ww. j a va2s . c o m*/ DBCollection data = DatabaseConfig.configure(); BasicDBObject query = new BasicDBObject(); query.append("bookname", book); data.remove(query); status = "Book deleted successfully"; } catch (Exception e) { status = "Book not deleted successfully"; e.printStackTrace(); } return status; }
From source file:com.arquivolivre.mongocom.management.CollectionManager.java
License:Apache License
private void indexFields(Object document) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { String collectionName = reflectCollectionName(document); Field[] fields = getFieldsByAnnotation(document, Index.class); Map<String, List<String>> compoundIndexes = new TreeMap<>(); BasicDBObject compoundIndexesOpt = new BasicDBObject("background", true); DBCollection collection = db.getCollection(collectionName); for (Field field : fields) { Annotation annotation = field.getAnnotation(Index.class); BasicDBObject options = new BasicDBObject(); BasicDBObject indexKeys = new BasicDBObject(); String indexName = (String) annotation.annotationType().getMethod("value").invoke(annotation); String type = (String) annotation.annotationType().getMethod("type").invoke(annotation); boolean unique = (boolean) annotation.annotationType().getMethod("unique").invoke(annotation); boolean sparse = (boolean) annotation.annotationType().getMethod("sparse").invoke(annotation); boolean dropDups = (boolean) annotation.annotationType().getMethod("dropDups").invoke(annotation); boolean background = (boolean) annotation.annotationType().getMethod("background").invoke(annotation); int order = (int) annotation.annotationType().getMethod("order").invoke(annotation); if (!indexName.equals("")) { options.append("name", indexName); }/*from www.jav a 2s. co m*/ options.append("background", background); options.append("unique", unique); options.append("sparse", sparse); options.append("dropDups", dropDups); String fieldName = field.getName(); if (indexName.equals("") && type.equals("")) { indexKeys.append(fieldName, order); collection.ensureIndex(indexKeys, options); } else if (!indexName.equals("") && type.equals("")) { List<String> result = compoundIndexes.get(indexName); if (result == null) { result = new ArrayList<>(); compoundIndexes.put(indexName, result); } result.add(fieldName + "_" + order); } else if (!type.equals("")) { indexKeys.append(fieldName, type); collection.ensureIndex(indexKeys, compoundIndexesOpt); } } Set<String> keys = compoundIndexes.keySet(); for (String key : keys) { BasicDBObject keysObj = new BasicDBObject(); compoundIndexesOpt.append("name", key); for (String value : compoundIndexes.get(key)) { boolean with_ = false; if (value.startsWith("_")) { value = value.replaceFirst("_", ""); with_ = true; } String[] opt = value.split("_"); if (with_) { opt[0] = "_" + opt[0]; } keysObj.append(opt[0], Integer.parseInt(opt[1])); } collection.ensureIndex(keysObj, compoundIndexesOpt); } }
From source file:com.arquivolivre.mongocom.management.CollectionManager.java
License:Apache License
private BasicDBObject loadDocument(Object document) throws SecurityException, InstantiationException, InvocationTargetException, NoSuchMethodException { Field[] fields = document.getClass().getDeclaredFields(); BasicDBObject obj = new BasicDBObject(); for (Field field : fields) { try {// w w w .j a va 2s . co m field.setAccessible(true); String fieldName = field.getName(); Object fieldContent = field.get(document); if (fieldContent == null && !field.isAnnotationPresent(GeneratedValue.class)) { continue; } if (fieldContent instanceof List) { BasicDBList list = new BasicDBList(); boolean isInternal = field.isAnnotationPresent(Internal.class); for (Object item : (List) fieldContent) { if (isInternal) { list.add(loadDocument(item)); } else { list.add(item); } } obj.append(fieldName, list); } else if (field.getType().isEnum()) { obj.append(fieldName, fieldContent.toString()); } else if (field.isAnnotationPresent(Reference.class)) { obj.append(fieldName, new org.bson.types.ObjectId(save(fieldContent))); } else if (field.isAnnotationPresent(Internal.class)) { obj.append(fieldName, loadDocument(fieldContent)); } else if (field.isAnnotationPresent(Id.class) && !fieldContent.equals("")) { obj.append(fieldName, reflectId(field)); } else if (field.isAnnotationPresent(GeneratedValue.class)) { Object value = reflectGeneratedValue(field, fieldContent); if (value != null) { obj.append(fieldName, value); } } else if (!field.isAnnotationPresent(ObjectId.class)) { obj.append(fieldName, fieldContent); } else if (!fieldContent.equals("")) { obj.append("_id", new org.bson.types.ObjectId((String) fieldContent)); } } catch (IllegalArgumentException | IllegalAccessException ex) { LOG.log(Level.SEVERE, null, ex); } } return obj; }