001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.rice.kim.util;
020
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.commons.beanutils.PropertyUtils;
025import org.apache.commons.lang.StringUtils;
026import org.kuali.kfs.sys.context.SpringContext;
027import org.kuali.rice.core.api.config.property.ConfigurationService;
028import org.kuali.rice.kew.api.KewApiServiceLocator;
029import org.kuali.rice.kew.api.doctype.DocumentType;
030import org.kuali.rice.kim.api.KimConstants;
031
032/**
033 * This is a description of what this class does - bhargavp don't forget to fill
034 * this in.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 *
038 */
039public class KimCommonUtils {
040    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimCommonUtils.class);
041
042    public static String getClosestParentDocumentTypeName( DocumentType documentType, Set<String> potentialParentDocumentTypeNames) {
043        if ( potentialParentDocumentTypeNames == null || documentType == null ) {
044            return null;
045        }
046        if (potentialParentDocumentTypeNames.contains(documentType.getName())) {
047            return documentType.getName();
048        } else {
049            if ( StringUtils.isBlank(documentType.getParentId())
050                    || StringUtils.equals( documentType.getParentId(), documentType.getId() ) ) {
051                return null;
052            } else {
053                return getClosestParentDocumentTypeName(KewApiServiceLocator.getDocumentTypeService().getDocumentTypeById(documentType.getParentId()), potentialParentDocumentTypeNames);
054            }
055        }
056    }
057
058    public static boolean storedValueNotSpecifiedOrInputValueMatches(Map<String,String> storedValues, Map<String,String> inputValues, String attributeName) {
059        return ((storedValues == null)
060                || (inputValues == null))
061                || storedValues.isEmpty()
062                || inputValues.isEmpty()
063                ||  !storedValues.containsKey(attributeName)
064                || StringUtils.equals( storedValues.get(attributeName), inputValues.get(attributeName));
065    }
066
067    public static boolean doesPropertyNameMatch(
068            String requestedDetailsPropertyName,
069            String permissionDetailsPropertyName) {
070        if (StringUtils.isBlank(permissionDetailsPropertyName)) {
071            return true;
072        }
073        return StringUtils.equals(requestedDetailsPropertyName, permissionDetailsPropertyName)
074                || (StringUtils.startsWith(requestedDetailsPropertyName,permissionDetailsPropertyName+"."));
075    }
076
077    public static String getComponentSimpleName(Class<? extends Object> clazz) {
078        return clazz.getSimpleName();
079    }
080
081    public static String getComponentFullName(Class<? extends Object> clazz) {
082        return clazz.getName();
083    }
084
085    public static void copyProperties(Object targetToCopyTo, Object sourceToCopyFrom){
086        if(targetToCopyTo!=null && sourceToCopyFrom!=null) {
087            try{
088                PropertyUtils.copyProperties(targetToCopyTo, sourceToCopyFrom);
089            } catch(Exception ex){
090                throw new RuntimeException("Failed to copy from source object: "+sourceToCopyFrom.getClass()+" to target object: "+targetToCopyTo,ex);
091            }
092        }
093    }
094
095    public static String getKimBasePath(){
096        String kimBaseUrl = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(KimConstants.KimUIConstants.KIM_URL_KEY);
097        if (!kimBaseUrl.endsWith(KimConstants.KimUIConstants.URL_SEPARATOR)) {
098            kimBaseUrl = kimBaseUrl + KimConstants.KimUIConstants.URL_SEPARATOR;
099        }
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}