Coverage Report - org.kuali.rice.kim.util.KimCommonUtilsInternal
 
Classes in this File Line Coverage Branch Coverage Complexity
KimCommonUtilsInternal
0%
0/87
0%
0/86
4.769
 
 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.core.util.AttributeSet;
 21  
 import org.kuali.rice.kim.api.entity.privacy.EntityPrivacyPreferences;
 22  
 import org.kuali.rice.kim.api.services.IdentityManagementService;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 import org.kuali.rice.kim.api.type.KimTypeAttribute;
 25  
 import org.kuali.rice.kim.bo.entity.dto.KimEntityDefaultInfo;
 26  
 import org.kuali.rice.kns.UserSession;
 27  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 28  
 import org.kuali.rice.kns.util.GlobalVariables;
 29  
 
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * This is a description of what this class does - bhargavp don't forget to fill
 34  
  * this in.
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  *
 38  
  */
 39  
 public final class KimCommonUtilsInternal {
 40  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtilsInternal.class);
 41  
 
 42  
     private static IdentityManagementService identityManagementService;
 43  
 
 44  0
         private KimCommonUtilsInternal() {
 45  0
                 throw new UnsupportedOperationException("do not call");
 46  
         }
 47  
 
 48  
     public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){
 49  0
                 if(targetToCopyTo!=null && sourceToCopyFrom!=null)
 50  
                 try{
 51  0
                         PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom);
 52  0
                 } catch(Exception ex){
 53  0
                         throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex);
 54  0
                 }
 55  0
         }
 56  
 
 57  
         public static String getKimBasePath(){
 58  0
                 String kimBaseUrl = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KimConstants.KimUIConstants.KIM_URL_KEY);
 59  0
                 if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) {
 60  0
                         kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR;
 61  
                 }
 62  0
                 return kimBaseUrl;
 63  
         }
 64  
 
 65  
         public static String getPathWithKimContext(String path, String kimActionName){
 66  0
                 String kimContext = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.URL_SEPARATOR;
 67  0
                 String kimContextParameterized = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.PARAMETERIZED_URL_SEPARATOR;
 68  0
             if(path.contains(kimActionName) && !path.contains(kimContext + kimActionName)
 69  
                             && !path.contains(kimContextParameterized + kimActionName))
 70  0
                     path = path.replace(kimActionName, kimContext+kimActionName);
 71  0
             return path;
 72  
         }
 73  
 
 74  
         public static String stripEnd(String toStripFrom, String toStrip){
 75  
                 String stripped;
 76  0
                 if(toStripFrom==null) return null;
 77  0
                 if(toStrip==null) return toStripFrom;
 78  0
         if(toStrip.length() > toStripFrom.length()) return toStripFrom;
 79  0
                 if(toStripFrom.endsWith(toStrip)){
 80  0
                         StringBuffer buffer = new StringBuffer(toStripFrom);
 81  0
                         buffer.delete(buffer.length()-toStrip.length(), buffer.length());
 82  0
                         stripped = buffer.toString();
 83  0
                 } else stripped = toStripFrom;
 84  0
                 return stripped;
 85  
         }
 86  
 
 87  
         protected static boolean canOverrideEntityPrivacyPreferences( String principalId ){
 88  0
                 return getIdentityManagementService().isAuthorized(
 89  
                                 GlobalVariables.getUserSession().getPrincipalId(),
 90  
                                 KimConstants.NAMESPACE_CODE,
 91  
                                 KimConstants.PermissionNames.OVERRIDE_ENTITY_PRIVACY_PREFERENCES,
 92  
                                 null,
 93  
                                 new AttributeSet(KimConstants.AttributeConstants.PRINCIPAL_ID, principalId) );
 94  
         }
 95  
 
 96  
         public static boolean isSuppressName(String entityId) {
 97  0
             EntityPrivacyPreferences privacy = null;
 98  0
         KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
 99  0
         if (entityInfo != null) {
 100  0
             privacy = entityInfo.getPrivacyPreferences();
 101  
         } else {
 102  0
                 return true;
 103  
         }
 104  0
             UserSession userSession = GlobalVariables.getUserSession();
 105  
 
 106  0
         boolean suppressName = false;
 107  0
         if (privacy != null) {
 108  0
             suppressName = privacy.isSuppressName();
 109  
         }
 110  
         
 111  0
         return suppressName
 112  
                 && userSession != null
 113  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 114  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 115  
     }
 116  
 
 117  
     public static boolean isSuppressEmail(String entityId) {
 118  0
         EntityPrivacyPreferences privacy = null;
 119  0
         KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
 120  0
         if (entityInfo != null) {
 121  0
             privacy = entityInfo.getPrivacyPreferences();
 122  
         } else {
 123  0
                 return true;
 124  
         }
 125  0
         UserSession userSession = GlobalVariables.getUserSession();
 126  
 
 127  0
         boolean suppressEmail = false;
 128  0
         if (privacy != null) {
 129  0
             suppressEmail = privacy.isSuppressEmail();
 130  
         }
 131  0
         return suppressEmail
 132  
                 && userSession != null
 133  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 134  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 135  
     }
 136  
 
 137  
     public static boolean isSuppressAddress(String entityId) {
 138  0
         EntityPrivacyPreferences privacy = null;
 139  0
         KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
 140  0
         if (entityInfo != null) {
 141  0
             privacy = entityInfo.getPrivacyPreferences();
 142  
         } else {
 143  0
                 return false;
 144  
         }
 145  0
         UserSession userSession = GlobalVariables.getUserSession();
 146  
 
 147  0
         boolean suppressAddress = false;
 148  0
         if (privacy != null) {
 149  0
             suppressAddress = privacy.isSuppressAddress();
 150  
         }
 151  0
         return suppressAddress
 152  
                 && userSession != null
 153  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 154  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 155  
     }
 156  
 
 157  
     public static boolean isSuppressPhone(String entityId) {
 158  0
         EntityPrivacyPreferences privacy = null;
 159  0
         KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
 160  0
         if (entityInfo != null) {
 161  0
             privacy = entityInfo.getPrivacyPreferences();
 162  
         } else { 
 163  0
                 return true;
 164  
         }
 165  0
         UserSession userSession = GlobalVariables.getUserSession();
 166  
 
 167  0
         boolean suppressPhone = false;
 168  0
         if (privacy != null) {
 169  0
             suppressPhone = privacy.isSuppressPhone();
 170  
         }
 171  0
         return suppressPhone
 172  
                 && userSession != null
 173  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 174  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 175  
     }
 176  
 
 177  
     public static boolean isSuppressPersonal(String entityId) {
 178  0
         EntityPrivacyPreferences privacy = null;
 179  0
         KimEntityDefaultInfo entityInfo = getIdentityManagementService().getEntityDefaultInfo(entityId);
 180  0
         if (entityInfo != null) {
 181  0
             privacy = entityInfo.getPrivacyPreferences();
 182  
         } else { 
 183  0
                 return true;
 184  
         }
 185  0
         UserSession userSession = GlobalVariables.getUserSession();
 186  
 
 187  0
         boolean suppressPersonal = false;
 188  0
         if (privacy != null) {
 189  0
             suppressPersonal = privacy.isSuppressPersonal();
 190  
         }
 191  0
         return suppressPersonal
 192  
                 && userSession != null
 193  
                 && !StringUtils.equals(userSession.getPerson().getEntityId(), entityId)
 194  
                 && !canOverrideEntityPrivacyPreferences(entityInfo.getPrincipals().get(0).getPrincipalId());
 195  
     }
 196  
 
 197  
         private static IdentityManagementService getIdentityManagementService() {
 198  0
                 if ( identityManagementService == null ) {
 199  0
                         identityManagementService = KimApiServiceLocator.getIdentityManagementService();
 200  
                 }
 201  0
                 return identityManagementService;
 202  
         }
 203  
 
 204  
     private static KimTypeAttribute getAttributeInfo(List<KimTypeAttribute> attributeInfoList, String attributeName) {
 205  0
         KimTypeAttribute kRet = null;
 206  0
         for (KimTypeAttribute attributeInfo : attributeInfoList) {
 207  0
             if (attributeInfo.getKimAttribute().getAttributeName().equals(attributeName)) {
 208  0
                 kRet = attributeInfo;
 209  0
                 break;
 210  
             }
 211  
         }
 212  0
         return kRet;
 213  
     }
 214  
 
 215  
 }