Commit f8cb9edc authored by Breixo Senra's avatar Breixo Senra

Asignación de mascotas funcionando

parent 7fe077dc
...@@ -35,7 +35,7 @@ import javax.xml.bind.annotation.XmlTransient; ...@@ -35,7 +35,7 @@ import javax.xml.bind.annotation.XmlTransient;
@Entity(name = "Pet") @Entity(name = "Pet")
@XmlRootElement(name = "pet", namespace = "http://entities.domain.xcs.esei.uvigo.es") @XmlRootElement(name = "pett", namespace = "http://entities.domain.xcs.esei.uvigo.es")
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class Pet implements Serializable { public class Pet implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
...@@ -69,7 +69,7 @@ public class Pet implements Serializable { ...@@ -69,7 +69,7 @@ public class Pet implements Serializable {
private Owner owner; private Owner owner;
@ManyToMany( @ManyToMany(
fetch = FetchType.LAZY, fetch = FetchType.LAZY, //es LAZY
cascade = { CascadeType.PERSIST, CascadeType.MERGE } cascade = { CascadeType.PERSIST, CascadeType.MERGE }
) )
@JoinTable( @JoinTable(
......
...@@ -13,6 +13,16 @@ ...@@ -13,6 +13,16 @@
<description>XCS Sample - JSF</description> <description>XCS Sample - JSF</description>
<dependencies> <dependencies>
<!--Componentes de PrimeFaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<!-- General --> <!-- General -->
<dependency> <dependency>
<groupId>javax</groupId> <groupId>javax</groupId>
......
...@@ -74,7 +74,9 @@ public class PetManagedBean { ...@@ -74,7 +74,9 @@ public class PetManagedBean {
} }
public List<Pet> getPets() { public List<Pet> getPets() {
return this.service.list(0, 100); List<Pet> list = this.service.getAll(0, 100);
System.out.println(list);
return list;
} }
public String edit(Long petId) { public String edit(Long petId) {
...@@ -138,4 +140,34 @@ public class PetManagedBean { ...@@ -138,4 +140,34 @@ public class PetManagedBean {
private String getViewId() { private String getViewId() {
return FacesContext.getCurrentInstance().getViewRoot().getViewId(); return FacesContext.getCurrentInstance().getViewRoot().getViewId();
} }
public void assignToMe(Long petId) {
try {
service.assignVetToPet(petId);
} catch (Exception e) {
this.errorMessage = e.getMessage();
}
}
public void unassignFromMe(Long petId) {
try {
service.unassignVetFromPet(petId);
} catch (Exception e) {
this.errorMessage = e.getMessage();
}
}
public boolean isAssignedToMe(Pet pet) {
return this.service.isAssignedToCurrentVet(pet.getId());
}
public void toggleAssignment(Pet pet) {
if (isAssignedToMe(pet)) {
unassignFromMe(pet.getId());
} else {
assignToMe(pet.getId());
}
}
} }
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
</div> </div>
</div> </div>
<h:outputScript name="webjars/jquery/1.11.1/jquery.min.js" /> <!--<h:outputScript name="webjars/jquery/1.11.1/jquery.min.js" />-->
<h:outputScript name="webjars/bootstrap/3.4.1/js/bootstrap.min.js" /> <h:outputScript name="webjars/bootstrap/3.4.1/js/bootstrap.min.js" />
</h:body> </h:body>
</html> </html>
\ No newline at end of file
...@@ -14,6 +14,11 @@ ...@@ -14,6 +14,11 @@
<param-value>Development</param-value> <param-value>Development</param-value>
</context-param> </context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bootstrap</param-value>
</context-param>
<!-- JSF mapping --> <!-- JSF mapping -->
<servlet> <servlet>
<servlet-name>Faces Servlet</servlet-name> <servlet-name>Faces Servlet</servlet-name>
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
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:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough"> xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
<head> <head>
<title>Pet Store - Index</title> <title>Pet Store - Index</title>
...@@ -13,6 +14,7 @@ ...@@ -13,6 +14,7 @@
<ui:define name="jumbotron"> <ui:define name="jumbotron">
<h2>Welcome!</h2> <h2>Welcome!</h2>
<div>This is the Pet Store web page, where you can manage your pets. Please, login to continue.</div> <div>This is the Pet Store web page, where you can manage your pets. Please, login to continue.</div>
<p:commandButton value="Open Dialog" />
</ui:define> </ui:define>
</ui:composition> </ui:composition>
</body> </body>
......
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Mis Mascotas</title>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{pet.pets}" var="p" border="1">
<h:column>
<f:facet name="header">Nombre</f:facet>
#{p.name}
</h:column>
<h:column>
<f:facet name="header">Nacimiento</f:facet>
#{p.birth}
</h:column>
<h:column>
<f:facet name="header">Tipo</f:facet>
#{p.animal}
</h:column>
<h:column>
<f:facet name="header">Acción</f:facet>
<h:commandButton value="#{pet.isAssignedToMe(p) ? 'Desasignar' : 'Asignar'}"
action="#{pet.toggleAssignment(p)}"/>
</h:column>
</h:dataTable>
<h:messages globalOnly="true"/>
</h:form>
</h:body>
</html>
...@@ -56,20 +56,10 @@ ...@@ -56,20 +56,10 @@
<f:selectItem itemLabel="MONODOSE" itemValue="MONODOSE"/> <f:selectItem itemLabel="MONODOSE" itemValue="MONODOSE"/>
<f:selectItem itemLabel="MULTIDOSE" itemValue="MULTIDOSE"/> <f:selectItem itemLabel="MULTIDOSE" itemValue="MULTIDOSE"/>
<f:selectItem itemLabel="PERIODIC" itemValue="PERIODIC"/> <f:selectItem itemLabel="PERIODIC" itemValue="PERIODIC"/>
<f:ajax render="doses periode periodicType"/> <f:ajax render="doses-label periode-label periodic-type-label doses periode periodicType"/>
</h:selectOneMenu> </h:selectOneMenu>
<!-- Doses solo si MULTIDOSE --> <h:outputLabel id="doses-label" value="Doses:" for="doses"
<!--<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>-->
<h:outputLabel value="Doses:" for="doses"
style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"/> style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"/>
<h:inputText id="doses" value="#{vaccine.doses}" required="#{vaccine.type eq 'MULTIDOSE'}" <h:inputText id="doses" value="#{vaccine.doses}" required="#{vaccine.type eq 'MULTIDOSE'}"
style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}"> style="display:#{vaccine.type eq 'MULTIDOSE' ? 'inline' : 'none'}">
...@@ -77,7 +67,7 @@ ...@@ -77,7 +67,7 @@
</h:inputText> </h:inputText>
<h:outputLabel value="Periode:" for="periode" <h:outputLabel id="periode-label" value="Periode:" for="periode"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/> style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/>
<h:inputText id="periode" value="#{vaccine.periode}" required="#{vaccine.type eq 'PERIODIC'}" <h:inputText id="periode" value="#{vaccine.periode}" required="#{vaccine.type eq 'PERIODIC'}"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"> style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
...@@ -85,7 +75,7 @@ ...@@ -85,7 +75,7 @@
</h:inputText> </h:inputText>
<h:outputLabel value="Periodic Type:" for="periodicType" <h:outputLabel id="periodic-type-label" value="Periodic Type:" for="periodicType"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/> style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"/>
<h:selectOneMenu id="periodicType" value="#{vaccine.periodicType}" <h:selectOneMenu id="periodicType" value="#{vaccine.periodicType}"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}"> style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
......
...@@ -79,7 +79,17 @@ ...@@ -79,7 +79,17 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<!--Componentes de PrimeFaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>13.0.10</version> <!-- Mejor llevar la versión a properties -->
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
<version>1.1.0</version> <!-- Mejor llevar la versión a properties -->
</dependency>
...@@ -744,6 +754,8 @@ ...@@ -744,6 +754,8 @@
=> "required", "module-options" => [("dsJndiName" => "java:jboss/datasources/xcs"),("principalsQuery" => "SELECT password FROM User WHERE => "required", "module-options" => [("dsJndiName" => "java:jboss/datasources/xcs"),("principalsQuery" => "SELECT password FROM User WHERE
login=?"),("rolesQuery" => "SELECT role, 'Roles' FROM User WHERE login=?"),("hashAlgorithm" => "MD5"),("hashEncoding" => login=?"),("rolesQuery" => "SELECT role, 'Roles' FROM User WHERE login=?"),("hashAlgorithm" => "MD5"),("hashEncoding" =>
"hex"),("ignorePasswordCase" => "true")]}])</command> "hex"),("ignorePasswordCase" => "true")]}])</command>
<command>/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=mail-smtp:write-attribute(name=port,value=2525)</command>
<command>:reload</command>
</commands> </commands>
</configuration> </configuration>
</execution> </execution>
......
...@@ -13,6 +13,20 @@ ...@@ -13,6 +13,20 @@
<description>XCS Sample - REST</description> <description>XCS Sample - REST</description>
<dependencies> <dependencies>
<!--Componentes de PrimeFaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<!-- General --> <!-- General -->
<dependency> <dependency>
<groupId>javax</groupId> <groupId>javax</groupId>
......
...@@ -4,6 +4,7 @@ package es.uvigo.esei.xcs.rest; ...@@ -4,6 +4,7 @@ package es.uvigo.esei.xcs.rest;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
...@@ -11,6 +12,7 @@ import javax.ws.rs.core.MediaType; ...@@ -11,6 +12,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import es.uvigo.esei.xcs.service.AdministratorService; import es.uvigo.esei.xcs.service.AdministratorService;
import es.uvigo.esei.xcs.service.EmailService;
@Path("administrator") @Path("administrator")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
...@@ -20,9 +22,19 @@ public class AdministratorResource { ...@@ -20,9 +22,19 @@ public class AdministratorResource {
@EJB @EJB
AdministratorService service; AdministratorService service;
@EJB
EmailService emailService;
@GET @GET
public Response list(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize) { public Response list(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize) {
return Response.ok(this.service.list(page, pageSize)).build(); return Response.ok(this.service.list(page, pageSize)).build();
} }
@POST
public Response sendEmail() {
this.emailService.send("email@fake.email", "Topic", "Text Message");
return Response.ok().build();
}
} }
...@@ -7,6 +7,7 @@ import javax.ejb.EJBAccessException; ...@@ -7,6 +7,7 @@ import javax.ejb.EJBAccessException;
import javax.persistence.EntityExistsException; import javax.persistence.EntityExistsException;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
...@@ -28,7 +29,7 @@ import es.uvigo.esei.xcs.service.PetService; ...@@ -28,7 +29,7 @@ import es.uvigo.esei.xcs.service.PetService;
* *
* @author Miguel Reboiro Jato * @author Miguel Reboiro Jato
*/ */
@Path("pet") @Path("pettt")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public class PetResource { public class PetResource {
...@@ -67,8 +68,19 @@ public class PetResource { ...@@ -67,8 +68,19 @@ public class PetResource {
* @return an {@code OK} response containing the complete list of pets of * @return an {@code OK} response containing the complete list of pets of
* the current owner. * the current owner.
*/ */
@GET
@Path("lista")
public Response getAll(
@QueryParam("page") @DefaultValue("0") int page,
@QueryParam("pageSize") @DefaultValue("10") int pageSize) {
return Response.ok(this.service.getAll(page, pageSize)).build();
}
@GET @GET
public Response list(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize) { public Response list(
@QueryParam("page") @DefaultValue("0") int page,
@QueryParam("pageSize") @DefaultValue("10") int pageSize) {
return Response.ok(this.service.list(page, pageSize)).build(); return Response.ok(this.service.list(page, pageSize)).build();
} }
......
...@@ -98,7 +98,7 @@ public class VetResource { ...@@ -98,7 +98,7 @@ public class VetResource {
} }
@GET @GET
@Path("pet") @Path("pets")
public Response listPets( public Response listPets(
//@PathParam("login") String login, //@PathParam("login") String login,
@QueryParam("page") @DefaultValue("0") int page, @QueryParam("page") @DefaultValue("0") int page,
...@@ -111,7 +111,7 @@ public class VetResource { ...@@ -111,7 +111,7 @@ public class VetResource {
@GET @GET
@Path("pet/{petId}") @Path("pets/{petId}")
public Response getPet(@PathParam("petId") Long petId) { public Response getPet(@PathParam("petId") Long petId) {
return Response.ok(this.petService.get(petId)).build(); return Response.ok(this.petService.get(petId)).build();
} }
...@@ -155,7 +155,7 @@ public class VetResource { ...@@ -155,7 +155,7 @@ public class VetResource {
} }
} }
@Path("pet/{petIdentifierType}/{petIdentifierValue}/vaccination") @Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination")
@GET @GET
public Response listVaccinations( public Response listVaccinations(
@PathParam("login") String login, @PathParam("login") String login,
...@@ -175,7 +175,7 @@ public class VetResource { ...@@ -175,7 +175,7 @@ public class VetResource {
)).build(); )).build();
} }
@Path("/pet/{petIdentifierType}/{petIdentifierValue}/vaccination") @Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination")
@POST @POST
public Response registerVaccination( public Response registerVaccination(
@QueryParam("date") String date, @QueryParam("date") String date,
...@@ -195,7 +195,7 @@ public class VetResource { ...@@ -195,7 +195,7 @@ public class VetResource {
@POST @POST
@Path("/assign/pet/{petId}") @Path("/assign/pets/{petId}")
public Response assignVetToPet( public Response assignVetToPet(
//@PathParam("login") String vetLogin, //@PathParam("login") String vetLogin,
@PathParam("petId") Long petId @PathParam("petId") Long petId
...@@ -216,7 +216,7 @@ public class VetResource { ...@@ -216,7 +216,7 @@ public class VetResource {
} }
@DELETE @DELETE
@Path("{login}/unassign/pet/{petId}") @Path("{login}/unassign/pets/{petId}")
public Response unassignVetFromPet( public Response unassignVetFromPet(
//@PathParam("login") String vetLogin, //@PathParam("login") String vetLogin,
@PathParam("petId") Long petId @PathParam("petId") Long petId
......
...@@ -7,12 +7,14 @@ import javax.mail.Session; ...@@ -7,12 +7,14 @@ import javax.mail.Session;
import javax.mail.Transport; import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@Stateless @Stateless
@PermitAll
public class EmailService { public class EmailService {
@Resource(name = "java:jboss/mail/Default") @Resource(name = "java:jboss/mail/Default")
private Session session; private Session session;
......
...@@ -2,6 +2,7 @@ package es.uvigo.esei.xcs.service; ...@@ -2,6 +2,7 @@ package es.uvigo.esei.xcs.service;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import java.io.Console;
import java.security.Principal; import java.security.Principal;
import java.util.List; import java.util.List;
...@@ -25,7 +26,7 @@ import es.uvigo.esei.xcs.domain.entities.Vet; ...@@ -25,7 +26,7 @@ import es.uvigo.esei.xcs.domain.entities.Vet;
* @author Miguel Reboiro Jato * @author Miguel Reboiro Jato
*/ */
@Stateless @Stateless
//@RolesAllowed("OWNER") //@RolesAllowed("VET")
@PermitAll @PermitAll
public class PetService { public class PetService {
@Inject @Inject
...@@ -59,6 +60,20 @@ public class PetService { ...@@ -59,6 +60,20 @@ public class PetService {
return em.find(Pet.class, id); return em.find(Pet.class, id);
} }
public List<Pet> getAll(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");
}
return em.createQuery("SELECT p FROM Pet p", Pet.class)
.setFirstResult(page * pageSize)
.setMaxResults(pageSize)
.getResultList();
}
/** /**
* Returns the complete list of pets of the current owner. * Returns the complete list of pets of the current owner.
* *
...@@ -192,6 +207,24 @@ public class PetService { ...@@ -192,6 +207,24 @@ public class PetService {
em.merge(pet); em.merge(pet);
} }
public Principal getCurrentUser() {
return this.currentUser;
}
public boolean isAssignedToCurrentVet(Long petId) {
requireNonNull(petId, "Pet ID can't be null");
Long count = em.createQuery(
"SELECT COUNT(p) FROM Pet p JOIN p.vets v WHERE p.id = :petId AND v.login = :login",
Long.class
)
.setParameter("petId", petId)
.setParameter("login", currentUser.getName())
.getSingleResult();
return count > 0;
}
} }
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