Coverage Report - org.kuali.rice.kns.uif.field.GeneratedField
 
Classes in this File Line Coverage Branch Coverage Complexity
GeneratedField
0%
0/28
0%
0/8
1.857
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.uif.field;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.kns.uif.container.View;
 20  
 import org.kuali.rice.kns.uif.core.Component;
 21  
 import org.springframework.util.MethodInvoker;
 22  
 
 23  
 /**
 24  
  * Field instance whose output is produced by invoking a method
 25  
  * 
 26  
  * <p>
 27  
  * Generated fields can be used to produce any kind of HTML output using code.
 28  
  * The properties configured for the <code>GeneratedField</code> configure the
 29  
  * class and method that should be invoked to render the component. The method
 30  
  * that will be invoked should take the <code>GeneratedField</code> instance as
 31  
  * a parameter and return a String (which can include state HTML)
 32  
  * </p>
 33  
  * 
 34  
  * <p>
 35  
  * If the renderingMethodToCall is set, it is assumed to be a method on the
 36  
  * configured <code>ViewHelperService</code>. For invoking other class methods
 37  
  * the renderMethodInvoker must be configured
 38  
  * </p>
 39  
  * 
 40  
  * <p>
 41  
  * e.g. public String sayHiInBold(GeneratedField field) { return "<b>HI!</b>"; }
 42  
  * </p>
 43  
  * 
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  
 public class GeneratedField extends FieldBase {
 47  
         private static final long serialVersionUID = 1575182633700024203L;
 48  
 
 49  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GeneratedField.class);
 50  
 
 51  
         private String renderingMethodToCall;
 52  
         private MethodInvoker renderingMethodInvoker;
 53  
 
 54  
         private String renderOutput;
 55  
 
 56  
         public GeneratedField() {
 57  0
                 super();
 58  0
         }
 59  
 
 60  
         /**
 61  
          * The following finalization is done here:
 62  
          * 
 63  
          * <ul>
 64  
          * <li>Setup the method invoker and invoke method to get the render output</li>
 65  
          * </ul>
 66  
          * 
 67  
          * @see org.kuali.rice.kns.uif.core.ComponentBase#performFinalize(org.kuali.rice.kns.uif.container.View,
 68  
          *      java.lang.Object, org.kuali.rice.kns.uif.core.Component)
 69  
          */
 70  
         @Override
 71  
         public void performFinalize(View view, Object model, Component parent) {
 72  0
                 super.performFinalize(view, model, parent);
 73  
 
 74  0
                 if (renderingMethodInvoker == null) {
 75  0
                         renderingMethodInvoker = new MethodInvoker();
 76  
                 }
 77  
 
 78  
                 // if method not set on invoker, use renderingMethodToCall
 79  0
                 if (StringUtils.isBlank(renderingMethodInvoker.getTargetMethod())) {
 80  0
                         renderingMethodInvoker.setTargetMethod(renderingMethodToCall);
 81  
                 }
 82  
 
 83  
                 // if target class or object not set, use view helper service
 84  0
                 if ((renderingMethodInvoker.getTargetClass() == null) && (renderingMethodInvoker.getTargetObject() == null)) {
 85  0
                         renderingMethodInvoker.setTargetObject(view.getViewHelperService());
 86  
                 }
 87  
 
 88  
                 // add the component instance as an argument
 89  0
                 Object[] arguments = new Object[1];
 90  0
                 arguments[0] = this;
 91  0
                 renderingMethodInvoker.setArguments(arguments);
 92  
 
 93  
                 // invoke method and get render output
 94  
                 try {
 95  0
                         LOG.debug("Invoking render method: " + renderingMethodInvoker.getTargetMethod() + " for component: "
 96  
                                         + getId());
 97  0
                         renderingMethodInvoker.prepare();
 98  
 
 99  0
                         renderOutput = (String) renderingMethodInvoker.invoke();
 100  
                 }
 101  0
                 catch (Exception e) {
 102  0
                         LOG.error("Error invoking rendering method for component: " + getId(), e);
 103  0
                         throw new RuntimeException("Error invoking rendering method for component: " + getId(), e);
 104  0
                 }
 105  0
         }
 106  
 
 107  
         /**
 108  
          * Name of the method that should be invoked for rendering the component
 109  
          * (full method name, without parameters or return type)
 110  
          * 
 111  
          * <p>
 112  
          * Note the method can also be set with the renderingMethodInvoker
 113  
          * targetMethod property. If the method is on the configured
 114  
          * <code>ViewHelperService</code>, only this property needs to be configured
 115  
          * </p>
 116  
          * 
 117  
          * @return String method name
 118  
          */
 119  
         public String getRenderingMethodToCall() {
 120  0
                 return this.renderingMethodToCall;
 121  
         }
 122  
 
 123  
         /**
 124  
          * Setter for the rendering method
 125  
          * 
 126  
          * @param renderingMethodToCall
 127  
          */
 128  
         public void setRenderingMethodToCall(String renderingMethodToCall) {
 129  0
                 this.renderingMethodToCall = renderingMethodToCall;
 130  0
         }
 131  
 
 132  
         /**
 133  
          * <code>MethodInvoker</code> instance for the method that should be invoked
 134  
          * for rendering the component
 135  
          * 
 136  
          * <p>
 137  
          * MethodInvoker can be configured to specify the class or object the method
 138  
          * should be called on. For static method invocations, the targetClass
 139  
          * property can be configured. For object invocations, that targetObject
 140  
          * property can be configured
 141  
          * </p>
 142  
          * 
 143  
          * @return MethodInvoker instance
 144  
          */
 145  
         public MethodInvoker getRenderingMethodInvoker() {
 146  0
                 return this.renderingMethodInvoker;
 147  
         }
 148  
 
 149  
         /**
 150  
          * Setter for the method invoker instance
 151  
          * 
 152  
          * @param renderingMethodInvoker
 153  
          */
 154  
         public void setRenderingMethodInvoker(MethodInvoker renderingMethodInvoker) {
 155  0
                 this.renderingMethodInvoker = renderingMethodInvoker;
 156  0
         }
 157  
 
 158  
         /**
 159  
          * Rendering output for the component that will be sent as part of the
 160  
          * response (can contain static text and HTML)
 161  
          * 
 162  
          * @return String render output
 163  
          */
 164  
         public String getRenderOutput() {
 165  0
                 return this.renderOutput;
 166  
         }
 167  
 
 168  
 }