KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > ModelScreen


1 /*
2  * $Id: ModelScreen.java 7061 2006-03-24 04:37:21Z jonesde $
3  *
4  * Copyright (c) 2004-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.screen;
25
26 import java.io.Serializable JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.GeneralException;
32 import org.ofbiz.base.util.UtilValidate;
33 import org.ofbiz.base.util.UtilXml;
34 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
35 import org.ofbiz.base.util.string.FlexibleStringExpander;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.entity.GenericEntity;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.ofbiz.entity.transaction.TransactionUtil;
40 import org.ofbiz.service.LocalDispatcher;
41 import org.w3c.dom.Element JavaDoc;
42
43 /**
44  * Widget Library - Screen model class
45  *
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  * @version $Rev: 7061 $
48  * @since 3.1
49  */

50 public class ModelScreen implements Serializable JavaDoc {
51
52     public static final String JavaDoc module = ModelScreen.class.getName();
53
54     protected String JavaDoc name;
55     protected String JavaDoc sourceLocation;
56     protected FlexibleStringExpander transactionTimeoutExdr;
57     protected Map JavaDoc modelScreenMap;
58     
59     protected ModelScreenWidget.Section section;
60
61     // ===== CONSTRUCTORS =====
62
/** Default Constructor */
63     protected ModelScreen() {}
64
65     /** XML Constructor */
66     public ModelScreen(Element JavaDoc screenElement, Map JavaDoc modelScreenMap, String JavaDoc sourceLocation) {
67         this.sourceLocation = sourceLocation;
68         this.name = screenElement.getAttribute("name");
69         this.transactionTimeoutExdr = new FlexibleStringExpander(screenElement.getAttribute("transaction-timeout"));
70         this.modelScreenMap = modelScreenMap;
71
72         // read in the section, which will read all sub-widgets too
73
Element JavaDoc sectionElement = UtilXml.firstChildElement(screenElement, "section");
74         if (sectionElement == null) {
75             throw new IllegalArgumentException JavaDoc("No section found for the screen definition with name: " + this.name);
76         }
77         this.section = new ModelScreenWidget.Section(this, sectionElement);
78     }
79
80     /**
81      * Renders this screen to a String, i.e. in a text format, as defined with the
82      * ScreenStringRenderer implementation.
83      *
84      * @param writer The Writer that the screen text will be written to
85      * @param context Map containing the screen context; the following are
86      * reserved words in this context:
87      * - parameters (contains any special initial parameters coming in)
88      * - userLogin (if a user is logged in)
89      * - autoUserLogin (if a user is automatically logged in, ie no password has been entered)
90      * - formStringRenderer
91      * - request, response, session, application (special case, only in HTML contexts, etc)
92      * - delegator, dispatcher, security
93      * - null (represents a null field value for entity operations)
94      * - sections (used for decorators to reference the sections to be decorated and render them)
95      * @param screenStringRenderer An implementation of the ScreenStringRenderer
96      * interface that is responsible for the actual text generation for
97      * different screen elements; implementing your own makes it possible to
98      * use the same screen definitions for many types of screen UIs
99      */

100     public void renderScreenString(Writer JavaDoc writer, Map JavaDoc context, ScreenStringRenderer screenStringRenderer) throws GeneralException {
101         // make sure the "null" object is in there for entity ops
102
context.put("null", GenericEntity.NULL_FIELD);
103
104         // wrap the whole screen rendering in a transaction, should improve performance in querying and such
105
boolean beganTransaction = false;
106         Map JavaDoc parameters = (Map JavaDoc) context.get("parameters");
107         int transactionTimeout = -1;
108         if (parameters != null) {
109             String JavaDoc transactionTimeoutPar = (String JavaDoc) parameters.get("TRANSACTION_TIMEOUT");
110             if (transactionTimeoutPar != null) {
111                 try {
112                     transactionTimeout = Integer.parseInt(transactionTimeoutPar);
113                 } catch(NumberFormatException JavaDoc nfe) {
114                     String JavaDoc msg = "TRANSACTION_TIMEOUT parameter for screen [" + this.sourceLocation + "#" + this.name + "] is invalid and it will be ignored: " + nfe.toString();
115                     Debug.logWarning(msg, module);
116                 }
117             }
118         }
119         
120         if (transactionTimeout < 0 && !transactionTimeoutExdr.isEmpty()) {
121             // no TRANSACTION_TIMEOUT parameter, check screen attribute
122
String JavaDoc transactionTimeoutStr = transactionTimeoutExdr.expandString(context);
123             if (UtilValidate.isNotEmpty(transactionTimeoutStr)) {
124                 try {
125                     transactionTimeout = Integer.parseInt(transactionTimeoutStr);
126                 } catch (NumberFormatException JavaDoc e) {
127                     Debug.logWarning(e, "Could not parse transaction-timeout value, original=[" + transactionTimeoutExdr + "], expanded=[" + transactionTimeoutStr + "]", module);
128                 }
129             }
130         }
131         
132         try {
133             // If transaction timeout is not present (i.e. is equal to -1), the default transaction timeout is used
134
// If transaction timeout is present, use it to start the transaction
135
// If transaction timeout is set to zero, no transaction is started
136
if (transactionTimeout < 0) {
137                 beganTransaction = TransactionUtil.begin();
138             }
139             if (transactionTimeout > 0) {
140                 beganTransaction = TransactionUtil.begin(transactionTimeout);
141             }
142
143             // render the screen, starting with the top-level section
144
this.section.renderWidgetString(writer, context, screenStringRenderer);
145         } catch (RuntimeException JavaDoc e) {
146             String JavaDoc errMsg = "Error rendering screen [" + this.sourceLocation + "#" + this.name + "]: " + e.toString();
147             Debug.logError(errMsg + ". Rolling back transaction.", module);
148             try {
149                 // only rollback the transaction if we started one...
150
TransactionUtil.rollback(beganTransaction, errMsg, e);
151             } catch (GenericEntityException e2) {
152                 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
153             }
154             // after rolling back, rethrow the exception
155
throw new GeneralException(errMsg, e);
156         } catch (Exception JavaDoc e) {
157             String JavaDoc errMsg = "Error rendering screen [" + this.sourceLocation + "#" + this.name + "]: " + e.toString();
158             Debug.logError(errMsg + ". Rolling back transaction.", module);
159             try {
160                 // only rollback the transaction if we started one...
161
TransactionUtil.rollback(beganTransaction, errMsg, e);
162             } catch (GenericEntityException e2) {
163                 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
164             }
165             
166             // throw nested exception, don't need to log details here: Debug.logError(e, errMsg, module);
167

168             // after rolling back, rethrow the exception
169
throw new GeneralException(errMsg, e);
170         } finally {
171             // only commit the transaction if we started one... this will throw an exception if it fails
172
try {
173                 TransactionUtil.commit(beganTransaction);
174             } catch (GenericEntityException e2) {
175                 Debug.logError(e2, "Could not commit transaction: " + e2.toString(), module);
176             }
177         }
178     }
179
180     public LocalDispatcher getDispatcher(Map JavaDoc context) {
181         LocalDispatcher dispatcher = (LocalDispatcher) context.get("dispatcher");
182         return dispatcher;
183     }
184
185     public GenericDelegator getDelegator(Map JavaDoc context) {
186         GenericDelegator delegator = (GenericDelegator) context.get("delegator");
187         return delegator;
188     }
189     
190     public String JavaDoc getName() {
191         return name;
192     }
193 }
194
195
Popular Tags