View Javadoc
1   /*
2    * Copyright 2012-2017 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      public String getId() {
50          return asDocMeta().id();
51      }
52  
53      public void setId(final String id) {
54          asDocMeta().id(id);
55      }
56  
57      public Long getVersionNo() {
58          return asDocMeta().version();
59      }
60  
61      public void setVersionNo(final Long version) {
62          asDocMeta().version(version);
63      }
64  
65      public void addSearchFieldLogValue(final String name, final String value) {
66          if (StringUtil.isNotBlank(name) && StringUtil.isNotBlank(value)) {
67              searchFieldLogList.add(new Pair<>(name, value));
68          }
69      }
70  
71      public void setSearchQuery(final String query) {
72          addSearchFieldLogValue(Constants.SEARCH_FIELD_LOG_SEARCH_QUERY, query);
73      }
74  
75      public OptionalEntity<UserInfo> getUserInfo() {
76          if (getUserInfoId() == null) {
77              return OptionalEntity.empty();
78          } else if (userInfo == null) {
79              final UserInfoBhv userInfoBhv = ComponentUtil.getComponent(UserInfoBhv.class);
80              userInfo = userInfoBhv.selectByPK(getUserInfoId());
81          }
82          return userInfo;
83      }
84  
85      public void setUserInfo(final OptionalEntity<UserInfo> userInfo) {
86          this.userInfo = userInfo;
87      }
88  
89      public List<Pair<String, String>> getSearchFieldLogList() {
90          return searchFieldLogList;
91      }
92  
93      public void addField(final String key, final Object value) {
94          fields.put(key, value);
95      }
96  
97      @Override
98      public Map<String, Object> toSource() {
99          final Map<String, Object> sourceMap = super.toSource();
100         if (fields != null) {
101             sourceMap.putAll(fields);
102         }
103         final Map<String, List<Object>> searchFieldMap =
104                 searchFieldLogList.stream().collect(
105                         Collectors.groupingBy(Pair::getFirst, Collectors.mapping(Pair::getSecond, Collectors.toList())));
106         sourceMap.put("searchField", searchFieldMap);
107         return sourceMap;
108     }
109 
110     @Override
111     protected void addFieldToSource(final Map<String, Object> sourceMap, final String field, final Object value) {
112         if (value instanceof LocalDateTime) {
113             final LocalDateTime ldt = (LocalDateTime) value;
114             final ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
115             super.addFieldToSource(sourceMap, field, DateTimeFormatter.ISO_INSTANT.format(zdt));
116         } else {
117             super.addFieldToSource(sourceMap, field, value);
118         }
119     }
120 
121     @Override
122     public void setUserInfoId(final String value) {
123         userInfo = null;
124         super.setUserInfoId(value);
125     }
126 
127     @Override
128     public String toString() {
129         return "SearchLog [searchFieldLogList=" + searchFieldLogList + ", userInfo=" + userInfo + ", accessType=" + accessType + ", user="
130                 + user + ", roles=" + Arrays.toString(roles) + ", queryId=" + queryId + ", clientIp=" + clientIp + ", hitCount=" + hitCount
131                 + ", queryOffset=" + queryOffset + ", queryPageSize=" + queryPageSize + ", referer=" + referer + ", requestedAt="
132                 + requestedAt + ", responseTime=" + responseTime + ", queryTime=" + queryTime + ", searchWord=" + searchWord
133                 + ", userAgent=" + userAgent + ", userInfoId=" + userInfoId + ", userSessionId=" + userSessionId + ", docMeta=" + docMeta
134                 + ", languages=" + languages + "]";
135     }
136 }