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