1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.BsUserBhv;
24 import org.codelibs.fess.opensearch.user.exentity.User;
25 import org.codelibs.fess.util.ComponentUtil;
26 import org.dbflute.exception.IllegalBehaviorStateException;
27 import org.dbflute.util.DfTypeUtil;
28
29
30
31
32 public class UserBhv extends BsUserBhv {
33
34 private static final String ROLES = "roles";
35 private static final String GROUPS = "groups";
36 private static final String PASSWORD = "password";
37 private static final String NAME = "name";
38
39 private String indexName = null;
40
41 @Override
42 protected String asEsIndex() {
43 if (indexName == null) {
44 final String name = ComponentUtil.getFessConfig().getIndexUserIndex();
45 indexName = super.asEsIndex().replaceFirst(Pattern.quote("fess_user"), name);
46 }
47 return indexName;
48 }
49
50 @Override
51 protected <RESULT extends User> RESULT createEntity(final Map<String, Object> source, final Class<? extends RESULT> entityType) {
52 try {
53 final RESULT result = entityType.newInstance();
54 result.setName(DfTypeUtil.toString(source.get(NAME)));
55 result.setPassword(DfTypeUtil.toString(source.get(PASSWORD)));
56 result.setGroups(toStringArray(source.get(GROUPS)));
57 result.setRoles(toStringArray(source.get(ROLES)));
58 result.setAttributes(source.entrySet()
59 .stream()
60 .filter(e -> isAttribute(e.getKey()))
61 .map(e -> new Pair<>(e.getKey(), (String) e.getValue()))
62 .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond)));
63 return result;
64 } catch (InstantiationException | IllegalAccessException e) {
65 final String msg = "Cannot create a new instance: " + entityType.getName();
66 throw new IllegalBehaviorStateException(msg, e);
67 }
68 }
69
70 private boolean isAttribute(final String key) {
71 return !NAME.equals(key) && !PASSWORD.equals(key) && !GROUPS.equals(key) && !ROLES.equals(key);
72 }
73
74 }