01: /*
02: * (C) Copyright 2006 Nabh Information Systems, Inc.
03: *
04: * All copyright notices regarding Nabh's products MUST remain
05: * intact in the scripts and in the outputted HTML.
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21: package com.nabhinc.portlet.mvcportlet.beanutil;
22:
23: import java.lang.reflect.Method;
24:
25: import javax.portlet.PortletException;
26: import javax.portlet.PortletRequest;
27:
28: import org.w3c.dom.Element;
29:
30: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
31: import com.nabhinc.portlet.mvcportlet.core.PropertySetter;
32: import com.nabhinc.util.StringUtil;
33:
34: /**
35: *
36: *
37: * @author Padmanabh Dabke
38: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
39: * @since 1.1
40: */
41: public class ObjectPropertySetter implements PropertySetter {
42: private ObjectRegistry opsFactory = null;
43:
44: private Method opsSetMethod = null;
45:
46: public ObjectPropertySetter(ObjectRegistry factory, Method setMethod) {
47: opsFactory = factory;
48: opsSetMethod = setMethod;
49: }
50:
51: public void setProperty(PortletRequest request, Object target,
52: String paramName, String propertyName)
53: throws PortletException {
54: try {
55: String objectId = request.getParameter(paramName);
56: if (StringUtil.isNotNullOrEmpty(objectId)) {
57: Object refValue = opsFactory.get(objectId);
58: if (refValue == null) {
59: throw new PortletException(
60: "Invalid object ID for property "
61: + propertyName);
62: } else {
63: opsSetMethod.invoke(target,
64: new Object[] { refValue });
65: }
66: }
67: } catch (Exception e) {
68: throw new PortletException("Failed to set property "
69: + propertyName, e);
70: }
71:
72: }
73:
74: public void init(Element config, ControllerPortletConfig cpConfig)
75: throws PortletException {
76: // No initialization needed
77:
78: }
79:
80: public Object getValue(PortletRequest request, String paramValue)
81: throws PortletException {
82: return opsFactory.get(paramValue);
83: }
84:
85: }
|