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.openecomp.policy.daoImp;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.hibernate.Criteria;
27 import org.hibernate.Query;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.Transaction;
31 import org.hibernate.criterion.Conjunction;
32 import org.hibernate.criterion.Disjunction;
33 import org.hibernate.criterion.Projections;
34 import org.hibernate.criterion.Restrictions;
35 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
36 import org.openecomp.policy.common.logging.flexlogger.Logger;
37 import org.openecomp.policy.rest.dao.CommonClassDao;
38 import org.openecomp.policy.rest.jpa.PolicyRoles;
39 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
43 @Service("CommonClassDao")
44 public class CommonClassDaoImpl implements CommonClassDao{
46 private static final Logger LOGGER = FlexLogger.getLogger(CommonClassDaoImpl.class);
49 SessionFactory sessionfactory;
51 @SuppressWarnings({ "unchecked", "rawtypes" })
53 public List<Object> getData(Class className) {
54 Session session = sessionfactory.openSession();
55 List<Object> data = null;
57 Criteria cr = session.createCriteria(className);
60 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
65 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
72 @SuppressWarnings({ "rawtypes", "unchecked" })
74 public List<Object> getDataById(Class className, String columnName, String key) {
75 Session session = sessionfactory.openSession();
76 List<Object> data = null;
78 Criteria cr = session.createCriteria(className);
79 if(columnName.contains(":") && key.contains(":")){
80 String[] columns = columnName.split(":");
81 String[] keys = key.split(":");
82 for(int i=0; i < columns.length; i++){
83 cr.add(Restrictions.eq(columns[i], keys[i]));
86 cr.add(Restrictions.eq(columnName, key));
89 } catch (Exception e) {
90 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
95 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
102 public void save(Object entity) {
103 Session session = sessionfactory.openSession();
104 Transaction tx = session.beginTransaction();
106 session.persist(entity);
109 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table"+e);
113 }catch(Exception e1){
114 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
121 public void delete(Object entity) {
122 Session session = sessionfactory.openSession();
123 Transaction tx = session.beginTransaction();
125 session.delete(entity);
128 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table"+e);
132 }catch(Exception e1){
133 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
141 public void update(Object entity) {
142 Session session = sessionfactory.openSession();
143 Transaction tx = session.beginTransaction();
145 session.update(entity);
148 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table"+e);
152 }catch(Exception e1){
153 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
160 @SuppressWarnings({ "unchecked", "rawtypes" })
162 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
163 Session session = sessionfactory.openSession();
164 Transaction tx = session.beginTransaction();
165 List<Object> data = null;
166 String[] columnNames = null;
167 if(columnName != null && columnName.contains(":")){
168 columnNames = columnName.split(":");
170 String[] values = null;
171 if(value != null && value.contains(":")){
172 values = value.split(":");
175 Criteria cr = session.createCriteria(className);
176 if(columnNames != null && values != null && columnNames.length == values.length){
177 for (int i = 0; i < columnNames.length; i++){
178 cr.add(Restrictions.eq(columnNames[i],values[i]));
181 cr.add(Restrictions.eq(columnName,value));
185 } catch (Exception e) {
186 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"+e + className);
190 }catch(Exception e1){
191 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
198 @SuppressWarnings("unchecked")
200 public List<PolicyRoles> getUserRoles() {
201 Session session = sessionfactory.openSession();
202 Transaction tx = session.beginTransaction();
203 List<PolicyRoles> rolesData = null;
205 Criteria cr = session.createCriteria(PolicyRoles.class);
206 Disjunction disjunction = Restrictions.disjunction();
207 Conjunction conjunction1 = Restrictions.conjunction();
208 conjunction1.add(Restrictions.eq("role", "admin"));
209 Conjunction conjunction2 = Restrictions.conjunction();
210 conjunction2.add(Restrictions.eq("role", "editor"));
211 Conjunction conjunction3 = Restrictions.conjunction();
212 conjunction3.add(Restrictions.eq("role", "guest"));
213 disjunction.add(conjunction1);
214 disjunction.add(conjunction2);
215 disjunction.add(conjunction3);
216 rolesData = cr.add(disjunction).list();
218 } catch (Exception e) {
219 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);
223 }catch(Exception e1){
224 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
232 public List<Object> checkExistingGroupListforUpdate(String arg0, String arg1) {
238 public void deleteAll() {}
241 @SuppressWarnings("unchecked")
243 public List<Object> getDataByQuery(String query) {
244 Session session = sessionfactory.openSession();
245 Transaction tx = session.beginTransaction();
246 List<Object> data = null;
248 Query hbquery = session.createQuery(query);
249 data = hbquery.list();
251 } catch (Exception e) {
252 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
256 }catch(Exception e1){
257 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
264 @SuppressWarnings("rawtypes")
266 public Object getEntityItem(Class className, String columnName, String key) {
267 System.out.println(columnName);
268 System.out.println(key);
269 Session session = sessionfactory.openSession();
270 Transaction tx = session.beginTransaction();
273 Criteria cr = session.createCriteria(className);
274 if(columnName.contains(":") && key.contains(":")){
275 String[] columns = columnName.split(":");
276 String[] keys = key.split(":");
277 for(int i=0; i < columns.length; i++){
278 cr.add(Restrictions.eq(columns[i], keys[i]));
281 cr.add(Restrictions.eq(columnName, key));
283 data = cr.list().get(0);
284 System.out.println(data);
286 } catch (Exception e) {
287 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
291 }catch(Exception e1){
292 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
300 public void updateClAlarms(String arg0, String arg1) {}
304 public void updateClYaml(String arg0, String arg1) {}
308 public void updateQuery(String query) {
309 Session session = sessionfactory.openSession();
310 Transaction tx = session.beginTransaction();
312 Query hbquery = session.createQuery(query);
313 hbquery.executeUpdate();
315 } catch (Exception e) {
316 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e);
320 }catch(Exception e1){
321 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
328 @SuppressWarnings({ "rawtypes", "unchecked" })
330 public List<String> getDataByColumn(Class className, String columnName) {
331 Session session = sessionfactory.openSession();
332 List<String> data = null;
334 Criteria cr = session.createCriteria(className);
335 cr.setProjection(Projections.property(columnName));
338 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
343 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
350 @SuppressWarnings({ "rawtypes", "unchecked" })
352 public List<Object> getMultipleDataOnAddingConjunction(Class className, String columnName, List<String> data) {
353 Session session = sessionfactory.openSession();
354 Transaction tx = session.beginTransaction();
355 List<Object> entityData = null;
357 Criteria cr = session.createCriteria(className);
358 Disjunction disjunction = Restrictions.disjunction();
359 List<Conjunction> conjunctionList = new ArrayList<Conjunction>();
360 String[] columNames = columnName.split(":");
361 for(int i =0; i < data.size(); i++){
362 String[] entiySplit = data.get(i).split(":");
363 Conjunction conjunction = Restrictions.conjunction();
364 conjunction.add(Restrictions.eq(columNames[0], entiySplit[0]));
365 conjunction.add(Restrictions.eq(columNames[1], entiySplit[1]));
366 conjunctionList.add(conjunction);
368 for(int j =0 ; j < conjunctionList.size(); j++){
369 disjunction.add(conjunctionList.get(j));
371 entityData = cr.add(disjunction).list();
373 } catch (Exception e) {
374 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table" +className +e);
378 }catch(Exception e1){
379 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);