package sn.unitech.stock.bean;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import sn.unitech.common.dao.ReadProperties;
import sn.unitech.stock.entity.SysAdherActivite;
import sn.unitech.stock.entity.SysAdherEvent;
import sn.unitech.stock.entity.SysAdherent;
import sn.unitech.stock.entity.SysContrat;
import sn.unitech.stock.entity.SysNiveau;
import sn.unitech.stock.entity.SysPiece;
import sn.unitech.stock.entity.SysSite;
import sn.unitech.stock.metier.SysAdherentM;
import sn.unitech.stock.metier.SysContratM;
import sn.unitech.stock.metier.SysPieceM;
import sn.unitech.stock.metier.SysSiteM;
import sn.unitech.stock.metier.iface.ISysAdherentM;
import sn.unitech.stock.metier.iface.ISysContratM;
import sn.unitech.stock.metier.iface.ISysPieceM;
import sn.unitech.stock.metier.iface.ISysSiteM;
import sn.unitech.stock.modal.Resultat;
@ManagedBean
@ViewScoped
public class SysAdherentBean{
private ISysAdherentM<SysAdherent> iSysAdherentM= new SysAdherentM<SysAdherent>();
private ISysContratM<SysContrat> iSysContratM=new SysContratM<SysContrat>();
private ISysSiteM<SysSite> iSysSiteM=new SysSiteM<SysSite>();
private ISysPieceM<SysPiece> iSysPieceM=new SysPieceM<SysPiece>();
private ReadProperties readProperties=ReadProperties.getReadProperties();
private SysAdherent sysAdherent=new SysAdherent();
private SysContrat sysContrat=new SysContrat();
private SysSite sysSite=new SysSite();
private AllRefs allRefs =new AllRefs();
private CMessages cmessages =new CMessages();
private Cproperties cproperties=new Cproperties();
private CItems citems=new CItems();
public ParamsUser getParamsUser(){
return ((ParamsUser)ToolsBean.getToolsBean().getAttribute(ToolsBean.idParamUser));
}
public void clickPersitAdherent(ActionEvent e){
this.sysAdherent.setSysSite(this.sysSite);
this.sysAdherent.setSysContrat(this.sysContrat);
SysAdherEvent sysAdherEvent;
SysAdherActivite sysAdherActivite;
DataModel<SysAdherEvent> dataAdherEvent=this.citems.getDataAdherEvents();
for(int i=0;i<dataAdherEvent.getRowCount();i++){
dataAdherEvent.setRowIndex(i);
sysAdherEvent=dataAdherEvent.getRowData();
if(sysAdherEvent.isSelected()){
sysAdherEvent.setSysAdherent(this.sysAdherent);
sysAdherEvent.getId().setIdAdherent(this.sysAdherent.getIdAdherent());
this.sysAdherent.getSysAdherEvents().add(sysAdherEvent);
}
}
DataModel<SysAdherActivite> dataAdherActivite=this.citems.getDataAdherActivite();
for(int i=0;i<dataAdherActivite.getRowCount();i++){
dataAdherActivite.setRowIndex(i);
sysAdherActivite=dataAdherActivite.getRowData();
if(sysAdherActivite.isSelected()){
sysAdherActivite.setSysAdherent(this.sysAdherent);
sysAdherActivite.getId().setIdAdherent(this.sysAdherent.getIdAdherent());
this.sysAdherent.getSysAdherActivites().add(sysAdherActivite);
}
}
Resultat result=this.iSysAdherentM.persistAdherent(this.sysAdherent);
if(result.isSucceed()){
this.sysAdherent=new SysAdherent();
this.cmessages.saveMessage=ToolsBean.getToolsBean().getBundleMessage("msgSucces");
}else this.cmessages.saveMessage=ToolsBean.getToolsBean().getBundleMessage("msgEchec");
}
public void clickCancelSysAdherent(ActionEvent e){
this.sysAdherent=new SysAdherent();
}
/****
* GETTERS AND SETTERS
*/
public SysAdherent getSysAdherent() {
return sysAdherent;
}
public void setSysAdherent(SysAdherent sysAdherent) {
this.sysAdherent = sysAdherent;
}
public SysContrat getSysContrat() {
return sysContrat;
}
public void setSysContrat(SysContrat sysContrat) {
this.sysContrat = sysContrat;
}
public SysSite getSysSite() {
return sysSite;
}
public void setSysSite(SysSite sysSite) {
this.sysSite = sysSite;
}
public AllRefs getAllRefs() {
return allRefs;
}
public void setAllRefs(AllRefs allRefs) {
this.allRefs = allRefs;
}
public CMessages getCmessages() {
return cmessages;
}
public void setCmessages(CMessages cmessages) {
this.cmessages = cmessages;
}
public Cproperties getCproperties() {
return cproperties;
}
public void setCproperties(Cproperties cproperties) {
this.cproperties = cproperties;
}
public CItems getCitems() {
return citems;
}
public void setCitems(CItems citems) {
this.citems = citems;
}
public class AllRefs implements Serializable{
private static final long serialVersionUID = -5630333814788995802L;
}
public class CMessages implements Serializable{
private static final long serialVersionUID = 9138586328427820689L;
String saveMessage;
public String getSaveMessage() {
return saveMessage;
}
public void setSaveMessage(String saveMessage) {
this.saveMessage = saveMessage;
}
}
public class Cproperties implements Serializable{
private static final long serialVersionUID = -8443852810437302883L;
}
public class CItems implements Serializable{
private static final long serialVersionUID = -5202741014089469969L;
Map<String,Integer> itemsContrat=null;
Map<String,Integer> itemsSite=null;
Map<String,Integer> itemsNiveau=null;
Map<String,Integer> itemsTypePiece=null;
DataModel<SysAdherEvent> dataAdherEvents;
DataModel<SysAdherActivite> dataAdherActivite;
public CItems() {
}
public Map<String, Integer> getItemsContrat() {
return itemsContrat=itemsContrat==null?iSysContratM.getAllContrat(readProperties.read("Contrat.allContrat")):itemsContrat;
}
public void setItemsContrat(Map<String, Integer> itemsContrat) {
this.itemsContrat = itemsContrat;
}
public Map<String, Integer> getItemsSite() {
return itemsSite=itemsSite==null?iSysSiteM.getAllSite(readProperties.read("Site.allSite")):itemsSite;
}
public void setItemsSite(Map<String, Integer> itemsSite) {
this.itemsSite = itemsSite;
}
public Map<String, Integer> getItemsTypePiece() {
return itemsTypePiece=itemsTypePiece==null?iSysPieceM.findAllTypePiece(readProperties.read("Piece.jpqlAllTypePiece")):itemsTypePiece;
}
public void setItemsTypePiece(Map<String, Integer> itemsTypePiece) {
this.itemsTypePiece = itemsTypePiece;
}
public DataModel<SysAdherEvent> getDataAdherEvents() {
return dataAdherEvents=this.dataAdherEvents==null?new ListDataModel<SysAdherEvent>(iSysAdherentM.getListAdherEvents(readProperties.read("Adherent.allEvents"),getParamsUser().getAdherent())):this.dataAdherEvents;
}
public void setDataAdherEvents(DataModel<SysAdherEvent> dataAdherEvents) {
this.dataAdherEvents = dataAdherEvents;
}
public DataModel<SysAdherActivite> getDataAdherActivite() {
return dataAdherActivite=this.dataAdherActivite==null?new ListDataModel<SysAdherActivite>(iSysAdherentM.getListAdherActivite(readProperties.read("Adherent.allActivite"), getParamsUser().getAdherent())):this.dataAdherActivite;
}
public void setDataAdherActivite(DataModel<SysAdherActivite> dataAdherActivite) {
this.dataAdherActivite = dataAdherActivite;
}
public Map<String, Integer> getItemsNiveau() {
return itemsNiveau=this.itemsNiveau==null?iSysAdherentM.findAllNiveau(readProperties.read("Adherent.allNiveau")):this.itemsNiveau;
}
public void setItemsNiveau(Map<String, Integer> itemsNiveau) {
this.itemsNiveau = itemsNiveau;
}
}
}