Addressing Technical Debt for POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / daoImp / SystemLogDbDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP 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.onap.policy.daoImp;
22
23
24 import java.util.List;
25
26 import org.hibernate.Criteria;
27 import org.hibernate.Session;
28 import org.springframework.stereotype.Service;
29
30 import org.onap.policy.xacml.api.XACMLErrorConstants;
31
32 import org.hibernate.Transaction;
33 import org.hibernate.criterion.Restrictions;
34 import org.onap.policy.conf.HibernateSession;
35 import org.onap.policy.controller.PolicyController;
36 import org.onap.policy.dao.SystemLogDbDao;
37 import org.onap.policy.rest.jpa.SystemLogDB;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger; 
39 import org.onap.policy.common.logging.flexlogger.Logger;
40
41
42 @Service("SystemLogDbDao")
43 public class SystemLogDbDaoImpl implements SystemLogDbDao {
44         private static final Logger logger = FlexLogger.getLogger(SystemLogDbDaoImpl.class);
45         
46         @SuppressWarnings("unchecked")
47         @Override
48         public List<SystemLogDB> getLoggingData() {
49                 Session session = HibernateSession.getSession();
50                 Transaction tx = session.beginTransaction();
51                 List<SystemLogDB> system = null;
52         try {
53                 String sqlWhere = null;
54                 if(PolicyController.isjUnit()){
55                         sqlWhere = "";
56                 }else{
57                         sqlWhere = "date > DATE_SUB(curdate(), INTERVAL 5 DAY) ORDER BY date DESC limit "+PolicyController.getLogTableLimit()+"";
58                 }
59                 Criteria cr = session.createCriteria(SystemLogDB.class);
60                 cr.add(Restrictions.sqlRestriction(sqlWhere));
61             system = cr.list();
62                         tx.commit();
63                 } catch (Exception e) {
64                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying SystemLogDB Table"+e);      
65                 }finally{
66                         try{
67                                 session.close();
68                         }catch(Exception e1){
69                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
70                         }
71                 }
72                 return system;
73         }
74
75         @SuppressWarnings("unchecked")
76         @Override
77         public List<SystemLogDB> getSystemAlertData() {
78                 Session session = HibernateSession.getSession();
79                 Transaction tx = session.beginTransaction();
80                 List<SystemLogDB> system = null;
81         try {
82                 String sqlWhere = null;
83                 if(PolicyController.isjUnit()){
84                         sqlWhere = "";
85                 }else{
86                         sqlWhere = "date > DATE_SUB(curdate(), INTERVAL 5 DAY) and logtype = 'error' ORDER BY date DESC limit "+PolicyController.getSystemAlertTableLimit()+"";
87                 }
88                 Criteria cr = session.createCriteria(SystemLogDB.class);
89                 cr.add(Restrictions.sqlRestriction(sqlWhere));
90             system = cr.list();
91                         tx.commit();
92                 } catch (Exception e) {
93                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying SystemLogDB Table"+e);      
94                 }finally{
95                         try{
96                                 session.close();
97                         }catch(Exception e1){
98                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
99                         }
100                 }
101                 return system;
102         }
103
104 }