KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > DefaultModelHandler


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
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;
8
9 import java.util.Map JavaDoc;
10
11 import com.inversoft.beans.BeanException;
12 import com.inversoft.util.typeconverter.TypeConversionException;
13 import com.inversoft.verge.mvc.MVCException;
14 import com.inversoft.verge.mvc.MVCRequest;
15 import com.inversoft.verge.mvc.config.BaseConfigStruct;
16 import com.inversoft.verge.mvc.config.BaseFormConfig;
17 import com.inversoft.verge.util.WebBean;
18 import com.inversoft.verge.util.WebBeanProperty;
19
20
21 /**
22  * <p>
23  * This class
24  * </p>
25  *
26  * @author Brian Pontarelli
27  */

28 public class DefaultModelHandler implements ModelHandler {
29
30     /**
31      * <p>
32      * Retrieves the FormBean's current value using model object stored inside
33      * the ModelResolution passed in.
34      * </p>
35      *
36      * @param mvcRequest The MVCRequest that may be used to help facilitate
37      * retrieval of the model object's value
38      * @param definition The model defintion that describes how the retrieve
39      * the model object's value
40      * @param resolution The odelResolution that contains the form bean
41      * @param extraParams Any extra parameters that may have been specified and
42      * were parsed by the ModelParser
43      * @return The model object's current value
44      * @throws MVCException If there was a problem retrieving the model object's
45      * value
46      */

47     public Object JavaDoc getValue(MVCRequest mvcRequest, String JavaDoc definition,
48             ModelResolution resolution, Map JavaDoc extraParams)
49     throws MVCException {
50
51         assert (definition != null) : "definition == null";
52         assert (resolution != null) : "resolution == null";
53         assert (resolution.getModel() != null) : "resolution.getModel() == null";
54
55         BaseConfigStruct struct = (BaseConfigStruct) mvcRequest.getConfiguration();
56         BaseFormConfig config = struct.baseFormConfig;
57         MetaData md = resolution.getMetaData();
58         WebBean webBean = config.getFormBean(md.getID());
59         Object JavaDoc model = resolution.getModel();
60         try {
61             return webBean.getPropertyValue(md.getProperty(), model);
62         } catch (BeanException be) {
63             throw new MVCException(be);
64         }
65     }
66
67     /**
68      * <p>
69      * Populates the FormBean's current value using the ModelResolution passed
70      * in.
71      * </p>
72      *
73      * @param mvcRequest The MVCRequest that may be used to help facilitate
74      * population of the model object's value
75      * @param definition The model defintion that describes how to populate
76      * the model object's value
77      * @param resolution The ModelResolution that contains the form bean
78      * @param value The value to set into the model object
79      * @param extraParams Any extra parameters that may have been specified and
80      * were parsed by the ModelParser
81      * @throws MVCException If there was a problem setting the model object's
82      * value
83      */

84     public void setValue(MVCRequest mvcRequest, String JavaDoc definition,
85             ModelResolution resolution, Object JavaDoc value, Map JavaDoc extraParams)
86     throws MVCException {
87
88         assert (definition != null) : "definition == null";
89         assert (resolution != null) : "resolution == null";
90
91         Object JavaDoc model = resolution.getModel();
92         BaseConfigStruct struct = (BaseConfigStruct) mvcRequest.getConfiguration();
93         BaseFormConfig config = struct.baseFormConfig;
94         MetaData md = resolution.getMetaData();
95         WebBean webBean = config.getFormBean(resolution.getMetaData().getID());
96         try {
97             webBean.setPropertyValue(md.getProperty(), model, value, true);
98         } catch (BeanException be) {
99             throw new MVCException(be);
100         } catch (TypeConversionException tce) {
101
102             WebBeanProperty wbp = null;
103             try {
104                 wbp = webBean.getWebBeanProperty(md.getProperty());
105             } catch (BeanException be) {
106                 throw new MVCException("FATAL: ", be);
107             }
108
109             // The conversion failed, so, we let the ValidatorParser handle it
110
mvcRequest.getValidatorParser().executeHandle(mvcRequest, tce, model, wbp);
111         }
112     }
113 }
Popular Tags