Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / WatchPolicyNotificationDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.daoImp;
22
23
24 import java.util.List;
25
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.Restrictions;
32 import org.openecomp.policy.dao.WatchPolicyNotificationDao;
33 import org.openecomp.policy.rest.jpa.WatchPolicyNotificationTable;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
38
39 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
40 import org.openecomp.policy.common.logging.flexlogger.Logger;
41
42 @Service("WatchPolicyNotificationDao")
43 public class WatchPolicyNotificationDaoImpl implements WatchPolicyNotificationDao{
44
45         private static final Logger logger = FlexLogger.getLogger(WatchPolicyNotificationDaoImpl.class);
46         
47         @Autowired
48         SessionFactory sessionfactory;
49         
50         @SuppressWarnings("unchecked")
51         @Override
52         public List<WatchPolicyNotificationTable> getListData() {
53                 Session session = sessionfactory.openSession();
54                 Transaction tx = session.beginTransaction();
55         List<WatchPolicyNotificationTable> attributeData =null;
56         try {
57                         Criteria cr = session.createCriteria(WatchPolicyNotificationTable.class);
58                         attributeData = cr.list();
59                         tx.commit();
60                 } catch (Exception e) {
61                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying WatchPolicyNotificationTable Table"+e);     
62                 }finally{
63                         try{
64                                 session.close();
65                         }catch(Exception e1){
66                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
67                         }
68                 }
69                 return attributeData;
70         }
71
72         @Override
73         public void save(WatchPolicyNotificationTable attribute) {
74                 Session session = sessionfactory.openSession();
75                 Transaction tx = session.beginTransaction();
76                 try {
77                         session.persist(attribute);
78                         tx.commit();    
79                 }catch(Exception e){
80                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving WatchPolicyNotificationTable Table"+e);       
81                 }finally{
82                         try{
83                                 session.close();
84                         }catch(Exception e1){
85                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
86                         }
87                 }
88                 
89         }
90
91         @Override
92         public void delete(WatchPolicyNotificationTable attribute) {
93                 Session session = sessionfactory.openSession();
94                 Transaction tx = session.beginTransaction();
95                 try {
96                         session.delete(attribute);
97                         tx.commit();    
98                 }catch(Exception e){
99                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting WatchPolicyNotificationTable Table"+e);     
100                 }finally{
101                         try{
102                                 session.close();
103                         }catch(Exception e1){
104                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
105                         }
106                 }
107         }
108
109         @Override
110         public void update(WatchPolicyNotificationTable attribute) {
111                 Session session = sessionfactory.openSession();
112                 Transaction tx = session.beginTransaction();
113                 try {
114                         session.update(attribute);
115                         tx.commit();    
116                 }catch(Exception e){
117                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating WatchPolicyNotificationTable Table"+e);     
118                 }finally{
119                         try{
120                                 session.close();
121                         }catch(Exception e1){
122                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
123                         }
124                 }
125                 
126         }
127
128         @SuppressWarnings("unchecked")
129         @Override
130         public List<WatchPolicyNotificationTable> getListDataByPolicyName(String policyName) {
131                 Session session = sessionfactory.openSession();
132                 Transaction tx = session.beginTransaction();
133                 List<WatchPolicyNotificationTable> data = null;
134                 try {
135                         Criteria cr = session.createCriteria(WatchPolicyNotificationTable.class);
136                         cr.add(Restrictions.eq("policyName",policyName));
137                         data = cr.list();
138                         tx.commit();
139                 } catch (Exception e) {
140                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying WatchPolicyNotificationTable Table"+e);     
141                 }finally{
142                         try{
143                                 session.close();
144                         }catch(Exception e1){
145                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
146                         }
147                 }
148                 return data;
149         }
150
151         @SuppressWarnings("unchecked")
152         @Override
153         public List<WatchPolicyNotificationTable> watchListQuery(String query) {
154                 Session session = sessionfactory.openSession();
155                 Transaction tx = session.beginTransaction();
156                 List<WatchPolicyNotificationTable> data = null;
157                 try {
158                         Query hbquery = session.createQuery(query);
159                         data = hbquery.list();
160                         tx.commit();
161                 } catch (Exception e) {
162                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying WatchPolicyNotificationTable Table"+e);     
163                 }finally{
164                         try{
165                                 session.close();
166                         }catch(Exception e1){
167                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
168                         }
169                 }
170                 return data;
171         }
172
173 }