Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / FirewallDictionaryListDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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.pap.xacml.rest.daoimpl;
22
23 import java.util.ArrayList;
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.pap.xacml.rest.HibernateSession;
33 import org.openecomp.policy.rest.dao.FirewallDictionaryListDao;
34 import org.openecomp.policy.rest.jpa.FirewallDictionaryList;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 @Service("FirewallDictionaryListDao")
44 public class FirewallDictionaryListDaoImpl implements FirewallDictionaryListDao {
45         private static final Log logger = LogFactory.getLog(FirewallDictionaryListDaoImpl.class);
46         @Autowired
47         SessionFactory sessionfactory;
48         
49         public SessionFactory getSessionfactory() {
50                 return sessionfactory;
51         }
52
53         public void setSessionfactory(SessionFactory sessionfactory) {
54                 this.sessionfactory = sessionfactory;
55         }
56         
57         @Override
58         public List<FirewallDictionaryList> getFWDictionaryListData() {
59                 Session session = HibernateSession.getSessionFactory();
60                 Transaction tx = session.beginTransaction();
61                 List<FirewallDictionaryList> attributeData =  null;
62         try {
63                 Criteria cr = session.createCriteria(FirewallDictionaryList.class);
64             attributeData = cr.list();
65                         tx.commit();
66                 } catch (Exception e) {
67                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying FirewallDictionaryList Table"+e);   
68                 }finally{
69                         try{
70                                 session.close();
71                         }catch(Exception e1){
72                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
73                         }
74                 }
75                 return attributeData;
76
77         }
78
79         @Override
80         public List<String> getFWDictionaryListDataByName() {
81                 Session session = HibernateSession.getSessionFactory();
82                 Transaction tx = session.beginTransaction();
83                 List<String> data = new ArrayList<String>();
84         try {
85                 Criteria cr = session.createCriteria(FirewallDictionaryList.class);
86             List<FirewallDictionaryList> attributeData = cr.list();    
87             for(int i = 0; i < attributeData.size(); i++){
88                  data.add(attributeData.get(i).getParentItemName());
89             }
90                         tx.commit();
91                 } catch (Exception e) {
92                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying FirewallDictionaryList Table"+e);   
93                 }finally{
94                         try{
95                                 session.close();
96                         }catch(Exception e1){
97                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
98                         }
99                 }
100                 return data;
101         }
102
103         @Override
104         public void Save(FirewallDictionaryList firewallDictionaryList) {
105                 Session session = HibernateSession.getSessionFactory();
106                 Transaction tx = session.beginTransaction();
107                 try {
108                         session.persist(firewallDictionaryList);
109                         tx.commit();    
110                 }catch(Exception e){
111                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving FirewallDictionaryList Table"+e);     
112                 }finally{
113                         try{
114                                 session.close();
115                         }catch(Exception e1){
116                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
117                         }
118                 }
119                 
120         }
121
122         @Override
123         public void delete(FirewallDictionaryList firewallDictionaryList) {
124                 Session session = HibernateSession.getSessionFactory();
125                 Transaction tx = session.beginTransaction();
126                 try {
127                         session.delete(firewallDictionaryList);
128                         tx.commit();    
129                 }catch(Exception e){
130                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting FirewallDictionaryList Table"+e);   
131                 }finally{
132                         try{
133                                 session.close();
134                         }catch(Exception e1){
135                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
136                         }
137                 }
138                 
139         }
140
141         @Override
142         public void update(FirewallDictionaryList firewallDictionaryList) {
143                 Session session = HibernateSession.getSessionFactory();
144                 Transaction tx = session.beginTransaction();
145                 try {
146                         session.update(firewallDictionaryList);
147                         tx.commit();    
148                 }catch(Exception e){
149                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating FirewallDictionaryList Table"+e);   
150                 }finally{
151                         try{
152                                 session.close();
153                         }catch(Exception e1){
154                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
155                         }
156                 }
157                 
158         }
159
160         @Override
161         public void updateQuery(String query) {
162                 Session session = HibernateSession.getSessionFactory();
163                 Transaction tx = session.beginTransaction();    
164                 try {
165                         Query hbquery = session.createQuery(query);
166                         hbquery.executeUpdate();
167                         tx.commit();
168                 } catch (Exception e) {
169                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating FirewallDictionaryList Table"+e);   
170                 }finally{
171                         try{
172                                 session.close();
173                         }catch(Exception e1){
174                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
175                         }
176                 }
177                 
178         }
179
180         @Override
181         public FirewallDictionaryList getFWDictionaryDataById(String value) {
182                 Session session = HibernateSession.getSessionFactory();
183                 Transaction tx = session.beginTransaction();
184                 FirewallDictionaryList data = null;
185         try {
186                 Criteria cr = session.createCriteria(FirewallDictionaryList.class);
187                 cr = cr.add(Restrictions.eq("parentItemName",value));
188                 data = (FirewallDictionaryList) cr.list().get(0);
189                         tx.commit();
190                 } catch (Exception e) {
191                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying FirewallDictionaryList Table"+e);   
192                 }finally{
193                         try{
194                                 session.close();
195                         }catch(Exception e1){
196                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
197                         }
198                 }
199                 return data;
200         }
201
202 }