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.exbhv;
17  
18  import java.time.Instant;
19  import java.time.LocalDateTime;
20  import java.time.ZoneId;
21  import java.time.format.DateTimeFormatter;
22  import java.time.format.DateTimeParseException;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.regex.Pattern;
26  
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.codelibs.core.misc.Pair;
30  import org.codelibs.fess.es.log.bsbhv.BsSearchLogBhv;
31  import org.codelibs.fess.es.log.exentity.SearchLog;
32  import org.codelibs.fess.util.ComponentUtil;
33  import org.dbflute.exception.IllegalBehaviorStateException;
34  import org.dbflute.util.DfTypeUtil;
35  
36  /**
37   * @author FreeGen
38   */
39  public class SearchLogBhv extends BsSearchLogBhv {
40      private static final Logger logger = LogManager.getLogger(SearchLogBhv.class);
41  
42      private String indexName = null;
43  
44      @Override
45      protected String asEsIndex() {
46          if (indexName == null) {
47              final String name = ComponentUtil.getFessConfig().getIndexLogIndex();
48              indexName = super.asEsIndex().replaceFirst(Pattern.quote("fess_log"), name);
49          }
50          return indexName;
51      }
52  
53      @Override
54      protected LocalDateTime toLocalDateTime(final Object value) {
55          if (value != null) {
56              try {
57                  final Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value.toString()));
58                  final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
59                  return date;
60              } catch (final DateTimeParseException e) {
61                  logger.debug("Invalid date format: {}", value, e);
62              }
63          }
64          return DfTypeUtil.toLocalDateTime(value);
65      }
66  
67      @SuppressWarnings("unchecked")
68      @Override
69      protected <RESULT extends SearchLog> RESULT createEntity(final Map<String, Object> source, final Class<? extends RESULT> entityType) {
70          try {
71              final RESULT result = super.createEntity(source, entityType);
72              final Object searchFieldObj = source.get("searchField");
73              if (searchFieldObj instanceof Map) {
74                  ((Map<String, ?>) searchFieldObj).entrySet().stream().forEach(e -> {
75                      if (e.getValue() instanceof String[]) {
76                          final String[] values = (String[]) e.getValue();
77                          for (final String v : values) {
78                              result.getSearchFieldLogList().add(new Pair<>(e.getKey(), v));
79                          }
80                      } else if (e.getValue() instanceof List) {
81                          final List<String> values = (List<String>) e.getValue();
82                          for (final String v : values) {
83                              result.getSearchFieldLogList().add(new Pair<>(e.getKey(), v));
84                          }
85                      } else if (e.getValue() != null) {
86                          result.getSearchFieldLogList().add(new Pair<>(e.getKey(), e.getValue().toString()));
87                      }
88                  });
89              }
90              return result;
91          } catch (final Exception e) {
92              final String msg = "Cannot create a new instance: " + entityType.getName();
93              throw new IllegalBehaviorStateException(msg, e);
94          }
95      }
96  
97  }