/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package paqueteCMD;

/**
 *
 * @author sysmq
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import paqueteBL.FvcBL;
import paqueteBL.VM;
import paqueteBL.VMext;
import paqueteDAO.VmDAO;
import paqueteView.VmView;

public class cmdVM implements interfaceCMD{

public String procesa(String accion,HttpServletRequest request, HttpServletResponse response) throws Exception
{
    if (accion.equals("migrar")) return migrarVM(request,response);
    else if (accion.equals("start")) return startVM(request,response);
    else if (accion.equals("stop")) return stopVM(request,response);
    else if (accion.equals("verMigrar")) return verMigrarVM(request,response);
    else if (accion.equals("verClonar")) return verClonarVM(request,response);
    else if (accion.equals("nueva")) return nuevaVM(request,response);
    else if (accion.equals("clonar")) return clonarVM(request,response);
    else if (accion.equals("guardar")) return guardar(request,response);
    else return viewVM(request,response);
}


public String viewVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    VMext vm;
    vm=new FvcBL().loadCompletaVM(id);
    return VmView.viewVM(request,vm);
}

public String startVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    VMext vm;
    vm=new FvcBL().startVM(id);
    return VmView.viewVM(request,vm);
}

public String stopVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{    
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    VMext vm;
    vm=new FvcBL().stopVM(id);
    return VmView.viewVM(request,vm);
}

public String verMigrarVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    VMext vm;
    vm=new FvcBL().loadVM(id);
    return VmView.migrarVM(request,vm);
}

public String verClonarVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    VMext vm;
    vm=null;//new FvcBL().clonarVM(id);
    return VmView.clonarVM(request,vm);
}

public String nuevaVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    String nombre;
    nombre=(String)request.getParameter("nombre");
    int idHost=Integer.valueOf(request.getParameter("idHost"));
    int id=Integer.valueOf(request.getParameter("id"));
    VMext vm=new FvcBL().desplegarVM(id, idHost, nombre);
    return VmView.viewVM(request,vm);
}


public String migrarVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    int idHost=Integer.valueOf(request.getParameter("lstHosts"));
    VMext vm;
    vm=new FvcBL().migrarVM(id,idHost);
    return VmView.viewVM(request,vm);
}

public String clonarVM(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    String target=request.getParameter("target");
    VMext vm;
    id=1;
    vm=new FvcBL().clonarVM(id,target);
    return VmView.viewVM(request,vm);
}


public String guardar(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    int id;
    id=Integer.valueOf(request.getParameter("id"));
    String nombre=request.getParameter("txtNombre");
    String ip=request.getParameter("txtIP");
    String responsable=request.getParameter("txtResponsable");
    VMext vm=new VMext();
    vm.setId(id);
    vm.setIp(ip);
    vm.setNombre(nombre);
    vm.setResponsable(responsable);
    vm=new FvcBL().updateVM(vm);
    return VmView.viewVM(request,vm);
}

}