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.regex.Pattern;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.codelibs.fess.es.log.bsbhv.BsFavoriteLogBhv;
28  import org.codelibs.fess.util.ComponentUtil;
29  import org.dbflute.util.DfTypeUtil;
30  
31  /**
32   * @author FreeGen
33   */
34  public class FavoriteLogBhv extends BsFavoriteLogBhv {
35      private static final Logger logger = LogManager.getLogger(FavoriteLogBhv.class);
36  
37      private String indexName = null;
38  
39      @Override
40      protected String asEsIndex() {
41          if (indexName == null) {
42              final String name = ComponentUtil.getFessConfig().getIndexLogIndex();
43              indexName = super.asEsIndex().replaceFirst(Pattern.quote("fess_log"), name);
44          }
45          return indexName;
46      }
47  
48      @Override
49      protected LocalDateTime toLocalDateTime(final Object value) {
50          if (value != null) {
51              try {
52                  final Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value.toString()));
53                  final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
54                  return date;
55              } catch (final DateTimeParseException e) {
56                  logger.debug("Invalid date format: {}", value, e);
57              }
58          }
59          return DfTypeUtil.toLocalDateTime(value);
60      }
61  
62  }