Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / CategoryDaoImpl.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.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
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.pap.xacml.rest.HibernateSession;
32 import org.openecomp.policy.rest.dao.CategoryDao;
33 import org.openecomp.policy.rest.jpa.Category;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
38
39 @Service("CategoryDao")
40 public class CategoryDaoImpl implements CategoryDao {
41         private static final Log logger = LogFactory.getLog(CategoryDaoImpl.class);
42
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<Category> getCategoryListData() {
57                 Session session = HibernateSession.getSessionFactory();
58                 Transaction tx = session.beginTransaction();            
59                 List<Category> categoryListData = null;
60                 try {
61                         Criteria cr = session.createCriteria(Category.class);
62                         categoryListData = cr.list();   
63                         tx.commit();
64                 } catch (Exception e) {
65                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Category Table"+e); 
66                 }finally{
67                         try{
68                                 session.close();
69                         }catch(Exception e1){
70                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
71                         }
72                 }
73                 return categoryListData;
74         }
75
76 }