View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.rice.kim.util;
20  
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.commons.beanutils.PropertyUtils;
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.kfs.sys.context.SpringContext;
27  import org.kuali.rice.core.api.config.property.ConfigurationService;
28  import org.kuali.rice.kew.api.KewApiServiceLocator;
29  import org.kuali.rice.kew.api.doctype.DocumentType;
30  import org.kuali.rice.kim.api.KimConstants;
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 class KimCommonUtils {
40      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtils.class);
41  
42      public static String getClosestParentDocumentTypeName( DocumentType documentType, Set<String> potentialParentDocumentTypeNames) {
43          if ( potentialParentDocumentTypeNames == null || documentType == null ) {
44              return null;
45          }
46          if (potentialParentDocumentTypeNames.contains(documentType.getName())) {
47              return documentType.getName();
48          } else {
49              if ( StringUtils.isBlank(documentType.getParentId())
50                      || StringUtils.equals( documentType.getParentId(), documentType.getId() ) ) {
51                  return null;
52              } else {
53                  return getClosestParentDocumentTypeName(KewApiServiceLocator.getDocumentTypeService().getDocumentTypeById(documentType.getParentId()), potentialParentDocumentTypeNames);
54              }
55          }
56      }
57  
58      public static boolean storedValueNotSpecifiedOrInputValueMatches(Map<String,String> storedValues, Map<String,String> inputValues, String attributeName) {
59          return ((storedValues == null)
60                  || (inputValues == null))
61                  || storedValues.isEmpty()
62                  || inputValues.isEmpty()
63                  ||  !storedValues.containsKey(attributeName)
64                  || StringUtils.equals( storedValues.get(attributeName), inputValues.get(attributeName));
65      }
66  
67      public static boolean doesPropertyNameMatch(
68              String requestedDetailsPropertyName,
69              String permissionDetailsPropertyName) {
70          if (StringUtils.isBlank(permissionDetailsPropertyName)) {
71              return true;
72          }
73          return StringUtils.equals(requestedDetailsPropertyName, permissionDetailsPropertyName)
74                  || (StringUtils.startsWith(requestedDetailsPropertyName,permissionDetailsPropertyName+"."));
75      }
76  
77      public static String getComponentSimpleName(Class<? extends Object> clazz) {
78          return clazz.getSimpleName();
79      }
80  
81      public static String getComponentFullName(Class<? extends Object> clazz) {
82          return clazz.getName();
83      }
84  
85      public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){
86          if(targetToCopyTo!=null && sourceToCopyFrom!=null) {
87              try{
88                  PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom);
89              } catch(Exception ex){
90                  throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex);
91              }
92          }
93      }
94  
95      public static String getKimBasePath(){
96          String kimBaseUrl = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(KimConstants.KimUIConstants.KIM_URL_KEY);
97          if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) {
98              kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR;
99          }
100         return kimBaseUrl;
101     }
102 
103     public static String getPathWithKimContext(String path, String kimActionName){
104         String kimContext = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.URL_SEPARATOR;
105         String kimContextParameterized = KimConstants.KimUIConstants.KIM_APPLICATION+KimConstants.KimUIConstants.PARAMETERIZED_URL_SEPARATOR;
106         if(path.contains(kimActionName) && !path.contains(kimContext + kimActionName)
107                 && !path.contains(kimContextParameterized + kimActionName)) {
108             path = path.replace(kimActionName, kimContext+kimActionName);
109         }
110         return path;
111     }
112 
113 }