2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.daoImp;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.hibernate.Criteria;
28 import org.hibernate.Query;
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.Transaction;
32 import org.hibernate.criterion.Restrictions;
33 import org.openecomp.policy.dao.PolicyVersionDao;
34 import org.openecomp.policy.rest.jpa.PolicyVersion;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
38 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
40 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
41 import org.openecomp.policy.common.logging.flexlogger.Logger;
43 @Service("PolicyVersionDao")
44 public class PolicyVersionDaoImpl implements PolicyVersionDao {
45 private static final Logger logger = FlexLogger.getLogger(PolicyVersionDaoImpl.class);
47 SessionFactory sessionfactory;
49 @SuppressWarnings("unchecked")
51 public List<PolicyVersion> getPolicyVersionData() {
52 Session session = sessionfactory.openSession();
53 Transaction tx = session.beginTransaction();
54 List<PolicyVersion> versionData = null;
56 Criteria cr = session.createCriteria(PolicyVersion.class);
57 versionData = cr.list();
59 } catch (Exception e) {
60 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion Table"+e);
65 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
72 @SuppressWarnings("unchecked")
74 public List<String> getPolicyVersionDataByName() {
75 Session session = sessionfactory.openSession();
76 Transaction tx = session.beginTransaction();
77 List<String> data = new ArrayList<String>();
79 Criteria cr = session.createCriteria(PolicyVersion.class);
80 List<PolicyVersion> attributeData = cr.list();
81 for(int i = 0; i < attributeData.size(); i++){
82 data.add(attributeData.get(i).getPolicyName());
85 } catch (Exception e) {
86 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion Table"+e);
91 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
98 public void Save(PolicyVersion policyVersion) {
99 Session session = sessionfactory.openSession();
100 Transaction tx = session.beginTransaction();
102 session.persist(policyVersion);
105 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving PolicyVersion Table"+e);
109 }catch(Exception e1){
110 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
116 public void delete(PolicyVersion policyVersion) {
117 Session session = sessionfactory.openSession();
118 Transaction tx = session.beginTransaction();
120 session.delete(policyVersion);
123 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting PolicyVersion Table"+e);
127 }catch(Exception e1){
128 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
134 public void update(PolicyVersion policyVersion) {
135 Session session = sessionfactory.openSession();
136 Transaction tx = session.beginTransaction();
138 session.update(policyVersion);
141 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating PolicyVersion Table"+e);
145 }catch(Exception e1){
146 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
151 @SuppressWarnings("unchecked")
153 public List<PolicyVersion> getPolicyVersionEntityByName(String policyVersion) {
154 Session session = sessionfactory.openSession();
155 Transaction tx = session.beginTransaction();
156 List<PolicyVersion> data = null;
158 Criteria cr = session.createCriteria(PolicyVersion.class);
159 cr.add(Restrictions.eq("policyName",policyVersion));
162 } catch (Exception e) {
163 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion Table"+e);
167 }catch(Exception e1){
168 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
174 @SuppressWarnings("unchecked")
176 public List<PolicyVersion> getActiveVersionPolicy(String query) {
177 Session session = sessionfactory.openSession();
178 Transaction tx = session.beginTransaction();
179 List<PolicyVersion> data = null;
181 Query hbquery = session.createQuery(query);
182 data = hbquery.list();
184 } catch (Exception e) {
185 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion Table"+e);
189 }catch(Exception e1){
190 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
197 public void updateQuery(String policyVersion) {
198 Session session = sessionfactory.openSession();
199 Transaction tx = session.beginTransaction();
201 Query hbquery = session.createQuery(policyVersion);
202 hbquery.executeUpdate();
204 } catch (Exception e) {
205 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating PolicyVersion Table"+e);
209 }catch(Exception e1){
210 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);