1 7 package com.inversoft.verge.mvc.view.jsp.model; 8 9 10 import java.io.IOException ; 11 import java.util.Map ; 12 13 import javax.servlet.jsp.JspException ; 14 import javax.servlet.jsp.PageContext ; 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 40 public class ModelHelper { 41 42 45 private static final Logger logger = Logger.getLogger(ModelHelper.class); 46 47 48 51 private ModelHelper() { 52 super(); 53 } 54 55 56 70 public static ModelResolution getModelResolution(String key, String property, 71 PageContext pageContext) 72 throws JspException { 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 ("Unable to locate model object in any scope"); 83 } else if (jScope == PageContext.PAGE_SCOPE) { 84 throw new JspException ("Model object can not be in the pageContext"); 85 } 86 87 int scope = ScopeTools.convertFromJ2EE(jScope); 88 Object 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 (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 123 public static Object getValue(ModelResolution modelResolution, 124 PageContext pageContext) 125 throws JspException { 126 127 MetaData metaData = modelResolution.getMetaData(); 128 MVCRequest mvcRequest = JspTools.getMVCRequest(pageContext); 129 Object 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 (mvce); 136 } 137 138 return value; 139 } 140 141 145 public static void outputModelExtra(MetaData metaData, String name, 146 PageContext pageContext) 147 throws JspException { 148 149 StringBuffer buf = new StringBuffer (); 151 String system = metaData.getModelSystem(); 152 Map 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 ioe) { 162 throw new JspException (ioe); 163 } 164 } 165 166 173 public static void generateModelExtraParams(Map params, MetaData metaData, 174 String name, Object value) { 175 176 String system = metaData.getModelSystem(); 177 String valueStr = ObjectTools.cleanToString(value); 178 Map 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 |