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