Package

Source Code of ScrumMasterServlet

import java.io.*; 
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import ApiScrumClass.PersistenceMySQL;
import ApiScrumClass.Person;
import ApiScrumClass.Product;
import ApiScrumClass.ScrumManager;
import ApiScrumClass.Theme;
import org.json.JSONObject;


public class ScrumMasterServlet extends HttpServlet {

//objects of the API
    private static ScrumManager scrum = new ScrumManager();
    private static PersistenceMySQL persistencia = new PersistenceMySQL();
    private static final long serialVersionUID = 8032611514671727168L;
    private static List<Product> products = new LinkedList<Product>();
    private static List<Person> persons = new LinkedList<Person>();
    private static List<Theme> themes = new LinkedList<Theme>();
    public static int ID;

   
    /* Válida la contraseña para un usuario dado
     * IN:Usuario y Contraseña
     * Out: Boolean; True si la pwd es correcta, false en caso contrario
     */
    public boolean checkPassword(String username, String password) {
        Person p = giveUser(username);
        if (p == null) {         
            return false;           
        }
        if (!(p.getPassword().equals(password))) {
            return false;
        }
        return true;
    }
    public boolean SaveProyect(HttpServletRequest request,HttpServletResponse response) {
        //Creamos un proyecto en la BBDD
            String name=request.getParameter("name");
            String initialEstimation=request.getParameter("initialestimation");          
            String assigned= request.getParameter("assigned");
            String planned=request.getParameter("planned");
            String done=request.getParameter("done");
            String description=request.getParameter("description");
            String User=request.getParameter("user");
            Person per = giveUser(User);
            int Idp=per.getIdPerson();
            Product p=scrum.createProduct(name);
            p.setInitialEstimation(Float.valueOf(initialEstimation));
            p.setDone(Boolean.valueOf(done));
            p.setPlanned(Boolean.valueOf(planned));
            p.setAssigned(Boolean.valueOf(assigned));
            p.setDescription(description);
            p.setName(name);
            persistencia.UpdateProduct(p);           
            try{
                ID=p.getId()
                //Asignamos el proyecto a la persona
                boolean result = persistencia.WorkPersonInProduct(ID, Idp);
                return result;
           } catch (Exception e) {
            return false;
        }
    }
    public boolean SaveTheme(HttpServletRequest request,HttpServletResponse response) {
        //Creamos un proyecto en la BBDD
            String name=request.getParameter("name");
            String description=request.getParameter("description");
            String parent=request.getParameter("parent");
            String parentProduct=request.getParameter("parentproduct");
            if (parent.equals("null")) {
            //Tema de un producto
                Theme t=scrum.createTheme(name,-1,Integer.valueOf(parentProduct));                
                t.setDescription(description);
                try{
                    persistencia.UpdateTheme(t);
                    ID=t.getId();
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }else{   
                //Tema de un tema
                Theme t=scrum.createTheme(name,Integer.valueOf(parent),Integer.valueOf(parentProduct));                                
                t.setDescription(description);
                try{
                    persistencia.UpdateTheme(t);
                    ID=t.getId();
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }          
    }
    public boolean SavePerson(HttpServletRequest request,HttpServletResponse response) {
        //Creamos una persona en la BBDD
            String name=request.getParameter("name");           
            String pwd=request.getParameter("pwd");
            String active = request.getParameter("active");
            Person p=scrum.createPerson(name);                     
            p.setName(name);
            p.setPassword(pwd);
            p.setStored(Boolean.valueOf(active));
            persistencia.UpdatePerson(p);           
            try{
                ID=p.getIdPerson();                      
                return true;
           } catch (Exception e) {
            return false;
        }
    }
    /*Elimina de la tabla product el Id pasado
     * In: Id del proyecto a eliminar
     */
    public boolean DeleteProyect(String Id) {
        //Eliminamos un producto de la BBDD
        Product prod =scrum.getProduct(Integer.valueOf(Id));
        try {
          scrum.deleteProduct(prod, persistencia);                 
          return true;
        }catch (Exception e) {
            return false;
        }
    }
    /*Elimina de la tabla person el Id pasado
     * In: Id de la persona a eliminar
     */
    public boolean DeletePerson(String Id) {
        //Eliminamos una persona de la BBDD
        Person pers =scrum.getPerson(Integer.valueOf(Id));
        try {
          scrum.deletePerson(pers, persistencia);                 
          return true;
        }catch (Exception e) {
            return false;
        }
    }
    /*Elimina de la tabla theme el Id pasado
     * In: Id del tema a eliminar
     */
    public boolean DeleteTheme(String Id) {
        //Eliminamos una persona de la BBDD
        Theme tema =scrum.getTheme(Integer.valueOf(Id));
        try {
          scrum.deleteTheme(tema, persistencia);                 
          return true;
        }catch (Exception e) {
            return false;
        }
    }
    /*
     * Asigna una persona a un proyecto
     * In: Id de persona, Id de producto
     * out:boolean del resultado
     */
    public boolean AssignProyect(HttpServletRequest request,HttpServletResponse response) {
        String idProd=request.getParameter("productid");
        String idPers=request.getParameter("personid");
        try{
           boolean res = persistencia.AssignPersonInProduct(Integer.parseInt(idProd),Integer.parseInt(idPers));
           return res;
        }catch (Exception e) {
           return false;
        }        
    }
    /*
     * Desasigna una persona a un producto
     * in: Id de persona , Id de producto
     * out: boolean del resultado
     */
        public boolean UnAssignProyect(HttpServletRequest request,HttpServletResponse response) {
        String idProd=request.getParameter("productid");
        String idPers=request.getParameter("personid");
        try{
           boolean res = persistencia.UnassignPersonInProduct(Integer.parseInt(idProd),Integer.parseInt(idPers));
           return res;
        }catch (Exception e) {
           return false;
        }       
    }
        /*
         * modifica los datos de un proyecto
         * in: datos de proyecto; id,name,estimation, ...
         * out: boolean del resultado obtenido
         */
    public boolean ModifyProyect(HttpServletRequest request,HttpServletResponse response) {
        //modificamos el producto con los datos que recibimos
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String initialEstimation=request.getParameter("initialestimation");
        String currentEstimation=request.getParameter("currentestimation");
        String assigned= request.getParameter("assigned");
        String planned=request.getParameter("planned");
        String done=request.getParameter("done");
        String description=request.getParameter("description");
        Product p=scrum.getProduct(Integer.valueOf(id));
        p.setCurrentEstimation(Float.valueOf(currentEstimation));
        p.setInitialEstimation(Float.valueOf(initialEstimation));
        p.setDone(Boolean.valueOf(done));
        p.setPlanned(Boolean.valueOf(planned));
        p.setAssigned(Boolean.valueOf(assigned));
        p.setDescription(description);
        p.setName(name);
        try{
           persistencia.UpdateProduct(p);
           return true;
        }catch (Exception e) {
           return false;
        }
     }
          /*
         * modifica los datos de un tema
         * in: nombre,descripcion e id del tema
         * out: boolean del resultado obtenido
         */
    public boolean ModifyTheme(HttpServletRequest request,HttpServletResponse response) {
        //modificamos el tema con los datos que recibimos
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String description=request.getParameter("description");
        Theme t=scrum.getTheme(Integer.valueOf(id));       
        t.setDescription(description);
        t.setName(name);
        try{
           persistencia.UpdateTheme(t);
           return true;
        }catch (Exception e) {
           return false;
        }
     }
    /*
     * modifica los datos de  una persona
     * in:id,name,password
     * out: boolean del resultado obtenido en la actualizacion
     */
    public boolean ModifyPerson(HttpServletRequest request,HttpServletResponse response) {
        //modificamos la persona con los datos que recibimos
        String id=request.getParameter("id");
        String name=request.getParameter("name");       
        String pwd = request.getParameter("pwd");
        Person p=scrum.getPerson(Integer.valueOf(id));
        p.setPassword(pwd);
        p.setName(name);
        try{
           persistencia.UpdatePerson(p);
           return true;
        }catch (Exception e) {
           return false;
        }
     }
    /*
     * Funcion de apoyo que dado un usuario devuelve sus datos
     * in:nombre de usuario
     * out: objeto persona del usuario pasado
     */
    public Person giveUser(String username) {
        Person person;
        try {
            List <Person> listPerson = scrum.getCompleteStaffList();
           
            Iterator iterProducts = listPerson.iterator();
            while (iterProducts.hasNext()) {
                person = (Person) iterProducts.next();
                if (person.getName().equals(username))
                    return person;
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }   
   
    @Override
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws IOException, ServletException
            //Ejemplo de llamada a url
           //http://localhost:8080/ScrumMasterServer/ScrumMasterServlet?user=eduardo&password=eduardo         
    {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        //Primero recogemos los parametros
        String User=request.getParameter("user");
        String Password=request.getParameter("password");
        String Product=request.getParameter("product");
        String Person=request.getParameter("person");       
        if ((Product==null) && (Person==null)) { //llamada de entrada
           try {
            JSONObject responseObj = new JSONObject();           
            Person p = giveUser(User);
            int Idp=p.getIdPerson();
            List<Product> productos = scrum.getProductListPerson(Idp);  
         // Recogemos los datos de los productos obtenidos
            List<JSONObject> productObjects = new LinkedList<JSONObject>();
            for (Product product : productos) {
                JSONObject productObj = new JSONObject();
                productObj.put("Description", product.getDescription());
                productObj.put("name", product.getName());
                productObj.put("Done", product.getDone());
                productObj.put("CurrentEstimation",product.getCurrentEstimation());
                productObj.put("InitialEstimation",product.getInitialEstimation())
                productObj.put("Assigned",product.getAssigned());
                productObj.put("Planned",product.getPlanned());
                productObj.put("id", product.getId());
                productObjects.add(productObj);
            }                             
            //Escribimos la respuesta
            responseObj.put("paso", checkPassword(User,Password));
            //responseObj.put("productos",jsonString.toString());
            responseObj.put("productos", productObjects);
            PrintWriter writer = response.getWriter();          
            writer.write( responseObj.toString());           
            writer.flush();
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e);
            }
        }else if ((Product != null) && (Product.equals("assigned"))){
          //Recuperamos las personas asignadas a este producto
            try {
                List<Person> persons ;
                JSONObject responseObj = new JSONObject();
                String id=request.getParameter("id");
                String Asiggned=request.getParameter("work");  
                if (Asiggned.equals("no")){                
                  persons = scrum.getProductPersonList(Integer.valueOf(id),false);                
                }else{
                  persons = scrum.getProductPersonList(Integer.valueOf(id),true);  
                }                                
                List<JSONObject> personsObjects = new LinkedList<JSONObject>();
                for (Person person : persons) {
                    JSONObject personObj = new JSONObject();
                    personObj.put("name", person.getName());
                    personObj.put("id", person.getIdPerson());
                    personsObjects.add(personObj);
                }          
                //Escribimos la respuesta
                responseObj.put("paso", checkPassword(User,Password));               
                responseObj.put("personas", personsObjects);
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {              
                throw new ServletException(e);
            }
        }else if ((Product != null) && (Product.equals("rescue"))){
             //Para recuperar un producto y sus themes dando su id
             try {
                JSONObject responseObj = new JSONObject();
                String id=request.getParameter("id");
                Product product= scrum.getProduct(Integer.valueOf(id));               
                responseObj.put("Description", product.getDescription());
                responseObj.put("name", product.getName());
                responseObj.put("Done", product.getDone());
                responseObj.put("CurrentEstimation",product.getCurrentEstimation());
                responseObj.put("InitialEstimation",product.getInitialEstimation())
                responseObj.put("Assigned",product.getAssigned());
                responseObj.put("Planned",product.getPlanned());
                responseObj.put("id", product.getId());    
                /*Sacamos los themes de el producto para enviarlos */                              
                List<Theme> temas = product.getThemeList();
                List<JSONObject> themeObjects = new LinkedList<JSONObject>();
                for (Theme tema : temas) {
                 JSONObject themeObj = new JSONObject();
                 themeObj.put("description", tema.getDescription());
                 themeObj.put("name", tema.getName());
                 themeObj.put("id", tema.getId());
                 themeObj.put("parent",tema.getParent());
                 themeObj.put("parentproduct",tema.getParentProduct())
                 themeObj.put("stored",tema.getStored());                   
                 themeObjects.add(themeObj);
                }
                /*Sacamos los Subtemas del producto para enviarlos */
                List<Theme> Subtemas = product.getSubThemeList();
                List<JSONObject> SubthemeObjects = new LinkedList<JSONObject>();
                for (Theme tema : Subtemas) {
                 JSONObject themeObj = new JSONObject();
                 themeObj.put("description", tema.getDescription());
                 themeObj.put("name", tema.getName());
                 themeObj.put("id", tema.getId());
                 themeObj.put("parent",tema.getParent());
                 themeObj.put("parentproduct",tema.getParentProduct())
                 themeObj.put("stored",tema.getStored());                   
                 SubthemeObjects.add(themeObj);
                }
                //Escribimos la respuesta
                responseObj.put("paso", checkPassword(User,Password));               
                responseObj.put("themes", themeObjects);
                responseObj.put("subthemes",SubthemeObjects);
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {              
                throw new ServletException(e);
            }
        }else if ((Person != null) && (Person.equals("rescue"))){
            /* Mostramos las personas
             * Si viene un 0 sacaremos a todas.
             */
             try {
                JSONObject responseObj = new JSONObject();
                String id=request.getParameter("id");
                if (id.equals("0")){
                  List<Person> persons = scrum.getCompleteStaffList();                       
                  List<JSONObject> personObjects = new LinkedList<JSONObject>();
                  for (Person person : persons) {
                    JSONObject personObj = new JSONObject();                   
                    personObj.put("name", person.getName());                    
                    personObj.put("id", person.getIdPerson());
                    personObj.put("stored", person.getStored());
                    personObjects.add(personObj);
                  }
                  responseObj.put("paso", checkPassword(User,Password));          
                  responseObj.put("personas", personObjects);                 
                  PrintWriter writer = response.getWriter();          
                  writer.write( responseObj.toString());           
                  writer.flush();
                }else{
                  Person person= scrum.getPerson(Integer.valueOf(id));                  
                  responseObj.put("name", person.getName());                   
                  responseObj.put("parent", person.getParent());                   
                  responseObj.put("id", person.getIdPerson());
                  responseObj.put("stored", person.getStored());                 
                   //Escribimos la respuesta
                  responseObj.put("paso", checkPassword(User,Password));               
                  PrintWriter writer = response.getWriter();
                  writer.write(responseObj.toString());
                  writer.flush();
                }              
            }
            catch (Exception e) {              
                throw new ServletException(e);
            }
        }
      }
       
    @Override
    public void doPost(HttpServletRequest request,HttpServletResponse response)
        throws IOException, ServletException
    {  
        //Primero recogemos el parametro para saber que tenemos que hacer
        //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
        //RETURN ID OF PRODUCT CREATED
        String Product=request.getParameter("product");
        String Person=request.getParameter("person");
        String Theme=request.getParameter("theme");
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        if (Product!=null) {
            //Recibimos datos de un proyecto
            if (Product.equals("save")) {
            //llamada para guardar un proyecto nuevo                                  
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", SaveProyect(request,response));
                responseObj.put("id", ID);
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
                }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
            }else if(Product.equals("delete")) {            
            //Borramos el proyecto recogiendo el ID  
            //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", DeleteProyect(request.getParameter("id")));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }catch (Exception e) {
                e.printStackTrace();
             throw new ServletException(e);
                }
            }else if (Product.equals("assigned")){
              //Asignamos el producto al usuario
              JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", AssignProyect(request,response));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
            }else if (Product.equals("unassigned")){
              //Desasignamos el producto al usuario
              JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", UnAssignProyect(request,response));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
            }else if (Product.equals("modify")){
            //MODIFICAMOS EL PRODUCTO CON LOS DATOS ENVIADOS
            //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", ModifyProyect(request,response));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
           }
        }else if (Person!=null) {
            //Recibimos datos de una persona
             if (Person.equals("save")) {
            //llamada para guardar una nueva persona                                  
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", SavePerson(request,response));
                responseObj.put("id", ID);
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
                }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
            }else if(Person.equals("delete")) {            
            //Borramos la persona recogiendo el ID  
            //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", DeletePerson(request.getParameter("id")));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }catch (Exception e) {
                e.printStackTrace();
             throw new ServletException(e);
                }
            }else if (Person.equals("modify")){
            //MODIFICAMOS EL PRODUCTO CON LOS DATOS ENVIADOS
            //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", ModifyPerson(request,response));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
           }  
         }else if (Theme!=null) {
            //Recibimos datos de un TEMA
             if (Theme.equals("save")) {
              JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", SaveTheme(request,response));
                responseObj.put("id", ID);
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
                }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
            }else if(Theme.equals("delete")) {            
            //Borramos el tema recogiendo el ID  
            //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", DeleteTheme(request.getParameter("id")));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }catch (Exception e) {
                e.printStackTrace();
             throw new ServletException(e);
                }
            }else if (Theme.equals("modify")){
            //MODIFICAMOS EL tema CON LOS DATOS ENVIADOS
            //RETURN BOOLEAN 'PASO' AS RESULT OF OPERATION
            JSONObject responseObj = new JSONObject();
            try {
                responseObj.put("paso", ModifyTheme(request,response));
                PrintWriter writer = response.getWriter();
                writer.write(responseObj.toString());
                writer.flush();
            }
            catch (Exception e) {
                e.printStackTrace();
            throw new ServletException(e);
                }
           }  
         }         
        }      
    }
TOP

Related Classes of ScrumMasterServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.