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