package net.sourceforge.javydreamercsw.validation.manager.web;

import com.vaadin.data.Item;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.VerticalLayout;
import com.validation.manager.core.db.Project;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.vaadin.teemu.wizards.Wizard;
import org.vaadin.teemu.wizards.WizardStep;

/**
 *
 * @author Javier A. Ortiz Bultron <javier.ortiz.78@gmail.com>
 */
public class SelectTestCasesStep implements WizardStep {

    private final Project p;
    private final Wizard w;
    private final TreeTable testTree = new TreeTable("Available Tests");
    private final List<Integer> projects = new ArrayList<>();
    private static final Logger LOG
            = Logger.getLogger(SelectTestCasesStep.class.getSimpleName());

    public SelectTestCasesStep(Wizard w, Project p) {
        this.p = p;
        this.w = w;
    }

    @Override
    public String getCaption() {
        return "Select Test Cases";
    }

    @Override
    public Component getContent() {
        VerticalLayout l = new VerticalLayout();
        //Add menu
        HorizontalLayout menu = new HorizontalLayout();
        if (p != null) {
            //Show the Test Plans for the selected project (including sub projects
            testTree.addContainerProperty("Name", TreeTableCheckBox.class, "");
            testTree.addContainerProperty("Description", String.class, "");
            testTree.setWidth("20em");
            addProjectTestPlanning(testTree, p);
        }
        testTree.setSizeFull();
        l.addComponent(menu);
        l.addComponent(testTree);
        return l;
    }

    @Override
    public boolean onAdvance() {
        //Get a list of selected test cases
        List<Integer> testCases
                = processChildren("project" + p.getId());
        testCases.forEach((i) -> {
            LOG.log(Level.INFO, "Test Case: {0}", i);
        });
        if (testCases.isEmpty()) {
            return false;
        }
        //update next step
        DetailStep next = ((DetailStep) w.getSteps().get(1));
        next.setTestCases(testCases);
        next.setProjects(projects);
        return true;
    }

    @Override
    public boolean onBack() {
        return false;
    }

    private void addProjectTestPlanning(TreeTable testTree, Project p) {
        //Add the test projects
        testTree.addItem(new Object[]{new TreeTableCheckBox(testTree,
            p.getName(), "project" + p.getId()), ""},
                "project" + p.getId());
        if (p.getParentProjectId() != null) {
            //Add as child
            testTree.setParent("project" + p.getId(),
                    "project" + p.getParentProjectId().getId());
        }
        p.getTestProjectList().stream().map((tp) -> {
            TreeTableCheckBox cb = new TreeTableCheckBox(testTree,
                    tp.getName(), "testproject" + tp.getId());
            cb.setIcon(ValidationManagerUI.TEST_SUITE_ICON);
            testTree.addItem(new Object[]{cb, ""},
                    "testproject" + tp.getId());
            return tp;
        }).map((tp) -> {
            testTree.setParent("testproject" + tp.getId(),
                    "project" + p.getId());
            return tp;
        }).map((tp) -> {
            tp.getTestPlanList().stream().map((plan) -> {
                TreeTableCheckBox pcb = new TreeTableCheckBox(testTree,
                        plan.getName(), plan.getTestPlanPK());
                pcb.setIcon(ValidationManagerUI.PLAN_ICON);
                testTree.addItem(new Object[]{pcb, ""},
                        plan.getTestPlanPK());
                return plan;
            }).map((plan) -> {
                testTree.setParent(plan.getTestPlanPK(),
                        "testproject" + tp.getId());
                return plan;
            }).forEachOrdered((plan) -> {
                plan.getTestCaseList().stream().map((tc) -> {
                    TreeTableCheckBox tccb = new TreeTableCheckBox(testTree,
                            tc.getName(), "tc" + tc.getId());
                    tccb.setIcon(ValidationManagerUI.TEST_ICON);
                    testTree.addItem(new Object[]{tccb,
                        tc.getSummary() != null
                        ? new String(tc.getSummary()) : ""},
                            "tc" + tc.getId());
                    return tc;
                }).map((tc) -> {
                    testTree.setParent("tc" + tc.getId(),
                            plan.getTestPlanPK());
                    return tc;
                }).forEachOrdered((tc) -> {
                    testTree.setChildrenAllowed("tc" + tc.getId(), false);
                });
            });
            return tp;
        }).forEachOrdered((tp) -> {
            testTree.setCollapsed("testproject" + tp.getId(), false);
        });
        p.getProjectList().forEach((sp) -> {
            addProjectTestPlanning(testTree, sp);
        });
        testTree.setCollapsed("project" + p.getId(), false);
    }

    private List<Integer> processChildren(Object parent) {
        List<Integer> testCases = new ArrayList<>();
        //Get a list of selected test cases
        testTree.getChildren(parent).stream().map((o) -> {
            if (o instanceof String) {
                String id = (String) o;
                if (id.startsWith("tc")) {
                    //Is a Test Case
                    Item item = testTree.getItem(id);
                    Object val = item.getItemProperty("Name").getValue();
                    if (val instanceof TreeTableCheckBox) {
                        TreeTableCheckBox ttcb = (TreeTableCheckBox) val;
                        if (ttcb.getValue()) {
                            //Selected
                            LOG.log(Level.FINE, "Included TC: {0}",
                                    ttcb.getObjectId());
                            testCases.add(Integer.parseInt(id.substring(2)));
                            Object pid = id;
                            //Add the related project to the list.
                            while (pid != null) {
                                if (pid instanceof String) {
                                    String s = (String) pid;
                                    if (s.startsWith("project")) {
                                        LOG.log(Level.FINE, "Processing: {0}", s);
                                        getProjects().add(Integer.parseInt(s.substring(7)));
                                        break;
                                    }
                                }
                                pid = testTree.getParent(pid);
                            }
                        }
                    }
                }
            }
            return o;
        }).filter((o) -> (testTree.hasChildren(o))).forEachOrdered((o) -> {
            testCases.addAll(processChildren(o));
        });
        return testCases;
    }

    /**
     * @return the projects
     */
    public List<Integer> getProjects() {
        return projects;
    }
}

@@ -154,32 +154,68 @@
 izontalLayout;%0D%0A
+import com.vaadin.ui.Notification;%0D%0A
 import com.vaadi
@@ -2102,12 +2102,12 @@
 vel.
+F
 IN
-FO
+E
 , %22T
@@ -2172,24 +2172,215 @@
 Empty()) %7B%0D%0A
+            Notification.show(%22Unable to proceed%22,%0D%0A                    %22Please select at least one Test Case prior to continuing.%22,%0D%0A                    Notification.Type.WARNING_MESSAGE);%0D%0A
             

Add notification when no test case selected and prevent advancing on wizard.