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