package sn.unitech.stock.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.jboss.seam.ui.handler.CommandButtonParameterComponentHandler;
import sn.unitech.common.dao.ReadProperties;
import sn.unitech.stock.entity.SysAccessUser;
import sn.unitech.stock.entity.SysMenu;
import sn.unitech.stock.entity.SysUsers;
import sn.unitech.stock.entity.SysUsersId;
import sn.unitech.stock.metier.SysUsersM;
import sn.unitech.stock.metier.iface.ISysUsersM;
import sn.unitech.stock.modal.Resultat;
@ManagedBean
@ViewScoped
public class SysUsersBean implements Serializable{
private static final long serialVersionUID = -710733258656932695L;
private ReadProperties readProperties=ReadProperties.getReadProperties();
private ISysUsersM sysUsersM= new SysUsersM();
private SysUsers sysUsers=new SysUsers(new SysUsersId());
private CMessages cmessages =new CMessages();
private CItems citems=new CItems();
private Cproperties cproperties = citems;
private AllRefs allRefs=new AllRefs();
public SysUsersBean(){
sysUsers.setIsActiv("True");
}
public ParamsUser getParamsUser(){ //alias
return ((ParamsUser)ToolsBean.getToolsBean().getAttribute(ToolsBean.idParamUser));
}
@SuppressWarnings("unchecked")
public List<SysMenu> getModelMenu(){
return (List<SysMenu>) this.citems.dataModelMenu.getWrappedData();
}
public void saveUser(ActionEvent e){
ParamsUser paramUser=getParamsUser();
this.sysUsers.setId(new SysUsersId(this.sysUsers.getId().getIdUser(),paramUser.adherent.getIdAdherent()));
this.sysUsers.setSysProfil(sysUsersM.findProfilMenu(this.cproperties.getProfilMenu(), null));
boolean response = sysUsersM.saveUser(sysUsers);
if(response){
this.cmessages.setSaveMessage("Utilisateur cree avec succes!");
}
else{
if(sysUsersM.findUserById(sysUsers.getId())!=null){
this.cmessages.setSaveMessage("Login Utilisateur existe deja!");
}else this.cmessages.setSaveMessage("Creation de l'utilisateur echouee.");
}
}
public void changeProfilMenu(ValueChangeEvent event){
this.cproperties.setProfilMenu(Integer.parseInt(event.getNewValue().toString()));
}
public void clickAttribuerMenu(ActionEvent e){
this.cmessages.saveMessage="";
Map<Integer, SysMenu> mapMenu=this.sysUsersM.findAllMenu(readProperties.read("User.jpqlAllMenu"));
this.getModelMenu().clear();
this.getModelMenu().addAll(mapMenu.values());
}
public void clickAnnuler(ActionEvent e){
this.sysUsers=new SysUsers(new SysUsersId("",""));
this.cmessages.saveMessage="";
}
public void clickModification(ActionEvent e){
this.cmessages.saveMessage="";
this.sysUsers=this.citems.dataListSysUsers.getRowData();
this.attribMenu();
}
public void attribMenu(){
Map<Integer, SysMenu> mapMenu=this.sysUsersM.findAllMenu(readProperties.read("User.jpqlAllMenu"));
List<SysAccessUser> listAccessUser=this.sysUsers.getSysAccessUsers();
for(SysAccessUser sysAccesUser:listAccessUser){
if(sysAccesUser.getIsAccept().equalsIgnoreCase("True"))sysAccesUser.getSysMenu().setSelected(true);
else sysAccesUser.getSysMenu().setSelected(false);
mapMenu.put(sysAccesUser.getSysMenu().getIdMenu(), sysAccesUser.getSysMenu());
}
this.getModelMenu().clear();
this.getModelMenu().addAll(mapMenu.values());
}
public void clickUpdate(ActionEvent e){
if(this.sysUsers.getSysProfil()==null){
this.sysUsers.setSysProfil(sysUsersM.findProfilMenu(this.cproperties.getProfilMenu(), null));
this.sysUsers.setId(new SysUsersId(this.sysUsers.getId().getIdUser(),getParamsUser().adherent.getIdAdherent()));
}
Resultat result=this.sysUsersM.updateUser(this.getModelMenu(),this.sysUsers);
this.cmessages.setSaveMessage(result.getMessage());
}
public void clickAnnuleModif(ActionEvent e){
this.cmessages.saveMessage="";
this.getModelMenu().clear();
}
/***
* GETTERS AND SETTERS
*/
public SysUsers getSysUsers() {
return sysUsers;
}
public void setSysUsers(SysUsers sysUsers) {
this.sysUsers = sysUsers;
}
public class AllRefs{
}
public class CMessages{
public String saveMessage;
public String getSaveMessage() {
return saveMessage;
}
public void setSaveMessage(String saveMessage) {
this.saveMessage = saveMessage;
}
}
public class Cproperties{
public Integer profilMenu;
public Integer getProfilMenu() {
return profilMenu;
}
public void setProfilMenu(Integer profilMenu) {
this.profilMenu = profilMenu;
}
}
public class CItems extends Cproperties{
Map<String,Integer> itemsMenu;
Map<String,String> itemsProfilMenu;
DataModel<SysMenu> dataModelMenu;
DataModel<SysUsers> dataListSysUsers=new ListDataModel<SysUsers>(new ArrayList<SysUsers>());
public CItems(){
if(this.getItemsProfilMenu().size()>0){
this.setProfilMenu(this.getItemsProfilMenu().values().iterator().next());
Map param=new HashMap<Integer,Integer>();
param.put(0, this.getProfilMenu());
this.setDataModelMenu(new ListDataModel(sysUsersM.findMenuByProfil(readProperties.read("User.jpqlMenuByProfil"),param)));
this.setItemsMenu(sysUsersM.findAllMenuNotProfil(readProperties.read("User.jpqlAllMenuNotProfil"),param));
}
}
/**
* GETTERS AND SETTERS
*/
public boolean getSizeDataModelMenu(){
return this.dataModelMenu.getRowCount()>0?true:false;
}
@SuppressWarnings("unchecked")
public DataModel<SysMenu> getDataModelMenu(){
return dataModelMenu;
}
public void setDataModelMenu(DataModel<SysMenu> dataModelMenu) {
this.dataModelMenu = dataModelMenu;
}
public Map<String, Integer> getItemsMenu() {
return itemsMenu!=null?itemsMenu:new HashMap<String, Integer>();
}
public void setItemsMenu(Map<String, Integer> itemsMenu) {
this.itemsMenu = itemsMenu;
}
public Map<String, Integer> getItemsProfilMenu(){
return sysUsersM.findAllProfilMenu(readProperties.read("User.jpqlAllProfilMenu"));
}
public void setItemsProfilMenu(Map<String, String> itemsProfilMenu) {
this.itemsProfilMenu = itemsProfilMenu;
}
public DataModel<SysUsers> getDataListSysUsers() {
Map<Integer,String> param=new HashMap<Integer, String>();
param.put(0,getParamsUser().adherent.getIdAdherent());
dataListSysUsers.setWrappedData((List<SysUsers>) sysUsersM.listSysUsers(readProperties.read("User.jpqlListUsers"), param));
return dataListSysUsers;
}
public void setDataListSysUsers(DataModel<SysUsers> dataListSysUsers) {
this.dataListSysUsers = dataListSysUsers;
}
}
/**
* GETTERS AND SETTERS
*
*/
public CMessages getCmessages() {
return cmessages;
}
public void setCmessages(CMessages cmessages) {
this.cmessages = cmessages;
}
public CItems getCitems() {
return citems;
}
public void setCitems(CItems citems) {
this.citems = citems;
}
public Cproperties getCproperties() {
return cproperties;
}
public void setCproperties(Cproperties cproperties) {
this.cproperties = cproperties;
}
public AllRefs getAllRefs() {
return allRefs;
}
public void setAllRefs(AllRefs allRefs) {
this.allRefs = allRefs;
}
}