+package eu.etaxonomy.taxeditor.ui.section.grantedAuthority;
+
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+import org.springframework.security.core.GrantedAuthority;
+
+import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.IDescriptionService;
+import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.api.service.IUserService;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
+
+import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionClass;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+public class GrantedAuthorityLabelTextProvider {
+
+ public static final Logger logger = Logger.getLogger(CdmAuthority.class);
+
+
+ public static String getText(GrantedAuthority grantedAuthority){
+ String labelText = grantedAuthority.getAuthority();
+ try {
+ CdmAuthority cdmAuthority = CdmAuthority.fromGrantedAuthority(grantedAuthority);
+ String targetLabelText = getTargetText(cdmAuthority);
+ if(targetLabelText.length() > 0){
+ labelText = grantedAuthority.getAuthority().replace(cdmAuthority.getTargetUUID().toString(), targetLabelText);
+ }
+ } catch (Exception e) {
+ /* will in most cases catch a ParsingException
+ * this is ignored
+ */
+ if(!e.getClass().getSimpleName().equals("ParsingException")){
+ e.printStackTrace();
+ }
+ // in case it is a Role
+ labelText = grantedAuthority.getAuthority();
+ }
+ return labelText;
+ }
+
+ public static String getTargetText(GrantedAuthority grantedAuthority){
+ if(grantedAuthority instanceof CdmAuthority){
+ return getCdmAuthorityTargetText((CdmAuthority)grantedAuthority);
+
+ }
+ return "";
+ }
+
+ private static String getCdmAuthorityTargetText(CdmAuthority cdmAuthority) {
+
+ UUID uuid = cdmAuthority.getTargetUUID();
+ String targetText = "";
+
+ if(uuid != null){
+ targetText = uuid.toString();
+ if(cdmAuthority.getPermissionClass() != null){
+ try{
+ switch(cdmAuthority.getPermissionClass()) {
+ case USER:
+ targetText = CdmStore.getService(IUserService.class).load(uuid).getUsername();
+ break;
+ case DESCRIPTIONBASE:
+ targetText = CdmStore.getService(IDescriptionService.class).load(uuid).getTitleCache();
+ break;
+ case DESCRIPTIONELEMENTBASE:
+ targetText = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null).toString();
+ break;
+ case TAXONBASE:
+ targetText = CdmStore.getService(ITaxonService.class).load(uuid).getTitleCache();
+ break;
+ case ALL:
+ // makes not much sense here
+ break;
+ case TAXONNODE:
+ TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(uuid);
+ if(node.getClassification() != null){
+ targetText = node.getClassification().getTitleCache() + " : ";
+ }
+ if(node.getTaxon() != null){
+ targetText += node.getTaxon().getTitleCache();
+ }
+ break;
+ case CLASSIFICATION:
+ targetText = CdmStore.getService(IClassificationService.class).load(uuid).getTitleCache();
+ break;
+ }
+ } catch (NullPointerException e){
+ logger.warn("Either no service found for " + cdmAuthority.getPermissionClass() + " or entitiy not found" , e);
+ }
+ }
+ }
+
+ return targetText;
+ }
+
+
+}