1
|
1 package de.mpiwg.itgroup.ismi.entry.dataBeans;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.HashMap;
|
|
5 import java.util.List;
|
|
6 import java.util.Map;
|
|
7
|
|
8 import org.apache.commons.lang.StringUtils;
|
|
9 import org.mpi.openmind.repository.bo.Entity;
|
|
10 import org.mpi.openmind.repository.bo.Relation;
|
|
11 import org.mpi.openmind.repository.services.ServiceRegistry;
|
|
12
|
|
13 public class RoleCache extends AbstractCache{
|
|
14 Map<String, List<Long>> roleMap = null;
|
|
15
|
|
16 public RoleCache(ServiceRegistry sr) {
|
|
17 super(sr);
|
|
18 }
|
|
19
|
|
20 @Override
|
|
21 public void reset(){
|
|
22 super.reset();
|
|
23 loadRoleMap();
|
|
24 }
|
|
25
|
|
26 private void loadRoleMap(){
|
|
27 roleMap = new HashMap<String, List<Long>>();
|
|
28 //List<Entity> roles = getOtl().getLightweightAssertions("ROLE", null, -1);
|
|
29 List<Entity> roles = getWrapper().getEntitiesByDef("ROLE");
|
|
30 for(Entity role : roles){
|
|
31 if(StringUtils.isNotEmpty(role.getOwnValue())){
|
|
32 String roleLowCase = role.getOwnValue().toLowerCase();
|
|
33 getRoleMap().put(roleLowCase, new ArrayList<Long>());
|
|
34 List<Relation> relList = getWrapper().getTargetRelations(role, "has_role", "PERSON", -1);
|
|
35 for(Relation hasRole : relList){
|
|
36 getRoleMap().get(roleLowCase).add(hasRole.getSourceId());
|
|
37 }
|
|
38 }else{
|
|
39 System.err.println("****************");
|
|
40 System.err.println("Entity Role without Label: " + role);
|
|
41 System.err.println("****************");
|
|
42 }
|
|
43 }
|
|
44 }
|
|
45
|
|
46 public List<String> getRoleList(Long id){
|
|
47 List<String> list = new ArrayList<String>();
|
|
48 for(String roleName : this.getRoleMap().keySet()){
|
|
49 if(this.getRoleMap().get(roleName).contains(id)){
|
|
50 list.add(roleName);
|
|
51
|
|
52 }
|
|
53 }
|
|
54 return list;
|
|
55 }
|
|
56
|
|
57 public Map<String, List<Long>> getRoleMap() {
|
|
58 if(roleMap == null)
|
|
59 loadRoleMap();
|
|
60 return roleMap;
|
|
61 }
|
|
62
|
|
63 public boolean roleContainsPersonId(String role, Long id){
|
|
64 if(StringUtils.isEmpty(role) || this.getRoleMap().get(role.toLowerCase()) == null)
|
|
65 return false;
|
|
66
|
|
67 return this.getRoleMap().get(role.toLowerCase()).contains(id);
|
|
68 }
|
|
69 }
|