Commit 7fe077dc authored by Breixo Senra's avatar Breixo Senra

Plantillas de JSF funcionando

parent 4190697b
package es.uvigo.esei.xcs.jsf;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.faces.context.FacesContext;
import java.util.Map;
import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.service.PetService;
@Named("petDetails")
@RequestScoped
public class PetDetailsManagedBean {
@Inject
private PetService service;
private Pet pet;
@PostConstruct
public void init() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext()
.getRequestParameterMap();
String id = params.get("id");
if (id != null) {
this.pet = service.get(Long.parseLong(id));
}
}
public Pet getPet() {
return pet;
}
}
......@@ -16,14 +16,14 @@ import es.uvigo.esei.xcs.service.PetService;
@Named("pet")
@RequestScoped
public class PetManagedBean {
/* @Inject
@Inject
private PetService service;
private String name;
private Date birth;
private AnimalType animal;
private Integer id;
private Long id;
private String errorMessage;
......@@ -65,19 +65,19 @@ public class PetManagedBean {
return this.id != null;
}
public Integer getId() {
public Long getId() {
return id;
}
public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}
public List<Pet> getPets() {
return this.service.list();
return this.service.list(0, 100);
}
public String edit(int petId) {
public String edit(Long petId) {
final Pet pet = this.service.get(petId);
this.id = pet.getId();
......@@ -94,7 +94,7 @@ public class PetManagedBean {
return this.getViewId();
}
public String remove(int id) {
public String remove(Long id) {
this.service.remove(id);
return redirectTo(this.getViewId());
......@@ -110,7 +110,7 @@ public class PetManagedBean {
this.service.update(pet);
} else {
this.service.create(new Pet(name, animal, birth));
//this.service.create(new Pet(name, animal, birth));
}
this.clear();
......@@ -137,5 +137,5 @@ public class PetManagedBean {
private String getViewId() {
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}*/
}
}
......@@ -61,9 +61,13 @@ public class VetManagedBean {
public List<Vet> getVets() {
return this.service.list();
}
public List<Pet> getPets() {
return this.service.getPets(0, 100);
}
public String getPetNames(String login) {
return this.service.getPets(login, 0, 100).stream()
public String getPetNames() {
return this.service.getPets(0, 100).stream()
.map(Pet::getName)
.collect(joining(", "));
}
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Detalle de Mascota</title>
</h:head>
<h:body>
<ui:composition template="../WEB-INF/template.xhtml">
<ui:define name="content">
<h2>Detalles de Mascota</h2>
<h:panelGrid columns="2">
<h:outputText value="ID:" />
<h:outputText value="#{petDetails.pet.id}" />
<h:outputText value="Nombre:" />
<h:outputText value="#{petDetails.pet.name}" />
<h:outputText value="Animal:" />
<h:outputText value="#{petDetails.pet.animal}" />
<h:outputText value="Fecha de Nacimiento:" />
<h:outputText value="#{petDetails.pet.birth}" />
</h:panelGrid>
<h3>Vacunas</h3>
<h:dataTable value="#{petDetails.pet.vaccinations}" var="v">
<h:column>
<f:facet name="header">Fecha</f:facet>
#{v.date}
</h:column>
<h:column>
<f:facet name="header">Vacuna</f:facet>
#{v.vaccine.name}
</h:column>
</h:dataTable>
</ui:define>
</ui:composition>
</h:body>
</html>
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<head>
<title>Vet - Pets Supervised</title>
</head>
<body>
<ui:composition template="../WEB-INF/template.xhtml">
<ui:define name="jumbotron">
<h2>Mascotas Supervisadas</h2>
<p>Listado de las mascotas que supervisas (nombre y tipo de animal).</p>
</ui:define>
<ui:define name="content">
<h:dataTable value="#{vet.pets}" var="supervisedPet" styleClass="table table-striped table-bordered">
<h:column>
<f:facet name="header">ID</f:facet>
#{supervisedPet.id}
</h:column>
<h:column>
<f:facet name="header">Nombre</f:facet>
#{supervisedPet.name}
</h:column>
<h:column>
<f:facet name="header">Tipo de Animal</f:facet>
#{supervisedPet.animal}
</h:column>
<!--Añadidos los links-->
<h:column>
<f:facet name="header">Acciones</f:facet>
<h:link value="Ver Detalles" outcome="petDetails.xhtml">
<f:param name="id" value="#{supervisedPet.id}" />
</h:link>
</h:column>
</h:dataTable>
<h:panelGroup class="alert alert-danger" role="alert" rendered="#{vet.error}">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
Error: #{vet.errorMessage}
</h:panelGroup>
</ui:define>
</ui:composition>
</body>
</html>
......@@ -16,21 +16,20 @@
<h:column><f:facet name="header">ID</f:facet>#{v.id}</h:column>
<h:column><f:facet name="header">Nombre</f:facet>#{v.name}</h:column>
<h:column><f:facet name="header">Tipo</f:facet>#{v.class.simpleName}</h:column>
<h:column>
<f:facet name="header">Doses</f:facet>
#{v.class.simpleName eq 'MultidoseVaccine' ? v.doses : ''}
</h:column>
<h:column rendered="#{v.class.simpleName eq 'MultidoseVaccine'}">
<f:facet name="header">Doses</f:facet>
#{v.doses}
</h:column>
<h:column rendered="#{v.class.simpleName eq 'PeriodicVaccine'}">
<f:facet name="header">Periode</f:facet>
#{v.periode}
</h:column>
<h:column>
<f:facet name="header">Periode</f:facet>
#{v.class.simpleName eq 'PeriodicVaccine' ? v.periode : ''}
</h:column>
<h:column rendered="#{v.class.simpleName eq 'PeriodicVaccine'}">
<f:facet name="header">PeriodicType</f:facet>
#{v.periodicType}
</h:column>
<h:column>
<f:facet name="header">Periodic Type</f:facet>
#{v.class.simpleName eq 'PeriodicVaccine' ? v.periodicType : ''}
</h:column>
<h:column>
<f:facet name="header">Acciones</f:facet>
......@@ -61,30 +60,40 @@
</h:selectOneMenu>
<!-- Doses solo si MULTIDOSE -->
<h:outputLabel value="Doses:" for="doses" rendered="#{vaccine.type eq 'MULTIDOSE'}"/>
<!--<h:outputLabel value="Doses:" for="doses" rendered="#{vaccine.type eq 'MULTIDOSE'}"/>
<h:inputText id="doses" value="#{vaccine.doses}"
rendered="#{vaccine.type eq 'MULTIDOSE'}"
required="#{vaccine.type eq 'MULTIDOSE'}"
validatorMessage="Las dosis deben ser mayores a 0">
<f:validateLongRange minimum="1"/>
</h:inputText>
<!-- Periode y PeriodicType solo si PERIODIC -->
<h:outputLabel value="Periode:" for="periode" rendered="#{vaccine.type eq 'PERIODIC'}"/>
<h:inputText id="periode" value="#{vaccine.periode}"
rendered="#{vaccine.type eq 'PERIODIC'}"
required="#{vaccine.type eq 'PERIODIC'}"
validatorMessage="El periodo debe ser mayor a 0">
<f:validateLongRange minimum="1"/>
</h:inputText>
<h:outputLabel value="Periodic Type:" for="periodicType" rendered="#{vaccine.type eq 'PERIODIC'}"/>
<h:selectOneMenu id="periodicType" value="#{vaccine.periodicType}"
rendered="#{vaccine.type eq 'PERIODIC'}">
<f:selectItem itemLabel="DAYS" itemValue="DAYS"/>
<f:selectItem itemLabel="MONTHS" itemValue="MONTHS"/>
<f:selectItem itemLabel="YEARS" itemValue="YEARS"/>
</h:selectOneMenu>
</h:inputText>-->
<h:outputLabel value="Doses:" for="doses"
style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"/>
<h:inputText id="doses" value="#{vaccine.doses}" required="#{vaccine.type eq 'MULTIDOSE'}"
style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}">
<f:validateLongRange minimum="1"/>
</h:inputText>
<h:outputLabel value="Periode:" for="periode"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/>
<h:inputText id="periode" value="#{vaccine.periode}" required="#{vaccine.type eq 'PERIODIC'}"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
<f:validateLongRange minimum="1"/>
</h:inputText>
<h:outputLabel value="Periodic Type:" for="periodicType"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/>
<h:selectOneMenu id="periodicType" value="#{vaccine.periodicType}"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
<f:selectItem itemLabel="DAYS" itemValue="DAYS"/>
<f:selectItem itemLabel="MONTHS" itemValue="MONTHS"/>
<f:selectItem itemLabel="YEARS" itemValue="YEARS"/>
</h:selectOneMenu>
</h:panelGrid>
<br/>
......
......@@ -98,15 +98,15 @@ public class VetResource {
}
@GET
@Path("{login}/pet")
@Path("pet")
public Response listPets(
@PathParam("login") String login,
//@PathParam("login") String login,
@QueryParam("page") @DefaultValue("0") int page,
@QueryParam("pageSize") @DefaultValue("10") int pageSize
) {
requireNonNull(login, "login can't be null");
//requireNonNull(login, "login can't be null");
return Response.ok(this.vetService.getPets(login, page, pageSize)).build();
return Response.ok(this.vetService.getPets(page, pageSize)).build();
}
......@@ -155,7 +155,7 @@ public class VetResource {
}
}
@Path("{login}/pet/{petIdentifierType}/{petIdentifierValue}/vaccination")
@Path("pet/{petIdentifierType}/{petIdentifierValue}/vaccination")
@GET
public Response listVaccinations(
@PathParam("login") String login,
......@@ -167,7 +167,7 @@ public class VetResource {
requireNonNull(login, "login can't be null");
return Response.ok(this.vetService.getVaccinationsFromOwnPet(
login,
//login,
petIdentifierType,
petIdentifierValue,
page,
......@@ -218,16 +218,16 @@ public class VetResource {
@DELETE
@Path("{login}/unassign/pet/{petId}")
public Response unassignVetFromPet(
@PathParam("login") String vetLogin,
//@PathParam("login") String vetLogin,
@PathParam("petId") Long petId
) {
requireNonNull(vetLogin, "vetLogin can't be null");
//requireNonNull(vetLogin, "vetLogin can't be null");
requireNonNull(petId, "petId can't be null");
try {
petService.unassignVetFromPet(petId, vetLogin);
petService.unassignVetFromPet(petId);
return Response.ok()
.entity("Vet " + vetLogin + " unassigned from pet " + petId)
//.entity("Vet " + vetLogin + " unassigned from pet " + petId)
.build();
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.NOT_FOUND)
......
......@@ -175,15 +175,15 @@ public class PetService {
}
public void unassignVetFromPet(Long petId, String vetLogin) {
public void unassignVetFromPet(Long petId) {
requireNonNull(petId, "Pet ID can't be null");
requireNonNull(vetLogin, "Vet login can't be null");
//requireNonNull(vetLogin, "Vet login can't be null");
Pet pet = em.find(Pet.class, petId);
if (pet == null)
throw new IllegalArgumentException("Pet not found");
Vet vet = em.find(Vet.class, vetLogin);
Vet vet = em.find(Vet.class, currentUser.getName());
if (vet == null)
throw new IllegalArgumentException("Vet not found");
......
......@@ -5,6 +5,7 @@ package es.uvigo.esei.xcs.service;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -18,6 +19,7 @@ import es.uvigo.esei.xcs.domain.entities.Vaccine;
@Stateless
@PermitAll
//@RolesAllowed("VET")
public class VaccineService {
@PersistenceContext
private EntityManager em;
......
......@@ -2,11 +2,13 @@ package es.uvigo.esei.xcs.service;
import static java.util.Objects.requireNonNull;
import java.security.Principal;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -19,6 +21,9 @@ import es.uvigo.esei.xcs.domain.entities.Vet;
//@RolesAllowed("VET")
@PermitAll
public class VetService {
@Inject
private Principal currentUser;
@PersistenceContext
EntityManager em;
......@@ -68,8 +73,8 @@ public class VetService {
}
public List<Pet> getPets(String login, int page , int pageSize) {
requireNonNull(login, "Login can't be null");
public List<Pet> getPets(int page , int pageSize) {
//requireNonNull(login, "Login can't be null");
if (page < 0) {
throw new IllegalArgumentException("The page can't be negative");
}
......@@ -80,18 +85,18 @@ public class VetService {
+ "WHERE v.login = :login", Pet.class)
.setFirstResult(page * pageSize)
.setMaxResults(pageSize)
.setParameter("login", login)
.setParameter("login", currentUser.getName())
.getResultList();
}
public List<Vaccination> getVaccinationsFromOwnPet(
String login,
//String login,
IdentifierType identifierType,
String identifierValue,
int page,
int pageSize
){
requireNonNull(login, "login can't be null");
//requireNonNull(login, "login can't be null");
requireNonNull(identifierType, "pet's identifier type can't be null");
requireNonNull(identifierValue, "pet's identifier value can't be null");
if (page < 0) {
......@@ -110,7 +115,7 @@ public class VetService {
+ "i.identifierType = :identifierType AND "
+ "i.identifierValue = :identifierValue",
Vaccination.class)
.setParameter("login", login)
.setParameter("login", currentUser.getName())
.setParameter("identifierType", identifierType)
.setParameter("identifierValue", identifierValue)
.setFirstResult(page * pageSize)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment