a7b71612a0738a5e1451078fca222051237fe48d
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / EPAuditServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.time.LocalDate;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.stereotype.Service;
31 import org.springframework.transaction.annotation.Transactional;
32
33 import org.openecomp.portalsdk.core.domain.AuditLog;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalsdk.core.service.DataAccessService;
36 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
37 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
38
39 @Service("epAuditService")
40 @Transactional
41 @org.springframework.context.annotation.Configuration
42 @EnableAspectJAutoProxy
43 @EPMetricsLog
44 public class EPAuditServiceImpl implements EPAuditService {
45
46         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPAuditServiceImpl.class);
47
48         @Autowired
49         private DataAccessService dataAccessService;
50
51         @Override
52         /*
53          * get the guest last login time with orgUserId as param. If record not
54          * found in table, return null.
55          * 
56          * (non-Javadoc)
57          * 
58          * @see
59          * org.openecomp.portalapp.portal.service.EPUserService#getGuestLastLogin(
60          * java.lang.String)
61          */
62         public Date getGuestLastLogin(String userId) {
63                 Map<String, String> params = new HashMap<>();
64                 params.put("userId", userId);
65                 @SuppressWarnings("unchecked")
66                 List<Date> list = getDataAccessService().executeNamedQuery("getGuestLastLogin", params, null);
67                 Date date = null;
68                 if (list != null) {
69                         /*
70                          * if list only contains one item, meaning this is the first time
71                          * user logs in or record not found in db
72                          */
73                         if (list.size() == 1)
74                                 date = list.get(0); /* the guest's current log in time */
75                         else if (list.size() == 2)
76                                 date = list.get(1); /* most recent login date from db */
77                 }
78                 return date;
79         }
80
81         /*
82          * Cleans all the records in fn_audit_log table that are less than defined
83          * date in system.property
84          * 
85          * (non-Javadoc)
86          * 
87          * @see
88          * org.openecomp.portalapp.portal.service.EPAuditService#delAuditLogFromDay(
89          * )
90          */
91         @Override
92         public void delAuditLogFromDay() {
93                 if (EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.AUDITLOG_DEL_DAY_FROM)) {
94                         String day = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUDITLOG_DEL_DAY_FROM);
95                         LocalDate removeDateFrom = LocalDate.now().minusDays(Integer.valueOf(day));
96                         getDataAccessService().deleteDomainObjects(AuditLog.class, "audit_date  <'" + removeDateFrom + "'", null);
97                 } else {
98                         logger.error(EELFLoggerDelegate.errorLogger,
99                                         "delAuditLogFromDay Exception = system.propertiy value is empty on"
100                                                         + EPCommonSystemProperties.AUDITLOG_DEL_DAY_FROM);
101                 }
102         }
103
104         public DataAccessService getDataAccessService() {
105                 return dataAccessService;
106         }
107
108         public void setDataAccessService(DataAccessService dataAccessService) {
109                 this.dataAccessService = dataAccessService;
110         }
111
112 }