Commit efe99619 authored by kolo1984's avatar kolo1984
Browse files

ejercicio9

parent fa2c6659
......@@ -35,22 +35,7 @@
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Bean Validation Implementation Provides portable constraints such
as @Email Hibernate Validator is shipped in JBoss EAP -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the JPA API, we use provided scope as the API is included in
JBoss EAP -->
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Dependency on the EJB module so we can use it's services if needed -->
<dependency>
......@@ -119,6 +104,12 @@
<version>18.0.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
<build>
......
package ejercicio.practico.tse.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import ejercicio.practico.entitidades.Recurso;
import ejercicio.practico.negocio.IServicioRecursoRemoto;
import ejercicio.practico.entitidades.TipoRecurso;
import ejercicio.practico.negocio.IServicioRecursoLocal;
@WebServlet("/AltaRecurso")
public class AltaRecursoControlador extends HttpServlet {
@Named
@ViewScoped
public class AltaRecursoControlador implements Serializable {
private static final long serialVersionUID = 415524636378502522L;
private static final long serialVersionUID = 2242277694589261370L;
@EJB
private IServicioRecursoRemoto servicioRecursoRemoto;
private IServicioRecursoLocal servicioRecursoLocal;
private String codigo;
private String cantidad;
private String preciounitario;
private String nombre;
private List<TipoRecurso> tiporecursos;
public AltaRecursoControlador() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
@PostConstruct
private void init() {
this.tiporecursos = this.servicioRecursoLocal.listarTipoRecursos();
}
public void altaRecurso() {
FacesMessage msg;
PrintWriter out = response.getWriter();
if (this.codigo == null || /* this.nombre.equals("") || */ this.cantidad == null
|| this.preciounitario == null) {
msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", "Falta agregar informacion.");
} else {
try {
try {
BigDecimal precioU = BigDecimal.valueOf(Long.parseLong(preciounitario));
int cant = Integer.parseInt(cantidad);
String codigo = request.getParameter("codigo");
BigDecimal precioUnitario = BigDecimal.valueOf(Long.parseLong(request.getParameter("precioUnitario")));
int cantidad = Integer.parseInt(request.getParameter("cantidad"));
String nombreTipoRecurso = request.getParameter("nombreTipoRecurso");
boolean isOk = this.servicioRecursoRemoto.altaRecurso(codigo, precioUnitario, cantidad,
nombreTipoRecurso);
if (servicioRecursoLocal.altaRecurso(codigo, precioU, cant, nombre)) {
msg = new FacesMessage("Recurso", "El recurso fue dado de alta");
} else {
msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", "No se pudo dar de alta al recurso.");
}
if(isOk) {
Recurso recurso = this.servicioRecursoRemoto.obtenerRecurso(codigo);
mostrarOk(out, recurso);
} else {
mostrarNotOk(out);
} catch (Exception e) {
msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", "No se pudo dar de alta al recurso.");
}
} catch (Exception e) {
}
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public String getCodigo() {
return codigo;
}
public void setCodigo(String codigo) {
this.codigo = codigo;
}
private void mostrarOk(PrintWriter out, Recurso recurso) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">");
out.println("<title>Alta recurso</title>");
out.println("</head>");
out.println("<body>");
out.println("<div class=\"mx-auto col-6\" style=\"background-color: rgb(217,234,211)\">");
out.println("<br><h3 class=\"text-center\">Recurso</h3><br>");
out.println("<br><h4 class=\"text-left\">Recurso: "+ recurso.getCodigo()+"</h4><br>");
out.println("<table class=\"table\">\r\n" +
" <thead>\r\n" +
" <tr>\r\n" +
" <th scope=\"col\">Tipo</th>\r\n" +
" <th scope=\"col\">URL</th>\r\n" +
" </tr>\r\n" +
" </thead>\r\n" +
" <tbody>\r\n" +
" <tr>\r\n" +
" <td>"+recurso.getCantidad()+"</td>\r\n" +
" <td>"+recurso.getPrecioUnitario()+"</td>\r\n" +
" </tr>\r\n" +
" </tbody>\r\n" +
"</table>");
out.println("<br><a class=\"btn btn-secondary\" href=\"index.jsp\">Volver</a>");
out.println("</div>");
out.println("<script src=\"https://code.jquery.com/jquery-3.3.1.slim.min.js\" integrity=\"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js\" integrity=\"sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"></script>");
out.println("</body>");
out.println("</html>");
public String getCantidad() {
return cantidad;
}
public void setCantidad(String cantidad) {
this.cantidad = cantidad;
}
private void mostrarNotOk(PrintWriter out) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">");
out.println("<title>Alta rechazada</title>");
out.println("</head>");
out.println("<body>");
out.println("<div class=\"mx-auto col-6\" style=\"background-color: rgb(217,234,211)\">");
out.println("<br><h3 class=\"text-center\">Alta rechazada</h3><br>");
out.println("<br><a class=\"btn btn-secondary\" href=\"index.jsp\">Volver</a>");
out.println("</div>");
out.println("<script src=\"https://code.jquery.com/jquery-3.3.1.slim.min.js\" integrity=\"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js\" integrity=\"sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"></script>");
out.println("</body>");
out.println("</html>");
public String getPreciounitario() {
return preciounitario;
}
public void setPreciounitario(String preciounitario) {
this.preciounitario = preciounitario;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public List<TipoRecurso> getTiporecursos() {
return tiporecursos;
}
public void setTiporecursos(List<TipoRecurso> tiporecursos) {
this.tiporecursos = tiporecursos;
}
}
......@@ -2,118 +2,87 @@ package ejercicio.practico.tse.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ejercicio.practico.entitidades.Recurso;
import ejercicio.practico.entitidades.TipoRecurso;
import ejercicio.practico.negocio.IServicioRecursoLocal;
import ejercicio.practico.negocio.IServicioRecursoRemoto;
@WebServlet("/ListarTipoRecursos")
public class ListarTipoRecursosControlador extends HttpServlet {
@Named
@ViewScoped
public class ListarTipoRecursosControlador implements Serializable {
private static final long serialVersionUID = 415524636378502522L;
@EJB
private IServicioRecursoRemoto servicioRecursoRemoto;
private IServicioRecursoLocal servicioRecursoLocal;
private List<TipoRecurso> tipoRecursos;
private TipoRecurso tipoRecursoMos;
private List<Recurso> recursos;
private String nombre;
@PostConstruct
private void init() {
this.tipoRecursos = servicioRecursoLocal.listarTipoRecursos();
this.nombre = this.tipoRecursos.get(0).getNombre();
this.recursos = servicioRecursoLocal.listarRecursosPorTipo(nombre);
}
public void onTipoRecursoChange() {
if (this.nombre != null && !this.nombre.equals("")) {
this.recursos = servicioRecursoLocal.listarRecursosPorTipo(nombre);
this.tipoRecursoMos = servicioRecursoLocal.obtenerTipoRecursos(nombre);
}
}
public ListarTipoRecursosControlador() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
public List<TipoRecurso> getTipoRecursos() {
return tipoRecursos;
}
PrintWriter out = response.getWriter();
public void setTipoRecursos(List<TipoRecurso> tipoRecursos) {
this.tipoRecursos = tipoRecursos;
}
try {
List<TipoRecurso> tipoRecursos = this.servicioRecursoRemoto.listarTipoRecursos();
public TipoRecurso getTipoRecursoMos() {
return tipoRecursoMos;
}
if(tipoRecursos != null && !tipoRecursos.isEmpty()) {
mostrarListado(out, tipoRecursos);
} else {
mostrarNotOk(out);
}
} catch (Exception e) {
public void setTipoRecursoMos(TipoRecurso tipoRecurso) {
this.tipoRecursoMos = tipoRecurso;
}
}
public List<Recurso> getRecursos() {
return recursos;
}
public void setRecursos(List<Recurso> recursos) {
this.recursos = recursos;
}
private void mostrarListado(PrintWriter out, List<TipoRecurso> tipoRecursos) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">");
out.println("<title>Listado de tipos de recursos</title>");
out.println("</head>");
out.println("<body>");
out.println("<div class=\"mx-auto col-6\" style=\"background-color: rgb(217,234,211)\">");
out.println("<br><h3 class=\"text-center\">Listado de tipos de recursos</h3><br>");
out.println("<table class=\"table\">\r\n" +
" <thead>\r\n" +
" <tr>\r\n" +
" <th scope=\"col\">Nombre</th>\r\n" +
" <th scope=\"col\">Descripcion</th>\r\n" +
" <th scope=\"col\">Precio referencia</th>\r\n" +
" </tr>\r\n" +
" </thead>\r\n" +
" <tbody>\r\n");
tipoRecursos.stream().forEach(tr -> {
out.println(" <tr>\r\n" +
" <td>"+tr.getNombre()+"</td>\r\n" +
" <td>"+tr.getDescripcion()+"</td>\r\n" +
" <td>"+tr.getPrecioReferencia()+"</td>\r\n"+
" </tr>\r\n"); });
out.println(" </tbody>\r\n" +
"</table>");
out.println("<br><a class=\"btn btn-secondary\" href=\"index.jsp\">Volver</a>");
out.println("</div>");
out.println("<script src=\"https://code.jquery.com/jquery-3.3.1.slim.min.js\" integrity=\"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js\" integrity=\"sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"></script>");
out.println("</body>");
out.println("</html>");
public String getNombre() {
return nombre;
}
private void mostrarNotOk(PrintWriter out) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">");
out.println("<title>Listado de tipo de recursos</title>");
out.println("</head>");
out.println("<body>");
out.println("<div class=\"mx-auto col-6\" style=\"background-color: rgb(217,234,211)\">");
out.println("<br><h3 class=\"text-center\">No se encontraron tipo de recursos.</h3><br>");
out.println("<br><a class=\"btn btn-secondary\" href=\"index.jsp\">Volver</a>");
out.println("</div>");
out.println("<script src=\"https://code.jquery.com/jquery-3.3.1.slim.min.js\" integrity=\"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js\" integrity=\"sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1\" crossorigin=\"anonymous\"></script>\r\n" +
" <script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"></script>");
out.println("</body>");
out.println("</html>");
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Alta recurso</title>
</head>
<body>
<div class="mx-auto col-4" style="background-color: rgb(217,234,211)">
<br><h4 class="text-center">Alta recurso</h4><br>
<form action="AltaRecurso" method="post">
<p class="h5">Codigo:</p>
<input class="form-control" type="text" name="codigo" value="">
<p class="h5">Precio unitario:</p>
<input class="form-control" type="text" name="precioUnitario" value="">
<p class="h5">Cantidad:</p>
<input class="form-control" type="text" name="cantidad" value="">
<br>
<p class="h5">Nombre tipo de recurso:</p>
<input class="form-control" type="text" name="nombreTipoRecurso" value="">
<br>
<div class="nav justify-content">
<input class="btn btn-secondary " type="submit" value="Aceptar" style="">
</div>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core"
template="/WEB-INF/templates/layout.xhtml">
<ui:define name="content">
<div class="container" align="left">
<h:form>
<h3>Alta de recurso</h3>
<p:fieldset id="fields">
<p:growl id="growl" showDetail="true" />
<div class="row">
<div class="col">
<div class="col">
<h:outputLabel value="Codigo:" />
</div>
</div>
<div class="col">
<div class="form-group">
<h:inputText value="#{altaRecursoControlador.codigo}" var="codigo" styleClass="form-control" />
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="col">
<h:outputLabel value="Cantidad:" />
</div>
</div>
<div class="col">
<div class="form-group">
<h:inputText value="#{altaRecursoControlador.cantidad}" var="cantidad" styleClass="form-control" />
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="col">
<h:outputLabel value="Precio unitario:" />
</div>
</div>
<div class="col">
<div class="form-group">
<h:inputText value="#{altaRecursoControlador.preciounitario}" var="preciounitario" styleClass="form-control" />
</div>
</div>
</div>
<p:separator />
<div class="row">
<div class="col">
<div class="col">
<div class="form-group">
<h:outputLabel value="Tipo de recurso: " />
</div>
</div>
</div>
<div class="col">
<div class="form-group">
<p:selectOneMenu id="nombre" value="#{altaRecursoControlador.nombre}">
<f:selectItem itemLabel="Seleccione un tipo de recurso" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{altaRecursoControlador.tiporecursos}" var="nomTipRec" itemLabel="#{nomTipRec.nombre}" itemValue="#{nomTipRec.nombre}" />
</p:selectOneMenu>
</div>
</div>
</div>
<p:separator />
<div class="row">
<div class="col" align="right">
<p:commandButton value="Alta recurso" action="#{altaRecursoControlador.altaRecurso}" update="fields" styleClass="btn btn-primary" oncomplete="clear()" />
</div>
</div>
</p:fieldset>
</h:form>
</div>
<script>
function clear() {
document.getElementById("test").value = "";
}
</script>
</ui:define>
</ui:composition>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
<title>Listar recursos por nombre del tipo de recurso</title>
</head>
<body>
<div class="mx-auto col-4" style="background-color: rgb(217, 234, 211)">
<form method="get" action="ListarRecursosPorNombreDelTipo">
<div class="form-group">
<br>
<p class="h5">Listar recursos por nombre del tipo de recurso</p>
<input class="form-control" name=nombreTipoRecurso
placeholder="Ingrese nombre tipo de recurso">
</div>
<div class="nav justify-content">
<button class="btn btn-secondary" type="submit"
class="btn btn-secondary btn-sm">Buscar</button>