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