how can I validate a junction of hours in jsf hibernate mysql

1

This is my managed bean

public class PeliculaMB{

public EntityManager em;
private Pelicula pelicula= new Pelicula();
private Categoria categoria = new Categoria();
private List<Pelicula> peliculas = new ArrayList<Pelicula>();
private List<Categoria> categorias = new ArrayList<Categoria>();

public String regseries() {
    pelicula = new Pelicula();
    return "registro_series";
}
private void guardarpelicula(){
    EntityManager em = JPAUtil.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.persist(pelicula);
    tx.commit();
}
public String guardarpel(){
    guardarpelicula();
    pelicula = new Pelicula();
    FacesMessage fm = new FacesMessage("Se guardo la serie corectamente", "insertado correctamente");
    FacesContext.getCurrentInstance().addMessage(null, fm);
    return null;
}


private void guardarcategoria(){
    EntityManager em = JPAUtil.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.merge(categoria);
    tx.commit();
}
public String guardar() {
    guardarcategoria();
    categoria = new Categoria();
    // se crea el objeto FacesMessage para guardar el mensaje
    FacesMessage message = new FacesMessage("Se guardó correctamente",
            "El registro se guardó satisfactoriamente ");
    // colocamos el mensaje en el contexto de Faces
    FacesContext.getCurrentInstance().addMessage(null, message);
    return null;
}

public Categoria getCategoria() {
    return categoria;
}
public void setCategoria(Categoria categoria) {
    this.categoria = categoria;
}
public List<Categoria> getCategorias() {
    em = JPAUtil.getEntityManager();
    categorias = em.createQuery("from Categoria", Categoria.class).getResultList();
    return categorias;
}
public void setCategorias(List<Categoria> categorias) {
    this.categorias = categorias;
}
public Pelicula getPelicula() {
    return pelicula;
}
public void setPelicula(Pelicula pelicula) {
    this.pelicula = pelicula;
}
public List<Pelicula> getPeliculas() {
     em = JPAUtil.getEntityManager();
    return peliculas;
}
public void setPeliculas(List<Pelicula> peliculas) {
    this.peliculas = peliculas;
}

this is my page jsf register series

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/template/BasicTemplate.xhtml">
    <ui:define name="content">
        <h:form>
            <h:messages globalOnly="true"  showDetail="true" showSummary="false"></h:messages>
                <h:panelGrid>
                <h:outputText value="Titulo: "></h:outputText>
                <h:inputText id="titulo" value="#{peliculaMB.pelicula.titulo}" maxlength="20" required="true"
                 requiredMessage="Debe llenar este campo"></h:inputText>
                <h:message for="titulo" ></h:message> 

                <h:outputText value="Hora Inicio"></h:outputText>
                <h:inputText id="horain" value="#{peliculaMB.pelicula.horain}" >
                    <f:convertDateTime pattern="HH:mm" type="time"></f:convertDateTime>
                </h:inputText>

                <h:outputText value="Hora Fin"></h:outputText>
                <h:inputText id="horafin" value="#{peliculaMB.pelicula.horafin}" >
                    <f:convertDateTime pattern="HH:mm" type="time"></f:convertDateTime>
                </h:inputText>

                <h:outputText value="Cobertura"></h:outputText>
                <h:selectOneMenu id="cobertura" value="#{peliculaMB.pelicula.cobertura}">
                        <f:selectItem itemLabel="APT-14" itemValue="A1" />
                        <f:selectItem itemLabel="APT-18" itemValue="A2" />
                        <f:selectItem itemLabel="APT-T" itemValue="T"></f:selectItem>
                </h:selectOneMenu>

                <h:outputText value="Categoria"></h:outputText>
                <h:selectOneMenu id="cat" value="#{peliculaMB.categoria.cat}">
                    <f:selectItems value="#{peliculaMB.categorias}" var="categ" itemValue="#{categ.id}" itemLabel="#{categ.cat}" ></f:selectItems>
                </h:selectOneMenu>

                </h:panelGrid>

                <h:commandButton value="Guardar" action="#{peliculaMB.guardarpel}" />

        </h:form>
    </ui:define>
</ui:composition>

</html>

if the class should be called series, but because of the trouble I did not read the title of the exercise and I put a movie what I need is to validate that when inserting a new series (movie) the schedules do not cross, for example: the simpsons from 1:00 pm to 2:00 pm and can not register another series that is in that range of hours, besides I also got an error when inserting an hour is the next one

 INFORMACIÓN: ADVERTENCIA: FacesMessage(s) se han puesto en la cola, pero es posible que no se muestren. sourceId=j_idt6:horafin[severity=(ERROR 2), summary=(j_idt6:horafin: '10.20' no se ha podido reconocer como fecha.), detail=(j_idt6:horafin: '10.20' no se ha podido reconocer como fecha. Ejemplo: 20:07)]oct 11, 2016 3:07:24 PM com.sun.faces.renderkit.RenderKitUtils renderUnhandledMessages INFORMACIÓN: ADVERTENCIA: FacesMessage(s) se han puesto en la cola, pero es posible que no se muestren. sourceId=j_idt6:horafin[severity=(ERROR 2), summary=(j_idt6:horafin: '10.20' no se ha podido reconocer como fecha.), detail=(j_idt6:horafin: '10.20' no se ha podido reconocer como fecha. Ejemplo: 20:07 )] oct 11, 2016 3:08:12 PM org.apache.catalina.core.StandardContext reload

what you should do to be able to insert without error and validate the crossing of schedules

Update: this is my class

ackage
Import java.util.Date;
Import javax.persistence.Column;
import javax.persistence.Entity;
Import javax.persistence.GeneratedValue;
import javax.persistence.Id;
Import javax.persistence.ManyToOne;
import javax.persistence.Table;
Import javax.persistence.Temporal;
Import javax.persistence.TemporalType;
@Entity
@Table(name="tb_pelicula")
Public class Pelicula {
@Id
@GeneratedValue
private long id;
@Column(name="titulo", length=20)
private String titulo;
@Temporal(TemporalType.TIME)
private Date horain;
@Temporal(TemporalType.TIME)
private Date horafin;
private String cobertura;
private String categoria;
@ManyToOne
private Categoria cat = new Categoria(); 
public Categoria getCat() {
    return cat;
}
public void setCat(Categoria cat) {
    this.cat = cat;
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getTitulo() {
    return titulo;
}
public void setTitulo(String titulo) {
    this.titulo = titulo;
}
public Date getHorain() {
    return horain;
}
public void setHorain(Date horain) {
    this.horain = horain;
}
public Date getHorafin() {
    return horafin;
}
public void setHorafin(Date horafin) {
    this.horafin = horafin;
}
public String getCobertura() {
    return cobertura;
}
public void setCobertura(String cobertura) {
    this.cobertura = cobertura;
}
public String getCategoria() {
    return categoria;
}
public void setCategoria(String categoria) {
    this.categoria = categoria;
}
}
    
asked by Lrawls 11.10.2016 в 22:21
source

1 answer

0

From your JSF I infer that the class Pelicula has attributes horain and horafin . Let's say that pelicula is the object you want to register. What you should do is a query about Pelicula entities that meet either of these two conditions:

  • Your horain is before or equal to pelicula.horain and your horafin is later or equal to pelicula.horain (starts before your movie / series but ends after it starts and therefore overlaps)
  • Your horain is before or equal to pelicula.horafin and your horafin is later or equal to pelicula.horafin
  • This covers the four cases of overlap:

    ----------------(///////////////////)------------ Pelicula que quieres registrar
    -----------(//////////)-------------------------- Caso A
    --------------------------------(///////)-------- Caso B
    -----------(//////////////////////////////)------ Caso C
    ----------------------(///////)------------------ Caso D
    

    You can make a query with a where clause as follows (with the necessary adaptations for your code):

    em.createQuery("select p from Pelicula p where (p.horain <= :horain AND p.horafin >= :horain) OR (p.horafin <= :horain AND p.horafin >= :horafin)", Pelicula.class)
        .setParameter("horain", pelicula.getHorain())
        .setParameter("horafin", pelicula.getHorafin())
        .getResultList();
    

    If the resulting list is empty, there are no persisted objects that overlap at the time you want to insert and it is safe to do persist()

        
    answered by 11.10.2016 / 23:27
    source