KickJava   Java API By Example, From Geeks To Geeks.

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


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;
8
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11
12 import org.apache.log4j.Logger;
13
14 import com.inversoft.util.StringTools;
15 import com.inversoft.verge.mvc.MVCException;
16 import com.inversoft.verge.mvc.MVCRegistry;
17 import com.inversoft.verge.mvc.MVCRequest;
18
19
20 /**
21  * <p>
22  * This class is the default implementation of the {@link
23  * ModelParser ModelParser} interface for the Inversoft
24  * MVC system. This implementation reads the http parameters
25  * to determine how to handle the Model elements within
26  * the request.
27  * </p>
28  *
29  * <p>
30  * Based on each input from a form, this class will determine
31  * what type of model each input is, locate the model object
32  * and set the value.
33  * </p>
34  *
35  * <p>
36  * The request will contain zero or more parameters named
37  * '_ii_'. These parameters value will be the name of the
38  * http parameter that carries the value. Also stored in
39  * these parameters values is the model type and the model
40  * defintion, which is the indentifier that describes the
41  * model object as well as the model's value that is being
42  * set. A {@link ModelMVCInfo ModelMVCInfo} implementation
43  * class is then used to decode the input parameters value
44  * into the information needed to set the model's value.
45  * </p>
46  *
47  * <p>
48  * The model object, which could be a simple bean, or a
49  * repository item, or even a EJB, is located using the
50  * {@link ModelResolver ModelResolver} that is retrieved
51  * from the {@link ModelMVCInfo ModelMVCInfo}. Likewise,
52  * the {@link ModelHandler ModelHandler} is located in the
53  * same fashion. If a new ModelResolver or ModelHandler is
54  * to be written, it must be registered under a unique name
55  * with the MVCRegistry class.
56  * </p>
57  *
58  * @author Brian Pontarelli
59  * @since 2.0
60  * @version 2.0
61  */

62 public class DefaultModelParser implements ModelParser {
63
64     /**
65      * This classes logger
66      */

67     private static final Logger logger = Logger.getLogger(DefaultModelParser.class);
68
69     /**
70      * The name of the input parameter used to store all the input definitions
71      * described in the class comment
72      */

73     public static final String JavaDoc INPUT_PARAMETER = "_vi_";
74
75
76     /**
77      * Constructs a new <code>DefaultModelParser</code>.
78      */

79     public DefaultModelParser() {
80     }
81
82
83     /**
84      * Empty
85      */

86     public void preExecute(MVCRequest mvcRequest) throws MVCException {
87         // Empty
88
}
89
90     /**
91      * Using the HttpServletRequest, this method parses out the information
92      * needed to populate the model of the application and does the work or
93      * delegates the work of populating the model. Implementations should
94      * perform this operation however they see fit.
95      *
96      * @param mvcRequest Used to determine if the model values should be set and
97      * if validation should occur. This information is based on the
98      * action taken and therefore is used by the model. This also
99      * contains the servlet request used for parsing.
100      * @throws com.inversoft.verge.mvc.MVCException If there was any problem parse the HttpServletRequest
101      * @since 2.0
102      */

103     public void execute(MVCRequest mvcRequest) throws MVCException {
104
105         // If the model has been disabled, bail
106
if (!mvcRequest.isModelEnabled()) {
107             logger.debug("Model is disabled");
108             return;
109         }
110
111         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
112         String JavaDoc [] inputs = request.getParameterValues(INPUT_PARAMETER);
113         if (inputs == null || inputs.length == 0) {
114             logger.debug("No model parameters");
115             return;
116         }
117
118         ModelMVCInfo modelInfo = new ModelMVCInfo();
119         String JavaDoc [] modelValues;
120         ModelResolution resolution;
121         ModelResolver resolver;
122         ModelHandler handler;
123
124         for (int i = 0; i < inputs.length; i++) {
125             modelInfo.decode(inputs[i]);
126
127             // Retrieve the value of the input (or null if it doesn't exist)
128
modelValues = request.getParameterValues(modelInfo.getInputName());
129
130             // Maybe nullify the values?
131
modelValues = StringTools.convertEmpty(modelValues);
132
133             // Lookup the ModelResolver
134
resolver = MVCRegistry.lookupModelResolver(modelInfo.getModelSystem());
135             if (resolver == null) {
136                 logger.error("Invalid modelResolver: " + modelInfo.getModelSystem() +
137                     ". No ModelResolver registered under that name");
138                 throw new MVCException("Invalid modelResolver: " +
139                     modelInfo.getModelSystem() + ". No ModelResolver" +
140                     " registered under that name");
141             }
142
143             resolution = resolver.resolve(mvcRequest, modelInfo.getModelDefinition(),
144                 modelInfo.getExtraParams());
145
146             // If the model was not found, log an error and throw an exception
147
if (resolution == null) {
148                 logger.error("Model does exist for definition: " +
149                     modelInfo.getModelDefinition());
150                 throw new MVCException("Model does exist for definition: " +
151                     modelInfo.getModelDefinition());
152             }
153
154             // Add the model to the set, if that instance already exists, it will
155
// not be added again
156
mvcRequest.putModelObject(resolution.getMetaData().getID(),
157                 resolution.getModel());
158
159             handler = MVCRegistry.lookupModelHandler(modelInfo.getModelSystem());
160             if (handler == null) {
161                 logger.error("Invalid modelHandler: " + modelInfo.getModelSystem() +
162                     ". No ModelHandler registered under that name");
163                 throw new MVCException("Invalid modelHandler: " +
164                     modelInfo.getModelSystem() + ". No ModelHandler" +
165                     " registered under that name");
166             }
167             handler.setValue(mvcRequest, modelInfo.getModelDefinition(), resolution,
168                 modelValues, modelInfo.getExtraParams());
169         }
170     }
171 }
Popular Tags