001    /**
002     * Copyright 2005-2014 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.lifecycle.finalize;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.rice.krad.uif.component.Component;
023    import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
024    import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
025    import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    import org.springframework.util.MethodInvoker;
029    
030    /**
031     * Invoke finalizer methods on the component.
032     * 
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class InvokeFinalizerTask extends ViewLifecycleTaskBase<Component> {
036        
037        private final Logger LOG = LoggerFactory.getLogger(InvokeFinalizerTask.class);
038    
039        /**
040         * Constructor.
041         * 
042         * @param phase The apply model phase for the component.
043         */
044        public InvokeFinalizerTask(ViewLifecyclePhase phase) {
045            super(phase, Component.class);
046        }
047    
048        /**
049         * Invokes the finalize method for the component (if configured) and sets the render output for
050         * the component to the returned method string (if method is not a void type)
051         */
052        @Override
053        protected void performLifecycleTask() {
054            Component component = (Component) getElementState().getElement();
055            String finalizeMethodToCall = component.getFinalizeMethodToCall();
056            MethodInvoker finalizeMethodInvoker = component.getFinalizeMethodInvoker();
057    
058            if (StringUtils.isBlank(finalizeMethodToCall) && (finalizeMethodInvoker == null)) {
059                return;
060            }
061    
062            if (finalizeMethodInvoker == null) {
063                finalizeMethodInvoker = new MethodInvoker();
064            }
065    
066            // if method not set on invoker, use finalizeMethodToCall, note staticMethod could be set(don't know since
067            // there is not a getter), if so it will override the target method in prepare
068            if (StringUtils.isBlank(finalizeMethodInvoker.getTargetMethod())) {
069                finalizeMethodInvoker.setTargetMethod(finalizeMethodToCall);
070            }
071    
072            // if target class or object not set, use view helper service
073            if ((finalizeMethodInvoker.getTargetClass() == null) && (finalizeMethodInvoker.getTargetObject() == null)) {
074                finalizeMethodInvoker.setTargetObject(ViewLifecycle.getHelper());
075            }
076    
077            // setup arguments for method
078            List<Object> additionalArguments = component.getFinalizeMethodAdditionalArguments();
079            if (additionalArguments == null) {
080                additionalArguments = new ArrayList<Object>();
081            }
082    
083            Object[] arguments = new Object[2 + additionalArguments.size()];
084            arguments[0] = component;
085            arguments[1] = ViewLifecycle.getModel();
086    
087            int argumentIndex = 1;
088            for (Object argument : additionalArguments) {
089                argumentIndex++;
090                arguments[argumentIndex] = argument;
091            }
092            finalizeMethodInvoker.setArguments(arguments);
093    
094            // invoke finalize method
095            try {
096                LOG.debug("Invoking finalize method: "
097                        + finalizeMethodInvoker.getTargetMethod()
098                        + " for component: "
099                        + component.getId());
100                finalizeMethodInvoker.prepare();
101    
102                Class<?> methodReturnType = finalizeMethodInvoker.getPreparedMethod().getReturnType();
103                if (StringUtils.equals("void", methodReturnType.getName())) {
104                    finalizeMethodInvoker.invoke();
105                } else {
106                    String renderOutput = (String) finalizeMethodInvoker.invoke();
107    
108                    component.setSelfRendered(true);
109                    component.setRenderedHtmlOutput(renderOutput);
110                }
111            } catch (Exception e) {
112                LOG.error("Error invoking finalize method for component: " + component.getId(), e);
113                throw new RuntimeException("Error invoking finalize method for component: " + component.getId(), e);
114            }
115        }
116    
117    }