KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > web > WebModelHandler


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.model.web;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14
15 import com.inversoft.beans.BeanException;
16 import com.inversoft.util.typeconverter.TypeConversionException;
17 import com.inversoft.verge.mvc.MVCException;
18 import com.inversoft.verge.mvc.MVCRequest;
19 import com.inversoft.verge.mvc.model.ModelHandler;
20 import com.inversoft.verge.mvc.model.ModelResolution;
21 import com.inversoft.verge.util.ScopeConstants;
22 import com.inversoft.verge.util.WebBeanProperty;
23
24
25 /**
26  * <p>
27  * This class is the model handler for the web MVC. This uses
28  * the {@link WebMetaData WebMetaData} stored in the
29  * extraParams Map by the {@link WebModelResolver WebModelResolver}
30  * to get and set the property on the WebBean.
31  * </p>
32  *
33  * @author Brian Pontarelli
34  * @since 2.0
35  * @version 2.0
36  */

37 public class WebModelHandler implements ModelHandler {
38
39     /**
40      * Constructor for WebModelHandler.
41      */

42     public WebModelHandler() {
43         super();
44     }
45
46     /**
47      * Retrieves the store meta data, creates a new WebBeanProperty and gets the
48      * value.
49      *
50      * @param mvcRequest Used to retrieve the value from the WebBeanProperty
51      * @param definition Not used
52      * @param resolution The ModelResolution of the model object
53      * @param extraParams Used to retrieve the meta data
54      */

55     public Object JavaDoc getValue(MVCRequest mvcRequest, String JavaDoc definition,
56             ModelResolution resolution, Map JavaDoc extraParams)
57     throws MVCException {
58
59         WebMetaData md = (WebMetaData) resolution.getMetaData();
60         if (md == null) {
61             md = buildMetaData(definition, resolution, extraParams);
62         }
63
64         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
65         WebBeanProperty wbp = null;
66         try {
67             wbp = md.createWebBeanProperty();
68             return wbp.getPropertyValue(request);
69         } catch (BeanException be) {
70             throw new MVCException(be);
71         }
72     }
73
74     /**
75      * Retrieves the stored meta data, creates a nw WebBeanProperty and sets the
76      * value.
77      *
78      * @param mvcRequest Used to set the value on the WebBeanProperty
79      * @param definition Not used
80      * @param resolution The ModelResolution of the model object
81      * @param value The value to set
82      * @param extraParams Used to retrieve the meta data
83      */

84     public void setValue(MVCRequest mvcRequest, String JavaDoc definition,
85         ModelResolution resolution, Object JavaDoc value, Map JavaDoc extraParams)
86     throws MVCException {
87
88         WebMetaData md = (WebMetaData) resolution.getMetaData();
89         if (md == null) {
90             md = buildMetaData(definition, resolution, extraParams);
91         }
92
93         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
94         WebBeanProperty wbp = null;
95         try {
96             wbp = md.createWebBeanProperty();
97             wbp.setPropertyValue(request, value); // Converts
98
} catch (BeanException be) {
99             throw new MVCException(be);
100         } catch (TypeConversionException tce) {
101
102             assert (wbp != null) : "wbp == null";
103             
104             // The conversion failed, so, we let the ValidatorParser handle it
105
mvcRequest.getValidatorParser().executeHandle(mvcRequest, tce,
106                 resolution.getModel(), wbp);
107         }
108     }
109
110     WebMetaData buildMetaData(String JavaDoc definition, ModelResolution resolution,
111             Map JavaDoc extraParams)
112     throws MVCException {
113         if (extraParams == null) {
114             extraParams = new HashMap JavaDoc();
115             extraParams.put(WebMetaData.CLASS_PARAMETER,
116                 resolution.getModel().getClass().getName());
117             extraParams.put(WebMetaData.SCOPE_PARAMETER,
118                 Integer.toString(ScopeConstants.REQUEST_INT));
119         }
120
121         return new WebMetaData(definition, extraParams);
122     }
123 }
Popular Tags