/**
   * Get viewable subsystem list
   *
   * @param request
   * @return never null, elements are nds.schema.SubSystem
   */
  public List getSubSystems(HttpServletRequest request) {
    UserWebImpl userWeb =
        ((UserWebImpl)
            WebUtils.getSessionContextManager(request.getSession())
                .getActor(nds.util.WebKeys.USER));
    ArrayList subs = new ArrayList();
    if (userWeb.getUserId() == userWeb.GUEST_ID) {
      return subs;
    }
    List al = (List) userWeb.getProperty("subsystems"); // elements are subystem.id
    TableManager manager = TableManager.getInstance();
    if (al != null) {

      for (int i = 0; i < al.size(); i++) {
        int sid = ((Integer) al.get(i)).intValue();
        SubSystem ss = manager.getSubSystem(sid);
        if (ss != null) subs.add(ss);
      }
    } else {
      // search all tablecategoris for subsystem
      // add users subsystems param
      al = new ArrayList();
      String[] sub_list;
      try {
        String subsystems =
            (String)
                QueryEngine.getInstance()
                    .doQueryOne("SELECT subsystems from users where id=" + userWeb.getUserId());
        if (Validator.isNotNull(subsystems)) {
          sub_list = subsystems.split(",");
          for (int m = 0; m < sub_list.length; m++) {
            SubSystem usersub = manager.getSubSystem(sub_list[m].trim());

            if (usersub != null) {
              if (usersub.getId() == 10) continue;
              al.add(new Integer(usersub.getId()));
              subs.add(usersub);
            }
          }
          userWeb.setProperty("subsystems", al);
          return subs;
        }
      } catch (QueryException e) {
        logger.error("Fail to load subsystems from users", e);
      }

      for (int i = 0; i < manager.getSubSystems().size(); i++) {
        SubSystem ss = (SubSystem) manager.getSubSystems().get(i);
        if (containsViewableChildren(request, ss)) {
          al.add(new Integer(ss.getId()));
          subs.add(ss);
        }
      }
      userWeb.setProperty("subsystems", al);
    }
    return subs;
  }
  /**
   * @param request
   * @param permissionType PERMISSION_VIEWABLE, PERMISSION_NO_PERM or PERMISSION_NO_LICENSE
   * @return never null, elements are nds.schema.SubSystem
   */
  public List<SubSystem> getSubSystems(HttpServletRequest request, int permissionType)
      throws Exception {
    if (permissionType == PERMISSION_VIEWABLE) return getSubSystems(request);

    UserWebImpl userWeb =
        ((UserWebImpl)
            WebUtils.getSessionContextManager(request.getSession())
                .getActor(nds.util.WebKeys.USER));

    String subsystems =
        (String)
            QueryEngine.getInstance()
                .doQueryOne("SELECT subsystems from users where id=" + userWeb.getUserId());
    if (Validator.isNotNull(subsystems)) {
      return Collections.EMPTY_LIST;
    }
    TableManager manager = TableManager.getInstance();

    if (permissionType == PERMISSION_NO_PERM) {
      ArrayList subs = new ArrayList();
      List al = (List) userWeb.getProperty("subsystems"); // elements are subystem.id
      if (al == null) {
        getSubSystems(request);
        al = (List) userWeb.getProperty("subsystems");
      }
      //			 no perm
      List ss = manager.getSubSystems();
      for (int i = 0; i < ss.size(); i++) {
        SubSystem sa = (SubSystem) ss.get(i);
        boolean found = false;
        for (int j = 0; j < al.size(); j++) {
          if (((Integer) al.get(j)).intValue() == sa.getId()) {
            found = true;
            break;
          }
        }
        if (!found) subs.add(sa);
      }
      return subs;
    } // else{
    // no license
    if (subSystemNoLicense == null) {
      subSystemNoLicense = new ArrayList<SubSystem>();
      List al =
          QueryEngine.getInstance()
              .doQueryList(
                  "select id, name, orderno, iconurl,url from ad_subsystem s where exists(select 1 from ad_tablecategory c where c.ad_subsystem_id=s.id) order by orderno asc");
      for (int i = 0; i < al.size(); i++) {
        List als = (List) al.get(i);
        if (manager.getSubSystem(Tools.getInt(als.get(0), -1)) == null) {
          SubSystem ss = new SubSystem();
          ss.setId(Tools.getInt(als.get(0), -1));
          ss.setName((String) als.get(1));
          ss.setOrderno(Tools.getInt(als.get(2), -1));
          ss.setIconURL((String) als.get(3));
          ss.setPageURL((String) als.get(4));
          subSystemNoLicense.add(ss);
        }
      }
    }
    return subSystemNoLicense;
    // }

  }