Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / dao / BaseDao.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.aai.esr.dao;
18
19 import io.dropwizard.hibernate.AbstractDAO;
20 import io.dropwizard.util.Generics;
21
22 import org.hibernate.Criteria;
23 import org.hibernate.HibernateException;
24 import org.hibernate.Query;
25 import org.hibernate.Session;
26 import org.hibernate.SessionFactory;
27 import org.hibernate.criterion.Restrictions;
28 import org.onap.aai.esr.exception.ExtsysException;
29 import org.onap.aai.esr.util.HqlFactory;
30
31 import java.util.List;
32 import java.util.Map;
33
34 /**
35  * a base class for Hibernate DAO classes.<br>
36  * provide the common methods to create,delete,update and query data
37  * 
38  * 
39  */
40 public class BaseDao<T> extends AbstractDAO<T> {
41
42   /**
43    * init session.
44    * 
45    * @param sessionFactory session Factory
46    */
47   public BaseDao(SessionFactory sessionFactory) {
48     super(sessionFactory);
49     this.sessionFactory = sessionFactory;
50     this.entityClass = Generics.getTypeParameter(getClass());
51   }
52
53   public String[] excludeProperties;
54   private SessionFactory sessionFactory;
55   protected Session session;
56   private final Class<?> entityClass;
57
58   @Override
59   protected Session currentSession() {
60     return this.session;
61   }
62
63   /**
64    * update entity .
65    * 
66    * @param data the object to update
67    * @throws ExtsysException when db abnormal
68    */
69   public void update(T data, String filter) throws ExtsysException {
70     try {
71       String hql = HqlFactory.getUpdateHql(data, excludeProperties, filter);
72       beginTransaction();
73       Query query = this.session.createQuery(hql);
74       query.executeUpdate();
75       closeTransaction();
76     } catch (Exception error) {
77       transactionRollBack();
78       throw new ExtsysException("", "error while updating data.errorMsg:" + error.getMessage());
79     } finally {
80       closeSession();
81     }
82   }
83
84   /**
85    * delete entity.
86    * 
87    * @param data the object to delete
88    * @throws ExtsysException when db abnormal
89    */
90   public void delete(T data) throws ExtsysException {
91     try {
92       beginTransaction();
93       this.session.delete(data);
94       closeTransaction();
95     } catch (Exception error) {
96       transactionRollBack();
97       throw new ExtsysException("", "error while deleting data.errorMsg:" + error.getMessage());
98     } finally {
99       closeSession();
100     }
101   }
102
103   /**
104    * create entity.
105    * 
106    * @param data the object to create
107    * @return T
108    * @throws ExtsysException when db abnormal
109    * 
110    */
111   public T create(T data) throws ExtsysException {
112     try {
113       beginTransaction();
114       session.save(data);
115       closeTransaction();
116     } catch (HibernateException error) {
117       transactionRollBack();
118       throw new ExtsysException("", "error while creating data.errorMsg:" + error.getMessage());
119     } finally {
120       closeSession();
121     }
122     return data;
123   }
124
125   /**
126    * query entity by condition.
127    * 
128    * @param unionHql query condition.
129    * @return T
130    * @throws ExtsysException when db abnormal
131    * 
132    */
133   public List<T> unionQuery(String unionHql) throws ExtsysException {
134     List<T> data;
135     try {
136       beginTransaction();
137       Query query = this.session.createQuery(unionHql);
138       data = query.list();
139       closeTransaction();
140     } catch (Exception error) {
141       transactionRollBack();
142       throw new ExtsysException("", "error while union query data.errorMsg:" + error.getMessage());
143     } finally {
144       closeSession();
145     }
146     return data;
147   }
148
149   /**
150    * delete entity by condition.
151    * 
152    * @param unionHql delete condition.
153    * @return T
154    * @throws ExtsysException when db abnormal
155    * 
156    */
157   public int unionDelete(String unionHql) throws ExtsysException {
158     int num = 0;
159     try {
160       beginTransaction();
161       Query query = this.session.createQuery(unionHql);
162       num = query.executeUpdate();
163       closeTransaction();
164     } catch (Exception error) {
165       transactionRollBack();
166       throw new ExtsysException("", "error while union query data.errorMsg:" + error.getMessage());
167     } finally {
168       closeSession();
169     }
170     return num;
171   }
172
173   /**
174    * query entity by condition map.
175    * 
176    * @param queryParams the condition map used to query objects
177    * @return List
178    * @throws ExtsysException when db abnormal
179    */
180   @SuppressWarnings("unchecked")
181   public List<T> query(Map<String, String> queryParams) throws ExtsysException {
182     List<T> result = null;
183     try {
184       beginTransaction();
185       Criteria criteria = this.session.createCriteria(entityClass);
186       for (String key : queryParams.keySet()) {
187         criteria.add(Restrictions.eq(key, queryParams.get(key)));
188       }
189       result = (List<T>) criteria.list();
190       closeTransaction();
191     } catch (HibernateException error) {
192       throw new ExtsysException("", "error while querying data.errorMsg:" + error.getMessage());
193     } finally {
194       closeSession();
195     }
196     return result;
197   }
198
199   protected void beginTransaction() {
200     this.session = this.sessionFactory.openSession();
201     this.session.beginTransaction();
202   }
203
204   protected void closeTransaction() {
205     this.session.getTransaction().commit();
206   }
207
208   protected void closeSession() {
209     this.session.close();
210   }
211
212   protected void transactionRollBack() {
213     this.session.getTransaction().rollback();
214   }
215
216 }