@SuppressWarnings("unchecked")
public Resultat saveUnite(SysUnite sysUnite,SysSousUnite sysSousUnite,String idActivite){
EntityManager em=getEntityManager();
EntityTransaction tx= em.getTransaction();
Resultat resultat=new Resultat();
try{
tx.begin();
if((this.find(SysUnite.class,sysUnite.getIdUnite(), em))==null){
SysUnite sysUniteFind=(SysUnite)this.find(SysUnite.class,sysSousUnite.getId().getIdSousUnite(),em);
SysTypUnite sysTypUniteFind=sysUniteFind.getSysTypUnite();
SysTypUnite sysTypUnite= new SysTypUnite(ToolsDao.idTypAutresUnites+"_"+sysTypUniteFind.getIdTypUnite());
sysTypUnite.setLibelle(sysTypUnite.getIdTypUnite().replace("-"," "));
sysTypUnite.setIsInter("False");
sysUnite.setSysTypUnite(sysTypUnite);
/* Verifie si unite existe avant de l'inserer */
if((this.find(SysTypUnite.class,sysUnite.getSysTypUnite().getIdTypUnite(),em))==null){
this.persist(sysUnite.getSysTypUnite(), em);
}
/* Verifie si type unite existe dans l'activite avant de l'inserer */
SysActiviTypUnitId sysActiviTypUnitId=new SysActiviTypUnitId(sysUnite.getSysTypUnite().getIdTypUnite(),Integer.parseInt(idActivite));
if(this.find(SysActiviTypUnit.class,sysActiviTypUnitId, em)==null){
SysActiviTypUnit sysActiviTypUnit=new SysActiviTypUnit();
sysActiviTypUnit.setId(sysActiviTypUnitId);
sysActiviTypUnit.setSysActivite(new SysActivite(Integer.parseInt(idActivite)));
sysActiviTypUnit.setSysTypUnite(sysUnite.getSysTypUnite());
this.persist(sysActiviTypUnit,em);
}
this.persist(sysUnite, em);
sysSousUnite.setSysUnite(sysUnite);
this.persist(sysSousUnite, em);
tx.commit();
resultat.setMessage(ToolsBean.getToolsBean().getBundleMessage("msgSucces"));
resultat.setSucceed(true);
return resultat;
} else {
resultat.setMessage(ToolsBean.getToolsBean().getBundleMessage("Unite.msgUnite")+" "+sysUnite.getIdUnite()+" "+ToolsBean.getToolsBean().getBundleMessage("Unite.msgDejaSaisi"));
resultat.setSucceed(false);
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();
}