KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > model > ModelHelper


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.view.jsp.model;
8
9
10 import java.io.IOException JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.servlet.jsp.JspException JavaDoc;
14 import javax.servlet.jsp.PageContext JavaDoc;
15
16 import org.apache.log4j.Logger;
17
18 import com.inversoft.util.ObjectTools;
19 import com.inversoft.verge.mvc.MVCException;
20 import com.inversoft.verge.mvc.MVCRequest;
21 import com.inversoft.verge.mvc.model.DefaultModelParser;
22 import com.inversoft.verge.mvc.model.MetaData;
23 import com.inversoft.verge.mvc.model.ModelMVCInfo;
24 import com.inversoft.verge.mvc.model.ModelResolution;
25 import com.inversoft.verge.mvc.model.web.WebMetaData;
26 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
27 import com.inversoft.verge.mvc.view.jsp.JspTools;
28 import com.inversoft.verge.util.ScopeTools;
29
30
31 /**
32  * <p>
33  * This class is a view helper for Form MVC tags.
34  * </p>
35  *
36  * @author Brian Pontarelli
37  * @since 2.0
38  * @version 2.0
39  */

40 public class ModelHelper {
41
42     /**
43      * This classes logger
44      */

45     private static final Logger logger = Logger.getLogger(ModelHelper.class);
46
47
48     /**
49      * Constructor for ModelHelper.
50      */

51     private ModelHelper() {
52         super();
53     }
54
55
56     /**
57      * Returns the ModelResolution which should have been be stored in the
58      * ModelResolutionRegistry. This also sets the property on the MetaData and
59      * if necessary, creates a ModelResoltuion (mosty for web models) if
60      * necessary. If a ModelResolution is created, it is also stored in the
61      * ModelResolutionRegistry.
62      *
63      * @param key The key of the ModelResolution
64      * @param property The property to set into the ModelResolution
65      * @param pageContext The PageContext to use to lookup and possibly store
66      * the ModelResolution
67      * @return The ModelResolution and never null
68      * @throws JspException If the ModelResolution could not be found or created
69      */

70     public static ModelResolution getModelResolution(String JavaDoc key, String JavaDoc property,
71             PageContext JavaDoc pageContext)
72     throws JspException JavaDoc {
73
74         ModelResolution modelResolution = ModelResolutionRegistry.lookup(key,
75             pageContext);
76         MetaData metaData = null;
77         if (modelResolution == null) {
78             logger.debug("Model tag using web model object");
79
80             int jScope = pageContext.getAttributesScope(key);
81             if (jScope == 0) {
82                 throw new JspException JavaDoc("Unable to locate model object in any scope");
83             } else if (jScope == PageContext.PAGE_SCOPE) {
84                 throw new JspException JavaDoc("Model object can not be in the pageContext");
85             }
86
87             int scope = ScopeTools.convertFromJ2EE(jScope);
88             Object JavaDoc model = pageContext.findAttribute(key);
89
90             if (logger.isDebugEnabled()) {
91                 logger.debug("Using object in scope " +
92                     ScopeTools.convertScope(scope) + " under key " + key);
93             }
94
95             try {
96                 metaData = new WebMetaData(key, model.getClass().getName(), scope);
97                 modelResolution = new ModelResolution(model, metaData);
98                 ModelResolutionRegistry.store(key, modelResolution, pageContext);
99             } catch (MVCException mvce) {
100                 throw new JspException JavaDoc(mvce);
101             }
102         } else {
103             if (modelResolution.getModel() == null) {
104                 modelResolution.setModel(pageContext.getAttribute(key));
105
106                 if (logger.isDebugEnabled()) {
107                     logger.debug("Retrieved model object from PageContext under" +
108                         " key " + key);
109                 }
110             }
111
112             metaData = modelResolution.getMetaData();
113         }
114
115         metaData.setProperty(property);
116
117         return modelResolution;
118     }
119
120     /**
121      * Returns the value of the given model object
122      */

123     public static Object JavaDoc getValue(ModelResolution modelResolution,
124             PageContext JavaDoc pageContext)
125     throws JspException JavaDoc {
126
127         MetaData metaData = modelResolution.getMetaData();
128         MVCRequest mvcRequest = JspTools.getMVCRequest(pageContext);
129         Object JavaDoc value = null;
130         try {
131             value = metaData.getModelHandler().getValue(mvcRequest,
132                 metaData.getDefinition(), modelResolution,
133                 metaData.getExtraParams());
134         } catch (MVCException mvce) {
135             throw new JspException JavaDoc(mvce);
136         }
137
138         return value;
139     }
140
141     /**
142      * Outputs the extra model information to the JspWriter in the PageContext
143      * given, using the information in the tag given and MetaData given.
144      */

145     public static void outputModelExtra(MetaData metaData, String JavaDoc name,
146             PageContext JavaDoc pageContext)
147     throws JspException JavaDoc {
148
149         // Get the handler and resolver names
150
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
151         String JavaDoc system = metaData.getModelSystem();
152         Map JavaDoc extraParams = metaData.getExtraParams();
153
154         ModelMVCInfo info = new ModelMVCInfo(name, system, metaData.getDefinition(),
155             extraParams);
156         HtmlViewToolkit.appendHiddenTag(buf, DefaultModelParser.INPUT_PARAMETER,
157             info.encode());
158
159         try {
160             pageContext.getOut().print(buf.toString());
161         } catch (IOException JavaDoc ioe) {
162             throw new JspException JavaDoc(ioe);
163         }
164     }
165
166     /**
167      * Adds a name value pair to the given Map that are the model extra parameters.
168      * This is the same as the model extra information written using the {@link
169      * #outputModelExtra(MetaData, String, PageContext) outputModelExtra}
170      * method. The parameters use the name of the BaseModelTag. Therefore, this
171      * must be set.
172      */

173     public static void generateModelExtraParams(Map JavaDoc params, MetaData metaData,
174             String JavaDoc name, Object JavaDoc value) {
175
176         String JavaDoc system = metaData.getModelSystem();
177         String JavaDoc valueStr = ObjectTools.cleanToString(value);
178         Map JavaDoc extraParams = metaData.getExtraParams();
179
180         ModelMVCInfo info = new ModelMVCInfo(name, system, metaData.getDefinition(),
181             extraParams);
182         params.put(name, valueStr);
183         params.put(DefaultModelParser.INPUT_PARAMETER, info.encode());
184     }
185 }
Popular Tags