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

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());
                }
            }
    }

 

转载于:https://www.cnblogs.com/maxlei/p/5954098.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值