001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.workflow.attribute;
017:
018: import java.lang.reflect.Method;
019: import java.util.HashMap;
020:
021: import net.sf.cglib.proxy.Enhancer;
022: import net.sf.cglib.proxy.InvocationHandler;
023:
024: import org.kuali.core.bo.BusinessObject;
025: import org.kuali.core.util.ObjectUtils;
026: import org.kuali.core.web.format.BooleanFormatter;
027:
028: /**
029: * This class provides access to the properties of business objects returned as search results by the WorkflowLookupableImpl.
030: *
031: *
032: * @see org.kuali.workflow.attribute.WorkflowLookupableImpl
033: * @deprecated This will go away once workflow supports simple url integration for custom attribute lookups.
034: */
035: public class WorkflowLookupableInvocationHandler implements
036: InvocationHandler {
037: private BusinessObject proxiedBusinessObject;
038: private ClassLoader classLoader;
039:
040: private String returnUrl;
041:
042: /**
043: * Constructs a WorkflowLookupableInvocationHandler.java.
044: *
045: * @param proxiedBusinessObject The BusinessObject that this instance is providing access to.
046: */
047: public WorkflowLookupableInvocationHandler(
048: BusinessObject proxiedBusinessObject,
049: ClassLoader classLoader) {
050: this .proxiedBusinessObject = proxiedBusinessObject;
051: this .classLoader = classLoader;
052: }
053:
054: /**
055: * Constructs a WorkflowLookupableInvocationHandler.java.
056: *
057: * @param proxiedBusinessObject The BusinessObject that this instance is providing access to.
058: * @param returnUrl The returnUrl String for selection of a result from the UI
059: */
060: public WorkflowLookupableInvocationHandler(
061: BusinessObject proxiedBusinessObject, String returnUrl,
062: ClassLoader classLoader) {
063: this .proxiedBusinessObject = proxiedBusinessObject;
064: this .returnUrl = returnUrl;
065: this .classLoader = classLoader;
066: }
067:
068: /**
069: * This method intercepts "getReturnUrl" and returns this objects returnUrl attribute. It proxies access to nested
070: * BusinessObjects to ensure that the application plugin classloader is used to resolve OJB proxies. And, it translates booleans
071: * for the UI, using the BooleanFormatter.
072: *
073: * @see net.sf.cglib.proxy.InvocationHandler#invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[]
074: * args)
075: *
076: */
077: public Object invoke(Object proxy, Method method, Object[] args)
078: throws Throwable {
079: ClassLoader oldClassloader = Thread.currentThread()
080: .getContextClassLoader();
081: try {
082: Thread.currentThread().setContextClassLoader(classLoader);
083: if ("getReturnUrl".equals(method.getName())) {
084: return returnUrl;
085: } else if ("getWorkflowLookupableResult".equals(method
086: .getName())) {
087: return Enhancer.create(HashMap.class,
088: new Class[] { WorkflowLookupableResult.class },
089: this );
090: } else if ("get".equals(method.getName())) {
091: Object propertyValue = ObjectUtils.getNestedValue(
092: proxiedBusinessObject, args[0].toString());
093: if (propertyValue instanceof BusinessObject) {
094: return Enhancer.create(propertyValue.getClass(),
095: new WorkflowLookupableInvocationHandler(
096: (BusinessObject) propertyValue,
097: classLoader));
098: } else {
099: if (propertyValue instanceof Boolean) {
100: return new BooleanFormatter()
101: .format(propertyValue);
102: }
103: return propertyValue;
104: }
105: } else {
106: return method.invoke(proxiedBusinessObject, args);
107: }
108: } catch (Exception e) {
109: throw (e.getCause() != null ? e.getCause() : e);
110: } finally {
111: Thread.currentThread()
112: .setContextClassLoader(oldClassloader);
113: }
114: }
115: }
|