View Javadoc
1   /*
2    * Copyright 2012-2019 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.es.log.exentity;
17  
18  import java.time.LocalDateTime;
19  import java.time.ZoneId;
20  import java.time.ZonedDateTime;
21  import java.time.format.DateTimeFormatter;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.stream.Collectors;
27  
28  import org.codelibs.core.lang.StringUtil;
29  import org.codelibs.core.misc.Pair;
30  import org.codelibs.fess.Constants;
31  import org.codelibs.fess.es.log.bsentity.BsSearchLog;
32  import org.codelibs.fess.es.log.exbhv.UserInfoBhv;
33  import org.codelibs.fess.util.ComponentUtil;
34  import org.dbflute.optional.OptionalEntity;
35  
36  /**
37   * @author FreeGen
38   */
39  public class SearchLog extends BsSearchLog {
40  
41      private static final long serialVersionUID = 1L;
42  
43      private final List<Pair<String, String>> searchFieldLogList = new ArrayList<>();
44  
45      private OptionalEntity<UserInfo> userInfo;
46  
47      private Map<String, Object> fields;
48  
49      private final List<Map<String, Object>> documentList = new ArrayList<>();
50  
51      public String getId() {
52          return asDocMeta().id();
53      }
54  
55      public void setId(final String id) {
56          asDocMeta().id(id);
57      }
58  
59      public Long getVersionNo() {
60          return asDocMeta().version();
61      }
62  
63      public void setVersionNo(final Long version) {
64          asDocMeta().version(version);
65      }
66  
67      public void addSearchFieldLogValue(final String name, final String value) {
68          if (StringUtil.isNotBlank(name) && StringUtil.isNotBlank(value)) {
69              searchFieldLogList.add(new Pair<>(name, value));
70          }
71      }
72  
73      public void addDocument(final Map<String, Object> doc) {
74          documentList.add(doc);
75      }
76  
77      public void setSearchQuery(final String query) {
78          addSearchFieldLogValue(Constants.SEARCH_FIELD_LOG_SEARCH_QUERY, query);
79      }
80  
81      public OptionalEntity<UserInfo> getUserInfo() {
82          if (getUserInfoId() == null) {
83              return OptionalEntity.empty();
84          } else if (userInfo == null) {
85              final UserInfoBhv userInfoBhv = ComponentUtil.getComponent(UserInfoBhv.class);
86              userInfo = userInfoBhv.selectByPK(getUserInfoId());
87          }
88          return userInfo;
89      }
90  
91      public void setUserInfo(final OptionalEntity<UserInfo> userInfo) {
92          this.userInfo = userInfo;
93          userInfo.ifPresent(e -> {
94              super.setUserInfoId(e.getId());
95          });
96      }
97  
98      public List<Pair<String, String>> getSearchFieldLogList() {
99          return searchFieldLogList;
100     }
101 
102     public void addField(final String key, final Object value) {
103         fields.put(key, value);
104     }
105 
106     public String getLogMessage() {
107         return getSearchWord();
108     }
109 
110     @Override
111     public Map<String, Object> toSource() {
112         final Map<String, Object> sourceMap = super.toSource();
113         if (fields != null) {
114             sourceMap.putAll(fields);
115         }
116         final Map<String, List<String>> searchFieldMap =
117                 searchFieldLogList.stream().collect(
118                         Collectors.groupingBy(Pair::getFirst, Collectors.mapping(Pair::getSecond, Collectors.toList())));
119         sourceMap.put("searchField", searchFieldMap);
120         sourceMap.put("documents", documentList);
121         return sourceMap;
122     }
123 
124     @Override
125     protected void addFieldToSource(final Map<String, Object> sourceMap, final String field, final Object value) {
126         if (value instanceof LocalDateTime) {
127             final LocalDateTime ldt = (LocalDateTime) value;
128             final ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
129             super.addFieldToSource(sourceMap, field, DateTimeFormatter.ISO_INSTANT.format(zdt));
130         } else {
131             super.addFieldToSource(sourceMap, field, value);
132         }
133     }
134 
135     @Override
136     public void setUserInfoId(final String value) {
137         userInfo = null;
138         super.setUserInfoId(value);
139     }
140 
141     @Override
142     public String toString() {
143         return "SearchLog [searchFieldLogList=" + searchFieldLogList + ", userInfo=" + userInfo + ", fields=" + fields + ", accessType="
144                 + accessType + ", clientIp=" + clientIp + ", hitCount=" + hitCount + ", languages=" + languages + ", queryId=" + queryId
145                 + ", queryOffset=" + queryOffset + ", queryPageSize=" + queryPageSize + ", queryTime=" + queryTime + ", referer=" + referer
146                 + ", requestedAt=" + requestedAt + ", responseTime=" + responseTime + ", roles=" + Arrays.toString(roles) + ", searchWord="
147                 + searchWord + ", user=" + user + ", userAgent=" + userAgent + ", userInfoId=" + userInfoId + ", userSessionId="
148                 + userSessionId + ", virtualHost=" + virtualHost + ", documents=" + documentList + "]";
149     }
150 
151 }