[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / EPAuditServiceImpl.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.service;\r
21 \r
22 import java.time.LocalDate;\r
23 import java.util.Date;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 \r
28 import org.springframework.beans.factory.annotation.Autowired;\r
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
30 import org.springframework.stereotype.Service;\r
31 import org.springframework.transaction.annotation.Transactional;\r
32 \r
33 import org.openecomp.portalsdk.core.domain.AuditLog;\r
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
35 import org.openecomp.portalsdk.core.service.DataAccessService;\r
36 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;\r
37 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;\r
38 \r
39 @Service("epAuditService")\r
40 @Transactional\r
41 @org.springframework.context.annotation.Configuration\r
42 @EnableAspectJAutoProxy\r
43 @EPMetricsLog\r
44 public class EPAuditServiceImpl implements EPAuditService {\r
45         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPAuditServiceImpl.class);\r
46 \r
47         @Autowired\r
48         private DataAccessService  dataAccessService;\r
49 \r
50         @Override\r
51         /* get the guest last login time with orgUserId as param.\r
52          * If record not found in table, return null.\r
53          *  \r
54          * (non-Javadoc)\r
55          * @see org.openecomp.portalapp.portal.service.EPUserService#getGuestLastLogin(java.lang.String)\r
56          */\r
57         public Date getGuestLastLogin(String userId) {\r
58                 Map<String, String> params = new HashMap<>();\r
59                 params.put("userId", userId);\r
60                 List<Date> list = getDataAccessService().executeNamedQuery("getGuestLastLogin", params, null);  \r
61                 Date date=null;\r
62                 if(list!=null){\r
63                         if(list.size()==1) /* if list only contains one item, meaning this is the first time user logs in or record not found in db*/\r
64                                 date = list.get(0); /*the guest's current log in time*/\r
65                         else if(list.size()==2)\r
66                                 date = list.get(1); /*most recent login date from db*/\r
67                 }\r
68                 return date;    \r
69         }\r
70 \r
71         @Override\r
72         /* Clean all the records in fn_audit_log table that are less than defined date in system.property\r
73          * \r
74          * (non-Javadoc)\r
75          * @see org.openecomp.portalapp.portal.service.EPAuditService#delAuditLogFromDay()\r
76          */\r
77         public void delAuditLogFromDay(){       \r
78                 if (EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.AUDITLOG_DEL_DAY_FROM)) {\r
79                         String day = EPCommonSystemProperties.getProperty(EPCommonSystemProperties.AUDITLOG_DEL_DAY_FROM);                              \r
80                         LocalDate removeDateFrom = LocalDate.now().minusDays(Integer.valueOf(day));\r
81                         getDataAccessService().deleteDomainObjects(AuditLog.class, "audit_date  <'"+removeDateFrom+"'",null);\r
82                 }else{\r
83                         logger.error(EELFLoggerDelegate.errorLogger, "delAuditLogFromDay Exception = system.propertiy value is empty on" + EPCommonSystemProperties.AUDITLOG_DEL_DAY_FROM);\r
84                 }       \r
85         }\r
86         \r
87         public DataAccessService getDataAccessService() {\r
88                 return dataAccessService;\r
89         }\r
90 \r
91         public void setDataAccessService(DataAccessService dataAccessService) {\r
92                 this.dataAccessService = dataAccessService;\r
93         }\r
94 \r
95 \r
96 }\r