View Javadoc
1   /*
2    * Copyright 2012-2021 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.Map;
23  
24  import org.codelibs.fess.entity.SearchLogEvent;
25  import org.codelibs.fess.es.log.bsentity.BsUserInfo;
26  
27  /**
28   * @author FreeGen
29   */
30  public class UserInfo extends BsUserInfo implements SearchLogEvent {
31  
32      private static final long serialVersionUID = 1L;
33  
34      private Map<String, Object> fields;
35  
36      @Override
37      public String getId() {
38          return asDocMeta().id();
39      }
40  
41      public void setId(final String id) {
42          asDocMeta().id(id);
43      }
44  
45      @Override
46      public Long getVersionNo() {
47          return asDocMeta().version();
48      }
49  
50      public void setVersionNo(final Long version) {
51          asDocMeta().version(version);
52      }
53  
54      public String getLogMessage() {
55          return getId();
56      }
57  
58      public LocalDateTime getRequestedAt() {
59          return getUpdatedAt();
60      }
61  
62      public void addField(final String key, final Object value) {
63          fields.put(key, value);
64      }
65  
66      @Override
67      public Map<String, Object> toSource() {
68          final Map<String, Object> sourceMap = super.toSource();
69          if (fields != null) {
70              sourceMap.putAll(fields);
71          }
72          return sourceMap;
73      }
74  
75      @Override
76      protected void addFieldToSource(final Map<String, Object> sourceMap, final String field, final Object value) {
77          if (value instanceof LocalDateTime) {
78              final LocalDateTime ldt = (LocalDateTime) value;
79              final ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
80              super.addFieldToSource(sourceMap, field, DateTimeFormatter.ISO_INSTANT.format(zdt));
81          } else {
82              super.addFieldToSource(sourceMap, field, value);
83          }
84      }
85  
86      @Override
87      public String toString() {
88          return "UserInfo [createdAt=" + createdAt + ", updatedAt=" + updatedAt + ", docMeta=" + docMeta + "]";
89      }
90  
91      @Override
92      public String getEventType() {
93          return "user";
94      }
95  }