2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.daoimpl;
23 import java.util.List;
26 import javax.script.SimpleBindings;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.hibernate.Criteria;
31 import org.hibernate.HibernateException;
32 import org.hibernate.Query;
33 import org.hibernate.Session;
34 import org.hibernate.SessionFactory;
35 import org.hibernate.Transaction;
36 import org.hibernate.criterion.Conjunction;
37 import org.hibernate.criterion.Criterion;
38 import org.hibernate.criterion.Disjunction;
39 import org.hibernate.criterion.Projections;
40 import org.hibernate.criterion.Restrictions;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.ClosedLoops;
43 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
44 import org.onap.policy.rest.jpa.PolicyRoles;
45 import org.onap.policy.xacml.api.XACMLErrorConstants;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.stereotype.Service;
49 @Service("CommonClassDao")
50 public class CommonClassDaoImpl implements CommonClassDao{
52 private static final Log LOGGER = LogFactory.getLog(CommonClassDaoImpl.class);
55 private static SessionFactory sessionFactory;
58 private CommonClassDaoImpl(SessionFactory sessionFactory){
59 CommonClassDaoImpl.sessionFactory = sessionFactory;
62 public CommonClassDaoImpl(){
66 @SuppressWarnings({ "unchecked", "rawtypes" })
68 public List<Object> getData(Class className) {
69 Session session = sessionFactory.openSession();
70 List<Object> data = null;
72 Criteria cr = session.createCriteria(className);
75 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
80 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
87 @SuppressWarnings({ "rawtypes", "unchecked" })
89 public List<Object> getDataById(Class className, String columnName, String key) {
90 Session session = sessionFactory.openSession();
91 List<Object> data = null;
93 Criteria cr = session.createCriteria(className);
94 if(columnName.contains(":") && key.contains(":")){
95 String[] columns = columnName.split(":");
96 String[] keys = key.split(":");
97 for(int i=0; i < columns.length; i++){
98 cr.add(Restrictions.eq(columns[i], keys[i]));
101 cr.add(Restrictions.eq(columnName, key));
104 } catch (Exception e) {
105 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
109 }catch(Exception e1){
110 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
116 @SuppressWarnings({ "unchecked", "rawtypes" })
118 public List<String> getDataByColumn(Class className, String columnName) {
119 Session session = sessionFactory.openSession();
120 List<String> data = null;
122 Criteria cr = session.createCriteria(className);
123 cr.setProjection(Projections.property(columnName));
126 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
131 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
138 public void save(Object entity) {
139 Session session = sessionFactory.openSession();
140 Transaction tx = session.beginTransaction();
142 session.persist(entity);
145 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table"+e);
149 }catch(Exception e1){
150 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
157 public void delete(Object entity) {
158 Session session = sessionFactory.openSession();
159 Transaction tx = session.beginTransaction();
161 session.delete(entity);
164 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table"+e);
168 }catch(Exception e1){
169 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
177 public void update(Object entity) {
178 Session session = sessionFactory.openSession();
179 Transaction tx = session.beginTransaction();
181 session.update(entity);
184 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table"+e);
188 }catch(Exception e1){
189 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
196 @SuppressWarnings({ "unchecked", "rawtypes" })
198 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
199 Session session = sessionFactory.openSession();
200 Transaction tx = session.beginTransaction();
201 List<Object> data = null;
203 String[] columnNames = null;
204 if(columnName != null && columnName.contains(":")){
205 columnNames = columnName.split(":");
207 String[] values = null;
208 if(value != null && value.contains(":")){
209 values = value.split(":");
212 Criteria cr = session.createCriteria(className);
213 if(columnNames != null && values != null && columnNames.length == values.length){
214 for (int i = 0; i < columnNames.length; i++){
215 cr.add(Restrictions.eq(columnNames[i],values[i]));
218 cr.add(Restrictions.eq(columnName,value));
223 } catch (Exception e) {
224 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"+e + className);
228 }catch(Exception e1){
229 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
236 @SuppressWarnings("unchecked")
238 public List<Object> getDataByQuery(String query, SimpleBindings params) {
239 Session session = sessionFactory.openSession();
240 Transaction tx = session.beginTransaction();
241 List<Object> data = null;
243 Query hbquery = session.createQuery(query);
244 for (Map.Entry<String, Object> paramPair : params.entrySet()) {
245 hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
247 data = hbquery.list();
249 } catch (Exception e) {
250 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
255 }catch(HibernateException e1){
256 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement",e1);
264 public void updateQuery(String query) {
265 Session session = sessionFactory.openSession();
266 Transaction tx = session.beginTransaction();
268 Query hbquery = session.createQuery(query);
269 hbquery.executeUpdate();
271 } catch (Exception e) {
272 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e);
276 }catch(Exception e1){
277 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
283 @SuppressWarnings("rawtypes")
285 public Object getEntityItem(Class className, String columnName, String key) {
286 Session session = sessionFactory.openSession();
287 Transaction tx = session.beginTransaction();
290 Criteria cr = session.createCriteria(className);
291 if(columnName.contains(":") && key.contains(":")){
292 String[] columns = columnName.split(":");
293 String[] keys = key.split(":");
294 for(int i=0; i < columns.length; i++){
295 cr.add(Restrictions.eq(columns[i], keys[i]));
298 cr.add(Restrictions.eq(columnName, key));
300 data = cr.list().get(0);
302 } catch (Exception e) {
303 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
307 }catch(Exception e1){
308 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
315 @SuppressWarnings("unchecked")
317 public List<PolicyRoles> getUserRoles() {
318 Session session = sessionFactory.openSession();
319 Transaction tx = session.beginTransaction();
320 List<PolicyRoles> rolesData = null;
322 Criteria cr = session.createCriteria(PolicyRoles.class);
323 Disjunction disjunction = Restrictions.disjunction();
324 Conjunction conjunction1 = Restrictions.conjunction();
325 conjunction1.add(Restrictions.eq("role", "admin"));
326 Conjunction conjunction2 = Restrictions.conjunction();
327 conjunction2.add(Restrictions.eq("role", "editor"));
328 Conjunction conjunction3 = Restrictions.conjunction();
329 conjunction3.add(Restrictions.eq("role", "guest"));
330 disjunction.add(conjunction1);
331 disjunction.add(conjunction2);
332 disjunction.add(conjunction3);
333 rolesData = cr.add(disjunction).list();
335 } catch (Exception e) {
336 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);
340 }catch(Exception e1){
341 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
348 @SuppressWarnings("unchecked")
350 public void updateClAlarms(String clName, String alarms) {
351 Session session = sessionFactory.openSession();
352 List<ClosedLoops> closedloopsdata = null;
353 Transaction tx = session.beginTransaction();
355 Criteria cr = session.createCriteria(ClosedLoops.class);
356 cr.add(Restrictions.eq("closedLoopControlName",clName));
357 closedloopsdata = cr.list();
358 ClosedLoops closedloop = closedloopsdata.get(0);
359 closedloop.setAlarmConditions(alarms);
360 session.update(closedloop);
363 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);
370 @SuppressWarnings("unchecked")
372 public void updateClYaml(String clName, String yaml) {
373 Session session = sessionFactory.openSession();
374 List<ClosedLoops> closedloopsdata = null;
375 Transaction tx = session.beginTransaction();
377 Criteria cr = session.createCriteria(ClosedLoops.class);
378 cr.add(Restrictions.eq("closedLoopControlName",clName));
379 closedloopsdata = cr.list();
380 ClosedLoops closedloop = closedloopsdata.get(0);
381 closedloop.setYaml(yaml);
382 session.update(closedloop);
385 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);
392 @SuppressWarnings("unchecked")
394 public void deleteAll() {
395 Session session = sessionFactory.openSession();
396 Transaction tx = session.beginTransaction();
397 List<ClosedLoops> closedloopsdata = null;
399 Criteria cr = session.createCriteria(ClosedLoops.class);
400 closedloopsdata = cr.list();
402 if(closedloopsdata!=null && !closedloopsdata.isEmpty()) {
403 LOGGER.info("ClosedLoops exist in the database, we need to delete them in our first step to buildCache().");
404 for(int i=0; i < closedloopsdata.size(); i++) {
405 ClosedLoops cl = closedloopsdata.get(i);
409 LOGGER.info("No ClosedLoops exist in the database, no need to delete.");
413 }catch(Exception e) {
414 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while deleting ClosedLoops from the table"+e);
420 @SuppressWarnings({ "unchecked"})
422 public List<Object> checkExistingGroupListforUpdate(String groupListValue, String groupNameValue) {
423 Session session = sessionFactory.openSession();
424 Transaction tx = session.beginTransaction();
425 List<Object> data = null;
427 Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
428 cr.add(Restrictions.eq("groupList",groupListValue));
429 Criterion expression = Restrictions.eq("name", groupNameValue);
430 cr.add(Restrictions.not(expression));
433 } catch (Exception e) {
434 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for GroupPolicyScopeList Table"+e);
438 }catch(Exception e1){
439 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
447 public List<Object> getMultipleDataOnAddingConjunction(@SuppressWarnings("rawtypes") Class className, String columnName, List<String> data) {
451 public static void setSessionfactory(SessionFactory sessionfactory) {
452 sessionFactory = sessionfactory;