Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / RemoteCatalogValuesDaoImpl.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.hibernate.Criteria;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.Transaction;
31 import org.openecomp.policy.dao.RemoteCatalogValuesDao;
32 import org.openecomp.policy.rest.jpa.RemoteCatalogValues;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35
36 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
37
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
39 import org.openecomp.policy.common.logging.flexlogger.Logger;
40
41 @Service("RemoteCatalogValuesDao")
42 public class RemoteCatalogValuesDaoImpl implements RemoteCatalogValuesDao {
43         private static final Logger logger = FlexLogger.getLogger(RemoteCatalogValuesDaoImpl.class);
44         @Autowired
45         SessionFactory sessionfactory;
46         
47         
48         @SuppressWarnings("unchecked")
49         @Override
50         public List<RemoteCatalogValues> getRemoteCatalogValuesData() {
51                 Session session = sessionfactory.openSession();
52                 Transaction tx = session.beginTransaction();
53                 List<RemoteCatalogValues> attributeData = null;
54         try {
55                 Criteria cr = session.createCriteria(RemoteCatalogValues.class);
56             attributeData = cr.list();
57                         tx.commit();
58                 } catch (Exception e) {
59                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying RemoteCatalogValues Table"+e);      
60                 }finally{
61                         try{
62                                 session.close();
63                         }catch(Exception e1){
64                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
65                         }
66                 }
67                 return attributeData;
68         }
69
70         @SuppressWarnings("unchecked")
71         @Override
72         public List<String> getRemoteCatalogValuesDataByName() {
73                 Session session = sessionfactory.openSession();
74                 Transaction tx = session.beginTransaction();
75                 List<String> data = new ArrayList<String>();
76         try {
77                 Criteria cr = session.createCriteria(RemoteCatalogValues.class);
78             List<RemoteCatalogValues> attributeData = cr.list();    
79             for(int i = 0; i < attributeData.size(); i++){
80                  data.add(attributeData.get(i).getName());
81             }
82                         tx.commit();
83                 } catch (Exception e) {
84                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying RemoteCatalogValues Table"+e);      
85                 }finally{
86                         try{
87                                 session.close();
88                         }catch(Exception e1){
89                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
90                         }
91                 }
92                 return data;
93         }
94
95         @Override
96         public void Save(RemoteCatalogValues attribute) {
97                 Session session = sessionfactory.openSession();
98                 Transaction tx = session.beginTransaction();
99                 try {
100                         session.persist(attribute);
101                         tx.commit();    
102                 }catch(Exception e){
103                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving RemoteCatalogValues Table"+e);        
104                 }finally{
105                         try{
106                                 session.close();
107                         }catch(Exception e1){
108                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
109                         }
110                 }
111         }
112
113         @Override
114         public void delete(RemoteCatalogValues attribute) {
115                 Session session = sessionfactory.openSession();
116                 Transaction tx = session.beginTransaction();
117                 try {
118                         session.delete(attribute);
119                         tx.commit();    
120                 }catch(Exception e){
121                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting RemoteCatalogValues Table"+e);      
122                 }finally{
123                         try{
124                                 session.close();
125                         }catch(Exception e1){
126                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
127                         }
128                 }
129         }
130
131         @Override
132         public void update(RemoteCatalogValues attribute) {
133                 Session session = sessionfactory.openSession();
134                 Transaction tx = session.beginTransaction();
135                 try {
136                         session.update(attribute);
137                         tx.commit();    
138                 }catch(Exception e){
139                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating RemoteCatalogValues Table"+e);      
140                 }finally{
141                         try{
142                                 session.close();
143                         }catch(Exception e1){
144                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
145                         }
146                 }
147                 
148         }
149
150 }