Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / TermListDaoImpl.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.ArrayList;
25 import java.util.List;
26
27 //import org.apache.commons.logging.Log;
28 //import org.apache.commons.logging.LogFactory;
29 import org.hibernate.Criteria;
30 import org.hibernate.Session;
31 import org.hibernate.SessionFactory;
32 import org.hibernate.Transaction;
33 import org.hibernate.criterion.Restrictions;
34 import org.openecomp.policy.rest.dao.TermListDao;
35 import org.openecomp.policy.rest.jpa.TermList;
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 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
42 import org.openecomp.policy.common.logging.flexlogger.Logger;
43
44 @Service("TermListDao")
45 public class TermListDaoImpl implements TermListDao{
46         private static final Logger logger = FlexLogger.getLogger(TermListDaoImpl.class);
47         @Autowired
48         SessionFactory sessionfactory;
49         
50         @SuppressWarnings("unchecked")
51         @Override
52         public List<TermList> getTermListData() {
53                 Session session = sessionfactory.openSession();
54                 Transaction tx = session.beginTransaction();
55                 List<TermList> attributeData = null;
56         try {
57                 Criteria cr = session.createCriteria(TermList.class);
58             attributeData = cr.list();
59                         tx.commit();
60                 } catch (Exception e) {
61                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying TermList 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
73         @Override
74         public void Save(TermList attribute) {
75                 Session session = sessionfactory.openSession();
76                 Transaction tx = session.beginTransaction();
77                 try {
78                         session.persist(attribute);
79                         tx.commit();    
80                 }catch(Exception e){
81                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving TermList Table"+e);   
82                 }finally{
83                         try{
84                                 session.close();
85                         }catch(Exception e1){
86                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
87                         }
88                 }
89                 
90         }
91
92         @Override
93         public void delete(TermList attribute) {
94                 Session session = sessionfactory.openSession();
95                 Transaction tx = session.beginTransaction();
96                 try {
97                         session.delete(attribute);
98                         tx.commit();    
99                 }catch(Exception e){
100                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting TermList Table"+e); 
101                 }finally{
102                         try{
103                                 session.close();
104                         }catch(Exception e1){
105                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
106                         }
107                 }
108         }
109
110         @Override
111         public void update(TermList attribute) {
112                 Session session = sessionfactory.openSession();
113                 Transaction tx = session.beginTransaction();
114                 try {
115                         session.update(attribute);
116                         tx.commit();    
117                 }catch(Exception e){
118                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating TermList Table"+e); 
119                 }finally{
120                         try{
121                                 session.close();
122                         }catch(Exception e1){
123                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
124                         }
125                 }
126                 
127         }
128
129         @SuppressWarnings("unchecked")
130         @Override
131         public List<String> getTermListDataByName() {
132                 Session session = sessionfactory.openSession();
133                 Transaction tx = session.beginTransaction();
134                 List<String> data = new ArrayList<String>();
135         try {
136                 Criteria cr = session.createCriteria(TermList.class);
137             List<TermList> attributeData = cr.list();   
138             for(int i = 0; i < attributeData.size(); i++){
139                  data.add(attributeData.get(i).getTermName());
140             }
141                         tx.commit();
142                 } catch (Exception e) {
143                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying TermList Table"+e); 
144                 }finally{
145                         try{
146                                 session.close();
147                         }catch(Exception e1){
148                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
149                         }
150                 }
151                 return data;
152         }
153
154         @Override
155         public TermList getTermListValueByName(String name) {
156                 Session session = sessionfactory.openSession();
157                 Transaction tx = session.beginTransaction();
158                 TermList data = null;
159                 try {
160                         Criteria cr = session.createCriteria(TermList.class);
161                         cr.add(Restrictions.eq("termName",name));
162                         data = (TermList) cr.list().get(0);
163                         tx.commit();
164                 } catch (Exception e) {
165                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying TermList Table"+e); 
166                 }finally{
167                         try{
168                                 session.close();
169                         }catch(Exception e1){
170                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
171                         }
172                 }
173                 return data;
174         }
175
176 }