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

import static com.validation.manager.core.DataBaseManager.getEntityManagerFactory;
import com.validation.manager.core.EntityServer;
import com.validation.manager.core.db.Requirement;
import com.validation.manager.core.db.RiskControl;
import com.validation.manager.core.db.Step;
import com.validation.manager.core.db.TestCase;
import com.validation.manager.core.db.TestCasePK;
import com.validation.manager.core.db.controller.TestCaseJpaController;
import com.validation.manager.core.db.controller.TestJpaController;
import com.validation.manager.core.db.controller.exceptions.IllegalOrphanException;
import com.validation.manager.core.db.controller.exceptions.NonexistentEntityException;
import com.validation.manager.core.db.controller.exceptions.PreexistingEntityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

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

    public TestCaseServer(TestCasePK pk) {
        super(pk);
        TestCaseJpaController controller
                = new TestCaseJpaController(getEntityManagerFactory());
        TestCase temp = controller.findTestCase(getTestCasePK());
        update((TestCaseServer) this, temp);
    }

    public TestCaseServer(TestCase tc) {
        update((TestCaseServer) this, tc);
    }

    public TestCaseServer(String name, int testCaseId, short version, Date creationDate) {
        super(name, new TestCasePK(testCaseId), version, creationDate);
        setTest(new TestJpaController(getEntityManagerFactory()).findTest(testCaseId));
    }

    @Override
    public int write2DB() throws IllegalOrphanException,
            NonexistentEntityException, Exception {
        TestCaseJpaController controller
                = new TestCaseJpaController(getEntityManagerFactory());
        if (controller.findTestCase(getTestCasePK()) != null || getTestCasePK().getId() > 0) {
            TestCase temp = controller.findTestCase(getTestCasePK());
            update(temp, this);
            controller.edit(temp);
        } else {
            TestCase temp = new TestCase(getName(), getTestCasePK(),
                    getVersion(), getCreationDate());
            update(temp, this);
            controller.create(temp);
        }
        return getTestCasePK().getId();
    }

    @Override
    public TestCase getEntity() {
        return new TestCaseJpaController(
                getEntityManagerFactory())
                .findTestCase(getTestCasePK());
    }

    @Override
    public void update(TestCase target, TestCase source) {
        target.setActive(source.getActive());
        target.setCreationDate(source.getCreationDate());
        target.setAuthorId(source.getAuthorId());
        target.setExpectedResults(source.getExpectedResults());
        target.setRiskControlList(source.getRiskControlList() == null
                ? new ArrayList<RiskControl>() : source.getRiskControlList());
        target.setStepList(source.getStepList() == null
                ? new ArrayList<Step>() : source.getStepList());
        target.setIsOpen(source.getIsOpen());
        target.setSummary(source.getSummary());
        target.setTest(source.getTest());
        target.setVersion(source.getVersion());
        target.setTestCasePK(source.getTestCasePK());
        target.setName(source.getName());
    }

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

    public Step addStep(int sequence, String text, String note, String criteria,
            List<Requirement> requirements)
            throws PreexistingEntityException, Exception {
        StepServer ss = new StepServer(getEntity(), sequence, text);
        int amount = getStepList().size();
        ss.setNotes(note);
        ss.setExpectedResult(criteria.getBytes("UTF-8"));
        if (ss.getRequirementList() == null) {
            ss.setRequirementList(new ArrayList<Requirement>());
        }
        ss.write2DB();
        List<String> processed = new ArrayList<String>();
        for (Requirement req : requirements) {
            //Make sure there are no duplicate requirements in list
            if (!processed.contains(req.getUniqueId().trim())) {
                processed.add(req.getUniqueId().trim());
                Requirement max = Collections.max(new RequirementServer(req).getVersions(), null);
                ss.getRequirementList().add(max);
                RequirementServer rs = new RequirementServer(max);
                rs.getStepList().add(ss.getEntity());
                rs.write2DB();
            }
        }
        ss.write2DB();
        update(this, getEntity());
        assert getStepList().size() > amount;
        return ss.getEntity();
    }
}

@@ -235,60 +235,8 @@
 t;%0D%0A
-import com.validation.manager.core.db.RiskControl;%0D%0A
 impo
@@ -3065,27 +3065,16 @@
 rayList%3C
-RiskControl
 %3E() : so
@@ -3191,20 +3191,16 @@
 rayList%3C
-Step
 %3E() : so
@@ -4088,27 +4088,16 @@
 rayList%3C
-Requirement
 %3E());%0D%0A 
@@ -4181,20 +4181,55 @@
 ist%3C
-String
 %3E();%0D%0A
+        if (requirements != null) %7B%0D%0A    
     
@@ -4268,24 +4268,28 @@
 rements) %7B%0D%0A
+    
             
@@ -4345,16 +4345,20 @@
 n list%0D%0A
+    
         
@@ -4431,16 +4431,20 @@
         
+    
 processe
@@ -4485,32 +4485,36 @@
                 
+    
 Requirement max 
@@ -4512,16 +4512,45 @@
 ment max
+%0D%0A                           
  = Colle
@@ -4586,16 +4586,54 @@
 ver(req)
+%0D%0A                                    
 .getVers
@@ -4664,16 +4664,20 @@
         
+  
+  
 ss.getRe
@@ -4695,32 +4695,36 @@
 st().add(max);%0D%0A
+    
                 
@@ -4791,16 +4791,20 @@
         
+    
 rs.getSt
@@ -4850,16 +4850,20 @@
         
+ 
+   
 rs.write
@@ -4862,32 +4862,51 @@
 rs.write2DB();%0D%0A
+                %7D%0D%0A
             %7D%0D%0A 

Handle null list of requirements.