Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / SecurityZoneDaoImpl.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.openecomp.policy.rest.dao.SecurityZoneDao;
34 import org.openecomp.policy.rest.jpa.SecurityZone;
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.openecomp.policy.common.logging.flexlogger.FlexLogger; 
41 import org.openecomp.policy.common.logging.flexlogger.Logger;
42
43 @Service("SecurityZoneDao")
44 public class SecurityZoneDaoImpl implements SecurityZoneDao{
45         private static final Logger logger = FlexLogger.getLogger(SecurityZoneDaoImpl.class);
46         @Autowired
47         SessionFactory sessionfactory;
48         
49         @SuppressWarnings("unchecked")
50         @Override
51         public List<SecurityZone> getSecurityZoneData() {
52                 Session session = sessionfactory.openSession();
53                 Transaction tx = session.beginTransaction();
54                 List<SecurityZone> attributeData = null;
55         try {
56                 Criteria cr = session.createCriteria(SecurityZone.class);
57             attributeData = cr.list();
58                         tx.commit();
59                 } catch (Exception e) {
60                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying SecurityZone Table"+e);     
61                 }finally{
62                         try{
63                                 session.close();
64                         }catch(Exception e1){
65                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
66                         }
67                 }
68                 return attributeData;
69                 
70         }
71
72         @Override
73         public void Save(SecurityZone 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 SecurityZone 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         @Override
91         public void delete(SecurityZone attribute) {
92                 Session session = sessionfactory.openSession();
93                 Transaction tx = session.beginTransaction();
94                 try {
95                         session.delete(attribute);
96                         tx.commit();    
97                 }catch(Exception e){
98                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting SecurityZone Table"+e);     
99                 }finally{
100                         try{
101                                 session.close();
102                         }catch(Exception e1){
103                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
104                         }
105                 }
106         }
107
108         @Override
109         public void update(SecurityZone attribute) {
110                 Session session = sessionfactory.openSession();
111                 Transaction tx = session.beginTransaction();
112                 try {
113                         session.update(attribute);
114                         tx.commit();    
115                 }catch(Exception e){
116                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating SecurityZone Table"+e);     
117                 }finally{
118                         try{
119                                 session.close();
120                         }catch(Exception e1){
121                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
122                         }
123                 }
124                 
125         }
126
127         @SuppressWarnings("unchecked")
128         @Override
129         public List<String> getSecurityZoneDataByName() {
130                 Session session = sessionfactory.openSession();
131                 Transaction tx = session.beginTransaction();
132                 List<String> data = new ArrayList<String>();
133         try {
134                 Criteria cr = session.createCriteria(SecurityZone.class);
135             List<SecurityZone> attributeData = cr.list();     
136             for(int i = 0; i < attributeData.size(); i++){
137                  data.add(attributeData.get(i).getZoneName());
138            }
139                         tx.commit();
140                 } catch (Exception e) {
141                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying SecurityZone Table"+e);     
142                 }finally{
143                         try{
144                                 session.close();
145                         }catch(Exception e1){
146                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
147                         }
148                 }
149                 return data;
150         }
151
152 }