Commit 25685e70 authored by Breixo Senra's avatar Breixo Senra

Punto 4 BETA

parent 06edf8e1
......@@ -38,7 +38,7 @@ public class PetDetailsManagedBean implements Serializable{
private Pet pet;
private Long selectedVaccineId;
private String vaccinationDate; // yyyy-MM-dd
private Date vaccinationDate; // yyyy-MM-dd
private List<Vaccine> allVaccines;
@PostConstruct
......@@ -69,33 +69,33 @@ public class PetDetailsManagedBean implements Serializable{
this.selectedVaccineId = selectedVaccineId;
}
public String getVaccinationDate() {
public Date getVaccinationDate() {
return vaccinationDate;
}
public void setVaccinationDate(String vaccinationDate) {
public void setVaccinationDate(Date vaccinationDate) {
this.vaccinationDate = vaccinationDate;
}
// Método para vacunar
public String vaccinate() {
if (selectedVaccineId == null || vaccinationDate == null || vaccinationDate.isEmpty()) {
if (selectedVaccineId == null || vaccinationDate == null /*|| vaccinationDate.isEmpty()*/) {
FacesContext.getCurrentInstance().addMessage(null,
new javax.faces.application.FacesMessage("Debe seleccionar vacuna y fecha"));
return null;
}
try {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(vaccinationDate);
Vaccination v = vaccinationService.create(pet.getId(), selectedVaccineId, date);
//Date date = new SimpleDateFormat("yyyy-MM-dd").parse(vaccinationDate);
Vaccination v = vaccinationService.create(pet.getId(), selectedVaccineId, vaccinationDate);
// refrescar la mascota para mostrar la nueva vacunación
this.pet = petService.get(pet.getId());
FacesContext.getCurrentInstance().addMessage(null,
new javax.faces.application.FacesMessage("Vacunación creada correctamente"));
} catch (ParseException e) {
} /*catch (ParseException e) {
FacesContext.getCurrentInstance().addMessage(null,
new javax.faces.application.FacesMessage("Formato de fecha inválido"));
} catch (IllegalArgumentException e) {
}*/ catch (IllegalArgumentException e) {
FacesContext.getCurrentInstance().addMessage(null,
new javax.faces.application.FacesMessage(e.getMessage()));
}
......
package es.uvigo.esei.xcs.jsf;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import es.uvigo.esei.xcs.domain.entities.Vaccine;
import es.uvigo.esei.xcs.service.VaccineService;
@Named("vaccine")
@RequestScoped
public class VaccineManagedBean{
//@RequestScoped
@ViewScoped
public class VaccineManagedBean implements Serializable{
private static final long serialVersionUID = 1L;
@EJB
private VaccineService vaccineService;
......@@ -38,6 +42,27 @@ public class VaccineManagedBean{
vaccines = vaccineService.list(0, 100);
}
private Vaccine selectedVaccine; // vacuna que vamos a editar
private String newName; // nuevo nombre para editar
public Vaccine getSelectedVaccine() { return selectedVaccine; }
public void setSelectedVaccine(Vaccine selectedVaccine) {
this.selectedVaccine = selectedVaccine;
if (selectedVaccine != null) {
this.newName = selectedVaccine.getName(); // precarga el nombre actual
}
}
public String getNewName() { return newName; }
public void setNewName(String newName) { this.newName = newName; }
public void editVaccine() {
if (selectedVaccine != null && newName != null && !newName.trim().isEmpty()) {
vaccineService.updateName(selectedVaccine.getId(), newName);
vaccines = vaccineService.list(0, 100);
selectedVaccine = null;
newName = null;
}
}
private void clearForm() {
name = null;
......
......@@ -3,6 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Detalle de Mascota</title>
......@@ -38,9 +39,10 @@
</h:column>
</h:dataTable>
<p:commandButton value="Open Dialog" />
<h3>Registrar nueva vacunación</h3>
<h:form>
<!--<h:messages globalOnly="true" layout="table" style="color:red;" />-->
<h:panelGrid columns="2">
<h:outputLabel value="Vacuna:" for="vaccine"/>
<h:selectOneMenu value="#{petDetails.selectedVaccineId}" id="vaccine">
......@@ -49,7 +51,7 @@
</h:selectOneMenu>
<h:outputLabel value="Fecha:" for="date"/>
<h:inputText value="#{petDetails.vaccinationDate}" id="date" placeholder="yyyy-MM-dd"/>
<p:datePicker id="date" value="#{petDetails.vaccinationDate}" pattern="yyyy-MM-dd" showIcon="true" />
<h:commandButton value="Vacunar" action="#{petDetails.vaccinate}" />
</h:panelGrid>
......
......@@ -36,6 +36,8 @@
<h:form>
<h:commandButton value="Eliminar" action="#{vaccine.removeVaccine(v.id)}"
onclick="return confirm('¿Seguro que deseas eliminar esta vacuna?');"/>
<h:commandButton value="Editar" action="#{vaccine.setSelectedVaccine(v)}"
onclick="return true;"/>
</h:form>
</h:column>
</h:dataTable>
......@@ -89,5 +91,20 @@
<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>
</html>
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