Commit dc5935d4 authored by Breixo Senra's avatar Breixo Senra

Endpoints canVaccinate y create funcionando

parent f8cb9edc
package es.uvigo.esei.xcs.rest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import es.uvigo.esei.xcs.domain.entities.Vaccination;
import es.uvigo.esei.xcs.service.VaccinationService;
@Path("vaccination")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class VaccinationResource {
@EJB
private VaccinationService vaccinationService;
@GET
@Path("canVaccinate/{petId}/{vaccineId}")
public Response canVaccinate(
@PathParam("petId") Long petId,
@PathParam("vaccineId") Long vaccineId
) {
Date now = new Date();
Boolean result = vaccinationService.canVaccinate(petId, vaccineId, now);
return Response.ok(result).build();
}
@POST
@Path("create/{petId}/{vaccineId}")
public Response createVaccination(
@PathParam("petId") Long petId,
@PathParam("vaccineId") Long vaccineId,
@QueryParam("date") String textDate // opcional, formato yyyy-MM-dd
) {
Date date = new Date();
if (textDate != null) {
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(textDate);
} catch (ParseException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Invalid date format, expected yyyy-MM-dd").build();
}
}
try {
Vaccination vaccination = vaccinationService.create(petId, vaccineId, date);
return Response.ok(vaccination).build();
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(e.getMessage()).build();
}
}
}
...@@ -178,12 +178,12 @@ public class VetResource { ...@@ -178,12 +178,12 @@ public class VetResource {
@Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination") @Path("pets/{petIdentifierType}/{petIdentifierValue}/vaccination")
@POST @POST
public Response registerVaccination( public Response registerVaccination(
@QueryParam("date") String date, @QueryParam("date") Date date,
VaccinationCreationData vaccinationData VaccinationCreationData vaccinationData
) { ) {
Vaccination vaccination = this.vaccinationService.create( Vaccination vaccination = this.vaccinationService.create(
vaccinationData.getPetId().intValue(), vaccinationData.getPetId(),
vaccinationData.getVaccineId().intValue(), vaccinationData.getVaccineId(),
date date
); );
......
...@@ -7,15 +7,19 @@ import java.text.ParseException; ...@@ -7,15 +7,19 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List; import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import es.uvigo.esei.xcs.domain.entities.MultidoseVaccine;
import es.uvigo.esei.xcs.domain.entities.PeriodicVaccine;
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.Vaccination;
import es.uvigo.esei.xcs.domain.entities.Vaccine; import es.uvigo.esei.xcs.domain.entities.Vaccine;
@Stateless @Stateless
@PermitAll
public class VaccinationService { public class VaccinationService {
@PersistenceContext @PersistenceContext
EntityManager em; EntityManager em;
...@@ -40,7 +44,7 @@ public class VaccinationService { ...@@ -40,7 +44,7 @@ public class VaccinationService {
} }
public Vaccination create(int petId, int vaccineId, String textDate) { /*public Vaccination create(int petId, int vaccineId, String textDate) {
Pet pet = requireNonNull(em.find(Pet.class, petId), "Pet can't be null"); Pet pet = requireNonNull(em.find(Pet.class, petId), "Pet can't be null");
Vaccine vaccine = requireNonNull(em.find(Vaccine.class, vaccineId), "Vaccine can't be null"); Vaccine vaccine = requireNonNull(em.find(Vaccine.class, vaccineId), "Vaccine can't be null");
Date date = new Date(); Date date = new Date();
...@@ -53,12 +57,26 @@ public class VaccinationService { ...@@ -53,12 +57,26 @@ public class VaccinationService {
} }
} }
Vaccination vaccination = new Vaccination(pet, vaccine, date);
em.persist(vaccination);
return vaccination;
}*/
public Vaccination create(Long petId, Long vaccineId, Date date) {
Pet pet = requireNonNull(em.find(Pet.class, petId), "Pet can't be null");
Vaccine vaccine = requireNonNull(em.find(Vaccine.class, vaccineId), "Vaccine can't be null");
if (!canVaccinate(petId, vaccineId, date)) {
throw new IllegalArgumentException("This vaccination cannot be created due to vaccine rules");
}
Vaccination vaccination = new Vaccination(pet, vaccine, date); Vaccination vaccination = new Vaccination(pet, vaccine, date);
em.persist(vaccination); em.persist(vaccination);
return vaccination; return vaccination;
} }
public Vaccination updateDate(int vaccinationId, Date date) { public Vaccination updateDate(int vaccinationId, Date date) {
Vaccination vaccination = em.find(Vaccination.class, vaccinationId); Vaccination vaccination = em.find(Vaccination.class, vaccinationId);
requireNonNull(vaccination, "Vaccination can't be null"); requireNonNull(vaccination, "Vaccination can't be null");
...@@ -74,4 +92,53 @@ public class VaccinationService { ...@@ -74,4 +92,53 @@ public class VaccinationService {
} }
public Boolean canVaccinate(Long petId, Long vaccineId, Date date) {
Pet pet = em.find(Pet.class, petId);
Vaccine vaccine = em.find(Vaccine.class, vaccineId);
if (pet == null || vaccine == null) return false;
List<Vaccination> prevVaccinations = em.createQuery(
"SELECT v FROM Vaccination v WHERE v.pet.id = :petId AND v.vaccine.id = :vaccineId ORDER BY v.date DESC",
Vaccination.class)
.setParameter("petId", petId)
.setParameter("vaccineId", vaccineId)
.getResultList();
if (vaccine instanceof MultidoseVaccine) {
MultidoseVaccine multi = (MultidoseVaccine) vaccine;
Integer doses = multi.getDoses();
if (doses == null) return false;
return prevVaccinations.size() < doses;
} else if (vaccine instanceof PeriodicVaccine) {
PeriodicVaccine periodic = (PeriodicVaccine) vaccine;
if (prevVaccinations.isEmpty()) return true;
Vaccination last = prevVaccinations.get(0);
if (last.getDate() == null || date == null) return false;
long diffDays;
switch (periodic.getPeriodicType()) {
case YEARS:
diffDays = periodic.getPeriode() * 365L;
break;
case MONTHS:
diffDays = periodic.getPeriode() * 30L;
break;
case DAYS:
diffDays = periodic.getPeriode();
break;
default:
return false;
}
long diffMillis = date.getTime() - last.getDate().getTime();
return diffMillis >= diffDays * 24L * 60L * 60L * 1000L;
} else { // MONODOSE
return prevVaccinations.isEmpty();
}
}
} }
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