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,22 +2,32 @@ package es.uvigo.esei.xcs.jsf; ...@@ -2,22 +2,32 @@ 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;
private String login; private String login;
...@@ -25,6 +35,28 @@ public class VetManagedBean { ...@@ -25,6 +35,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:f="http://xmlns.jcp.org/jsf/core"> xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
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>
<!--NUEVA TABLA--> <ui:composition template="../WEB-INF/template.xhtml">
<p:dataTable value="#{pet.pets}" var="p" paginator="true" rows="10" lazy="true"> <ui:define name="jumbotron">
<p:column headerText="Nombre">#{p.name}</p:column> <h2>Mascotas</h2>
<p:column headerText="Nacimiento">#{p.birth}</p:column> <p>Listado de las mascotas</p>
<p:column headerText="Tipo">#{p.animal}</p:column> </ui:define>
<p:column headerText="Acción"> <ui:define name="content">
<p:commandButton value="#{pet.isAssignedToMe(p) ? 'Desasignar' : 'Asignar'}" <!--NUEVA TABLA-->
action="#{pet.toggleAssignment(p)}" update="@this"/> <p:dataTable value="#{pet.pets}" var="p" paginator="true" rows="10" lazy="true">
</p:column> <p:column headerText="Nombre">#{p.name}</p:column>
</p:dataTable> <p:column headerText="Nacimiento">#{p.birth}</p:column>
<p:column headerText="Tipo">#{p.animal}</p:column>
<p:column headerText="Acción">
<h:messages globalOnly="true"/> <p:commandButton value="#{pet.isAssignedToMe(p) ? 'Desasignar' : 'Asignar'}"
action="#{pet.toggleAssignment(p)}" update="@this"/>
</p:column>
</p:dataTable>
<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> <f:param name="id" value="#{supervisedPet.id}" />
#{supervisedPet.name} </h:link>
</h:column> </p:column>
<h:column> </p:dataTable>
<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}"> <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,102 +9,106 @@ ...@@ -9,102 +9,106 @@
</h:head> </h:head>
<h:body> <h:body>
<h2>CRUD de Vacunas</h2> <ui:composition template="../WEB-INF/template.xhtml">
<ui:define name="jumbotron">
<!-- Tabla de vacunas --> <h2>Gestión de Vacunas</h2>
<h:dataTable value="#{vaccine.vaccines}" var="v" border="1"> </ui:define>
<h:column><f:facet name="header">ID</f:facet>#{v.id}</h:column> <ui:define name="content">
<h:column><f:facet name="header">Nombre</f:facet>#{v.name}</h:column> <!-- Tabla de vacunas -->
<h:column><f:facet name="header">Tipo</f:facet>#{v.class.simpleName}</h:column> <h:dataTable value="#{vaccine.vaccines}" var="v" border="1" styleClass="table table-striped table-bordered">
<h:column> <h:column><f:facet name="header">ID</f:facet>#{v.id}</h:column>
<f:facet name="header">Doses</f:facet> <h:column><f:facet name="header">Nombre</f:facet>#{v.name}</h:column>
#{v.class.simpleName eq 'MultidoseVaccine' ? v.doses : ''} <h:column><f:facet name="header">Tipo</f:facet>#{v.class.simpleName}</h:column>
</h:column> <h:column>
<f:facet name="header">Doses</f:facet>
<h:column> #{v.class.simpleName eq 'MultidoseVaccine' ? v.doses : ''}
<f:facet name="header">Periode</f:facet> </h:column>
#{v.class.simpleName eq 'PeriodicVaccine' ? v.periode : ''}
</h:column> <h:column>
<f:facet name="header">Periode</f:facet>
<h:column> #{v.class.simpleName eq 'PeriodicVaccine' ? v.periode : ''}
<f:facet name="header">Periodic Type</f:facet> </h:column>
#{v.class.simpleName eq 'PeriodicVaccine' ? v.periodicType : ''}
</h:column> <h:column>
<f:facet name="header">Periodic Type</f:facet>
<h:column> #{v.class.simpleName eq 'PeriodicVaccine' ? v.periodicType : ''}
<f:facet name="header">Acciones</f:facet> </h:column>
<h:form>
<h:commandButton value="Eliminar" action="#{vaccine.removeVaccine(v.id)}" <h:column>
onclick="return confirm('¿Seguro que deseas eliminar esta vacuna?');"/> <f:facet name="header">Acciones</f:facet>
<h:commandButton value="Editar" action="#{vaccine.setSelectedVaccine(v)}" <h:form>
onclick="return true;"/> <h:commandButton value="Eliminar" action="#{vaccine.removeVaccine(v.id)}"
</h:form> onclick="return confirm('¿Seguro que deseas eliminar esta vacuna?');"/>
</h:column> <h:commandButton value="Editar" action="#{vaccine.setSelectedVaccine(v)}"
</h:dataTable> onclick="return true;"/>
</h:form>
<hr/> </h:column>
</h:dataTable>
<!-- Formulario para añadir vacuna -->
<h:form> <hr/>
<h3>Añadir nueva vacuna</h3>
<!-- Formulario para añadir vacuna -->
<h:panelGrid columns="2" cellpadding="5"> <h:form>
<h:outputLabel value="Nombre:" for="name"/> <h3>Añadir nueva vacuna</h3>
<h:inputText id="name" value="#{vaccine.name}" required="true"/>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Tipo:" for="type"/> <h:outputLabel value="Nombre:" for="name"/>
<h:selectOneMenu id="type" value="#{vaccine.type}"> <h:inputText id="name" value="#{vaccine.name}" required="true"/>
<f:selectItem itemLabel="Seleccione..." itemValue=""/>
<f:selectItem itemLabel="MONODOSE" itemValue="MONODOSE"/> <h:outputLabel value="Tipo:" for="type"/>
<f:selectItem itemLabel="MULTIDOSE" itemValue="MULTIDOSE"/> <h:selectOneMenu id="type" value="#{vaccine.type}">
<f:selectItem itemLabel="PERIODIC" itemValue="PERIODIC"/> <f:selectItem itemLabel="Seleccione..." itemValue=""/>
<f:ajax render="doses-label periode-label periodic-type-label doses periode periodicType"/> <f:selectItem itemLabel="MONODOSE" itemValue="MONODOSE"/>
</h:selectOneMenu> <f:selectItem itemLabel="MULTIDOSE" itemValue="MULTIDOSE"/>
<f:selectItem itemLabel="PERIODIC" itemValue="PERIODIC"/>
<h:outputLabel id="doses-label" value="Doses:" for="doses" <f:ajax render="doses-label periode-label periodic-type-label doses periode periodicType"/>
style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"/> </h:selectOneMenu>
<h:inputText id="doses" value="#{vaccine.doses}" required="#{vaccine.type eq 'MULTIDOSE'}"
style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"> <h:outputLabel id="doses-label" value="Doses:" for="doses"
<f:validateLongRange minimum="1"/> style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"/>
</h:inputText> <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 id="periode-label" 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 id="periodic-type-label" 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/>
<h:commandButton value="Crear Vacuna" action="#{vaccine.createVaccine}"/>
</h:form>
<h:outputLabel id="periode-label" value="Periode:" for="periode" <!--Formulario para editar vacuna-->
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/> <h:form rendered="#{not empty vaccine.selectedVaccine}">
<h:inputText id="periode" value="#{vaccine.periode}" required="#{vaccine.type eq 'PERIODIC'}" <h3>Editar vacuna #{vaccine.selectedVaccine.name}</h3>
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
<f:validateLongRange minimum="1"/> <h:panelGrid columns="2" cellpadding="5">
</h:inputText> <h:outputLabel value="Nuevo nombre:" for="newName"/>
<h:inputText id="newName" value="#{vaccine.newName}" required="true"/>
</h:panelGrid>
<h:outputLabel id="periodic-type-label" value="Periodic Type:" for="periodicType"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/> <br/>
<h:selectOneMenu id="periodicType" value="#{vaccine.periodicType}" <h:commandButton value="Guardar cambios" action="#{vaccine.editVaccine}"/>
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"> <h:commandButton value="Cancelar" action="#{vaccine.setSelectedVaccine(null)}" immediate="true"/>
<f:selectItem itemLabel="DAYS" itemValue="DAYS"/> </h:form>
<f:selectItem itemLabel="MONTHS" itemValue="MONTHS"/> </ui:define>
<f:selectItem itemLabel="YEARS" itemValue="YEARS"/> </ui:composition>
</h:selectOneMenu>
</h:panelGrid>
<br/>
<h:commandButton value="Crear Vacuna" action="#{vaccine.createVaccine}"/>
</h:form>
<!--Formulario para editar vacuna-->
<h:form rendered="#{not empty vaccine.selectedVaccine}">
<h3>Editar vacuna #{vaccine.selectedVaccine.name}</h3>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel value="Nuevo nombre:" for="newName"/>
<h:inputText id="newName" value="#{vaccine.newName}" required="true"/>
</h:panelGrid>
<br/>
<h:commandButton value="Guardar cambios" action="#{vaccine.editVaccine}"/>
<h:commandButton value="Cancelar" action="#{vaccine.setSelectedVaccine(null)}" immediate="true"/>
</h:form>
</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