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=========================================================
20 package org.onap.policy.rest.daoimpl;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
27 import javax.script.SimpleBindings;
29 import org.hibernate.Criteria;
30 import org.hibernate.HibernateException;
31 import org.hibernate.Query;
32 import org.hibernate.Session;
33 import org.hibernate.SessionFactory;
34 import org.hibernate.Transaction;
35 import org.hibernate.criterion.Conjunction;
36 import org.hibernate.criterion.Disjunction;
37 import org.hibernate.criterion.Projections;
38 import org.hibernate.criterion.Restrictions;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.rest.dao.CommonClassDao;
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.stereotype.Component;
48 public class PolicyValidationDaoImpl implements CommonClassDao{
50 private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationDaoImpl.class);
51 private static final String DB_CONNECTION_CLOSING_ERROR = "Error While Closing Connection/Statement";
52 private static final String DBTABLE_QUERY_ERROR = "Error While Querying Table";
53 private static SessionFactory sessionfactory;
55 public static SessionFactory getSessionfactory() {
56 return sessionfactory;
59 public static void setSessionfactory(SessionFactory sessionfactory) {
60 PolicyValidationDaoImpl.sessionfactory = sessionfactory;
64 private PolicyValidationDaoImpl(SessionFactory sessionfactory){
65 PolicyValidationDaoImpl.sessionfactory = sessionfactory;
68 public PolicyValidationDaoImpl(){
72 @SuppressWarnings({ "unchecked", "rawtypes" })
74 public List<Object> getData(Class className) {
75 Session session = sessionfactory.openSession();
76 List<Object> data = null;
78 Criteria cr = session.createCriteria(className);
81 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
86 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR+e);
93 @SuppressWarnings({ "rawtypes", "unchecked" })
95 public List<Object> getDataById(Class className, String columnName, String key) {
96 Session session = sessionfactory.openSession();
97 List<Object> data = null;
99 Criteria cr = session.createCriteria(className);
100 if(columnName.contains(":") && key.contains(":")){
101 String[] columns = columnName.split(":");
102 String[] keys = key.split(":");
103 for(int i=0; i < columns.length; i++){
104 cr.add(Restrictions.eq(columns[i], keys[i]));
107 cr.add(Restrictions.eq(columnName, key));
110 } catch (Exception e) {
111 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
115 }catch(Exception e1){
116 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
123 public void save(Object entity) {
124 Session session = sessionfactory.openSession();
125 Transaction tx = session.beginTransaction();
127 session.persist(entity);
130 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table"+e);
134 }catch(Exception e1){
135 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
142 public void delete(Object entity) {
143 Session session = sessionfactory.openSession();
144 Transaction tx = session.beginTransaction();
146 session.delete(entity);
149 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table"+e);
153 }catch(Exception e1){
154 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
162 public void update(Object entity) {
163 Session session = sessionfactory.openSession();
164 Transaction tx = session.beginTransaction();
166 session.update(entity);
169 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table"+e);
173 }catch(Exception e1){
174 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
181 @SuppressWarnings({ "unchecked", "rawtypes" })
183 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
184 Session session = sessionfactory.openSession();
185 Transaction tx = session.beginTransaction();
186 List<Object> data = null;
187 String[] columnNames = null;
188 if(columnName != null && columnName.contains(":")){
189 columnNames = columnName.split(":");
191 String[] values = null;
192 if(value != null && value.contains(":")){
193 values = value.split(":");
196 Criteria cr = session.createCriteria(className);
197 if(columnNames != null && values != null && columnNames.length == values.length){
198 for (int i = 0; i < columnNames.length; i++){
199 cr.add(Restrictions.eq(columnNames[i],values[i]));
202 cr.add(Restrictions.eq(columnName,value));
206 } catch (Exception e) {
207 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"+e + className);
211 }catch(Exception e1){
212 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
219 @SuppressWarnings("unchecked")
221 public List<PolicyRoles> getUserRoles() {
222 Session session = sessionfactory.openSession();
223 Transaction tx = session.beginTransaction();
224 List<PolicyRoles> rolesData = null;
226 Criteria cr = session.createCriteria(PolicyRoles.class);
227 Disjunction disjunction = Restrictions.disjunction();
228 Conjunction conjunction1 = Restrictions.conjunction();
229 conjunction1.add(Restrictions.eq("role", "admin"));
230 Conjunction conjunction2 = Restrictions.conjunction();
231 conjunction2.add(Restrictions.eq("role", "editor"));
232 Conjunction conjunction3 = Restrictions.conjunction();
233 conjunction3.add(Restrictions.eq("role", "guest"));
234 disjunction.add(conjunction1);
235 disjunction.add(conjunction2);
236 disjunction.add(conjunction3);
237 rolesData = cr.add(disjunction).list();
239 } catch (Exception e) {
240 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);
244 }catch(Exception e1){
245 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
253 public List<Object> checkExistingGroupListforUpdate(String arg0, String arg1) {
254 return Collections.emptyList();
259 public void deleteAll() {
260 // Do nothing because this method is not used and is a placeholder to avoid 'Unimplemented Method' error
264 @SuppressWarnings("unchecked")
266 public List<Object> getDataByQuery(String query, SimpleBindings params) {
267 Session session = sessionfactory.openSession();
268 Transaction tx = session.beginTransaction();
269 List<Object> data = null;
271 Query hbquery = session.createQuery(query);
272 for (Map.Entry<String, Object> paramPair : params.entrySet()) {
273 if(paramPair.getValue() instanceof java.lang.Long){
274 hbquery.setLong(paramPair.getKey(), (long) paramPair.getValue());
277 hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
280 data = hbquery.list();
282 } catch (Exception e) {
283 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
288 }catch(HibernateException e1){
289 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
296 @SuppressWarnings("rawtypes")
298 public Object getEntityItem(Class className, String columnName, String key) {
299 Session session = sessionfactory.openSession();
300 Transaction tx = session.beginTransaction();
303 Criteria cr = session.createCriteria(className);
304 if(columnName.contains(":") && key.contains(":")){
305 String[] columns = columnName.split(":");
306 String[] keys = key.split(":");
307 for(int i=0; i < columns.length; i++){
308 cr.add(Restrictions.eq(columns[i], keys[i]));
311 cr.add(Restrictions.eq(columnName, key));
313 data = cr.list().get(0);
315 } catch (Exception e) {
316 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
320 }catch(Exception e1){
321 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
329 public void updateClAlarms(String arg0, String arg1) {
330 // Do nothing because this method is not used and is a placeholder to avoid 'Unimplemented Method' error
335 public void updateClYaml(String arg0, String arg1) {
336 // Do nothing because this method is not used and is a placeholder to avoid 'Unimplemented Method' error
341 public void updateQuery(String query) {
342 Session session = sessionfactory.openSession();
343 Transaction tx = session.beginTransaction();
345 Query hbquery = session.createQuery(query);
346 hbquery.executeUpdate();
348 } catch (Exception e) {
349 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e);
353 }catch(Exception e1){
354 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
361 @SuppressWarnings({ "rawtypes", "unchecked" })
363 public List<String> getDataByColumn(Class className, String columnName) {
364 Session session = sessionfactory.openSession();
365 List<String> data = null;
367 Criteria cr = session.createCriteria(className);
368 cr.setProjection(Projections.property(columnName));
371 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
376 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e);
383 @SuppressWarnings({ "rawtypes", "unchecked" })
385 public List<Object> getMultipleDataOnAddingConjunction(Class className, String columnName, List<String> data) {
386 Session session = sessionfactory.openSession();
387 Transaction tx = session.beginTransaction();
388 List<Object> entityData = null;
390 Criteria cr = session.createCriteria(className);
391 Disjunction disjunction = Restrictions.disjunction();
392 List<Conjunction> conjunctionList = new ArrayList<>();
393 String[] columNames = columnName.split(":");
394 for(int i =0; i < data.size(); i++){
395 String[] entiySplit = data.get(i).split(":");
396 Conjunction conjunction = Restrictions.conjunction();
397 conjunction.add(Restrictions.eq(columNames[0], entiySplit[0]));
398 conjunction.add(Restrictions.eq(columNames[1], entiySplit[1]));
399 conjunctionList.add(conjunction);
401 for(int j =0 ; j < conjunctionList.size(); j++){
402 disjunction.add(conjunctionList.get(j));
404 entityData = cr.add(disjunction).list();
406 } catch (Exception e) {
407 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + className + e);
411 }catch(Exception e1){
412 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);