1
|
package eu.etaxonomy.taxeditor.ui.section.grantedAuthority;
|
2
|
|
3
|
import java.util.UUID;
|
4
|
|
5
|
import org.apache.log4j.Logger;
|
6
|
import org.springframework.security.core.GrantedAuthority;
|
7
|
|
8
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
9
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
10
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
11
|
import eu.etaxonomy.cdm.api.service.INameService;
|
12
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
13
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
14
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
15
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
16
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
17
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
|
18
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
19
|
|
20
|
public class GrantedAuthorityLabelTextProvider {
|
21
|
|
22
|
public static final Logger logger = Logger.getLogger(CdmAuthority.class);
|
23
|
|
24
|
|
25
|
public static String getText(GrantedAuthority grantedAuthority){
|
26
|
String labelText = grantedAuthority.getAuthority();
|
27
|
try {
|
28
|
CdmAuthority cdmAuthority = CdmAuthority.fromGrantedAuthority(grantedAuthority);
|
29
|
String targetLabelText = getTargetText(cdmAuthority);
|
30
|
if(targetLabelText.length() > 0){
|
31
|
labelText = grantedAuthority.getAuthority().replace(cdmAuthority.getTargetUUID().toString(), targetLabelText);
|
32
|
}
|
33
|
} catch (Exception e) {
|
34
|
/* will in most cases catch a ParsingException
|
35
|
* this is ignored
|
36
|
*/
|
37
|
if(!e.getClass().getSimpleName().equals("ParsingException")){
|
38
|
e.printStackTrace();
|
39
|
}
|
40
|
// in case it is a Role
|
41
|
labelText = grantedAuthority.getAuthority();
|
42
|
}
|
43
|
return labelText;
|
44
|
}
|
45
|
|
46
|
public static String getTargetText(GrantedAuthority grantedAuthority){
|
47
|
if(grantedAuthority instanceof CdmAuthority){
|
48
|
return getCdmAuthorityTargetText((CdmAuthority)grantedAuthority);
|
49
|
|
50
|
}
|
51
|
return "";
|
52
|
}
|
53
|
|
54
|
private static String getCdmAuthorityTargetText(CdmAuthority cdmAuthority) {
|
55
|
|
56
|
UUID uuid = cdmAuthority.getTargetUUID();
|
57
|
String targetText = "";
|
58
|
|
59
|
if(uuid != null){
|
60
|
targetText = uuid.toString();
|
61
|
if(cdmAuthority.getPermissionClass() != null){
|
62
|
try{
|
63
|
switch(cdmAuthority.getPermissionClass()) {
|
64
|
case USER:
|
65
|
targetText = CdmStore.getService(IUserService.class).load(uuid).getUsername();
|
66
|
break;
|
67
|
case DESCRIPTIONBASE:
|
68
|
targetText = CdmStore.getService(IDescriptionService.class).load(uuid).getTitleCache();
|
69
|
break;
|
70
|
case DESCRIPTIONELEMENTBASE:
|
71
|
targetText = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null).toString();
|
72
|
break;
|
73
|
case TAXONBASE:
|
74
|
targetText = CdmStore.getService(ITaxonService.class).load(uuid).getTitleCache();
|
75
|
break;
|
76
|
case ALL:
|
77
|
// makes not much sense here
|
78
|
break;
|
79
|
case TAXONNODE:
|
80
|
TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(uuid);
|
81
|
if(node.getClassification() != null){
|
82
|
targetText = node.getClassification().getTitleCache() + " : ";
|
83
|
}
|
84
|
if(node.getTaxon() != null){
|
85
|
targetText += node.getTaxon().getTitleCache();
|
86
|
}
|
87
|
break;
|
88
|
case CLASSIFICATION:
|
89
|
targetText = CdmStore.getService(IClassificationService.class).load(uuid).getTitleCache();
|
90
|
break;
|
91
|
case REFERENCE:
|
92
|
targetText = CdmStore.getService(IReferenceService.class).load(uuid).getTitleCache();
|
93
|
break;
|
94
|
case TAXONNAME:
|
95
|
targetText = CdmStore.getService(INameService.class).load(uuid).getTitleCache();
|
96
|
break;
|
97
|
case TEAMORPERSONBASE:
|
98
|
targetText = CdmStore.getService(IAgentService.class).load(uuid).getTitleCache();
|
99
|
break;
|
100
|
|
101
|
}
|
102
|
} catch (NullPointerException e){
|
103
|
logger.warn("Either no service found for " + cdmAuthority.getPermissionClass() + " or entitiy not found" , e);
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
|
108
|
return targetText;
|
109
|
}
|
110
|
|
111
|
|
112
|
}
|