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.user.exbhv;
17  
18  import java.util.Map;
19  import java.util.regex.Pattern;
20  import java.util.stream.Collectors;
21  
22  import org.codelibs.core.misc.Pair;
23  import org.codelibs.fess.opensearch.user.bsbhv.BsRoleBhv;
24  import org.codelibs.fess.opensearch.user.exentity.Role;
25  import org.codelibs.fess.util.ComponentUtil;
26  import org.dbflute.exception.IllegalBehaviorStateException;
27  import org.dbflute.util.DfTypeUtil;
28  
29  /**
30   * @author FreeGen
31   */
32  public class RoleBhv extends BsRoleBhv {
33      private String indexName = null;
34  
35      @Override
36      protected String asEsIndex() {
37          if (indexName == null) {
38              final String name = ComponentUtil.getFessConfig().getIndexUserIndex();
39              indexName = super.asEsIndex().replaceFirst(Pattern.quote("fess_user"), name);
40          }
41          return indexName;
42      }
43  
44      @Override
45      protected <RESULT extends Role> RESULT createEntity(final Map<String, Object> source, final Class<? extends RESULT> entityType) {
46          try {
47              final RESULT result = entityType.newInstance();
48              result.setName(DfTypeUtil.toString(source.get("name")));
49              result.setAttributes(source.entrySet()
50                      .stream()
51                      .filter(e -> !"name".equals(e.getKey()))
52                      .map(e -> new Pair<>(e.getKey(), (String) e.getValue()))
53                      .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond)));
54              return result;
55          } catch (InstantiationException | IllegalAccessException e) {
56              final String msg = "Cannot create a new instance: " + entityType.getName();
57              throw new IllegalBehaviorStateException(msg, e);
58          }
59      }
60  }