hikamen
2/25/2018 - 12:50 PM

更改目录的位置和深度

@ResponseBody
  @RequestMapping(value = "/reposition/{id}", method = RequestMethod.POST)
  public ActionResult reposition(@PathVariable("id") int id, int targetId, String point,
      boolean isDirectory, int type) throws ServletException {
    Directory targetDir = this.directoryService.load(targetId);
    Directory oldParent = null;
    if (isDirectory) {
      Directory source = this.directoryService.load(id);
      if (source.getParent() > 0) {
        oldParent = this.directoryService.load(source.getParent());
      }
      Map<String, Object> param = new HashMap<String, Object>();
      if (point != null && point.equals("append")) {
        List<Directory> descendants = this.directoryService.findDescendants(id);
        for (Directory d : descendants) {
          d = replace(d, targetDir, oldParent);
          this.directoryService.update(d);
        }
        source = replace(source, targetDir, oldParent);
        param.put("parent", targetId);
        int position = this.directoryService.generatePosition(param);
        source.setParent(targetId);
        source.setPosition(position);
        source.setDepth(targetDir.getDepth() + 1);
        this.directoryService.update(source);
        return new ActionResult(1, "保存成功", source.getParent());
      } else {
        param.put("parent", targetDir.getParent());
        List<Directory> targetChildren = this.directoryService.find(param);
        int position = -1;
        for (int i = 0; i < targetChildren.size(); i++) {
          if (targetChildren.get(i).getId() == targetId) {
            position = (i + 1) * 100;
          }
          targetChildren.get(i).setPosition((i + 1) * 100);
          this.directoryService.update(targetChildren.get(i));
        }
        if (point.equals("top")) {
          source.setPosition(position - 10);
        } else {
          source.setPosition(position + 10);
        }
        Directory newParent = null;
        if (targetDir.getParent() > 0) {
          newParent = this.directoryService.load(targetDir.getParent());
        }
        List<Directory> descendants = this.directoryService.findDescendants(id);
        for (Directory d : descendants) {
          this.directoryService.update(d);
        }
        source = replace(source, newParent, oldParent);
        source.setDepth(targetDir.getDepth());
        source.setParent(targetDir.getParent());
        this.directoryService.update(source);
        return new ActionResult(1, "保存成功", source.getParent());
      }
    } else {
      if (point != null && point.equals("append")) {
        if (type == DirectoryType.DOCUMENT.ordinal()) {
          StdDoc doc = this.docService.load(id);
          doc.setDirectory(targetId);
          this.docService.update(doc);
        } else {
          StdSet stdSet = this.stdSetService.load(id);
          stdSet.setDirectory(targetId);
          this.stdSetService.update(stdSet);
        }
        return new ActionResult(1, "保存成功", targetId);
      } else {
        if (targetDir.getParent() < 1) {
          return new ActionResult(-1, "不允许拖动到这一层目录");
        } else {
          return new ActionResult(0);
        }
      }
    }
  }