1 24 package org.ofbiz.widget.screen; 25 26 import java.io.Serializable ; 27 import java.io.Writer ; 28 import java.util.Map ; 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 ; 42 43 50 public class ModelScreen implements Serializable { 51 52 public static final String module = ModelScreen.class.getName(); 53 54 protected String name; 55 protected String sourceLocation; 56 protected FlexibleStringExpander transactionTimeoutExdr; 57 protected Map modelScreenMap; 58 59 protected ModelScreenWidget.Section section; 60 61 63 protected ModelScreen() {} 64 65 66 public ModelScreen(Element screenElement, Map modelScreenMap, String 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 Element sectionElement = UtilXml.firstChildElement(screenElement, "section"); 74 if (sectionElement == null) { 75 throw new IllegalArgumentException ("No section found for the screen definition with name: " + this.name); 76 } 77 this.section = new ModelScreenWidget.Section(this, sectionElement); 78 } 79 80 100 public void renderScreenString(Writer writer, Map context, ScreenStringRenderer screenStringRenderer) throws GeneralException { 101 context.put("null", GenericEntity.NULL_FIELD); 103 104 boolean beganTransaction = false; 106 Map parameters = (Map ) context.get("parameters"); 107 int transactionTimeout = -1; 108 if (parameters != null) { 109 String transactionTimeoutPar = (String ) parameters.get("TRANSACTION_TIMEOUT"); 110 if (transactionTimeoutPar != null) { 111 try { 112 transactionTimeout = Integer.parseInt(transactionTimeoutPar); 113 } catch(NumberFormatException nfe) { 114 String 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 String transactionTimeoutStr = transactionTimeoutExdr.expandString(context); 123 if (UtilValidate.isNotEmpty(transactionTimeoutStr)) { 124 try { 125 transactionTimeout = Integer.parseInt(transactionTimeoutStr); 126 } catch (NumberFormatException e) { 127 Debug.logWarning(e, "Could not parse transaction-timeout value, original=[" + transactionTimeoutExdr + "], expanded=[" + transactionTimeoutStr + "]", module); 128 } 129 } 130 } 131 132 try { 133 if (transactionTimeout < 0) { 137 beganTransaction = TransactionUtil.begin(); 138 } 139 if (transactionTimeout > 0) { 140 beganTransaction = TransactionUtil.begin(transactionTimeout); 141 } 142 143 this.section.renderWidgetString(writer, context, screenStringRenderer); 145 } catch (RuntimeException e) { 146 String errMsg = "Error rendering screen [" + this.sourceLocation + "#" + this.name + "]: " + e.toString(); 147 Debug.logError(errMsg + ". Rolling back transaction.", module); 148 try { 149 TransactionUtil.rollback(beganTransaction, errMsg, e); 151 } catch (GenericEntityException e2) { 152 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); 153 } 154 throw new GeneralException(errMsg, e); 156 } catch (Exception e) { 157 String errMsg = "Error rendering screen [" + this.sourceLocation + "#" + this.name + "]: " + e.toString(); 158 Debug.logError(errMsg + ". Rolling back transaction.", module); 159 try { 160 TransactionUtil.rollback(beganTransaction, errMsg, e); 162 } catch (GenericEntityException e2) { 163 Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); 164 } 165 166 168 throw new GeneralException(errMsg, e); 170 } finally { 171 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 context) { 181 LocalDispatcher dispatcher = (LocalDispatcher) context.get("dispatcher"); 182 return dispatcher; 183 } 184 185 public GenericDelegator getDelegator(Map context) { 186 GenericDelegator delegator = (GenericDelegator) context.get("delegator"); 187 return delegator; 188 } 189 190 public String getName() { 191 return name; 192 } 193 } 194 195 | Popular Tags |