2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017,2019 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;
25 import javax.script.SimpleBindings;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.hibernate.Criteria;
29 import org.hibernate.HibernateException;
30 import org.hibernate.Query;
31 import org.hibernate.Session;
32 import org.hibernate.SessionFactory;
33 import org.hibernate.Transaction;
34 import org.hibernate.criterion.Conjunction;
35 import org.hibernate.criterion.Criterion;
36 import org.hibernate.criterion.Disjunction;
37 import org.hibernate.criterion.Projections;
38 import org.hibernate.criterion.Restrictions;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.ClosedLoops;
41 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
42 import org.onap.policy.rest.jpa.PolicyRoles;
43 import org.onap.policy.xacml.api.XACMLErrorConstants;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.context.annotation.Primary;
46 import org.springframework.stereotype.Service;
48 @Service("CommonClassDao")
50 public class CommonClassDaoImpl implements CommonClassDao {
52 private static final Log LOGGER = LogFactory.getLog(CommonClassDaoImpl.class);
55 private static SessionFactory sessionFactory;
59 private CommonClassDaoImpl(SessionFactory sessionFactory) {
60 CommonClassDaoImpl.sessionFactory = sessionFactory;
63 public CommonClassDaoImpl() {
64 // Default Constructor
67 @SuppressWarnings({"unchecked", "rawtypes"})
69 public List<Object> getData(Class className) {
70 Session session = sessionFactory.openSession();
71 List<Object> data = null;
73 Criteria cr = session.createCriteria(className);
75 } catch (Exception e) {
76 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table" + e);
80 } catch (Exception e) {
81 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
82 + "Error While Closing Connection/Statement" + e);
89 @SuppressWarnings({"rawtypes", "unchecked"})
91 public List<Object> getDataById(Class className, String columnName, String key) {
92 Session session = sessionFactory.openSession();
93 List<Object> data = null;
95 Criteria cr = session.createCriteria(className);
96 if (columnName.contains(":") && key.contains(":")) {
97 String[] columns = columnName.split(":");
98 String[] keys = key.split(":");
99 for (int i = 0; i < columns.length; i++) {
100 cr.add(Restrictions.eq(columns[i], keys[i]));
103 cr.add(Restrictions.eq(columnName, key));
106 } catch (Exception e) {
107 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table" + e);
111 } catch (Exception e1) {
112 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
113 + "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));
128 } catch (Exception e) {
129 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table" + e);
133 } catch (Exception e) {
134 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
135 + "Error While Closing Connection/Statement" + e);
142 public void save(Object entity) {
143 Session session = sessionFactory.openSession();
144 Transaction tx = session.beginTransaction();
146 session.persist(entity);
148 } catch (Exception e) {
149 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
150 + "Error While Saving data to Table" + e);
154 } catch (Exception e1) {
155 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
156 + "Error While Closing Connection/Statement" + e1);
163 public void delete(Object entity) {
164 Session session = sessionFactory.openSession();
165 Transaction tx = session.beginTransaction();
167 session.delete(entity);
169 } catch (Exception e) {
170 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
171 + "Error While Deleting data from Table" + e);
175 } catch (Exception e1) {
176 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
177 + "Error While Closing Connection/Statement" + e1);
185 public void update(Object entity) {
186 Session session = sessionFactory.openSession();
187 Transaction tx = session.beginTransaction();
189 session.update(entity);
191 } catch (Exception e) {
192 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
193 + "Error While Updating data to Table" + e);
197 } catch (Exception e1) {
198 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
199 + "Error While Closing Connection/Statement" + e1);
206 @SuppressWarnings({"unchecked", "rawtypes"})
208 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
209 Session session = sessionFactory.openSession();
210 Transaction tx = session.beginTransaction();
211 List<Object> data = null;
213 String[] columnNames = null;
214 if (columnName != null && columnName.contains(":")) {
215 columnNames = columnName.split(":");
217 String[] values = null;
218 if (value != null && value.contains(":")) {
219 values = value.split(":");
222 Criteria cr = session.createCriteria(className);
223 if (columnNames != null && values != null && columnNames.length == values.length) {
224 for (int i = 0; i < columnNames.length; i++) {
225 cr.add(Restrictions.eq(columnNames[i], values[i]));
228 cr.add(Restrictions.eq(columnName, value));
233 } catch (Exception e) {
234 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
235 + "Error While Querying for Duplicate Entries for Table" + e + className);
239 } catch (Exception e1) {
240 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
241 + "Error While Closing Connection/Statement" + e1);
248 @SuppressWarnings("unchecked")
250 public List<Object> getDataByQuery(String query, SimpleBindings params) {
251 Session session = sessionFactory.openSession();
252 Transaction tx = session.beginTransaction();
253 List<Object> data = null;
255 Query hbquery = session.createQuery(query);
256 for (Map.Entry<String, Object> paramPair : params.entrySet()) {
257 if (paramPair.getValue() instanceof java.lang.Long) {
258 hbquery.setLong(paramPair.getKey(), (long) paramPair.getValue());
260 hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
263 data = hbquery.list();
265 } catch (Exception e) {
266 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
267 + "Error While Querying Database Table" + e);
272 } catch (HibernateException e1) {
273 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
274 + "Error While Closing Connection/Statement", e1);
282 public void updateQuery(String query) {
283 Session session = sessionFactory.openSession();
284 Transaction tx = session.beginTransaction();
286 Query hbquery = session.createQuery(query);
287 hbquery.executeUpdate();
289 } catch (Exception e) {
290 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
291 + "Error While Updating Database Table" + e);
295 } catch (Exception e1) {
296 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
297 + "Error While Closing Connection/Statement" + e1);
303 @SuppressWarnings("rawtypes")
305 public Object getEntityItem(Class className, String columnName, String key) {
306 Session session = sessionFactory.openSession();
307 Transaction tx = session.beginTransaction();
310 Criteria cr = session.createCriteria(className);
311 if (columnName.contains(":") && key.contains(":")) {
312 String[] columns = columnName.split(":");
313 String[] keys = key.split(":");
314 for (int i = 0; i < columns.length; i++) {
315 cr.add(Restrictions.eq(columns[i], keys[i]));
318 cr.add(Restrictions.eq(columnName, key));
320 data = cr.list().get(0);
322 } catch (Exception e) {
323 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
324 + "Error While Querying Database Table" + e);
328 } catch (Exception e1) {
329 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
330 + "Error While Closing Connection/Statement" + e1);
337 @SuppressWarnings("unchecked")
339 public List<PolicyRoles> getUserRoles() {
340 Session session = sessionFactory.openSession();
341 Transaction tx = session.beginTransaction();
342 List<PolicyRoles> rolesData = null;
344 Criteria cr = session.createCriteria(PolicyRoles.class);
345 Disjunction disjunction = Restrictions.disjunction();
346 Conjunction conjunction1 = Restrictions.conjunction();
347 conjunction1.add(Restrictions.eq("role", "admin"));
348 Conjunction conjunction2 = Restrictions.conjunction();
349 conjunction2.add(Restrictions.eq("role", "editor"));
350 Conjunction conjunction3 = Restrictions.conjunction();
351 conjunction3.add(Restrictions.eq("role", "guest"));
352 disjunction.add(conjunction1);
353 disjunction.add(conjunction2);
354 disjunction.add(conjunction3);
355 rolesData = cr.add(disjunction).list();
357 } catch (Exception e) {
358 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
359 + "Error While Querying PolicyRoles Table" + e);
363 } catch (Exception e1) {
364 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
365 + "Error While Closing Connection/Statement" + e1);
372 @SuppressWarnings("unchecked")
374 public void updateClAlarms(String clName, String alarms) {
375 Session session = sessionFactory.openSession();
376 List<ClosedLoops> closedloopsdata = null;
377 Transaction tx = session.beginTransaction();
379 Criteria cr = session.createCriteria(ClosedLoops.class);
380 cr.add(Restrictions.eq("closedLoopControlName", clName));
381 closedloopsdata = cr.list();
382 ClosedLoops closedloop = closedloopsdata.get(0);
383 closedloop.setAlarmConditions(alarms);
384 session.update(closedloop);
386 } catch (Exception e) {
387 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
388 + "Error While Updating ClosedLoops Table" + e);
395 @SuppressWarnings("unchecked")
397 public void updateClYaml(String clName, String yaml) {
398 Session session = sessionFactory.openSession();
399 List<ClosedLoops> closedloopsdata = null;
400 Transaction tx = session.beginTransaction();
402 Criteria cr = session.createCriteria(ClosedLoops.class);
403 cr.add(Restrictions.eq("closedLoopControlName", clName));
404 closedloopsdata = cr.list();
405 ClosedLoops closedloop = closedloopsdata.get(0);
406 closedloop.setYaml(yaml);
407 session.update(closedloop);
409 } catch (Exception e) {
410 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
411 + "Error While Updating ClosedLoops Table" + e);
418 @SuppressWarnings("unchecked")
420 public void deleteAll() {
421 Session session = sessionFactory.openSession();
422 Transaction tx = session.beginTransaction();
423 List<ClosedLoops> closedloopsdata = null;
425 Criteria cr = session.createCriteria(ClosedLoops.class);
426 closedloopsdata = cr.list();
428 if (closedloopsdata != null && !closedloopsdata.isEmpty()) {
430 "ClosedLoops exist in the database, we need to delete them in our first step to buildCache().");
431 for (int i = 0; i < closedloopsdata.size(); i++) {
432 ClosedLoops cl = closedloopsdata.get(i);
436 LOGGER.info("No ClosedLoops exist in the database, no need to delete.");
440 } catch (Exception e) {
441 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
442 + "Error while deleting ClosedLoops from the table" + e);
448 @SuppressWarnings({"unchecked"})
450 public List<Object> checkExistingGroupListforUpdate(String groupListValue,
451 String groupNameValue) {
452 Session session = sessionFactory.openSession();
453 Transaction tx = session.beginTransaction();
454 List<Object> data = null;
456 Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
457 cr.add(Restrictions.eq("groupList", groupListValue));
458 Criterion expression = Restrictions.eq("name", groupNameValue);
459 cr.add(Restrictions.not(expression));
462 } catch (Exception e) {
463 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
464 + "Error While Querying for Duplicate Entries for GroupPolicyScopeList Table"
469 } catch (Exception e1) {
470 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
471 + "Error While Closing Connection/Statement" + e1);
479 public List<Object> getMultipleDataOnAddingConjunction(
480 @SuppressWarnings("rawtypes") Class className, String columnName, List<String> data) {
484 public static void setSessionfactory(SessionFactory sessionfactory) {
485 sessionFactory = sessionfactory;