Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / FunctionDefinitionDaoImpl.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.FunctionDefinitionDao;
32 import org.openecomp.policy.rest.jpa.FunctionDefinition;
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("FunctionDefinitionDao")
42 public class FunctionDefinitionDaoImpl implements FunctionDefinitionDao{
43         private static final Logger logger = FlexLogger.getLogger(FunctionDefinitionDaoImpl.class);
44         @Autowired
45         SessionFactory sessionfactory;
46         
47         @SuppressWarnings("unchecked")
48         @Override
49         public List<String> getFunctionDefinitionByName() {
50                 Session session = sessionfactory.openSession();
51                 Transaction tx = session.beginTransaction();    
52         List<String> data = new ArrayList<String>();      
53         try {
54                 Criteria cr = session.createCriteria(FunctionDefinition.class);
55             List<FunctionDefinition> functionDefinitionData = cr.list();
56             for(int i = 0; i < functionDefinitionData.size(); i++){
57                  data.add(functionDefinitionData.get(i).getShortname());
58            }            
59                         tx.commit();
60                 } catch (Exception e) {
61                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying FunctionDefinition Table"+e);       
62                 }finally{
63                         try{
64                                 session.close();
65                         }catch(Exception e1){
66                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
67                         }
68                 }
69                 return data;
70         }
71
72         @SuppressWarnings("unchecked")
73         @Override
74         public List<FunctionDefinition> getFunctionDefinition() {
75                 Session session = sessionfactory.openSession();
76                 Transaction tx = session.beginTransaction();    
77         List<FunctionDefinition> functionDefinitionData = null;
78         try {
79                 Criteria cr = session.createCriteria(FunctionDefinition.class);
80                         functionDefinitionData = cr.list();             
81                         tx.commit();
82                 } catch (Exception e) {
83                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying FunctionDefinition Table"+e);       
84                 }finally{
85                         try{
86                                 session.close();
87                         }catch(Exception e1){
88                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
89                         }
90                 }
91                 return functionDefinitionData;
92         }
93
94 }