Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / RiskTypeDaoImpl.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.openecomp.policy.pap.xacml.rest.HibernateSession;
33 import org.openecomp.policy.rest.dao.EcompNameDao;
34 import org.openecomp.policy.rest.dao.RiskTypeDao;
35 import org.openecomp.policy.rest.jpa.EcompName;
36 import org.openecomp.policy.rest.jpa.RiskType;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39
40 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
41
42 @Service("RiskTypeDao")
43 public class RiskTypeDaoImpl implements RiskTypeDao {
44         private static final Log logger = LogFactory.getLog(RiskTypeDaoImpl.class);
45         @Autowired
46         SessionFactory sessionfactory;
47         
48         public SessionFactory getSessionfactory() {
49                 return sessionfactory;
50         }
51
52         public void setSessionfactory(SessionFactory sessionfactory) {
53                 this.sessionfactory = sessionfactory;
54         }
55
56         @SuppressWarnings("unchecked")
57         @Override
58         public List<RiskType> getRiskName() {
59                 System.out.println("RiskTypeDaoImpl:  getRiskName() is called");
60                 logger.debug("RiskTypeDaoImpl:  getRiskName() is called");
61                 Session session = HibernateSession.getSessionFactory();
62                 Transaction tx = session.beginTransaction();    
63         List<RiskType> riskTypeData = null;
64         try {
65                 Criteria cr = session.createCriteria(RiskType.class);
66                 riskTypeData = cr.list();
67                         logger.debug("Data returned from RiskType table:  " + riskTypeData.toString());
68                         tx.commit();
69                 } catch (Exception e) {
70                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying RiskType Table"+e); 
71                 }finally{
72                         try{
73                                 session.close();
74                         }catch(Exception e1){
75                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
76                         }
77                 }
78                 return riskTypeData;
79         }
80
81         @Override
82         public void Save(RiskType riskName) {
83                 System.out.println("RiskTypeDaoImpl:  Save() is called");
84                 logger.debug("RiskTypeDaoImpl:  Save() is called");
85                 Session session = HibernateSession.getSessionFactory();
86                 Transaction tx = session.beginTransaction();
87                 try {
88                         session.persist(riskName);
89                         tx.commit();    
90                 }catch(Exception e){
91                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving RiskType Table"+e);   
92                 }finally{
93                         try{
94                                 session.close();
95                         }catch(Exception e1){
96                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
97                         }
98                 }
99                 
100         }
101
102         @Override
103         public void delete(RiskType riskName) {
104                 Session session = HibernateSession.getSessionFactory();
105                 Transaction tx = session.beginTransaction();
106                 try {
107                         session.delete(riskName);
108                         tx.commit();    
109                 }catch(Exception e){
110                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting RiskType Table"+e); 
111                 }finally{
112                         try{
113                                 session.close();
114                         }catch(Exception e1){
115                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
116                         }
117                 }
118                 
119         }
120
121         @Override
122         public void update(RiskType riskName) {
123                 Session session = HibernateSession.getSessionFactory();
124                 Transaction tx = session.beginTransaction();
125                 try {
126                         session.update(riskName);
127                         tx.commit();    
128                 }catch(Exception e){
129                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating RiskType Table"+e); 
130                 }finally{
131                         try{
132                                 session.close();
133                         }catch(Exception e1){
134                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
135                         }
136                 }
137                 
138         }
139
140         @SuppressWarnings("unchecked")
141         @Override
142         public List<String> getRiskTypeDataByName() {
143                 logger.info("getRiskTypeDataByName is call from the DAO implementation class.");
144                 Session session = HibernateSession.getSessionFactory();
145                 Transaction tx = session.beginTransaction();    
146         List<String> data = new ArrayList<String>();    
147         try {
148                 Criteria cr = session.createCriteria(RiskType.class);
149             List<RiskType> riskTypeData = cr.list();
150             for(int i = 0; i < riskTypeData.size(); i++){
151                  data.add(riskTypeData.get(i).getRiskName());
152            }
153             logger.info("data retrieved: " + data.toString());
154                         tx.commit();
155                 } catch (Exception e) {
156                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying RiskType Table"+e); 
157                 }finally{
158                         try{
159                                 session.close();
160                         }catch(Exception e1){
161                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
162                         }
163                 }
164                 return data;
165         }
166
167 }