Coverage Report - org.kuali.rice.kim.util.KimCommonUtilsInternal
 
Classes in this File Line Coverage Branch Coverage Complexity
KimCommonUtilsInternal
0%
0/90
0%
0/88
4.571
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.util;
 17  
 
 18  
 import org.apache.commons.beanutils.PropertyUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
 21  
 import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
 22  
 import org.kuali.rice.kim.api.services.IdentityService;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 import org.kuali.rice.kim.api.type.KimTypeAttribute;
 25  
 import org.kuali.rice.kim.service.PermissionService;
 26  
 import org.kuali.rice.krad.UserSession;
 27  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 28  
 import org.kuali.rice.krad.util.GlobalVariables;
 29  
 
 30  
 import java.util.Collections;
 31  
 import java.util.List;
 32  
 
 33  
 /**
 34  
  * This is a description of what this class does - bhargavp don't forget to fill
 35  
  * this in.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  *
 39  
  */
 40  
 public final class KimCommonUtilsInternal {
 41  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtilsInternal.class);
 42  
 
 43  
     private static IdentityService identityService;
 44  
     private static PermissionService permissionService;
 45  
 
 46  0
         private KimCommonUtilsInternal() {
 47  0
                 throw new UnsupportedOperationException("do not call");
 48  
         }
 49  
 
 50  
     public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){
 51  0
                 if(targetToCopyTo!=null && sourceToCopyFrom!=null)
 52  
                 try{
 53  0
                         PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom);
 54  0
                 } catch(Exception ex){
 55  0
                         throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex);
 56  0
                 }
 57  0
         }
 58  
 
 59  
         public static String getKimBasePath(){
 60  0
                 String kimBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyString(KimConstants.KimUIConstants.KIM_URL_KEY);
 61  0
                 if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) {
 62  0
                         kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR;
 63  
                 }
 64  0
                 return kimBaseUrl;
 65  
         }
 66  
 
 67  
         public static String getPathWithKimContext(String path, String kimActionName){
 68  0
                 String kimContext = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.URL_SEPARATOR;
 69  0
                 String kimContextParameterized = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.PARAMETERIZED_URL_SEPARATOR;
 70  0
             if(path.contains(kimActionName) && !path.contains(kimContext + kimActionName)
 71  
                             && !path.contains(kimContextParameterized + kimActionName))
 72  0
                     path = path.replace(kimActionName, kimContext+kimActionName);
 73  0
             return path;
 74  
         }
 75  
 
 76  
         public static String stripEnd(String toStripFrom, String toStrip){
 77  
                 String stripped;
 78  0
                 if(toStripFrom==null) return null;
 79  0
                 if(toStrip==null) return toStripFrom;
 80  0
         if(toStrip.length() > toStripFrom.length()) return toStripFrom;
 81  0
                 if(toStripFrom.endsWith(toStrip)){
 82  0
                         StringBuffer buffer = new StringBuffer(toStripFrom);
 83  0
                         buffer.delete(buffer.length()-toStrip.length(), buffer.length());
 84  0
                         stripped = buffer.toString();
 85  0
                 } else stripped = toStripFrom;
 86  0
                 return stripped;
 87  
         }
 88  
 
 89  
         protected static boolean canOverrideEntityPrivacyPreferences( String principalId ){
 90  0
                 return getPermissionService().isAuthorized(
 91  
                                 GlobalVariables.getUserSession().getPrincipalId(),
 92  
                                 KimConstants.NAMESPACE_CODE,
 93  
                                 KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES,
 94  
                                 null,
 95  
                                 Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, principalId) );
 96  
         }
 97  
 
 98  
         public static boolean isSuppressName(String entityId) {
 99  0
             EntityPrivacyPreferences privacy = null;
 100  0
         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
 101  0
         if (entityInfo != null) {
 102  0
             privacy = entityInfo.getPrivacyPreferences();
 103  
         } else {
 104  0
                 return true;
 105  
         }
 106  0
             UserSession userSession = GlobalVariables.getUserSession();
 107  
 
 108  0
         boolean suppressName = false;
 109  0
         if (privacy != null) {
 110  0
             suppressName = privacy.isSuppressName();
 111  
         }
 112  
         
 113  0
         return suppressName
 114  
                 && userSession != null
 115  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 116  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 117  
     }
 118  
 
 119  
     public static boolean isSuppressEmail(String entityId) {
 120  0
         EntityPrivacyPreferences privacy = null;
 121  0
         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
 122  0
         if (entityInfo != null) {
 123  0
             privacy = entityInfo.getPrivacyPreferences();
 124  
         } else {
 125  0
                 return true;
 126  
         }
 127  0
         UserSession userSession = GlobalVariables.getUserSession();
 128  
 
 129  0
         boolean suppressEmail = false;
 130  0
         if (privacy != null) {
 131  0
             suppressEmail = privacy.isSuppressEmail();
 132  
         }
 133  0
         return suppressEmail
 134  
                 && userSession != null
 135  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 136  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 137  
     }
 138  
 
 139  
     public static boolean isSuppressAddress(String entityId) {
 140  0
         EntityPrivacyPreferences privacy = null;
 141  0
         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
 142  0
         if (entityInfo != null) {
 143  0
             privacy = entityInfo.getPrivacyPreferences();
 144  
         } else {
 145  0
                 return false;
 146  
         }
 147  0
         UserSession userSession = GlobalVariables.getUserSession();
 148  
 
 149  0
         boolean suppressAddress = false;
 150  0
         if (privacy != null) {
 151  0
             suppressAddress = privacy.isSuppressAddress();
 152  
         }
 153  0
         return suppressAddress
 154  
                 && userSession != null
 155  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 156  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 157  
     }
 158  
 
 159  
     public static boolean isSuppressPhone(String entityId) {
 160  0
         EntityPrivacyPreferences privacy = null;
 161  0
         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
 162  0
         if (entityInfo != null) {
 163  0
             privacy = entityInfo.getPrivacyPreferences();
 164  
         } else { 
 165  0
                 return true;
 166  
         }
 167  0
         UserSession userSession = GlobalVariables.getUserSession();
 168  
 
 169  0
         boolean suppressPhone = false;
 170  0
         if (privacy != null) {
 171  0
             suppressPhone = privacy.isSuppressPhone();
 172  
         }
 173  0
         return suppressPhone
 174  
                 && userSession != null
 175  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 176  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 177  
     }
 178  
 
 179  
     public static boolean isSuppressPersonal(String entityId) {
 180  0
         EntityPrivacyPreferences privacy = null;
 181  0
         EntityDefault entityInfo = getIdentityService().getEntityDefault(entityId);
 182  0
         if (entityInfo != null) {
 183  0
             privacy = entityInfo.getPrivacyPreferences();
 184  
         } else { 
 185  0
                 return true;
 186  
         }
 187  0
         UserSession userSession = GlobalVariables.getUserSession();
 188  
 
 189  0
         boolean suppressPersonal = false;
 190  0
         if (privacy != null) {
 191  0
             suppressPersonal = privacy.isSuppressPersonal();
 192  
         }
 193  0
         return suppressPersonal
 194  
                 && userSession != null
 195  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 196  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 197  
     }
 198  
 
 199  
         private static IdentityService getIdentityService() {
 200  0
                 if ( identityService == null ) {
 201  0
                         identityService = KimApiServiceLocator.getIdentityService();
 202  
                 }
 203  0
                 return identityService;
 204  
         }
 205  
 
 206  
     private static PermissionService getPermissionService() {
 207  0
                 if ( permissionService == null ) {
 208  0
                         permissionService = KimApiServiceLocator.getPermissionService();
 209  
                 }
 210  0
                 return permissionService;
 211  
         }
 212  
 
 213  
     private static KimTypeAttribute getAttributeInfo(List<KimTypeAttribute> attributeInfoList, String attributeName) {
 214  0
         KimTypeAttribute kRet = null;
 215  0
         for (KimTypeAttribute attributeInfo : attributeInfoList) {
 216  0
             if (attributeInfo.getKimAttribute().getAttributeName().equals(attributeName)) {
 217  0
                 kRet = attributeInfo;
 218  0
                 break;
 219  
             }
 220  
         }
 221  0
         return kRet;
 222  
     }
 223  
 
 224  
 }