package net.sourceforge.javydreamercsw.client.ui.nodes;

import com.validation.manager.core.DataBaseManager;
import com.validation.manager.core.db.Requirement;
import com.validation.manager.core.db.Step;
import com.validation.manager.core.db.controller.StepJpaController;
import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.List;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;

/**
 *
 * @author Javier A. Ortiz Bultron <javier.ortiz.78@gmail.com>
 */
class StepChildFactory extends AbstractChildFactory {

    private Step step;

    public StepChildFactory(Step step) {
        this.step = step;
    }

    @Override
    protected boolean createKeys(List<Object> toPopulate) {
        List<Requirement> toAdd = new ArrayList<>();
        for (Requirement r : step.getRequirementList()) {
            boolean found = false;
            for (Requirement in : toAdd) {
                if (r.getUniqueId().equals(in.getUniqueId())) {
                    //They have the same Unique ID, so they are versions of the same requirement
                    if (in.compareTo(r) < 0) {
                        //The one in is older. Remove it and replace with the new one
                        toAdd.remove(in);
                        toAdd.add(r);
                        found = true;
                    } else {
                        //The one in is either the same or greater, just keep it
                    }
                }
            }
            if (!found) {
                toAdd.add(r);
            }
        }
        toPopulate.addAll(toAdd);
        return true;
    }

    @Override
    protected Node[] createNodesForKey(Object key) {
        return new Node[]{createNodeForKey(key)};
    }

    @Override
    protected Node createNodeForKey(Object key) {
        try {
            if (key instanceof Requirement) {
                Requirement req = (Requirement) key;
                return new UIRequirementNode(req, null);
            } else {
                return null;
            }
        } catch (IntrospectionException ex) {
            Exceptions.printStackTrace(ex);
            return null;
        }
    }

    @Override
    protected void updateBean() {
        StepJpaController controller
                = new StepJpaController(DataBaseManager.getEntityManagerFactory());
        step = controller.findStep(step.getStepPK());
    }
}

@@ -779,21 +779,136 @@
 rement%3E 
-toAdd
+finalList = new ArrayList%3C%3E();%0D%0A        List%3CRequirement%3E toAdd = new ArrayList%3C%3E();%0D%0A        List%3CRequirement%3E toRemove
  = new A
@@ -1051,21 +1051,25 @@
 nt in : 
-toAdd
+finalList
 ) %7B%0D%0A   
@@ -1389,19 +1389,22 @@
       to
-Add
+Remove
 .remove(
@@ -1703,36 +1703,222 @@
     
-toAdd.add(r);%0D%0A            %7D
+finalList.add(r);%0D%0A            %7D%0D%0A        %7D%0D%0A        for (Requirement add : toAdd) %7B%0D%0A            finalList.add(add);%0D%0A        %7D%0D%0A        for (Requirement remove : toRemove) %7B%0D%0A            finalList.remove(remove);
 %0D%0A  
@@ -1952,21 +1952,25 @@
 .addAll(
-toAdd
+finalList
 );%0D%0A    

Remove concurrent modification issues.