コード例 #1
1
  @Override
  public Integer createResource(Resource resource, User user) {
    // 生成structure
    String structure = "1";
    Resource parentResource = resourceMapper.selectByPrimaryKey(resource.getParentId());
    List<Resource> resources = resourceMapper.getResourceListByParentId(resource.getParentId());
    if (resources == null || resources.size() == 0) {
      structure = parentResource.getStructure() + "-1";
    } else {
      Integer parentLevel = parentResource.getStructure().split("-").length;

      for (Resource r : resources) {
        String[] structures = r.getStructure().split("-");
        if (structures.length == parentLevel + 1) {
          if (StringUtil.isNumber(structures[structures.length - 1])
              && StringUtil.compareTo(structures[structures.length - 1], structure) > 0) {
            structure = structures[structures.length - 1];
          }
        }
      }
      structure = String.valueOf(Integer.parseInt(structure) + 1);
      structure = parentResource.getStructure() + "-" + structure;
    }

    resource.setStructure(structure);

    setResourceInsert(resource, user);

    return resourceMapper.insertSelective(resource);
  }
コード例 #2
0
  @Override
  public String getResourceTree(Integer roleId) {
    Set<Integer> setResource = new HashSet<Integer>();

    if (roleId != null) {
      List<Resource> tempResourceList = resourceMapper.getResourceListByRoleId(roleId);
      if (tempResourceList != null && tempResourceList.size() > 0) {
        for (Resource r : tempResourceList) {
          setResource.add(r.getId());
        }
      }
    }

    List<Module> moduleList = moduleMapper.getModuleList();
    List<Resource> resourceList = resourceMapper.getResourceList();
    Collections.sort(moduleList, new ComparatorModule());
    Collections.sort(resourceList, new ComparatorResource());

    Set<Integer> setParent = new HashSet<Integer>();
    for (Resource r : resourceList) {
      setParent.add(r.getParentId());
    }

    StringBuilder sb = new StringBuilder();
    Map<String, Integer> mapModule = new HashMap<String, Integer>();
    sb.append("[");
    int i = 0;
    for (Module m : moduleList) {
      mapModule.put(m.getFlag(), m.getId());
      if (i != 0) {
        sb.append(",");
      }
      i++;
      sb.append("{")
          .append("\"id\":\"")
          .append("m_")
          .append(m.getId())
          .append("\"")
          .append(",\"parent\":\"")
          .append("#\"")
          .append(",\"text\":\"")
          .append(m.getName())
          .append("\"")
          .append(",\"li_attr\":{\"flag\":\"")
          .append(m.getFlag())
          .append("\"}");
      // 前两个级别默认打开
      sb.append(",\"state\":{");
      sb.append("\"opened\":true");
      sb.append("}");
      sb.append("}");
    }
    i = 0;
    for (Resource r : resourceList) {

      int level = r.getStructure().split("-").length;
      sb.append(",");
      i++;
      sb.append("{")
          .append("\"id\":\"")
          .append(r.getId())
          .append("\"")
          .append(",\"parent\":\"")
          .append(
              r.getParentId() == 0 ? ("m_" + mapModule.get(r.getModuleFlag())) : r.getParentId())
          .append("\"")
          .append(",\"text\":\"")
          .append(r.getName())
          .append("\"")
          .append(",\"li_attr\":{\"flag\":\"")
          .append(r.getModuleFlag())
          .append("\",\"sortNo\":")
          .append(r.getSortNo())
          .append("}");
      // 前两个级别默认打开
      if (level <= 2) {
        sb.append(",\"state\":{\"opened\":true");
        if (setResource.contains(r.getId())) {
          sb.append(",\"selected\":true");
        }
        sb.append("}");
      } else {

        if (setResource.contains(r.getId())) {
          sb.append(",\"state\":{\"opened\":true}");
        }
      }
      // 最后一个级别换个绿色图标
      if (!setParent.contains(r.getId())) {
        sb.append(", \"icon\": \"fa fa-briefcase icon-success\"");
      }
      sb.append("}");
    }
    sb.append("]");
    return sb.toString();
  }