查询类别多维度视图递归树结构

public ResultData searchCategories(User contextUser, Category category)
            throws RemoteException, QuetzacoException, Exception {
        Connection con = null;
        PreparedStatement ps1 = null;
        ArrayList models = new ArrayList();
        int status = QuetzacoConstants.SUCCESS;
        ResultData rd = null;
        try {
            if (contextUser == null || contextUser.getObjectId() == null) {
                throw new QuetzacoException(
                        "Improper context User Information ");
            }

            if (category == null) {
                throw new QuetzacoException("No categories to Search ");
            }

            String searchQuery = null;
            ResultSet rs = null;

            searchQuery = QuetzacoReadQuery.instance().getIndexMethodQueryValue("getCategorySearchQuery",new Class[]{Category.class},new Object[]{category});
            con = DBConnectionHelper.getConnection();
            ps1 = con.prepareStatement(searchQuery);
            rs = ps1.executeQuery();
            BaseModel prtModel = null;

            //新建一个HashMap集合
            HashMap<String,List<Category>> subMap = new HashMap<String, List<Category>>();
            List<Category> subCategory = null;
            //返回的结果集循环遍历,然后put到subMap集合中去
            while (rs.next()) {
                category = new Category(rs.getString(1));
                category.setObjectType("Category");
                category.setObjectName(rs.getString(2));
                category.setSpaceId(category.getSpaceId());
                category.setCategoryStatus(rs.getInt(3));
                prtModel = new BaseModel(rs.getString(4));
                category.setParentModel(prtModel);

                if(subMap.containsKey(prtModel.getObjectId())){
                    subMap.get(prtModel.getObjectId()).add(category);
                }else{
                    subCategory = new ArrayList<Category>();
                    subCategory.add(category);
                    subMap.put(prtModel.getObjectId(),subCategory);
                }
            }
            if(subMap.size()>0) {
                addSubItems(subMap, "0");
                if(subMap.get("0")!=null)
                    models.addAll(subMap.get("0"));
            }
            rs.close();
        } catch (Exception exp) {
            exp.printStackTrace();
            status = QuetzacoConstants.FAILURE;
            throw exp;
        } finally {
            cleanup(con, ps1);
        }

        rd = new ResultData(status, models);
        return rd;
    }
    private void addSubItems(HashMap<String, List<Category>> subMap, String key) {
        if(subMap.get(key)!=null)
            for(Category category : subMap.get(key)){
                if(category!=null&&category.getObjectId()!=null&&subMap.get(category.getObjectId())!=null) {
                    category.setSubCategory(subMap.get(category.getObjectId()));
                    if (!subMap.containsKey(category.getObjectId()))
                        continue;
                    addSubItems(subMap, category.getObjectId());
                }
            }
    }
业勤于精荒于嬉 http://www.cnblogs.com/maxlei/
原文地址:https://www.cnblogs.com/maxlei/p/5954098.html