Package sn.unitech.stock.dao

Source Code of sn.unitech.stock.dao.SysUniteDao

package sn.unitech.stock.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import sn.unitech.stock.bean.ToolsBean;
import sn.unitech.stock.dao.iface.ISysUniteDao;
import sn.unitech.stock.entity.SysActiviTypUnit;
import sn.unitech.stock.entity.SysActiviTypUnitId;
import sn.unitech.stock.entity.SysActivite;
import sn.unitech.stock.entity.SysSousUnite;
import sn.unitech.stock.entity.SysTypUnite;
import sn.unitech.stock.entity.SysUnite;
import sn.unitech.stock.modal.Resultat;

public class SysUniteDao extends DataAccess implements ISysUniteDao{
 
  public SysUniteDao(){
   
  }

  @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()
    }
  }
 
}
TOP

Related Classes of sn.unitech.stock.dao.SysUniteDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.