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.Requirement;
import com.validation.manager.core.db.Step;
import com.validation.manager.core.db.StepPK;
import com.validation.manager.core.db.TestCase;
import com.validation.manager.core.db.VmException;
import com.validation.manager.core.db.controller.StepJpaController;
import com.validation.manager.core.db.controller.VmExceptionJpaController;
import com.validation.manager.core.db.controller.exceptions.NonexistentEntityException;
import java.util.ArrayList;

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

    public StepServer(TestCase tc, int stepSequence, String text) {
        super(new StepPK(tc.getTestCasePK().getId(), 
                tc.getTestCasePK().getTestId()), stepSequence, text.getBytes());
        setTestCase(tc);
        if (getTestCase() == null) {
            throw new RuntimeException("Provided TestCase that doesn't exist in the database yet!");
        }
    }

    @Override
    public int write2DB() throws NonexistentEntityException, Exception {
        StepJpaController controller = new StepJpaController(DataBaseManager.getEntityManagerFactory());
        if (getStepPK().getId() > 0) {
            Step temp = controller.findStep(getStepPK());
            temp.setNotes(getNotes());
            temp.setRequirementList(getRequirementList());
            temp.setStepSequence(getStepSequence());
            temp.setTestCase(getTestCase());
            temp.setText(getText());
            temp.setVmExceptionList(getVmExceptionList());
            controller.edit(temp);
        } else {
            Step temp = new Step(getStepPK(), getStepSequence(), getText());
            temp.setNotes(getNotes());
            temp.setRequirementList(getRequirementList());
            temp.setTestCase(getTestCase());
            temp.setVmExceptionList(getVmExceptionList());
            controller.create(temp);
            setStepPK(temp.getStepPK());
            setRequirementList(temp.getRequirementList());
            setVmExceptionList(temp.getVmExceptionList());
        }
        return getStepPK().getId();
    }

    public static void deleteStep(Step s) throws NonexistentEntityException, Exception {
        for (VmException e : s.getVmExceptionList()) {
            new VmExceptionJpaController(DataBaseManager.getEntityManagerFactory()).destroy(e.getVmExceptionPK());
        }
        s.getVmExceptionList().clear();
        new StepJpaController(DataBaseManager.getEntityManagerFactory()).edit(s);
        new StepJpaController(DataBaseManager.getEntityManagerFactory()).destroy(s.getStepPK());
    }

    public Step getEntity() {
        return new StepJpaController(DataBaseManager.getEntityManagerFactory())
                .findStep(getStepPK());
    }
}

@@ -152,60 +152,8 @@
 r;%0D%0A
-import com.validation.manager.core.db.Requirement;%0D%0A
 impo
@@ -193,16 +193,16 @@
 .Step;%0D%0A
+
 import c
@@ -578,37 +578,8 @@
 on;%0D
-%0Aimport java.util.ArrayList;%0D
 %0A%0D%0A/
@@ -1089,24 +1089,24 @@
 se yet!%22);%0D%0A
-
         %7D%0D%0A 
@@ -1106,24 +1106,434 @@
    %7D%0D%0A    %7D%0D
+%0A    %0D%0A    public StepServer(Step step)%7B%0D%0A        super(step.getStepPK());%0D%0A        setExpectedResult(step.getExpectedResult());%0D%0A        setNotes(step.getNotes());%0D%0A        setRequirementList(step.getRequirementList());%0D%0A        setStepSequence(step.getStepSequence());%0D%0A        setTestCase(step.getTestCase());%0D%0A        setText(step.getText());%0D%0A        setVmExceptionList(step.getVmExceptionList());%0D%0A    %7D%0D
 %0A%0D%0A    @Over

Create constructor for creating from Step object.