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.ds.impl;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.codelibs.core.lang.StringUtil;
27  import org.codelibs.fess.Constants;
28  import org.codelibs.fess.ds.DataStore;
29  import org.codelibs.fess.ds.IndexUpdateCallback;
30  import org.codelibs.fess.es.config.exentity.DataConfig;
31  import org.codelibs.fess.helper.CrawlingInfoHelper;
32  import org.codelibs.fess.helper.SystemHelper;
33  import org.codelibs.fess.mylasta.direction.FessConfig;
34  import org.codelibs.fess.util.ComponentUtil;
35  import org.codelibs.fess.util.GroovyUtil;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  public abstract class AbstractDataStoreImpl implements DataStore {
40  
41      private static final Logger logger = LoggerFactory.getLogger(AbstractDataStoreImpl.class);
42  
43      public String mimeType = "application/datastore";
44  
45      protected boolean alive = true;
46  
47      @Override
48      public void stop() {
49          alive = false;
50      }
51  
52      @Override
53      public void store(final DataConfig config, final IndexUpdateCallback callback, final Map<String, String> initParamMap) {
54          final Map<String, String> configParamMap = config.getHandlerParameterMap();
55          final Map<String, String> configScriptMap = config.getHandlerScriptMap();
56          final CrawlingInfoHelper crawlingInfoHelper = ComponentUtil.getCrawlingInfoHelper();
57          final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
58          final Date documentExpires = crawlingInfoHelper.getDocumentExpires(config);
59          final FessConfig fessConfig = ComponentUtil.getFessConfig();
60  
61          initParamMap.putAll(configParamMap);
62          final Map<String, String> paramMap = initParamMap;
63  
64          // default values
65          final Map<String, Object> defaultDataMap = new HashMap<>();
66  
67          // cid
68          final String configId = config.getConfigId();
69          if (configId != null) {
70              defaultDataMap.put(fessConfig.getIndexFieldConfigId(), configId);
71          }
72          //  expires
73          if (documentExpires != null) {
74              defaultDataMap.put(fessConfig.getIndexFieldExpires(), documentExpires);
75          }
76          // segment
77          defaultDataMap.put(fessConfig.getIndexFieldSegment(), initParamMap.get(Constants.SESSION_ID));
78          // created
79          defaultDataMap.put(fessConfig.getIndexFieldCreated(), systemHelper.getCurrentTime());
80          // boost
81          defaultDataMap.put(fessConfig.getIndexFieldBoost(), config.getBoost().toString());
82          // label: labelType
83          final List<String> labelTypeList = new ArrayList<>();
84          for (final String labelType : config.getLabelTypeValues()) {
85              labelTypeList.add(labelType);
86          }
87          defaultDataMap.put(fessConfig.getIndexFieldLabel(), labelTypeList);
88          // role: roleType
89          final List<String> roleTypeList = new ArrayList<>();
90          stream(config.getPermissions()).of(stream -> stream.forEach(p -> roleTypeList.add(p)));
91          defaultDataMap.put(fessConfig.getIndexFieldRole(), roleTypeList);
92          // mimetype
93          defaultDataMap.put(fessConfig.getIndexFieldMimetype(), mimeType);
94          // title
95          // content
96          // cache
97          // digest
98          // host
99          // site
100         // url
101         // anchor
102         // content_length
103         // last_modified
104         // id
105 
106         storeData(config, callback, paramMap, configScriptMap, defaultDataMap);
107 
108     }
109 
110     protected Object convertValue(final String template, final Map<String, Object> paramMap) {
111         if (StringUtil.isEmpty(template)) {
112             return StringUtil.EMPTY;
113         }
114 
115         if (paramMap.containsKey(template)) {
116             return paramMap.get(template);
117         }
118 
119         return GroovyUtil.evaluate(template, paramMap);
120     }
121 
122     protected long getReadInterval(final Map<String, String> paramMap) {
123         long readInterval = 0;
124         final String value = paramMap.get("readInterval");
125         if (StringUtil.isNotBlank(value)) {
126             try {
127                 readInterval = Long.parseLong(value);
128             } catch (final NumberFormatException e) {
129                 logger.warn("Invalid read interval: " + value);
130             }
131         }
132         return readInterval;
133     }
134 
135     protected void sleep(final long interval) {
136         try {
137             Thread.sleep(interval);
138         } catch (final Exception e) {
139             // ignore
140         }
141     }
142 
143     protected abstract void storeData(DataConfig dataConfig, IndexUpdateCallback callback, Map<String, String> paramMap,
144             Map<String, String> scriptMap, Map<String, Object> defaultDataMap);
145 }