Unit/SONAR/Checkstyle in ONAP-REST
[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, 2019 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 import java.util.List;
24
25 import org.hibernate.Criteria;
26 import org.hibernate.Session;
27 import org.hibernate.Transaction;
28 import org.hibernate.criterion.Restrictions;
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.conf.HibernateSession;
32 import org.onap.policy.controller.PolicyController;
33 import org.onap.policy.dao.SystemLogDbDao;
34 import org.onap.policy.rest.jpa.SystemLogDb;
35 import org.onap.policy.xacml.api.XACMLErrorConstants;
36 import org.springframework.stereotype.Service;
37
38 @Service("SystemLogDbDao")
39 public class SystemLogDbDaoImpl implements SystemLogDbDao {
40     private static final Logger logger = FlexLogger.getLogger(SystemLogDbDaoImpl.class);
41
42     @SuppressWarnings("unchecked")
43     @Override
44     public List<SystemLogDb> getLoggingData() {
45         Session session = HibernateSession.getSession();
46         Transaction tx = session.beginTransaction();
47         List<SystemLogDb> system = null;
48         try {
49             String sqlWhere = null;
50             if (PolicyController.isjUnit()) {
51                 sqlWhere = "";
52             } else {
53                 sqlWhere = "date > DATE_SUB(curdate(), INTERVAL 5 DAY) ORDER BY date DESC limit "
54                         + PolicyController.getLogTableLimit() + "";
55             }
56             Criteria cr = session.createCriteria(SystemLogDb.class);
57             cr.add(Restrictions.sqlRestriction(sqlWhere));
58             system = cr.list();
59             tx.commit();
60         } catch (Exception e) {
61             logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying SystemLogDb 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 system;
70     }
71
72     @SuppressWarnings("unchecked")
73     @Override
74     public List<SystemLogDb> getSystemAlertData() {
75         Session session = HibernateSession.getSession();
76         Transaction tx = session.beginTransaction();
77         List<SystemLogDb> system = null;
78         try {
79             String sqlWhere = null;
80             if (PolicyController.isjUnit()) {
81                 sqlWhere = "";
82             } else {
83                 sqlWhere = "date > DATE_SUB(curdate(), INTERVAL 5 DAY) and logtype = 'error' ORDER BY date DESC limit "
84                         + PolicyController.getSystemAlertTableLimit() + "";
85             }
86             Criteria cr = session.createCriteria(SystemLogDb.class);
87             cr.add(Restrictions.sqlRestriction(sqlWhere));
88             system = cr.list();
89             tx.commit();
90         } catch (Exception e) {
91             logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying SystemLogDb 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         return system;
100     }
101
102 }