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;
@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)
public class Pet implements Serializable {
private static final long serialVersionUID = 1L;
......@@ -69,7 +69,7 @@ public class Pet implements Serializable {
private Owner owner;
@ManyToMany(
fetch = FetchType.LAZY,
fetch = FetchType.LAZY, //es LAZY
cascade = { CascadeType.PERSIST, CascadeType.MERGE }
)
@JoinTable(
......
......@@ -13,6 +13,16 @@
<description>XCS Sample - JSF</description>
<dependencies>
<!--Componentes de PrimeFaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<!-- General -->
<dependency>
<groupId>javax</groupId>
......
......@@ -74,7 +74,9 @@ public class PetManagedBean {
}
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) {
......@@ -138,4 +140,34 @@ public class PetManagedBean {
private String 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 @@
</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:body>
</html>
\ No newline at end of file
......@@ -14,6 +14,11 @@
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bootstrap</param-value>
</context-param>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
......
......@@ -4,6 +4,7 @@
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
<head>
<title>Pet Store - Index</title>
......@@ -13,6 +14,7 @@
<ui:define name="jumbotron">
<h2>Welcome!</h2>
<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:composition>
</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 @@
<f:selectItem itemLabel="MONODOSE" itemValue="MONODOSE"/>
<f:selectItem itemLabel="MULTIDOSE" itemValue="MULTIDOSE"/>
<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>
<!-- Doses solo si 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>-->
<h:outputLabel value="Doses:" for="doses"
<h:outputLabel id="doses-label" 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'}">
......@@ -77,7 +67,7 @@
</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'}"/>
<h:inputText id="periode" value="#{vaccine.periode}" required="#{vaccine.type eq 'PERIODIC'}"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
......@@ -85,7 +75,7 @@
</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'}"/>
<h:selectOneMenu id="periodicType" value="#{vaccine.periodicType}"
style="display:#{vaccine.type eq 'PERIODIC' ? 'inline' : 'none'}">
......
......@@ -79,7 +79,17 @@
<dependencyManagement>
<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 @@
=> "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" =>
"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>
</configuration>
</execution>
......
......@@ -13,6 +13,20 @@
<description>XCS Sample - REST</description>
<dependencies>
<!--Componentes de PrimeFaces-->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<!-- General -->
<dependency>
<groupId>javax</groupId>
......
......@@ -4,6 +4,7 @@ package es.uvigo.esei.xcs.rest;
import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
......@@ -11,6 +12,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import es.uvigo.esei.xcs.service.AdministratorService;
import es.uvigo.esei.xcs.service.EmailService;
@Path("administrator")
@Consumes(MediaType.APPLICATION_JSON)
......@@ -20,9 +22,19 @@ public class AdministratorResource {
@EJB
AdministratorService service;
@EJB
EmailService emailService;
@GET
public Response list(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize) {
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;
import javax.persistence.EntityExistsException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
......@@ -28,7 +29,7 @@ import es.uvigo.esei.xcs.service.PetService;
*
* @author Miguel Reboiro Jato
*/
@Path("pet")
@Path("pettt")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PetResource {
......@@ -67,8 +68,19 @@ public class PetResource {
* @return an {@code OK} response containing the complete list of pets of
* 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
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();
}
......
......@@ -98,7 +98,7 @@ public class VetResource {
}
@GET
@Path("pet")
@Path("pets")
public Response listPets(
//@PathParam("login") String login,
@QueryParam("page") @DefaultValue("0") int page,
......@@ -111,7 +111,7 @@ public class VetResource {
@GET
@Path("pet/{petId}")
@Path("pets/{petId}")
public Response getPet(@PathParam("petId") Long petId) {
return Response.ok(this.petService.get(petId)).build();
}
......@@ -155,7 +155,7 @@ public class VetResource {
}
}
@Path("pet/{petIdentifierType}/{petIdentifierValue}/vaccination")
@Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination")
@GET
public Response listVaccinations(
@PathParam("login") String login,
......@@ -175,7 +175,7 @@ public class VetResource {
)).build();
}
@Path("/pet/{petIdentifierType}/{petIdentifierValue}/vaccination")
@Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination")
@POST
public Response registerVaccination(
@QueryParam("date") String date,
......@@ -195,7 +195,7 @@ public class VetResource {
@POST
@Path("/assign/pet/{petId}")
@Path("/assign/pets/{petId}")
public Response assignVetToPet(
//@PathParam("login") String vetLogin,
@PathParam("petId") Long petId
......@@ -216,7 +216,7 @@ public class VetResource {
}
@DELETE
@Path("{login}/unassign/pet/{petId}")
@Path("{login}/unassign/pets/{petId}")
public Response unassignVetFromPet(
//@PathParam("login") String vetLogin,
@PathParam("petId") Long petId
......
......@@ -7,12 +7,14 @@ import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.mail.internet.MimeMessage;
import java.util.logging.Level;
import java.util.logging.Logger;
@Stateless
@PermitAll
public class EmailService {
@Resource(name = "java:jboss/mail/Default")
private Session session;
......
......@@ -2,6 +2,7 @@ package es.uvigo.esei.xcs.service;
import static java.util.Objects.requireNonNull;
import java.io.Console;
import java.security.Principal;
import java.util.List;
......@@ -25,7 +26,7 @@ import es.uvigo.esei.xcs.domain.entities.Vet;
* @author Miguel Reboiro Jato
*/
@Stateless
//@RolesAllowed("OWNER")
//@RolesAllowed("VET")
@PermitAll
public class PetService {
@Inject
......@@ -59,6 +60,20 @@ public class PetService {
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.
*
......@@ -192,6 +207,24 @@ public class PetService {
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