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