001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.util;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.uif.component.Component;
020    import org.springframework.beans.BeansException;
021    import org.springframework.beans.factory.config.BeanPostProcessor;
022    
023    /**
024     * Spring <code>BeanPostProcessor</code> that processes configured <code>Component</code>
025     * instances in the dictionary
026     *
027     * @author Kuali Rice Team (rice.collab@kuali.org)
028     */
029    public class ComponentBeanPostProcessor implements BeanPostProcessor {
030    
031        public ComponentBeanPostProcessor() {
032        }
033    
034        /**
035         * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object,
036         *      java.lang.String)
037         */
038        @Override
039        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
040            return bean;
041        }
042    
043        /**
044         * Sets the unique Id for a <code>Component</code> if bean name given (not generated) and the id property was
045         * not set for the view
046         *
047         * <p>
048         * The ID will only be set here if an id is given for the Spring bean. For inner beans, the ID will be generated
049         * during the view lifecycle
050         * </p>
051         *
052         * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object,
053         *      java.lang.String)
054         */
055        @Override
056        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
057            if (bean instanceof Component) {
058                Component component = (Component) bean;
059    
060                if (StringUtils.isBlank(component.getId())) {
061                    if (!StringUtils.contains(beanName, "$") && !StringUtils.contains(beanName, "#")) {
062                        component.setId(beanName);
063                        component.setFactoryId(beanName);
064                    }
065                }
066            }
067    
068            return bean;
069        }
070    }