zoukankan      html  css  js  c++  java
  • 查询类别多维度视图递归树结构

    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/
  • 相关阅读:
    Redis持久化
    《Hadoop权威指南·大数据的存储与分析》阅读笔记(未读完)
    《redis设计与实现》第一版 阅读笔记(未看完)
    LSMTree -> SStable 初体验
    Goland实现Set操作
    使用Goland操作Redis详解
    使用Python操作Redis详解
    学习笔记
    docker技术入门与实战 第三版
    Shell(笔记)
  • 原文地址:https://www.cnblogs.com/maxlei/p/5954098.html
Copyright © 2011-2022 走看看