Coverage Report - jp.co.y_net.amm.dao.AbstractDao
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDao
0%
0/45
0%
0/20
0
 
 1  
 package jp.co.y_net.amm.dao;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import jp.co.y_net.amm.common.AppUtils;
 6  
 
 7  
 import org.hibernate.Query;
 8  
 import org.hibernate.SessionFactory;
 9  
 import org.hibernate.classic.Session;
 10  
 import org.springframework.beans.factory.annotation.Autowired;
 11  
 import org.springframework.beans.factory.annotation.Qualifier;
 12  
 import org.springframework.orm.hibernate3.HibernateTemplate;
 13  
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 14  
 import org.springframework.stereotype.Component;
 15  
 
 16  
 /**
 17  
  * WebopacDao
 18  
  * @version 1.0.0
 19  
  * @author k_ohara
 20  
  */
 21  
 @Component("abstractDao")
 22  0
 abstract class AbstractDao extends HibernateDaoSupport {
 23  
     
 24  
 
 25  
     
 26  
     /**
 27  
      * sessionFactoryのインジェクションを行う
 28  
      * @param sessionFactory
 29  
      */
 30  
     @Autowired(required = true)
 31  
     @Qualifier("sessionFactory_Amm") // ← hibernateContextBase.xmlにて宣言されている
 32  
     public void init(SessionFactory sessionFactory){
 33  0
         super.setSessionFactory(sessionFactory);
 34  0
     }
 35  
     /**
 36  
      * 追加対象オブジェクト
 37  
      * @param item 対象オブジェクト
 38  
      * @param updateusrid 更新者ID
 39  
      */
 40  
     protected void add(AppDto item, Integer updateusrid) {
 41  0
         if(item.getId() != null) {
 42  0
             throw new RuntimeException("追加対象のオブジェクトに主キーが設定されています。");
 43  
         }
 44  
         
 45  
         /* 共通項目の登録 */
 46  0
         if(item != null) {
 47  0
             item.setUpdatedate(AppUtils.createNowLong()); // 更新日付を設定
 48  0
             item.setUpdateusrid(updateusrid); // 更新者IDを設定
 49  0
             item.setDeleted(AppDef.FLASE); // 削除フラグをFalse
 50  
         }
 51  
         
 52  
         try {
 53  0
             HibernateTemplate hibernateTemplate = getHibernateTemplate();
 54  0
             hibernateTemplate.save(item);
 55  0
             hibernateTemplate.flush();
 56  0
         } catch (Exception e) {
 57  0
             throw new RuntimeException(e);
 58  
         }
 59  0
     }
 60  
     /**
 61  
      * 更新処理
 62  
      * @param item 対象オブジェクト
 63  
      * @param updateusrid 更新者ID
 64  
      */
 65  
     protected void update(AppDto item, Integer updateusrid) {
 66  0
         if(item.getId() == null) {
 67  0
             throw new RuntimeException("追加対象のオブジェクトに主キーが設定されていません。");
 68  
         }
 69  
         
 70  
         /* 共通項目 「更新日付」の登録 */
 71  0
         if(item != null) {
 72  0
             item.setUpdatedate(AppUtils.createNowLong()); // 更新日付を設定
 73  0
             item.setUpdateusrid(updateusrid); // 更新者IDを設定
 74  
         }
 75  
         
 76  
         try {
 77  0
             HibernateTemplate hibernateTemplate = getHibernateTemplate();
 78  
 
 79  0
             hibernateTemplate.update(item);
 80  0
             hibernateTemplate.flush();
 81  
             
 82  0
         } catch (Exception e) {
 83  0
             throw new RuntimeException(e);
 84  
         }
 85  0
     }
 86  
     /**
 87  
      * 論理削除処理
 88  
      * @param item 対象オブジェクト
 89  
      * @param updateusrid 更新者ID
 90  
      */
 91  
     protected void removeLogical(AppDto item, Integer updateusrid) {
 92  0
         item.setDeleted(AppDef.TRUE);
 93  0
         this.update(item, updateusrid);
 94  0
     }
 95  
     /**
 96  
      * 物理削除処理
 97  
      */
 98  
     protected void remove(AppDto item) {
 99  0
         throw new RuntimeException("本アプリケーションでは物理削除を行わない。");
 100  
     }
 101  
     
 102  
     /**
 103  
      * 集合関数 MAX を使用して次に採番すべき値を取得する
 104  
      * @param entityname
 105  
      * @param colname
 106  
      * @return
 107  
      */
 108  
     public Integer getNextId(String entityname, String colname) {
 109  0
         return getNextId(entityname, colname, 1);
 110  
     }
 111  0
     public Integer getNextId(String entityname, String colname, int init) {
 112  0
         Session session = getSessionFactory().openSession();
 113  0
         Query query = session.createQuery("SELECT max(" + colname + ") from " + entityname + "");
 114  0
         @SuppressWarnings("rawtypes")
 115  0
         List list = query.list();
 116  0
         Integer currentId = 0;
 117  0
         Integer nextId;
 118  0
         if(list != null && list.isEmpty() == false && (Integer)list.get(0) != null) {
 119  0
             currentId = (Integer)list.get(0);
 120  0
             nextId = currentId + 1;
 121  0
         } else {
 122  0
             nextId = init;
 123  
         }
 124  0
         session.close();
 125  0
         return nextId;
 126  
     }
 127  
 
 128  
 }