package sn.unitech.stock.bean;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.jboss.seam.international.LocaleSelector;
import sn.unitech.common.dao.ReadProperties;
import sn.unitech.stock.entity.SysMenu;
import sn.unitech.stock.entity.SysUsers;
import sn.unitech.stock.entity.SysUsersId;
import sn.unitech.stock.metier.ConnexionM;
import sn.unitech.stock.metier.iface.IConnexionM;
@ManagedBean
@SessionScoped
public class ConnexionBean {
private SysUsers user;
private Cproperties cproperties=new Cproperties();
private IConnexionM iconnexionM=new ConnexionM();
private Menu menu=new Menu();
private static final String NAV_WELCOME = "/welcome";
private static final String NAV_REDIRECT = "?faces-redirect=true";
private static final String NAV_DECONNEXION="/stock/connexion/connexion.unit";
public ConnexionBean() {
user=new SysUsers(new SysUsersId());
user.getId().setIdAdherent("1");
FacesContext context = FacesContext.getCurrentInstance();
Collection<Locale> list=new ArrayList<Locale>();
list.add(Locale.FRENCH);
list.add(Locale.ENGLISH);
context.getApplication().setDefaultLocale(Locale.FRENCH);
context.getApplication().setSupportedLocales(list);
context.getViewRoot().setLocale(Locale.FRENCH);
LocaleSelector.instance().setLocale(Locale.FRENCH);
context.getApplication().setMessageBundle("messages");
}
public String connexion() throws IllegalArgumentException, IllegalAccessException{
SysUsers sysUsers;
if((sysUsers=this.iconnexionM.findUser(user))!=null){
this.user=sysUsers;
this.cproperties.setMessage("");
cproperties.setLogged(iconnexionM.connexion(user,cproperties.getPassword()));
if(!cproperties.isLogged()){
this.cproperties.setMessage("Connexion �chou�e, V�rifier les informations entr�es.");
}else{
ParamsUser paramUser=new ParamsUser();
paramUser.setAdherent(user.getSysAdherent());
paramUser.setSysUser(user);
ToolsBean.getToolsBean().setAttribute(ToolsBean.getToolsBean().idParamUser,paramUser);
getAuthorizedMenu();
this.cproperties.setUseDetail(user.getSysAdherent().getUseDetail());
this.cproperties.setIdDevise(user.getSysAdherent().getSysSite().getSysPays().getSysDevise().getIdDevise());
return NAV_WELCOME+NAV_REDIRECT;
}
}else this.cproperties.setMessage("Connexion �chou�e, V�rifier les informations entr�es.");
return null;
}
@SuppressWarnings("unchecked")
public void getAuthorizedMenu() throws IllegalArgumentException, IllegalAccessException{
Map param=new HashMap();
param.put(0, user.getId().getIdAdherent());
param.put(1, user.getId().getIdUser());
List<SysMenu> listMenu= iconnexionM.getAuthorizedMenu(ReadProperties.getReadProperties().read("User.accessMenu"), param);
java.lang.reflect.Field[] fields=this.menu.getClass().getDeclaredFields();
for(int j=0;j<fields.length;j++){
if(fields[j].getName().startsWith("menu")){
this.menu.getClass().getDeclaredFields()[j].set(this.menu, false);//initialisation
}
}
for(int i=0;i<listMenu.size();i++){
for(int j=0;j<fields.length;j++){
if(fields[j].getName().startsWith("menu")){
if((listMenu.get(i).getIdMenu()+"").equals(fields[j].getName().split("_")[1])){
this.menu.getClass().getDeclaredFields()[j].set(this.menu, true);
}
}
}
}
}
public String deconnexion(){
this.cproperties.setLogged(false);
return NAV_DECONNEXION+NAV_REDIRECT;
}
public static void main(String ar[]) throws IllegalArgumentException, IllegalAccessException{
ConnexionBean con=new ConnexionBean();
con.getUser().getId().setIdAdherent("1");
con.getUser().getId().setIdUser("DIOP");
con.getAuthorizedMenu();
}
public class Cproperties{
boolean logged=false;
String message;
String password;
String useDetail;
String idDevise;
public boolean isLogged() {
return logged;
}
public void setLogged(boolean logged) {
this.logged = logged;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUseDetail() {
return useDetail;
}
public void setUseDetail(String useDetail) {
this.useDetail = useDetail;
}
public String getIdDevise() {
return idDevise;
}
public void setIdDevise(String idDevise) {
this.idDevise = idDevise;
}
public String getRefresh() {
return this.logged?"":"Refresh";
}
}
public class Menu{
public Boolean menu_1=false; //PRODUIT
public Boolean menu_2=false; //COMMANDE
public Boolean menu_3=false; //FOURNISSEUR
public Boolean menu_4=false; //CLIENT
public Boolean menu_5=false; //UTILISATEUR
public Boolean menu_6=false; //OPERATIONS
public Boolean getMenu_1() {
return menu_1;
}
public void setMenu_1(Boolean menu_1) {
this.menu_1 = menu_1;
}
public Boolean getMenu_2() {
return menu_2;
}
public void setMenu_2(Boolean menu_2) {
this.menu_2 = menu_2;
}
public Boolean getMenu_3() {
return menu_3;
}
public void setMenu_3(Boolean menu_3) {
this.menu_3 = menu_3;
}
public Boolean getMenu_4() {
return menu_4;
}
public void setMenu_4(Boolean menu_4) {
this.menu_4 = menu_4;
}
public Boolean getMenu_5() {
return menu_5;
}
public void setMenu_5(Boolean menu_5) {
this.menu_5 = menu_5;
}
public Boolean getMenu_6() {
return menu_6;
}
public void setMenu_6(Boolean menu_6) {
this.menu_6 = menu_6;
}
}
public SysUsers getUser() {
return user;
}
public void setUser(SysUsers user) {
this.user = user;
}
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
public Cproperties getCproperties() {
return cproperties;
}
public void setCproperties(Cproperties cproperties) {
this.cproperties = cproperties;
}
}