简体   繁体   中英

Details with parameter and datatable for child entity

I am programming a Java EE application to do the yearly evaluation to the company workers. I want to display the details of the evaluation for every collaborateur when I click on Details.

The 3 impacted tables are "Collaborateur", "Entretien" and "NoteEntretien"

So when I select one collaborateur and click on View details, I want to be redirected to a new page with the collaborator details on a form and after a datatable with his evaluations. And when i click on evaluation details, I should be redirected to another page with the differents notes of the evaluation.

I am using JSF 2.x with Primefaces 5.0 on front, EclipseLink on the persistence and EJB 3.1 on the business side.

I already have the solution to show collaborator details on a popup without the datatable with the collaborator evaluations.

Please help me to find the best way to do that.

And thank you !

CollaborateurList.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:p="http://primefaces.org/ui">

<ui:composition template="/template.xhtml">


    <ui:define name="title">
        <h:outputText value="Liste des Collaborateurs"></h:outputText>
    </ui:define>

    <ui:define name="body">
        <h:form id="CollaborateurListForm">
            <p:panel header="Liste des Collaborateurs">
                <p:dataTable id="datalist" value="#{collaborateurController.items}" var="item"
                             selectionMode="single" selection="#{collaborateurController.selected}"
                             paginator="true"
                             rowKey="#{item.idCollaborateur}"
                             rows="10"
                             rowsPerPageTemplate="10,20,30,40,50"
                             >

                    <p:ajax event="rowSelect"   update="createButton viewButton editButton 
 deleteButton entretienButton"/>
                    <p:ajax event="rowUnselect" update="createButton viewButton editButton 
  deleteButton entretienButton"/>

                    <p:column sortBy="#{item.idCollaborateur}" filterBy="#{item.idCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Matricule"/>
                        </f:facet>
                        <h:outputText value="#{item.idCollaborateur}"/>
                    </p:column>
                    <p:column sortBy="#{item.nomCollaborateur}" filterBy="#{item.nomCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Nom Complet"/>
                        </f:facet>
                        <h:outputText value="#{item.nomCollaborateur}"/>
                    </p:column>
                    <p:column sortBy="#{item.genreCollaborateur}" filterBy="# 
 {item.genreCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Genre"/>
                        </f:facet>
                        <h:outputText value="#{item.genreCollaborateur}"/>
                    </p:column>
                    <p:column sortBy="#{item.dateNaissanceCollaborateur}" filterBy="# 
  {item.dateNaissanceCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Date de naissance"/>
                        </f:facet>
                        <h:outputText value="#{item.dateNaissanceCollaborateur}">
                            <f:convertDateTime pattern="MM/dd/yyyy" />
                        </h:outputText>
                    </p:column>
                    <p:column sortBy="#{item.dateRecrutementCollaborateur}" filterBy="# 
  {item.dateRecrutementCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Date de rectutement"/>
                        </f:facet>
                        <h:outputText value="#{item.dateRecrutementCollaborateur}">
                            <f:convertDateTime pattern="MM/dd/yyyy" />
                        </h:outputText>
                    </p:column>
                    <p:column sortBy="#{item.statutCollaborateur}" filterBy="# 
  {item.statutCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Statut"/>
                        </f:facet>
                        <h:outputText value="#{item.statutCollaborateur}"/>
                    </p:column>
                    <p:column sortBy="#{item.echelleCollaborateur}" filterBy="# 
   {item.echelleCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Echelle"/>
                        </f:facet>
                        <h:outputText value="#{item.echelleCollaborateur}"/>
                    </p:column>
                    <p:column sortBy="#{item.echelonCollaborateur}" filterBy="# 
  {item.echelonCollaborateur}">
                        <f:facet name="header">
                            <h:outputText value="Echellon"/>
                        </f:facet>
                        <h:outputText value="#{item.echelonCollaborateur}"/>
                    </p:column>
                    <p:column sortBy="#{item.idPosteCollaborateur.libellePoste}" filterBy="# 
  {item.idPosteCollaborateur.libellePoste}">
                        <f:facet name="header">
                            <h:outputText value="Poste"/>
                        </f:facet>
                        <h:outputText value="#{item.idPosteCollaborateur.libellePoste}"/>
                    </p:column>
                    <p:column sortBy="#{item.codeAgenceAffectation.nomAgence}" filterBy="# 
  {item.codeAgenceAffectation.nomAgence}">
                        <f:facet name="header">
                            <h:outputText value="Agence d'affectation"/>
                        </f:facet>
                        <h:outputText value="#{item.codeAgenceAffectation.nomAgence}"/>
                    </p:column>
                    <p:column sortBy="#{item.codeAgenceAffectation.codeGroupeAgence.nomGroupeAgence}"
                              filterBy="# 
  {item.codeAgenceAffectation.codeGroupeAgence.nomGroupeAgence}">
                        <f:facet name="header">
                            <h:outputText value="Groupe d'Agences"/>
                        </f:facet>
                        <h:outputText value="# 
    {item.codeAgenceAffectation.codeGroupeAgence.nomGroupeAgence}"/>
                    </p:column>
                    <p:column sortBy="# 
   {item.codeAgenceAffectation.codeGroupeAgence.codeDirectionRegionale.nomDirectionRegionale}"
                              filterBy="# 
   {item.codeAgenceAffectation.codeGroupeAgence.codeDirectionRegionale.nomDirectionRegionale}">
                        <f:facet name="header">
                            <h:outputText value="Direction régionale"/>
                        </f:facet>
                        <h:outputText value="# 
   {item.codeAgenceAffectation.codeGroupeAgence.codeDirectionRegionale.nomDirectionRegionale}"/>
                    </p:column>
                    <f:facet name="footer">
                        <p:commandButton id="createButton" icon="ui-icon-plus"   value="# 
 {bundle.Create}" actionListener="#{collaborateurController.prepareCreate}" 
  update=":CollaborateurCreateForm" oncomplete="PF('CollaborateurCreateDialog').show()"/>
                        <p:commandButton id="viewButton"   icon="ui-icon-search" value="# 
 {bundle.View}" update=":CollaborateurViewForm" oncomplete="PF('CollaborateurViewDialog').show()" 
  disabled="#{empty collaborateurController.selected}"/>
                        <p:commandButton id="editButton"   icon="ui-icon-pencil" value="# 
 {bundle.Edit}" update=":CollaborateurEditForm" oncomplete="PF('CollaborateurEditDialog').show()" 
 disabled="#{empty collaborateurController.selected}"/>
                        <p:commandButton id="deleteButton" icon="ui-icon-trash"  value="#            
{bundle.Delete}" actionListener="#{collaborateurController.destroy}" update=":growl,datalist" 
 disabled="#{empty collaborateurController.selected}"/>
                        <p:commandButton id="entretienButton" icon="ui-icon-document" value="# 
 {bundle.Entretien}" action="#{collaborateurController}" disabled="#{empty 
 collaborateurController.selected}"/>
                    </f:facet>
                </p:dataTable>
            </p:panel>
        </h:form>

        <ui:include src="Create.xhtml"/>
        <ui:include src="Edit.xhtml"/>
        <ui:include src="View.xhtml"/>
    </ui:define>
</ui:composition>

</html>

View.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:p="http://primefaces.org/ui">

<ui:composition>

    <p:dialog id="CollaborateurViewDlg" widgetVar="CollaborateurViewDialog" modal="true" resizable="false" appendTo="@(body)" header="#{bundle.ViewCollaborateurTitle}">
        <h:form id="CollaborateurViewForm">
            <h:panelGroup id="display">
                <p:panelGrid columns="2" rendered="#{collaborateurController.selected != null}">
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_idCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.idCollaborateur}" title="#{bundle.ViewCollaborateurTitle_idCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_nomCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.nomCollaborateur}" title="#{bundle.ViewCollaborateurTitle_nomCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_genreCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.genreCollaborateur}" title="#{bundle.ViewCollaborateurTitle_genreCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_dateNaissanceCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.dateNaissanceCollaborateur}" title="#{bundle.ViewCollaborateurTitle_dateNaissanceCollaborateur}">
                        <f:convertDateTime pattern="MM/dd/yyyy" />
                    </h:outputText>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_dateRecrutementCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.dateRecrutementCollaborateur}" title="#{bundle.ViewCollaborateurTitle_dateRecrutementCollaborateur}">
                        <f:convertDateTime pattern="MM/dd/yyyy" />
                    </h:outputText>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_statutCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.statutCollaborateur}" title="#{bundle.ViewCollaborateurTitle_statutCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_echelleCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.echelleCollaborateur}" title="#{bundle.ViewCollaborateurTitle_echelleCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_echelonCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.echelonCollaborateur}" title="#{bundle.ViewCollaborateurTitle_echelonCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_idPosteCollaborateur}"/>
                    <h:outputText value="#{collaborateurController.selected.idPosteCollaborateur.libellePoste}" title="#{bundle.ViewCollaborateurTitle_idPosteCollaborateur}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_codeAgenceAffectation}"/>
                    <h:outputText value="#{collaborateurController.selected.codeAgenceAffectation.nomAgence}" title="#{bundle.ViewCollaborateurTitle_codeAgenceAffectation}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_codeAgenceAffectation}"/>
                    <h:outputText value="# 
   {collaborateurController.selected.codeAgenceAffectation.codeGroupeAgence.nomGroupeAgence}" 
   title="#{bundle.ViewCollaborateurTitle_codeAgenceAffectation}"/>
                    <h:outputText value="#{bundle.ViewCollaborateurLabel_codeAgenceAffectation}"/>
                    <h:outputText value="# 
   {collaborateurController.selected.codeAgenceAffectation.
  codeGroupeAgence.codeDirectionRegionale.nomDirect 
 ionRegionale}" title="#{bundle.ViewCollaborateurTitle_codeAgenceAffectation}"/>
                </p:panelGrid>
                <p:commandButton value="#{bundle.Close}" onclick="CollaborateurViewDialog.hide()"/>
            </h:panelGroup>
            
        </h:form>
    </p:dialog>

</ui:composition>
</html>

CollaborateurController.class :

package com.abb.rh.business.controller;

import com.abb.rh.data.model.Collaborateur;
import com.abb.rh.business.controller.util.JsfUtil;
import com.abb.rh.business.controller.util.JsfUtil.PersistAction;
import com.abb.rh.business.session.CollaborateurFacade;
import com.abb.rh.data.model.Entretien;
import java.io.IOException;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.event.ActionEvent;

@Named("collaborateurController")
@SessionScoped
public class CollaborateurController implements Serializable {

@EJB
private com.abb.rh.business.session.CollaborateurFacade ejbFacade;
private List<Collaborateur> items = null;
private List<Entretien> entretienItems = null;
private Collaborateur selected;
private Entretien entretienCollaborateur;

public CollaborateurController() {
}

public Collaborateur getSelected() {
    return selected;
}

public void setSelected(Collaborateur selected) {
    this.selected = selected;
}

protected void setEmbeddableKeys() {
}

protected void initializeEmbeddableKey() {
}

private CollaborateurFacade getFacade() {
    return ejbFacade;
}

public Collaborateur prepareCreate() {
    selected = new Collaborateur();
    initializeEmbeddableKey();
    return selected;
}

public void create() {
    persist(PersistAction.CREATE, 
ResourceBundle.getBundle("/resources/Bundle").getString("CollaborateurCreated"));
    if (!JsfUtil.isValidationFailed()) {
        items = null;    // Invalidate list of items to trigger re-query.
    }
}

public void update() {
    persist(PersistAction.UPDATE, 
ResourceBundle.getBundle("/resources/Bundle").getString("CollaborateurUpdated"));
}

public void destroy() {
    persist(PersistAction.DELETE, 
 ResourceBundle.getBundle("/resources/Bundle").getString("CollaborateurDeleted"));
    if (!JsfUtil.isValidationFailed()) {
        selected = null; // Remove selection
        items = null;    // Invalidate list of items to trigger re-query.
    }
}

String action;

 //action listener event
 public void attrListener(ActionEvent event){

action = (String)event.getComponent().getAttributes().get("action");

 }

 public void rediriger(String url, Integer collaborateurId) {
  try {
      ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
      ec.redirect(ec.getRequestContextPath() + url);
  } catch (Exception e) {
  }
  }

   public List<Collaborateur> getItems() {
    if (items == null) {
        items = getFacade().findAll();
    }
    return items;
   }

   private void persist(PersistAction persistAction, String successMessage) {
    if (selected != null) {
        setEmbeddableKeys();
        try {
            if (persistAction != PersistAction.DELETE) {
                getFacade().edit(selected);
            } else {
                getFacade().remove(selected);
            }
            JsfUtil.addSuccessMessage(successMessage);
        } catch (EJBException ex) {
            String msg = "";
            Throwable cause = ex.getCause();
            if (cause != null) {
                msg = cause.getLocalizedMessage();
            }
            if (msg.length() > 0) {
                JsfUtil.addErrorMessage(msg);
            } else {
                JsfUtil.addErrorMessage(ex, 
    ResourceBundle.getBundle("/resources/Bundle").getString("PersistenceErrorOccured"));
            }
        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage(ex, 
    ResourceBundle.getBundle("/resources/Bundle").getString("PersistenceErrorOccured"));
        }
    }
}

public Collaborateur getCollaborateur(java.lang.Integer id) {
    return getFacade().find(id);
}

public List<Collaborateur> getItemsAvailableSelectMany() {
    return getFacade().findAll();
}

public List<Collaborateur> getItemsAvailableSelectOne() {
    return getFacade().findAll();
}

public Entretien getEntretienCollaborateur() {
    return entretienCollaborateur;
}

public void setEntretienCollaborateur(Entretien entretienCollaborateur) {
    this.entretienCollaborateur = entretienCollaborateur;
}

public String getAction() {
    return action;
}

public void setAction(String action) {
    this.action = action;
}



@FacesConverter(forClass = Collaborateur.class)
public static class CollaborateurControllerConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        CollaborateurController controller = (CollaborateurController) 
  facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "collaborateurController");
        return controller.getCollaborateur(getKey(value));
    }

    java.lang.Integer getKey(String value) {
        java.lang.Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Integer value) {
        StringBuilder sb = new StringBuilder();
        sb.append(value);
        return sb.toString();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Collaborateur) {
            Collaborateur o = (Collaborateur) object;
            return getStringKey(o.getIdCollaborateur());
        } else {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "object {0} is of type {1}; 
expected type: {2}", new Object[]{object, object.getClass().getName(), 
Collaborateur.class.getName()});
            return null;
        }
    }

}

}

Collaborateur.class

public class Collaborateur implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id_collaborateur")
private Integer idCollaborateur;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "nom_collaborateur")
private String nomCollaborateur;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "genre_collaborateur")
private String genreCollaborateur;
@Basic(optional = false)
@NotNull
@Column(name = "date_naissance_collaborateur")
@Temporal(TemporalType.DATE)
private Date dateNaissanceCollaborateur;
@Basic(optional = false)
@NotNull
@Column(name = "date_recrutement_collaborateur")
@Temporal(TemporalType.DATE)
private Date dateRecrutementCollaborateur;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "statut_collaborateur")
private String statutCollaborateur;
@Basic(optional = false)
@NotNull
@Column(name = "echelle_collaborateur")
private int echelleCollaborateur;
@Basic(optional = false)
@NotNull
@Column(name = "echelon_collaborateur")
private int echelonCollaborateur;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idCollaborateur")
private Collection<Entretien> entretienCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idCollaborateur")
private Collection<NoteEntretien> noteEntretienCollection;
@JoinColumn(name = "id_poste_collaborateur", referencedColumnName = "id_poste")
@ManyToOne
private Poste idPosteCollaborateur;
@JoinColumn(name = "code_agence_affectation", referencedColumnName = "code_agence")
@ManyToOne
private AgenceAffectation codeAgenceAffectation;

Enteretien.class

public class Entretien implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id_entretien")
private Integer idEntretien;
@Basic(optional = false)
@NotNull
@Column(name = "date_entretien")
@Temporal(TemporalType.DATE)
private Date dateEntretien;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "diagnostic_entretien")
private String diagnosticEntretien;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "observations_entretien")
private String observationsEntretien;
@JoinColumn(name = "id_collaborateur", referencedColumnName = "id_collaborateur")
@ManyToOne(optional = false)
private Collaborateur idCollaborateur;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idEntretien")
private Collection<NoteEntretien> noteEntretienCollection;

NoteEntretien.class

public class NoteEntretien implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id_note_entretien")
private Integer idNoteEntretien;
@Basic(optional = false)
@NotNull
@Column(name = "val_note_entretien")
private float valNoteEntretien;
@JoinColumn(name = "id_competence", referencedColumnName = "id_competence")
@ManyToOne(optional = false)
private Competence idCompetence;
@JoinColumn(name = "id_collaborateur", referencedColumnName = "id_collaborateur")
@ManyToOne(optional = false)
private Collaborateur idCollaborateur;
@JoinColumn(name = "id_entretien", referencedColumnName = "id_entretien")
@ManyToOne(optional = false)
private Entretien idEntretien;

It is a complex question. There is no "best solution for this". I just can recommend some ways to implement it.

  1. Prepare a separate template page with the URL and the controller CollaborateurDetailController .
  2. In an init() method of that controller, get the "id" parameter from the URL and get the edited entity by id from "Selected" of that controller. Or, if there is no "id" , then make a new instance.
  3. Change the listener on the edit button on your list to redirect to a page of the new controller with the "id" param, or to an empty page for a create button.

I recommend making separate controllers for detail. In grown projects you will then need to implement complex forms and components in detail. The modal window is a basic solution that in lots of cases you will just have to refactor in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM