001: package simpleorm.simplewebapp.core;
002:
003: import simpleorm.simplewebapp.context.WPageContext;
004: import simpleorm.simplewebapp.core.WMenuParentGlobal;
005: import simpleorm.simplewebapp.core.WMenuItemGlobal;
006:
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.Map;
010:
011: /**
012: * The JSP Bean that contains the state for the current page, ie. menus, buttons and fields.
013: * Normally extended by application specific beans for specific page.
014: * Normally created by the WPageTag tag, but can be instantiated directly for unit tests.<p>
015: *
016: * The wPage JSP instance variable
017: * should be Page JSP beanName scope as forwarded pages will have their own beanName
018: * But must be Request scope so that idiot JSP Tablibs can still see them.
019: * Note that the WPageTag always clobers any exiting RequestBean,
020: * so they essentially do have page scope even though they are store on the request.<p>
021: *
022: * (Was called WPageBean (but not page scope) then WRequestBean (but really per page) and finally WPage.)<p>
023: *
024: * Most of the processing is done by WPageStructure which is one-to-one with this.
025: * This class mainly contains the JSP visible methods.
026: *
027: */
028: public class WPage {
029:
030: WPageStructure pageStructure = new WPageStructure(this );
031:
032: /**
033: * Set by WWebeanTag to wrap the PageContext.
034: * Remains null for unit tests outside the Web framework.
035: */
036: WPageContext pageContext;
037:
038: WPageSecurity pageSecurity = new WPageSecurity(this );
039:
040: /**
041: * The button in the button bar that was pressed to submit the form.
042: * Normally set by retrieveParameters but can be manually set for unit tests.
043: */
044: WButton submitButton = null;
045:
046: /**
047: * List of any errors to be displayed to the user, normally empty.
048: */
049: ArrayList<WValidationException> errors = new ArrayList<WValidationException>(
050: 0);
051:
052: final WLog logger = new WLog(); // Here so that we get a timestamp.
053:
054: WI18n i18n = new WI18n(this );
055:
056: /**
057: * Used mainly to open DB connections.
058: * Needs to be on Page, not Pagelet, so that all pagelets share the same connection, eg. in listCrud.
059: */
060: protected void onInitialize() throws Exception {
061: }
062:
063: protected void onFinalize() throws Exception {
064: }
065:
066: /**
067: * The name of the page, as stored in its WMenuItemGlobal.
068: */
069: public String getPageName() {
070: WMenuItemGlobal it = getPageItem();
071: if (it != null)
072: return it.getName();
073: else
074: return this .getClass().getSimpleName();
075: }
076:
077: public boolean isErroneous() {
078: return getErrors().size() > 0;
079: }
080:
081: void onFinalizeInternal() throws Exception {
082: if (pageStructure != null)
083: pageStructure.onFinalizeInternal();
084: pageContext = null;
085: submitButton = null;
086: errors = null;
087: }
088:
089: /**
090: * Utility to dump page state to the debug log.
091: */
092: public void logPage() {
093: if (logger.isDebug()) {
094: logger.debug(">>>>>>>>>>>>>> " + this + " " + submitButton);
095: for (WValidationException ex : errors)
096: logger.debug(" ??? Exception " + ex);
097: for (WPagelet plet : pageStructure.pagelets.values()) {
098: logger.debug(" Pagelet " + plet);
099: for (WFieldGroup fgroup : plet.getFieldGroups()
100: .values()) {
101: logger.debug(" Field Group " + fgroup);
102: for (WField field : fgroup.getFields().values())
103: logger.debug(" Field " + field);
104: }
105: }
106: logger
107: .debug("<<<<<<<<<<<<<< " + this + " "
108: + logger.time());
109: }
110: }
111:
112: public String toString() {
113: return "{" + this .getClass().getSimpleName() + " "
114: + getPageName() + "}";
115: }
116:
117: /** Exception if null */
118: public WPageContext getPageContext() {
119: if (pageContext == null)
120: throw new WException("WPageContext not set");
121: return pageContext;
122: }
123:
124: /////////////// Page Field Delegates, needed from JSP, do not remove (All JSP calls from here) ///////////////
125:
126: public Map<String, WFieldGroup> getFieldGroups() {
127: return getPagelet().getFieldGroups();
128: }
129:
130: /**
131: * This must be a one step method to work arround bugs in JSP/EL
132: */
133: public Collection<WFieldGroup> getFieldGroupsValues() {
134: return getPagelet().getFieldGroupsValues();
135: }
136:
137: /**
138: * Used in jsp as ${wPage.fields.id.text}
139: * Finds the given field in the current (or only) WPagelet.
140: * Exception if not found (otherwise JSPs ignore!)
141: */
142: public Map<String, WField> getFields() {
143: return getPagelet().getAllFieldsStrict();
144: }
145:
146: /**
147: * Convienience for unit tests
148: */
149: public WField getField(String fullName) {
150: return getPagelet().getFieldStrict(fullName);
151: }
152:
153: public Collection<WField> getSearchFieldValues() {
154: return getPageStructure().getCurrentPageletList()
155: .getSearchFieldValues();
156: }
157:
158: /**
159: * These are needed ${webBean.crudFields.options} does not work!
160: */
161: public @Deprecated
162: Collection<WField> getListFieldValues() {
163: return getPageStructure().getCurrentPageletList()
164: .getFieldGroup("list").getValues();
165: }
166:
167: /**
168: * Row nr of the current row. Used to shade every other row in lists. First is 0.
169: */
170: public int getRowIndex() throws Exception {
171: return getPageStructure().getCurrentPageletList().rowIndex;
172: }
173:
174: public boolean isMultipart() {
175: return getPageStructure().isMultipart;
176: }
177:
178: public String translate(String key) {
179: return i18n.translate(key);
180: }
181:
182: ////////////// Convenient delegators //////////////////
183:
184: /**
185: * getPageStructure().getCurrentPagelet()
186: */
187: public WPagelet getPagelet() {
188: return getPageStructure().getCurrentPagelet();
189: }
190:
191: public WPagelet getListPagelet() {
192: return getPageStructure().getCurrentPageletList();
193: }
194:
195: public WMenuParentGlobal getMenu() {
196: return getPageStructure().getPageItem().getParent();
197: }
198:
199: //////////////// Generated (Might be referenced from .jsp) /////////////////////
200:
201: public WPageStructure getPageStructure() {
202: return pageStructure;
203: }
204:
205: public WPageSecurity getPageSecurity() {
206: return pageSecurity;
207: }
208:
209: public void setPageContext(WPageContext pageContext) {
210: this .pageContext = pageContext;
211: }
212:
213: public WButtonBar getButtonBar() {
214: return pageStructure.buttonBar;
215: }
216:
217: public WButton getSubmitButton() {
218: return submitButton;
219: }
220:
221: public void setSubmitButton(WButton submitButton) {
222: this .submitButton = submitButton;
223: }
224:
225: public WMenuItemGlobal getPageItem() {
226: return pageStructure.pageItem;
227: }
228:
229: public ArrayList<WValidationException> getErrors() {
230: return errors;
231: }
232:
233: public WLog getLogger() {
234: return logger;
235: }
236:
237: public WI18n getI18n() {
238: return i18n;
239: }
240: }
|