1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.BsUserBhv;
23 import org.codelibs.fess.es.user.exentity.User;
24 import org.codelibs.fess.util.ComponentUtil;
25 import org.dbflute.exception.IllegalBehaviorStateException;
26 import org.dbflute.util.DfTypeUtil;
27
28
29
30
31 public class UserBhv extends BsUserBhv {
32
33 private static final String ROLES = "roles";
34 private static final String GROUPS = "groups";
35 private static final String PASSWORD = "password";
36 private static final String NAME = "name";
37
38 @Override
39 protected String asEsIndex() {
40 return ComponentUtil.getFessConfig().getIndexUserIndex();
41 }
42
43 @Override
44 protected <RESULT extends User> RESULT createEntity(final Map<String, Object> source, final Class<? extends RESULT> entityType) {
45 try {
46 final RESULT result = entityType.newInstance();
47 result.setName(DfTypeUtil.toString(source.get(NAME)));
48 result.setPassword(DfTypeUtil.toString(source.get(PASSWORD)));
49 result.setGroups(toStringArray(source.get(GROUPS)));
50 result.setRoles(toStringArray(source.get(ROLES)));
51 result.setAttributes(source.entrySet().stream().filter(e -> isAttribute(e.getKey()))
52 .map(e -> new Pair<>(e.getKey(), (String) e.getValue()))
53 .collect(Collectors.toMap(t -> t.getFirst(), t -> t.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
61 private boolean isAttribute(final String key) {
62 return !NAME.equals(key) && !PASSWORD.equals(key) && !GROUPS.equals(key) && !ROLES.equals(key);
63 }
64
65 }