@SuppressWarnings("unchecked")
public Resultat saveProduit(SysProduit sysProduit,Stock stock,List listCaracProd){
EntityManager em=getEntityManager();
EntityTransaction tx= em.getTransaction();
Map<Integer,String> param=new HashMap<Integer, String>();
Resultat resultat=new Resultat();
try{
tx.begin();
SysProduit oldProduit=null;
param.clear();
param.put(0,sysProduit.getSysAdherent().getIdAdherent());
param.put(1,sysProduit.getDesignation());
try{
oldProduit=(SysProduit) this.getSingleResult(readProperties.read("Produit.jpqlProdByDesignation"), em, param);
}catch(Exception e){oldProduit=null;}
if(oldProduit!=null)sysProduit=oldProduit;
else {
this.persist(sysProduit, em);
for(int i=0;i < listCaracProd.size();i++){
SysProdCarac sysProdCarac=(SysProdCarac)listCaracProd.get(i);
sysProdCarac.getId().setIdProduit(sysProduit.getIdProduit());
sysProdCarac.setSysProduit(sysProduit);
sysProdCarac.setSysAdherent(sysProduit.getSysAdherent());
SysCarac syscarac=(SysCarac) this.find(SysCarac.class, sysProdCarac.getId().getIdCarac(), em);
sysProdCarac.setSysCarac(syscarac);
this.persist(sysProdCarac, em);
}
}
stock.setSysProduit(sysProduit);
Map<Integer,Object> param1=new HashMap<Integer, Object>();
param1.put(0,stock.getSysRangmnt().getIdRangmnt());
param1.put(1,stock.getSysProduit().getIdProduit());
Stock oldstock=null;
try{
oldstock=(Stock)this.getSingleResult(readProperties.read("Produit.jpqlUniciteRangmnt"), em, param1);
}catch(Exception e){oldstock=null;}
if(oldstock==null){
this.persist(stock,em);
}
tx.commit();
if(oldstock!=null)resultat.setMessage(ToolsBean.getToolsBean().getBundleMessage("Produit.msgProduitExisteRangement"));
else resultat.setMessage(ToolsBean.getToolsBean().getBundleMessage("msgSucces"));
resultat.setSucceed(true);
return resultat;
}
catch(Exception ex){
if((tx!=null)&&(tx.isActive()))tx.rollback();
ex.printStackTrace();
resultat.setMessage(ex.getMessage());
resultat.setSucceed(false);
return resultat;
}
finally{
em.close();
}