View Javadoc
1   /*
2    * Copyright 2012-2025 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.opensearch.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.opensearch.log.bsentity.BsFavoriteLog;
26  
27  /**
28   * @author FreeGen
29   */
30  public class FavoriteLog extends BsFavoriteLog 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 void addField(final String key, final Object value) {
55          fields.put(key, value);
56      }
57  
58      public String getLogMessage() {
59          return getUrl();
60      }
61  
62      public LocalDateTime getRequestedAt() {
63          return getCreatedAt();
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 final LocalDateTime ldt) {
78              final ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
79              super.addFieldToSource(sourceMap, field, DateTimeFormatter.ISO_INSTANT.format(zdt));
80          } else {
81              super.addFieldToSource(sourceMap, field, value);
82          }
83      }
84  
85      @Override
86      public String toString() {
87          return "FavoriteLog [createdAt=" + createdAt + ", url=" + url + ", docId=" + docId + ", queryId=" + queryId + ", userInfoId="
88                  + userInfoId + ", docMeta=" + docMeta + "]";
89      }
90  
91      @Override
92      public String getEventType() {
93          return "favorite";
94      }
95  }