package com.validation.manager.core.server.core;

import com.validation.manager.core.DataBaseManager;
import com.validation.manager.core.EntityServer;
import com.validation.manager.core.db.ExecutionStep;
import com.validation.manager.core.db.TestCase;
import com.validation.manager.core.db.TestCaseExecution;
import com.validation.manager.core.db.TestPlan;
import com.validation.manager.core.db.TestProject;
import com.validation.manager.core.db.controller.ExecutionStepJpaController;
import com.validation.manager.core.db.controller.TestCaseExecutionJpaController;
import java.util.ArrayList;
import org.openide.util.Exceptions;

/**
 *
 * @author Javier A. Ortiz Bultron <javier.ortiz.78@gmail.com>
 */
public final class TestCaseExecutionServer extends TestCaseExecution
        implements EntityServer<TestCaseExecution> {

    public TestCaseExecutionServer(String name, String scope) {
        super(name, scope);
    }

    public TestCaseExecutionServer(int id) {
        TestCaseExecution tces = new TestCaseExecutionJpaController(DataBaseManager
                .getEntityManagerFactory()).findTestCaseExecution(id);
        update(TestCaseExecutionServer.this, tces);
    }

    public TestCaseExecutionServer(TestCaseExecution tce) {
        TestCaseExecution tces = new TestCaseExecutionJpaController(DataBaseManager
                .getEntityManagerFactory()).findTestCaseExecution(tce.getId());
        update(TestCaseExecutionServer.this, tces);
    }

    public TestCaseExecutionServer() {
    }

    /**
     * Add a Test Case to this execution.
     *
     * @param tc Test Case to add
     */
    public void addTestCase(TestCase tc) {
        ExecutionStepJpaController econtroller
                = new ExecutionStepJpaController(DataBaseManager
                        .getEntityManagerFactory());
        tc.getStepList().forEach((s) -> {
            ExecutionStep executionStep = new ExecutionStep(getId(),
                    s.getStepPK().getId(), s.getStepPK().getTestCaseId());
            executionStep.setStep(s);
            executionStep.setTestCaseExecution(getEntity());
            try {
                econtroller.create(executionStep);
                getExecutionStepList().add(executionStep);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
        });
    }

    @Override
    public int write2DB() throws Exception {
        TestCaseExecutionJpaController controller
                = new TestCaseExecutionJpaController(DataBaseManager
                        .getEntityManagerFactory());
        TestCaseExecution tce;
        if (getId() == null) {
            //New one
            tce = new TestCaseExecution();
            update(tce, this);
            controller.create(tce);
        } else {
            tce = controller.findTestCaseExecution(getId());
            update(tce, this);
            controller.edit(tce);
        }
        update(this, tce);
        return getId();
    }

    @Override
    public TestCaseExecution getEntity() {
        return new TestCaseExecutionJpaController(DataBaseManager
                .getEntityManagerFactory()).findTestCaseExecution(getId());
    }

    @Override
    public void update(TestCaseExecution target, TestCaseExecution source) {
        target.setConclusion(source.getConclusion());
        target.setId(source.getId());
        target.setScope(source.getScope());
        target.setName(source.getName());
        if (target.getProjects() == null) {
            target.setProjects(new ArrayList<>());
        } else {
            target.getProjects().clear();
        }
        if (target.getExecutionStepList() == null) {
            target.setExecutionStepList(new ArrayList<>());
        } else {
            target.getExecutionStepList().clear();
        }
        if (source.getExecutionStepList() != null) {
            source.getExecutionStepList().forEach((es) -> {
                target.getExecutionStepList().add(es);
            });
        }
        if (source.getProjects() != null) {
            source.getProjects().forEach((p) -> {
                target.getProjects().add(p);
            });
        }
    }

    @Override
    public void update() {
        update(this, getEntity());
    }

    /**
     * Add a complete Test Project to the execution.
     *
     * @param tp Test Project to add
     */
    public void addTestProject(TestProject tp) {
        tp.getTestPlanList().forEach((plan) -> {
            addTestPlan(plan);
        });
    }

    /**
     * Add a complete Test Plan to the execution.
     *
     * @param plan Test Plan to add
     */
    private void addTestPlan(TestPlan plan) {
        plan.getTestCaseList().forEach((tc) -> {
            addTestCase(tc);
        });
    }
}

@@ -1692,24 +1692,43 @@
 Case tc) %7B%0D%0A
+        try %7B%0D%0A    
         Exec
@@ -1755,32 +1755,36 @@
 er econtroller%0D%0A
+    
                 
@@ -1849,32 +1849,36 @@
                 
+ 
+   
 .getEntityManage
@@ -1899,16 +1899,20 @@
         
+   
+ 
 tc.getSt
@@ -1930,32 +1930,36 @@
 rEach((s) -%3E %7B%0D%0A
+    
             Exec
@@ -2032,16 +2032,20 @@
         
+ 
+   
 s.getSte
@@ -2096,32 +2096,36 @@
 );%0D%0A            
+   
+ 
 executionStep.se
@@ -2131,24 +2131,28 @@
 etStep(s);%0D%0A
+    
             
@@ -2205,39 +2205,47 @@
 );%0D%0A            
+ 
+   
 try %7B%0D%0A
+    
                 
@@ -2296,16 +2296,20 @@
         
+    
 getExecu
@@ -2356,16 +2356,20 @@
         
+ 
+   
 %7D catch 
@@ -2382,24 +2382,28 @@
 tion ex) %7B%0D%0A
+    
             
@@ -2443,32 +2443,36 @@
 );%0D%0A            
+    
 %7D%0D%0A        %7D);%0D%0A
@@ -2458,35 +2458,154 @@
      %7D%0D%0A        
+ 
-%7D);
+   %7D);%0D%0A            write2DB();%0D%0A        %7D catch (Exception ex) %7B%0D%0A            Exceptions.printStackTrace(ex);%0D%0A        %7D
 %0D%0A    %7D%0D%0A%0D%0A    @

Write changes to database.