Exemplo n.º 1
0
  public Filter[] queryFilters(String user) {
    DataAccessSession das = DataAccessFactory.getInstance().getSysDas();

    List<Filter> filterList = new ArrayList<Filter>();

    PreparedStatement pstm = null;
    Connection conn = null;
    ResultSet rs = null;
    try {
      conn = DbPoolConnection.getInstance().getReadConnection();
      pstm =
          conn.prepareStatement(
              "SELECT * FROM filter WHERE (create_user = ? OR is_public = true AND create_user != ?)"
                  + " AND father_id IS NULL AND is_valid = true ORDER BY name");
      pstm.setString(1, user);
      pstm.setString(2, DataAccessFactory.sysUser);

      rs = pstm.executeQuery();
      while (rs.next()) {
        UUID id = DataAccessFactory.getInstance().createUUID(rs.getObject("id").toString());
        String createUser = rs.getString("create_user");
        Timestamp createTime = rs.getTimestamp("create_time");

        Filter filter = new FilterImpl(id, createUser, createTime, null);
        filter.setName(rs.getString("name"));
        filter.setXml(rs.getString("xml"));
        filter.setAnd(rs.getBoolean("is_and"));
        filter.setPublic(rs.getBoolean("is_public"));
        filter.setVisible(rs.getBoolean("is_visible"));

        filterList.add(filter);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DbPoolConnection.getInstance().closeResultSet(rs);
      DbPoolConnection.getInstance().closeStatment(pstm);
      DbPoolConnection.getInstance().closeConn(conn);
    }

    Map<UUID, Boolean> templateAllowMap = new HashMap<UUID, Boolean>();
    Map<UUID, List<Template>> allTemplateTypeMap = new HashMap<UUID, List<Template>>();
    Map<UUID, Template> allTemplateMap = new HashMap<UUID, Template>();
    Map<UUID, Flow> allFlowMap = new HashMap<UUID, Flow>();

    Iterator<Filter> filterItr = filterList.iterator();
    while (filterItr.hasNext()) {
      Filter filter = filterItr.next();

      Document xmlDoc = null;
      try {
        xmlDoc = XMLUtil.string2Document(filter.getXml(), "UTF-8");
      } catch (Exception e) {
        e.printStackTrace();
        System.err.println("error filter id: " + filter.getId());
      }

      if (xmlDoc == null) {
        filterItr.remove();
        continue;
      }

      Node queryNode = XMLUtil.getSingleNode(xmlDoc, "query");
      Node templateTypeNode = XMLUtil.getSingleNode(queryNode, "templateType");
      List<Node> templateNodeList = XMLUtil.getNodes(queryNode, "template");

      List<Template> templateList = new ArrayList<Template>();

      if (templateNodeList.size() == 0) {
        String templateTypeIdStr = XMLUtil.getAttribute(templateTypeNode, "id");
        UUID templateTypeId = DataAccessFactory.getInstance().createUUID(templateTypeIdStr);

        if (allTemplateTypeMap.get(templateTypeId) == null) {
          List<Template> templateTypeList = das.queryTemplates(templateTypeId);
          for (Template template : templateTypeList) {
            allTemplateMap.put(template.getId(), template);
          }
        }
        templateList.addAll(allTemplateTypeMap.get(templateTypeId));
      } else {
        for (Node templateNode : templateNodeList) {
          String templateIdStr = XMLUtil.getAttribute(templateNode, "id");
          UUID templateId = DataAccessFactory.getInstance().createUUID(templateIdStr);

          if (allTemplateMap.get(templateId) == null) {
            Template tmp = das.queryTemplate(templateId);
            allTemplateMap.put(templateId, tmp);
          }
          Template template = allTemplateMap.get(templateId);
          if (template != null) {
            templateList.add(template);
          }
        }
      }

      boolean filterAllow = false;

      for (Template template : templateList) {
        if (templateAllowMap.containsKey(template.getId())) {
          if (templateAllowMap.get(template.getId())) {
            filterAllow = true;
            break;
          }

          continue;
        }

        if (allFlowMap.get(template.getFlowId()) == null) {
          Flow tmp = das.queryFlow(template.getFlowId());
          allFlowMap.put(template.getFlowId(), tmp);
        }

        Flow flow = allFlowMap.get(template.getFlowId());

        if (flow == null) {
          templateAllowMap.put(template.getId(), false);
          continue;
        }

        Role[] roleArray = flow.queryUserNodeRoles(user, template.getId());
        if (roleArray != null && roleArray.length > 0) {
          filterAllow = true;
          templateAllowMap.put(template.getId(), true);
          break;
        }

        if (flow.isActionEveryoneRole(Action.readUUID)
            || flow.isActionEveryoneRole(Action.editUUID)) {
          filterAllow = true;
          templateAllowMap.put(template.getId(), true);
          break;
        }

        Action[] actionArray = flow.getActions();
        if (actionArray != null) {
          for (Action action : actionArray) {
            if (flow.isActionEveryoneRole(action.getId())) {
              filterAllow = true;
              templateAllowMap.put(template.getId(), true);
              break;
            }
          }
        }

        if (filterAllow) {
          break;
        }

        templateAllowMap.put(template.getId(), false);
      }

      if (!filterAllow) filterItr.remove();
    }

    return filterList.toArray(new Filter[filterList.size()]);
  }