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.context.annotation.Primary;
48 import org.springframework.stereotype.Service;
50 @Service("CommonClassDao")
52 public class CommonClassDaoImpl implements CommonClassDao{
54 private static final Log LOGGER = LogFactory.getLog(CommonClassDaoImpl.class);
57 private static SessionFactory sessionFactory;
61 private CommonClassDaoImpl(SessionFactory sessionFactory){
62 CommonClassDaoImpl.sessionFactory = sessionFactory;
65 public CommonClassDaoImpl(){
69 @SuppressWarnings({ "unchecked", "rawtypes" })
71 public List<Object> getData(Class className) {
72 Session session = sessionFactory.openSession();
73 List<Object> data = null;
75 Criteria cr = session.createCriteria(className);
78 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
83 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
90 @SuppressWarnings({ "rawtypes", "unchecked" })
92 public List<Object> getDataById(Class className, String columnName, String key) {
93 Session session = sessionFactory.openSession();
94 List<Object> data = null;
96 Criteria cr = session.createCriteria(className);
97 if(columnName.contains(":") && key.contains(":")){
98 String[] columns = columnName.split(":");
99 String[] keys = key.split(":");
100 for(int i=0; i < columns.length; i++){
101 cr.add(Restrictions.eq(columns[i], keys[i]));
104 cr.add(Restrictions.eq(columnName, key));
107 } catch (Exception e) {
108 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
112 }catch(Exception e1){
113 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
119 @SuppressWarnings({ "unchecked", "rawtypes" })
121 public List<String> getDataByColumn(Class className, String columnName) {
122 Session session = sessionFactory.openSession();
123 List<String> data = null;
125 Criteria cr = session.createCriteria(className);
126 cr.setProjection(Projections.property(columnName));
129 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
134 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
141 public void save(Object entity) {
142 Session session = sessionFactory.openSession();
143 Transaction tx = session.beginTransaction();
145 session.persist(entity);
148 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table"+e);
152 }catch(Exception e1){
153 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
160 public void delete(Object entity) {
161 Session session = sessionFactory.openSession();
162 Transaction tx = session.beginTransaction();
164 session.delete(entity);
167 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table"+e);
171 }catch(Exception e1){
172 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
180 public void update(Object entity) {
181 Session session = sessionFactory.openSession();
182 Transaction tx = session.beginTransaction();
184 session.update(entity);
187 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table"+e);
191 }catch(Exception e1){
192 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
199 @SuppressWarnings({ "unchecked", "rawtypes" })
201 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
202 Session session = sessionFactory.openSession();
203 Transaction tx = session.beginTransaction();
204 List<Object> data = null;
206 String[] columnNames = null;
207 if(columnName != null && columnName.contains(":")){
208 columnNames = columnName.split(":");
210 String[] values = null;
211 if(value != null && value.contains(":")){
212 values = value.split(":");
215 Criteria cr = session.createCriteria(className);
216 if(columnNames != null && values != null && columnNames.length == values.length){
217 for (int i = 0; i < columnNames.length; i++){
218 cr.add(Restrictions.eq(columnNames[i],values[i]));
221 cr.add(Restrictions.eq(columnName,value));
226 } catch (Exception e) {
227 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"+e + className);
231 }catch(Exception e1){
232 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
239 @SuppressWarnings("unchecked")
241 public List<Object> getDataByQuery(String query, SimpleBindings params) {
242 Session session = sessionFactory.openSession();
243 Transaction tx = session.beginTransaction();
244 List<Object> data = null;
246 Query hbquery = session.createQuery(query);
247 for (Map.Entry<String, Object> paramPair : params.entrySet()) {
248 if(paramPair.getValue() instanceof java.lang.Long){
249 hbquery.setLong(paramPair.getKey(), (long) paramPair.getValue());
252 hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
255 data = hbquery.list();
257 } catch (Exception e) {
258 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
263 }catch(HibernateException e1){
264 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement",e1);
272 public void updateQuery(String query) {
273 Session session = sessionFactory.openSession();
274 Transaction tx = session.beginTransaction();
276 Query hbquery = session.createQuery(query);
277 hbquery.executeUpdate();
279 } catch (Exception e) {
280 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e);
284 }catch(Exception e1){
285 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
291 @SuppressWarnings("rawtypes")
293 public Object getEntityItem(Class className, String columnName, String key) {
294 Session session = sessionFactory.openSession();
295 Transaction tx = session.beginTransaction();
298 Criteria cr = session.createCriteria(className);
299 if(columnName.contains(":") && key.contains(":")){
300 String[] columns = columnName.split(":");
301 String[] keys = key.split(":");
302 for(int i=0; i < columns.length; i++){
303 cr.add(Restrictions.eq(columns[i], keys[i]));
306 cr.add(Restrictions.eq(columnName, key));
308 data = cr.list().get(0);
310 } catch (Exception e) {
311 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
315 }catch(Exception e1){
316 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
323 @SuppressWarnings("unchecked")
325 public List<PolicyRoles> getUserRoles() {
326 Session session = sessionFactory.openSession();
327 Transaction tx = session.beginTransaction();
328 List<PolicyRoles> rolesData = null;
330 Criteria cr = session.createCriteria(PolicyRoles.class);
331 Disjunction disjunction = Restrictions.disjunction();
332 Conjunction conjunction1 = Restrictions.conjunction();
333 conjunction1.add(Restrictions.eq("role", "admin"));
334 Conjunction conjunction2 = Restrictions.conjunction();
335 conjunction2.add(Restrictions.eq("role", "editor"));
336 Conjunction conjunction3 = Restrictions.conjunction();
337 conjunction3.add(Restrictions.eq("role", "guest"));
338 disjunction.add(conjunction1);
339 disjunction.add(conjunction2);
340 disjunction.add(conjunction3);
341 rolesData = cr.add(disjunction).list();
343 } catch (Exception e) {
344 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);
348 }catch(Exception e1){
349 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
356 @SuppressWarnings("unchecked")
358 public void updateClAlarms(String clName, String alarms) {
359 Session session = sessionFactory.openSession();
360 List<ClosedLoops> closedloopsdata = null;
361 Transaction tx = session.beginTransaction();
363 Criteria cr = session.createCriteria(ClosedLoops.class);
364 cr.add(Restrictions.eq("closedLoopControlName",clName));
365 closedloopsdata = cr.list();
366 ClosedLoops closedloop = closedloopsdata.get(0);
367 closedloop.setAlarmConditions(alarms);
368 session.update(closedloop);
371 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);
378 @SuppressWarnings("unchecked")
380 public void updateClYaml(String clName, String yaml) {
381 Session session = sessionFactory.openSession();
382 List<ClosedLoops> closedloopsdata = null;
383 Transaction tx = session.beginTransaction();
385 Criteria cr = session.createCriteria(ClosedLoops.class);
386 cr.add(Restrictions.eq("closedLoopControlName",clName));
387 closedloopsdata = cr.list();
388 ClosedLoops closedloop = closedloopsdata.get(0);
389 closedloop.setYaml(yaml);
390 session.update(closedloop);
393 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);
400 @SuppressWarnings("unchecked")
402 public void deleteAll() {
403 Session session = sessionFactory.openSession();
404 Transaction tx = session.beginTransaction();
405 List<ClosedLoops> closedloopsdata = null;
407 Criteria cr = session.createCriteria(ClosedLoops.class);
408 closedloopsdata = cr.list();
410 if(closedloopsdata!=null && !closedloopsdata.isEmpty()) {
411 LOGGER.info("ClosedLoops exist in the database, we need to delete them in our first step to buildCache().");
412 for(int i=0; i < closedloopsdata.size(); i++) {
413 ClosedLoops cl = closedloopsdata.get(i);
417 LOGGER.info("No ClosedLoops exist in the database, no need to delete.");
421 }catch(Exception e) {
422 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while deleting ClosedLoops from the table"+e);
428 @SuppressWarnings({ "unchecked"})
430 public List<Object> checkExistingGroupListforUpdate(String groupListValue, String groupNameValue) {
431 Session session = sessionFactory.openSession();
432 Transaction tx = session.beginTransaction();
433 List<Object> data = null;
435 Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
436 cr.add(Restrictions.eq("groupList",groupListValue));
437 Criterion expression = Restrictions.eq("name", groupNameValue);
438 cr.add(Restrictions.not(expression));
441 } catch (Exception e) {
442 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for GroupPolicyScopeList Table"+e);
446 }catch(Exception e1){
447 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
455 public List<Object> getMultipleDataOnAddingConjunction(@SuppressWarnings("rawtypes") Class className, String columnName, List<String> data) {
459 public static void setSessionfactory(SessionFactory sessionfactory) {
460 sessionFactory = sessionfactory;