Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / EcompNameDaoImpl.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.jpa.EcompName;
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 @Service("EcompNameDao")
41 public class EcompNameDaoImpl implements EcompNameDao {
42         private static final Log logger = LogFactory.getLog(EcompNameDaoImpl.class);
43         @Autowired
44         SessionFactory sessionfactory;
45         
46         public SessionFactory getSessionfactory() {
47                 return sessionfactory;
48         }
49
50         public void setSessionfactory(SessionFactory sessionfactory) {
51                 this.sessionfactory = sessionfactory;
52         }
53
54         @SuppressWarnings("unchecked")
55         @Override
56         public List<EcompName> getEcompName() {
57                 System.out.println("EcompNameDaoImpl:  getEcompName() is called");
58                 logger.debug("EcompNameDaoImpl:  getEcompName() is called");
59                 Session session = HibernateSession.getSessionFactory();
60                 Transaction tx = session.beginTransaction();    
61         List<EcompName> ecompNameData = null;
62         try {
63                 Criteria cr = session.createCriteria(EcompName.class);
64                         ecompNameData = cr.list();
65                         System.out.println("Data returned from ecompname table"+ecompNameData.toString());
66                         logger.debug("Data returned from ecompname table:  " + ecompNameData.toString());
67                         tx.commit();
68                 } catch (Exception e) {
69                         System.out.println("Exception Occured while Querying ecompname"+e);
70                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying EcompName 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 ecompNameData;
79         }
80
81         @Override
82         public void Save(EcompName ecompName) {
83                 System.out.println("EcompNameDaoImpl:  Save() is called");
84                 logger.debug("EcompNameDaoImpl:  Save() is called");
85                 Session session = HibernateSession.getSessionFactory();
86                 Transaction tx = session.beginTransaction();
87                 try {
88                         session.persist(ecompName);
89                         tx.commit();    
90                 }catch(Exception e){
91                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving EcompName 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(EcompName ecompName) {
104                 Session session = HibernateSession.getSessionFactory();
105                 Transaction tx = session.beginTransaction();
106                 try {
107                         session.delete(ecompName);
108                         tx.commit();    
109                 }catch(Exception e){
110                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting EcompName 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(EcompName ecompName) {
123                 Session session = HibernateSession.getSessionFactory();
124                 Transaction tx = session.beginTransaction();
125                 try {
126                         session.update(ecompName);
127                         tx.commit();    
128                 }catch(Exception e){
129                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating EcompName 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> getEcompNameDataByName() {
143                 logger.info("getEcompNameDataByName 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(EcompName.class);
149             List<EcompName> ecompNameData = cr.list();
150             for(int i = 0; i < ecompNameData.size(); i++){
151                  data.add(ecompNameData.get(i).getEcompName());
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 EcompName 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 }