protected void initObjectId(Document document) {

    ObjectId objectId = document.getObjectId(MONGO_KEY__ID);

    if (objectId != null) {
      put(MONGO_KEY__ID, objectId);
    }
  }
  public String create() {
    MongoCollection<Document> commentCollection = mongoDatabase.getCollection("posts");

    Document document = new Document();
    document.put("comments", new ArrayList<Document>());
    commentCollection.insertOne(document);

    String generatedId = document.getObjectId("_id").toHexString();

    return generatedId;
  }
Example #3
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8"); // post 可以直接设置中文字符编号
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String result = "error";
    JSONObject json = null;

    String phone = request.getParameter("phone").toString().trim();
    String pwd = request.getParameter("pwd").toString().trim();
    StudentInfo stu = new StudentInfo();
    stu.setPhone(phone);
    stu.setPwd(pwd);

    try {
      int temp = 0;
      MongoCursor<Document> cur =
          DaoImpl.GetSelectCursor(
              StudentInfo.class, CreateQueryFromBean.EqualObj(stu), 1, new BasicDBObject());
      while (cur.hasNext()) {

        Document docu = cur.next();
        School sch = new School();
        sch.set_id(docu.getObjectId("SchoolId"));
        BasicDBObject obj = new BasicDBObject();
        obj.put(StaticString.School_id, 0);
        obj.put(StaticString.School_ShowUrl, 1);
        obj.put(StaticString.School_Name, 1);
        MongoCursor<Document> cursor =
            DaoImpl.GetSelectCursor(School.class, CreateQueryFromBean.EqualObj(sch), 1, obj);
        while (cursor.hasNext()) {
          Document document = cursor.next();
          json = jsonUtil.ParaFromDocument(docu);
          json.put("SchoolImg", document.getString("ShowUrl"));
          json.put("SchoolName", document.getString("Name"));
          temp = 1;
          result = json.toString();
        }
      }
      if (temp == 0) {
        result = "LoginError";
      }
    } catch (Exception e1) {
      result = "error";
    }
    out.println(result);
    out.flush();
    out.close();
  }
  /**
   * Move a directory
   *
   * @param u
   * @param d
   * @param newContainer
   * @return
   */
  public static Directory moveDirectory(User u, Directory d, Directory newContainer) {

    if (d.UID == null) return d;

    MongoCollection<Document> collection = DataBase.getInstance().getCollection("directories");
    MongoCollection<Document> collectionFiles = DataBase.getInstance().getCollection("files.files");
    Date now = new Date();

    // Change path
    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(d.UID));
    query.put("owner", new ObjectId(u.UID));

    String oldPath = d.path;
    String newPath = newContainer.name == null ? "/" : newContainer.path + newContainer.name + "/";

    BasicDBObject update = new BasicDBObject();
    update.append("$set", new BasicDBObject().append("path", newPath).append("edit", now));

    collection.updateOne(query, update);

    // Update paths
    query = new BasicDBObject();
    query.put("owner", new ObjectId(u.UID));

    if (d.getPathList().size() > 0) {
      String pathRegex = "^/" + String.join("/", d.getPathList()) + "/" + d.name + "/";
      query.put("path", new BasicDBObject("$regex", pathRegex));
    } else {
      String pathRegex = "^/" + d.name + "/";
      query.put("path", new BasicDBObject("$regex", pathRegex));
    }

    // Update directories
    for (Document doc : collection.find(query)) {
      BasicDBObject dirQuery = new BasicDBObject();
      dirQuery.put("_id", doc.getObjectId("_id"));

      update = new BasicDBObject();
      update.append(
          "$set",
          new BasicDBObject()
              .append("path", newPath + doc.getString("path").substring(oldPath.length()))
              .append("edit", now));

      collection.updateOne(dirQuery, update);
    }

    // Update files
    for (Document doc : collectionFiles.find(query)) {
      BasicDBObject fileQuery = new BasicDBObject();
      fileQuery.put("_id", doc.getObjectId("_id"));

      update = new BasicDBObject();
      update.append(
          "$set",
          new BasicDBObject()
              .append("path", newPath + doc.getString("path").substring(oldPath.length()))
              .append("edit", now));

      collectionFiles.updateOne(fileQuery, update);
    }

    updateDirectoryDate(u, newContainer);

    // Update local version
    d.path = newPath;

    return d;
  }
  /**
   * Rename a directory
   *
   * @param u The owner
   * @param d The directory
   * @param newName The new name
   * @return The renamed directory
   */
  public static Directory renameDirectory(User u, Directory d, String newName) {

    if (d.name == null) // Root can't be renamed
    return d;

    MongoCollection<Document> collection = DataBase.getInstance().getCollection("directories");
    MongoCollection<Document> collectionFiles = DataBase.getInstance().getCollection("files.files");
    Date now = new Date();
    String newPath = d.path + newName;
    String oldPath = d.path + d.name;

    // Change name
    BasicDBObject query = new BasicDBObject();
    query.put("_id", new ObjectId(d.UID));
    query.put("owner", new ObjectId(u.UID));

    BasicDBObject update = new BasicDBObject();
    update.append("$set", new BasicDBObject().append("name", newName).append("edit", now));

    collection.updateOne(query, update);

    // Update paths
    query = new BasicDBObject();
    query.put("owner", new ObjectId(u.UID));

    if (d.getPathList().size() > 0) {
      String pathRegex = "^/" + String.join("/", d.getPathList()) + "/" + d.name + "/";
      query.put("path", new BasicDBObject("$regex", pathRegex));
    } else {
      String pathRegex = "^/" + d.name + "/";
      query.put("path", new BasicDBObject("$regex", pathRegex));
    }

    // Update directories
    for (Document doc : collection.find(query)) {
      BasicDBObject dirQuery = new BasicDBObject();
      dirQuery.put("_id", doc.getObjectId("_id"));

      update = new BasicDBObject();
      update.append(
          "$set",
          new BasicDBObject()
              .append("path", newPath + doc.getString("path").substring(oldPath.length()))
              .append("edit", now));

      collection.updateOne(dirQuery, update);
    }

    // Update files
    for (Document doc : collectionFiles.find(query)) {
      BasicDBObject fileQuery = new BasicDBObject();
      fileQuery.put("_id", doc.getObjectId("_id"));

      update = new BasicDBObject();
      update.append(
          "$set",
          new BasicDBObject()
              .append("path", newPath + doc.getString("path").substring(oldPath.length()))
              .append("edit", now));

      collectionFiles.updateOne(fileQuery, update);
    }

    // Return the modified element
    d.name = newName;
    return d;
  }