Commit a92ee916 authored by Breixo Senra's avatar Breixo Senra

Restricciones de URL /vet a users VET

parent c8ba5a32
......@@ -16,7 +16,7 @@ import es.uvigo.esei.xcs.service.OwnerService;
@Named("owner")
@RequestScoped
public class OwnerManagedBean {
/*@Inject
@Inject
private OwnerService service;
private String login;
......@@ -59,11 +59,11 @@ public class OwnerManagedBean {
}
public List<Owner> getOwners() {
return this.service.list();
return this.service.list(0, 100);
}
public String getPetNames(String login) {
return this.service.getPets(login).stream()
return this.service.getPets(0, 100).stream()
.map(Pet::getName)
.collect(joining(", "));
}
......@@ -121,5 +121,5 @@ public class OwnerManagedBean {
private String getViewId() {
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}*/
}
}
......@@ -38,8 +38,6 @@ public class PetDetailsManagedBean implements Serializable{
@Inject
private VaccinationService vaccinationService;
@Inject
private EmailService emailService;
private Pet pet;
......
......@@ -2,20 +2,30 @@ package es.uvigo.esei.xcs.jsf;
import static java.util.stream.Collectors.joining;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.FilterMeta;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.domain.entities.Vet;
import es.uvigo.esei.xcs.service.VetService;
@Named("vet")
@RequestScoped
public class VetManagedBean {
//@RequestScoped
@ViewScoped
public class VetManagedBean implements Serializable{
private static final long serialVersionUID = 1L;
@Inject
private VetService service;
......@@ -26,6 +36,28 @@ public class VetManagedBean {
private boolean editing;
private String errorMessage;
private LazyDataModel<Pet> pets;
@PostConstruct
public void init() {
pets = new LazyDataModel<Pet>() {
@Override
public List<Pet> load(int first, int pageSize, Map<String, SortMeta> sortBy, Map<String, FilterMeta> filterBy) {
return service.getPets(first, pageSize);
}
@Override
public int count(Map<String, FilterMeta> filterBy) {
return service.countPets();
}
};
}
public LazyDataModel<Pet> getPets() {
return pets;
}
public String getLogin() {
return login;
}
......@@ -62,9 +94,9 @@ public class VetManagedBean {
return this.service.list();
}
public List<Pet> getPets() {
/*public List<Pet> getPets() {
return this.service.getPets(0, 100);
}
}*/
public String getPetNames() {
return this.service.getPets(0, 100).stream()
......
......@@ -60,6 +60,20 @@
<role-name>OWNER</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>vet</web-resource-name>
<url-pattern>/vet/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>VET</role-name>
</auth-constraint>
</security-constraint>
<!--Defining security constraint for type of roles available -->
<!--Denining security role -->
......@@ -70,5 +84,9 @@
<security-role>
<role-name>OWNER</role-name>
</security-role>
<security-role>
<role-name>VET</role-name>
</security-role>
<!--Denining security role -->
</web-app>
\ No newline at end of file
w<?xml version='1.0' encoding='UTF-8' ?>
<?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"
......
......@@ -10,8 +10,10 @@
</h:head>
<h:body>
<ui:composition template="../WEB-INF/template.xhtml">
<ui:define name="jumbotron">
<h2>Detalles de #{petDetails.pet.name}</h2>
</ui:define>
<ui:define name="content">
<h2>Detalles de Mascota</h2>
<h:panelGrid columns="2">
<h:outputText value="ID:" />
......@@ -28,10 +30,22 @@
<h:outputText value="Propietario:" />
<h:outputText value="#{petDetails.pet.owner.login}" />
<h4>Identificadores</h4>
<h:dataTable value="#{petDetails.pet.identifiers}" var="id" styleClass="table table-striped table-bordered">
<h:column>
<f:facet name="header">Valor</f:facet>
#{id.value}
</h:column>
<h:column>
<f:facet name="header">Tipo</f:facet>
#{id.type}
</h:column>
</h:dataTable>
</h:panelGrid>
<h3>Vacunas</h3>
<h:dataTable value="#{petDetails.pet.vaccinations}" var="v">
<h:dataTable value="#{petDetails.pet.vaccinations}" var="v" styleClass="table table-striped table-bordered">
<h:column>
<f:facet name="header">Fecha</f:facet>
#{v.date}
......@@ -42,7 +56,6 @@
</h:column>
</h:dataTable>
<p:commandButton value="Open Dialog" />
<h3>Registrar nueva vacunación</h3>
<h:form>
<h:messages globalOnly="true" style="color:red;" />
......@@ -61,9 +74,6 @@
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</h:body>
......
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Mis Mascotas</title>
</h:head>
<h:body>
<h:form>
<ui:composition template="../WEB-INF/template.xhtml">
<ui:define name="jumbotron">
<h2>Mascotas</h2>
<p>Listado de las mascotas</p>
</ui:define>
<ui:define name="content">
<!--NUEVA TABLA-->
<p:dataTable value="#{pet.pets}" var="p" paginator="true" rows="10" lazy="true">
<p:column headerText="Nombre">#{p.name}</p:column>
......@@ -20,6 +27,8 @@
<h:messages globalOnly="true"/>
</ui:define>
</ui:composition>
</h:form>
</h:body>
</html>
......@@ -3,6 +3,7 @@
<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:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<head>
......@@ -16,27 +17,16 @@
</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">
<p:dataTable value="#{vet.pets}" var="supervisedPet" paginator="true" rows="10" lazy="true">
<p:column headerText="ID">#{supervisedPet.id}</p:column>
<p:column headerText="Nombre">#{supervisedPet.name}</p:column>
<p:column headerText="Tipo de Animal">#{supervisedPet.animal}</p:column>
<p:column headerText="Acciones">
<h:link value="Ver Detalles" outcome="petDetails">
<f:param name="id" value="#{supervisedPet.id}" />
</h:link>
</h:column>
</h:dataTable>
</p:column>
</p:dataTable>
<h:panelGroup class="alert alert-danger" role="alert" rendered="#{vet.error}">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
......
......@@ -9,10 +9,13 @@
</h:head>
<h:body>
<h2>CRUD de Vacunas</h2>
<ui:composition template="../WEB-INF/template.xhtml">
<ui:define name="jumbotron">
<h2>Gestión de Vacunas</h2>
</ui:define>
<ui:define name="content">
<!-- Tabla de vacunas -->
<h:dataTable value="#{vaccine.vaccines}" var="v" border="1">
<h:dataTable value="#{vaccine.vaccines}" var="v" border="1" styleClass="table table-striped table-bordered">
<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>
......@@ -105,6 +108,7 @@
<h:commandButton value="Guardar cambios" action="#{vaccine.editVaccine}"/>
<h:commandButton value="Cancelar" action="#{vaccine.setSelectedVaccine(null)}" immediate="true"/>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
......@@ -2,9 +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.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -21,11 +25,16 @@ import es.uvigo.esei.xcs.domain.entities.Vaccination;
* @author Miguel Reboiro Jato
*/
@Stateless
@RolesAllowed("ADMIN")
//@RolesAllowed("ADMIN")
@PermitAll
public class OwnerService {
@PersistenceContext
private EntityManager em;
@Inject
private Principal currentUser;
/**
* Returns the owner identified by {@code login}. If there is no owner with
* the specified login, {@code null} will be returned.
......@@ -131,20 +140,18 @@ public class OwnerService {
* @throws IllegalArgumentException if {@code login} is {@code null} or it
* does not identifies a valid owner.
*/
public List<Pet> getPets(String login, int page, int pageSize) {
if (page < 0) {
throw new IllegalArgumentException("The page can't be negative");
}
if (pageSize <= 0) {
throw new IllegalArgumentException("The page size can't be negative or zero");
}
public List<Pet> getPets(int first, int pageSize) {
if (first < 0) throw new IllegalArgumentException("First can't be negative");
if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive");
return em.createQuery("SELECT p FROM Owner o "
+ "JOIN o.pets p "
+ "WHERE "
+ "o.login = :login",
Pet.class)
.setFirstResult(page * pageSize)
.setFirstResult(first)
.setMaxResults(pageSize)
.setParameter("login", currentUser.getName())
.getResultList();
}
......
......@@ -17,6 +17,7 @@ import javax.persistence.PersistenceContext;
import es.uvigo.esei.xcs.domain.entities.Owner;
import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.domain.entities.Vaccination;
import es.uvigo.esei.xcs.domain.entities.Vaccine;
import es.uvigo.esei.xcs.domain.entities.Vet;
......@@ -176,8 +177,6 @@ public class PetService {
em.remove(pet);
}
public List<Vaccine> getVaccinesByPetId(Long id, int page, int pageSize){
if (page < 0) {
throw new IllegalArgumentException("The page can't be negative");
......@@ -192,7 +191,6 @@ public class PetService {
}
public void assignVetToPet(Long petId) {
requireNonNull(petId, "Pet ID can't be null");
//requireNonNull(vetLogin, "Vet login can't be null");
......
......@@ -3,6 +3,7 @@ package es.uvigo.esei.xcs.service;
import java.util.Date;
import static java.util.Objects.requireNonNull;
import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
......@@ -10,6 +11,7 @@ import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -22,6 +24,9 @@ import es.uvigo.esei.xcs.domain.entities.Vaccine;
@Stateless
@PermitAll
public class VaccinationService {
@Inject
private Principal currentUser;
@PersistenceContext
EntityManager em;
......
......@@ -27,6 +27,15 @@ public class VetService {
@PersistenceContext
EntityManager em;
public int countPets() {
Long count = em.createQuery(
"SELECT COUNT(p) FROM Vet v JOIN v.pets p WHERE v.login = :login", Long.class)
.setParameter("login", currentUser.getName())
.getSingleResult();
return count.intValue();
}
public Vet get(String login) {
return em.find(Vet.class, login);
}
......@@ -73,17 +82,14 @@ public class VetService {
}
public List<Pet> getPets(int page , int pageSize) {
public List<Pet> getPets(int first , int pageSize) {
//requireNonNull(login, "Login can't be null");
if (page < 0) {
throw new IllegalArgumentException("The page can't be negative");
}
if (pageSize <= 0) {
throw new IllegalArgumentException("The page size can't be negative or zero");
}
if (first < 0) throw new IllegalArgumentException("First can't be negative");
if (pageSize <= 0) throw new IllegalArgumentException("Page size must be positive");
return this.em.createQuery("SELECT DISTINCT p FROM Vet v JOIN v.pets p "
+ "WHERE v.login = :login", Pet.class)
.setFirstResult(page * pageSize)
.setFirstResult(first)
.setMaxResults(pageSize)
.setParameter("login", currentUser.getName())
.getResultList();
......
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