0

問題リストがあります。各問題にはサブ カテゴリとメイン カテゴリがあり、各サブ カテゴリにはメイン カテゴリもあります。上記のような問題を列挙したいと思います。

--メインカテゴリ1 (1はid)
----サブカテゴリ1
------問題1
------問題2
----サブカテゴリ2
------問題3

--メインカテゴリ2
----サブカテゴリ3
------問題4
------問題5
----サブカテゴリ4
------問題6

等々。

html 部分はここにあります。

    <ui:repeat  var="mainCatvar" value="#{dprEdit.mainCategories}">
    <h:outputText value="#{mainCatvar.mainCatName}" styleClass="mainTitle" /><br></br>  
            <ui:repeat var="subCategory" value="#{mainCatvar.subCategories}">
            <h:outputText value="#{subCategory.subCatName}" styleClass="title" />
                <h:selectManyCheckbox value="#{dprEdit.selectedProblems}"  >
                <f:selectItems value="#{subCategory.problems}" var="problem" 
                itemValue="#{problem.problemName}" itemLabel="#{problem.problemName}" />                                    
                </h:selectManyCheckbox>
            </ui:repeat>
    </ui:repeat>
<p:commandButton action="#{dprCreate.save}" value="Save" update="form1" ajax="false" style="width:150px" />

これが私のバックエンド コードです。

    public class MainCatDAO extends BaseDAO {   
        private final static String SELECT_ALL_MAIN_CATEGORIES ="SELECT * FROM TESTAE.MAINCATPF";
        public MainCatDAO(Connection connection) {      
            super(connection);
        }
        private List<MainCat> getTable(String query, Object... values) throws ClassNotFoundException, SQLException {
            List<MainCat> table = new ArrayList<MainCat>();
            List<Map<String, Object>> data = this.getData(query, values);
            for (int i = 0; i < data.size(); i++) {
                ArrayList<SubCat> subList = new ArrayList<SubCat>();    
                // subList'i tekrardan tanımlama yani clear etmeme sebebim; maincat'a subList'i set etmeme rağmen,
                //sonrasında subList'i clear edince maincat'ın subList'i de sıfırlanıyor. 
                //Bu yüzden subList'i her seferinde tekrar tanımlıyorum.
                Map<String, Object> row = new HashMap<String, Object>();
                MainCat maincat = new MainCat();
                row = data.get(i);
                maincat.setMainCatId(((BigDecimal) row.get("MAINCATID")).intValue());
                maincat.setMainCatName(((String) row.get("MAINCATNAM")).trim());

                String sorgu = "SELECT * FROM TESTAE.SUBCATPF WHERE MAINCATID='"+((BigDecimal) row.get("MAINCATID")).intValue()+"'";            

                List<Map<String, Object>> dataSub = this.getData(sorgu);
                for (int a = 0; a < dataSub.size(); a++) {
                    ArrayList<Problem> problemList = new ArrayList<Problem>();
                    SubCat subcat = new SubCat();
                    row = dataSub.get(a);
                    if (row.get("SUBCATNAME") == null)
                        subcat.setSubCatName("");   
                    else
                        subcat.setSubCatName((((String) row.get("SUBCATNAME")).trim()));
                    subList.add(subcat);

                    String sorgu2 = "SELECT * FROM TESTAE.PROBLEMPF WHERE SUBCATID='"+((BigDecimal) row.get("SUBCATID")).intValue()+"'";
                    List<Map<String, Object>> dataProb = this.getData(sorgu2);
                    for (int p = 0; p < dataProb.size(); p++) {
                        Problem problem = new Problem();
                        row = dataProb.get(p);
                        Integer problemId = ((BigDecimal) row.get("PROBLEMID")).intValue();

                        if (row.get("PRBLMNAME") == null)
                            problem.setProblemName("");
                        else
                            problem.setProblemName((((String) row.get("PRBLMNAME")).trim()));
                        problemList.add(problem);
                    }
                    subcat.setProblems(problemList);
                }
                maincat.setSubCategories(subList);
                table.add(maincat);
            }
            return table;
        }

        public List<MainCat> getmainCategories () throws ClassNotFoundException, SQLException {

            return getTable(SELECT_ALL_MAIN_CATEGORIES);
    }}


Here is my save function;

            @ManagedBean(name = "dprEdit")
            @ViewScoped
            public class DprEdit implements Serializable {
            private List<String> selectedProblems = new ArrayList<String>();
    private List<MainCat> mainCategories;
    public DprEdit() throws ClassNotFoundException, SQLException {
    mainCategories = new MainCatDAO(ConnectionManager.getConnection()).getmainCategories();}

            public String save() throws ClassNotFoundException, SQLException, ParseException {
                    DprDAO dprDAO = new DprDAO(ConnectionManager.getConnection());


                    for (int i = 0; i < selectedProblems.size(); i++) {
                        dpr.setFormId(dpr.getFormId());
                        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                        Date today = new Date();
                        formDate = formatter.parse(formatter.format(today));
                    dpr.setFormDate(formDate);
                    dpr.setFilledBy(username);
                    dpr.setProblemName(selectedProblems.get(i));
                    String problemName = selectedProblems.get(i);
                    ProblemDAO problemDAO = new ProblemDAO(ConnectionManager.getConnection());
                    Problem prob = problemDAO.getProblemId(problemName);
                    dpr.setProblemId(prob.getProblemId());
                        dprDAO.insertDetail(dpr);
                    }
            public List<String> getSelectedProblems() {
                    return selectedProblems;
                }

            public void setSelectedProblems(List<String> selectedProblems) {
                    this.selectedProblems = selectedProblems;
                }
    public Integer getMaincatid() {
            return maincatid;
        }

        public void setMaincatid(Integer maincatid) {
            this.maincatid = maincatid;
        }}

私の問題モデルの属性;

private Integer problemId;
private String problemName;
private String subCategory;
private String mainCategory;

私のサブカテゴリ モデル属性;

private Integer subCatId;
private String subCatName;
private List<Problem> problems;

私のメイン カテゴリ モデルの属性。

private Integer mainCatId;
private String mainCatName;
private List<SubCat> subCategories;

したがって、問題は、リストでいくつかの問題をクリックしてから保存ボタンをクリックすると、selectedProblem リストがクリックした問題でいっぱいになることを期待していることです。ただし、selectedProblems リストは空です。

何かアドバイスはありますか?

皆さん、ありがとうございました。

4

1 に答える 1