2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest.daoimpl;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
29 import javax.script.SimpleBindings;
31 import org.hibernate.Criteria;
32 import org.hibernate.HibernateException;
33 import org.hibernate.Query;
34 import org.hibernate.Session;
35 import org.hibernate.SessionFactory;
36 import org.hibernate.Transaction;
37 import org.hibernate.criterion.Conjunction;
38 import org.hibernate.criterion.Disjunction;
39 import org.hibernate.criterion.Projections;
40 import org.hibernate.criterion.Restrictions;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.rest.dao.CommonClassDao;
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.Component;
50 public class PolicyValidationDaoImpl implements CommonClassDao {
52 private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationDaoImpl.class);
53 private static final String DB_CONNECTION_CLOSING_ERROR = "Error While Closing Connection/Statement";
54 private static final String DBTABLE_QUERY_ERROR = "Error While Querying Table";
55 private static SessionFactory sessionfactory;
58 private PolicyValidationDaoImpl(SessionFactory sessionfactory) {
59 PolicyValidationDaoImpl.sessionfactory = sessionfactory;
62 public PolicyValidationDaoImpl() {
63 // Default Constructor
66 public static SessionFactory getSessionfactory() {
67 return sessionfactory;
70 public static void setSessionfactory(SessionFactory sessionfactory) {
71 PolicyValidationDaoImpl.sessionfactory = sessionfactory;
74 @SuppressWarnings({ "unchecked", "rawtypes" })
76 public List<Object> getData(Class className) {
77 Session session = sessionfactory.openSession();
78 List<Object> data = null;
80 Criteria cr = session.createCriteria(className);
82 } catch (Exception e) {
83 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
87 } catch (Exception e) {
88 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e);
95 @SuppressWarnings({ "rawtypes", "unchecked" })
97 public List<Object> getDataById(Class className, String columnName, String key) {
98 Session session = sessionfactory.openSession();
99 List<Object> data = null;
101 Criteria cr = session.createCriteria(className);
102 if (columnName.contains(":") && key.contains(":")) {
103 String[] columns = columnName.split(":");
104 String[] keys = key.split(":");
105 for (int i = 0; i < columns.length; i++) {
106 cr.add(Restrictions.eq(columns[i], keys[i]));
109 cr.add(Restrictions.eq(columnName, key));
112 } catch (Exception e) {
113 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
117 } catch (Exception e1) {
118 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
125 public void save(Object entity) {
126 Session session = sessionfactory.openSession();
127 Transaction tx = session.beginTransaction();
129 session.persist(entity);
131 } catch (Exception e) {
132 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table" + e);
136 } catch (Exception e1) {
137 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
144 public void delete(Object entity) {
145 Session session = sessionfactory.openSession();
146 Transaction tx = session.beginTransaction();
148 session.delete(entity);
150 } catch (Exception e) {
151 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table" + e);
155 } catch (Exception e1) {
156 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
164 public void update(Object entity) {
165 Session session = sessionfactory.openSession();
166 Transaction tx = session.beginTransaction();
168 session.update(entity);
170 } catch (Exception e) {
171 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table" + e);
175 } catch (Exception e1) {
176 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
183 @SuppressWarnings({ "unchecked", "rawtypes" })
185 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
186 Session session = sessionfactory.openSession();
187 Transaction tx = session.beginTransaction();
188 List<Object> data = null;
189 String[] columnNames = null;
190 if (columnName != null && columnName.contains(":")) {
191 columnNames = columnName.split(":");
193 String[] values = null;
194 if (value != null && value.contains(":")) {
195 values = value.split(":");
198 Criteria cr = session.createCriteria(className);
199 if (columnNames != null && values != null && columnNames.length == values.length) {
200 for (int i = 0; i < columnNames.length; i++) {
201 cr.add(Restrictions.eq(columnNames[i], values[i]));
204 cr.add(Restrictions.eq(columnName, value));
208 } catch (Exception e) {
209 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"
214 } catch (Exception e1) {
215 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
222 @SuppressWarnings("unchecked")
224 public List<PolicyRoles> getUserRoles() {
225 Session session = sessionfactory.openSession();
226 Transaction tx = session.beginTransaction();
227 List<PolicyRoles> rolesData = null;
229 final Criteria cr = session.createCriteria(PolicyRoles.class);
230 final Disjunction disjunction = Restrictions.disjunction();
231 Conjunction conjunction1 = Restrictions.conjunction();
232 conjunction1.add(Restrictions.eq("role", "admin"));
233 Conjunction conjunction2 = Restrictions.conjunction();
234 conjunction2.add(Restrictions.eq("role", "editor"));
235 Conjunction conjunction3 = Restrictions.conjunction();
236 conjunction3.add(Restrictions.eq("role", "guest"));
237 disjunction.add(conjunction1);
238 disjunction.add(conjunction2);
239 disjunction.add(conjunction3);
240 rolesData = cr.add(disjunction).list();
242 } catch (Exception e) {
243 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table" + e);
247 } catch (Exception e1) {
248 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
256 public List<Object> checkExistingGroupListforUpdate(String arg0, String arg1) {
257 return Collections.emptyList();
262 public void deleteAll() {
263 // Do nothing because this method is not used and is a placeholder to avoid 'Unimplemented Method' error
267 @SuppressWarnings("unchecked")
269 public List<Object> getDataByQuery(String query, SimpleBindings params) {
270 Session session = sessionfactory.openSession();
271 Transaction tx = session.beginTransaction();
272 List<Object> data = null;
274 Query hbquery = session.createQuery(query);
275 for (Map.Entry<String, Object> paramPair : params.entrySet()) {
276 if (paramPair.getValue() instanceof java.lang.Long) {
277 hbquery.setLong(paramPair.getKey(), (long) paramPair.getValue());
279 hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
282 data = hbquery.list();
284 } catch (Exception e) {
285 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
290 } catch (HibernateException e1) {
291 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
298 @SuppressWarnings("rawtypes")
300 public Object getEntityItem(Class className, String columnName, String key) {
301 Session session = sessionfactory.openSession();
302 Transaction tx = session.beginTransaction();
305 Criteria cr = session.createCriteria(className);
306 if (columnName.contains(":") && key.contains(":")) {
307 String[] columns = columnName.split(":");
308 String[] keys = key.split(":");
309 for (int i = 0; i < columns.length; i++) {
310 cr.add(Restrictions.eq(columns[i], keys[i]));
313 cr.add(Restrictions.eq(columnName, key));
315 data = cr.list().get(0);
317 } catch (Exception e) {
318 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
322 } catch (Exception e1) {
323 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
331 public void updateClAlarms(String arg0, String arg1) {
332 // Do nothing because this method is not used and is a placeholder to avoid 'Unimplemented Method' error
337 public void updateClYaml(String arg0, String arg1) {
338 // Do nothing because this method is not used and is a placeholder to avoid 'Unimplemented Method' error
343 public void updateQuery(String query) {
344 Session session = sessionfactory.openSession();
345 Transaction tx = session.beginTransaction();
347 Query hbquery = session.createQuery(query);
348 hbquery.executeUpdate();
350 } catch (Exception e) {
351 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table" + e);
355 } catch (Exception e1) {
356 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
363 @SuppressWarnings({ "rawtypes", "unchecked" })
365 public List<String> getDataByColumn(Class className, String columnName) {
366 Session session = sessionfactory.openSession();
367 List<String> data = null;
369 Criteria cr = session.createCriteria(className);
370 cr.setProjection(Projections.property(columnName));
372 } catch (Exception e) {
373 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
377 } catch (Exception e) {
378 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e);
385 @SuppressWarnings({ "rawtypes", "unchecked" })
387 public List<Object> getMultipleDataOnAddingConjunction(Class className, String columnName, List<String> data) {
388 Session session = sessionfactory.openSession();
389 Transaction tx = session.beginTransaction();
390 List<Object> entityData = null;
392 Criteria cr = session.createCriteria(className);
393 Disjunction disjunction = Restrictions.disjunction();
394 List<Conjunction> conjunctionList = new ArrayList<>();
395 String[] columNames = columnName.split(":");
396 for (int i = 0; i < data.size(); i++) {
397 String[] entiySplit = data.get(i).split(":");
398 Conjunction conjunction = Restrictions.conjunction();
399 conjunction.add(Restrictions.eq(columNames[0], entiySplit[0]));
400 conjunction.add(Restrictions.eq(columNames[1], entiySplit[1]));
401 conjunctionList.add(conjunction);
403 for (int j = 0; j < conjunctionList.size(); j++) {
404 disjunction.add(conjunctionList.get(j));
406 entityData = cr.add(disjunction).list();
408 } catch (Exception e) {
409 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + className + e);
413 } catch (Exception e1) {
414 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);