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.pap.xacml.rest.daoimpl;
23 import java.util.List;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.hibernate.Criteria;
28 import org.hibernate.Query;
29 import org.hibernate.Session;
30 import org.hibernate.Transaction;
31 import org.hibernate.criterion.Conjunction;
32 import org.hibernate.criterion.Criterion;
33 import org.hibernate.criterion.Disjunction;
34 import org.hibernate.criterion.Projections;
35 import org.hibernate.criterion.Restrictions;
36 import org.openecomp.policy.pap.xacml.rest.HibernateSession;
37 import org.openecomp.policy.rest.dao.CommonClassDao;
38 import org.openecomp.policy.rest.jpa.ClosedLoops;
39 import org.openecomp.policy.rest.jpa.GroupPolicyScopeList;
40 import org.openecomp.policy.rest.jpa.PolicyRoles;
41 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
42 import org.springframework.stereotype.Service;
44 @Service("CommonClassDao")
45 public class CommonClassDaoImpl implements CommonClassDao{
47 private static final Log LOGGER = LogFactory.getLog(CommonClassDaoImpl.class);
51 @SuppressWarnings({ "unchecked", "rawtypes" })
53 public List<Object> getData(Class className) {
54 Session session = HibernateSession.getSessionFactory();
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 = HibernateSession.getSessionFactory();
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);
101 @SuppressWarnings({ "unchecked", "rawtypes" })
103 public List<String> getDataByColumn(Class className, String columnName) {
104 Session session = HibernateSession.getSessionFactory();
105 List<String> data = null;
107 Criteria cr = session.createCriteria(className);
108 cr.setProjection(Projections.property(columnName));
111 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);
116 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
123 public void save(Object entity) {
124 Session session = HibernateSession.getSessionFactory();
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 + "Error While Closing Connection/Statement"+e1);
142 public void delete(Object entity) {
143 Session session = HibernateSession.getSessionFactory();
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 + "Error While Closing Connection/Statement"+e1);
162 public void update(Object entity) {
163 Session session = HibernateSession.getSessionFactory();
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 + "Error While Closing Connection/Statement"+e1);
181 @SuppressWarnings({ "unchecked", "rawtypes" })
183 public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
184 Session session = HibernateSession.getSessionFactory();
185 Transaction tx = session.beginTransaction();
186 List<Object> data = null;
188 String[] columnNames = null;
189 if(columnName != null && columnName.contains(":")){
190 columnNames = columnName.split(":");
192 String[] values = null;
193 if(value != null && value.contains(":")){
194 values = value.split(":");
197 Criteria cr = session.createCriteria(className);
198 if(columnNames != null && values != null && columnNames.length == values.length){
199 for (int i = 0; i < columnNames.length; i++){
200 cr.add(Restrictions.eq(columnNames[i],values[i]));
203 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"+e + className);
213 }catch(Exception e1){
214 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
221 @SuppressWarnings("unchecked")
223 public List<Object> getDataByQuery(String query) {
224 Session session = HibernateSession.getSessionFactory();
225 Transaction tx = session.beginTransaction();
226 List<Object> data = null;
228 Query hbquery = session.createQuery(query);
229 data = hbquery.list();
231 } catch (Exception e) {
232 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
236 }catch(Exception e1){
237 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
245 public void updateQuery(String query) {
246 Session session = HibernateSession.getSessionFactory();
247 Transaction tx = session.beginTransaction();
249 Query hbquery = session.createQuery(query);
250 hbquery.executeUpdate();
252 } catch (Exception e) {
253 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e);
257 }catch(Exception e1){
258 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 Session session = HibernateSession.getSessionFactory();
268 Transaction tx = session.beginTransaction();
271 Criteria cr = session.createCriteria(className);
272 if(columnName.contains(":") && key.contains(":")){
273 String[] columns = columnName.split(":");
274 String[] keys = key.split(":");
275 for(int i=0; i < columns.length; i++){
276 cr.add(Restrictions.eq(columns[i], keys[i]));
279 cr.add(Restrictions.eq(columnName, key));
281 data = cr.list().get(0);
283 } catch (Exception e) {
284 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
288 }catch(Exception e1){
289 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
296 @SuppressWarnings("unchecked")
298 public List<PolicyRoles> getUserRoles() {
299 Session session = HibernateSession.getSessionFactory();
300 Transaction tx = session.beginTransaction();
301 List<PolicyRoles> rolesData = null;
303 Criteria cr = session.createCriteria(PolicyRoles.class);
304 Disjunction disjunction = Restrictions.disjunction();
305 Conjunction conjunction1 = Restrictions.conjunction();
306 conjunction1.add(Restrictions.eq("role", "admin"));
307 Conjunction conjunction2 = Restrictions.conjunction();
308 conjunction2.add(Restrictions.eq("role", "editor"));
309 Conjunction conjunction3 = Restrictions.conjunction();
310 conjunction3.add(Restrictions.eq("role", "guest"));
311 disjunction.add(conjunction1);
312 disjunction.add(conjunction2);
313 disjunction.add(conjunction3);
314 rolesData = cr.add(disjunction).list();
316 } catch (Exception e) {
317 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);
321 }catch(Exception e1){
322 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
329 @SuppressWarnings("unchecked")
331 public void updateClAlarms(String clName, String alarms) {
332 Session session = HibernateSession.getSessionFactory();
333 List<ClosedLoops> closedloopsdata = null;
334 Transaction tx = session.beginTransaction();
336 Criteria cr = session.createCriteria(ClosedLoops.class);
337 cr.add(Restrictions.eq("closedLoopControlName",clName));
338 closedloopsdata = cr.list();
339 ClosedLoops closedloop = closedloopsdata.get(0);
340 closedloop.setAlarmConditions(alarms);
341 session.update(closedloop);
344 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);
351 @SuppressWarnings("unchecked")
353 public void updateClYaml(String clName, String yaml) {
354 Session session = HibernateSession.getSessionFactory();
355 List<ClosedLoops> closedloopsdata = null;
356 Transaction tx = session.beginTransaction();
358 Criteria cr = session.createCriteria(ClosedLoops.class);
359 cr.add(Restrictions.eq("closedLoopControlName",clName));
360 closedloopsdata = cr.list();
361 ClosedLoops closedloop = closedloopsdata.get(0);
362 closedloop.setYaml(yaml);
363 session.update(closedloop);
366 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);
373 @SuppressWarnings("unchecked")
375 public void deleteAll() {
376 Session session = HibernateSession.getSessionFactory();
377 Transaction tx = session.beginTransaction();
378 List<ClosedLoops> closedloopsdata = null;
380 Criteria cr = session.createCriteria(ClosedLoops.class);
381 closedloopsdata = cr.list();
383 if(closedloopsdata!=null && !closedloopsdata.isEmpty()) {
384 LOGGER.info("ClosedLoops exist in the database, we need to delete them in our first step to buildCache().");
385 for(int i=0; i > closedloopsdata.size(); i++) {
386 ClosedLoops cl = closedloopsdata.get(i);
390 LOGGER.info("No ClosedLoops exist in the database, no need to delete.");
394 }catch(Exception e) {
395 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while deleting ClosedLoops from the table"+e);
401 @SuppressWarnings({ "unchecked"})
403 public List<Object> checkExistingGroupListforUpdate(String groupListValue, String groupNameValue) {
404 Session session = HibernateSession.getSessionFactory();
405 Transaction tx = session.beginTransaction();
406 List<Object> data = null;
408 Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
409 cr.add(Restrictions.eq("groupList",groupListValue));
410 Criterion expression = Restrictions.eq("name", groupNameValue);
411 cr.add(Restrictions.not(expression));
414 } catch (Exception e) {
415 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for GroupPolicyScopeList Table"+e);
419 }catch(Exception e1){
420 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
428 public List<Object> getMultipleDataOnAddingConjunction(@SuppressWarnings("rawtypes") Class className, String columnName, List<String> data) {