Save This Page
Home » apache-ofbiz-09.04 » org.ofbiz.widget.screen » [javadoc | source]
    1   /*******************************************************************************
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    *******************************************************************************/
   19   package org.ofbiz.widget.screen;
   20   
   21   import java.io.Serializable;
   22   import java.util.Collection;
   23   import java.util.List;
   24   import java.util.Map;
   25   import java.util.Set;
   26   
   27   import javolution.util.FastSet;
   28   
   29   import org.ofbiz.base.util.Debug;
   30   import org.ofbiz.base.util.GeneralException;
   31   import org.ofbiz.base.util.UtilValidate;
   32   import org.ofbiz.base.util.UtilXml;
   33   import org.ofbiz.base.util.string.FlexibleStringExpander;
   34   import org.ofbiz.entity.GenericDelegator;
   35   import org.ofbiz.entity.GenericEntity;
   36   import org.ofbiz.entity.GenericEntityException;
   37   import org.ofbiz.entity.transaction.TransactionUtil;
   38   import org.ofbiz.service.LocalDispatcher;
   39   import org.ofbiz.webapp.control.ConfigXMLReader;
   40   import org.ofbiz.widget.ModelWidget;
   41   import org.w3c.dom.Element;
   42   
   43   /**
   44    * Widget Library - Screen model class
   45    */
   46   @SuppressWarnings("serial")
   47   public class ModelScreen extends ModelWidget implements Serializable {
   48   
   49       public static final String module = ModelScreen.class.getName();
   50   
   51       protected String sourceLocation;
   52       protected FlexibleStringExpander transactionTimeoutExdr;
   53       protected Map<String, ModelScreen> modelScreenMap;
   54       protected boolean useCache;
   55   
   56       protected ModelScreenWidget.Section section;
   57   
   58       // ===== CONSTRUCTORS =====
   59       /** Default Constructor */
   60       protected ModelScreen() {}
   61   
   62       /** XML Constructor */
   63       public ModelScreen(Element screenElement, Map<String, ModelScreen> modelScreenMap, String sourceLocation) {
   64           super(screenElement);
   65           this.sourceLocation = sourceLocation;
   66           this.transactionTimeoutExdr = FlexibleStringExpander.getInstance(screenElement.getAttribute("transaction-timeout"));
   67           this.modelScreenMap = modelScreenMap;
   68           this.useCache = "true".equals(screenElement.getAttribute("use-cache"));
   69   
   70           // read in the section, which will read all sub-widgets too
   71           Element sectionElement = UtilXml.firstChildElement(screenElement, "section");
   72           if (sectionElement == null) {
   73               throw new IllegalArgumentException("No section found for the screen definition with name: " + this.name);
   74           }
   75           this.section = new ModelScreenWidget.Section(this, sectionElement);
   76           this.section.isMainSection = true;
   77       }
   78   
   79       public String getSourceLocation() {
   80           return sourceLocation;
   81       }
   82   
   83       public Set<String> getAllServiceNamesUsed() {
   84           Set<String> allServiceNamesUsed = FastSet.newInstance();
   85           findServiceNamesUsedInWidget(this.section, allServiceNamesUsed);
   86           return allServiceNamesUsed;
   87       }
   88   
   89       protected static void findServiceNamesUsedInWidget(ModelScreenWidget currentWidget, Set<String> allServiceNamesUsed) {
   90           if (currentWidget instanceof ModelScreenWidget.Section) {
   91               List<ModelScreenAction> actions = ((ModelScreenWidget.Section)currentWidget).actions;
   92               List<ModelScreenWidget> subWidgets = ((ModelScreenWidget.Section)currentWidget).subWidgets;
   93               List<ModelScreenWidget> failWidgets = ((ModelScreenWidget.Section)currentWidget).failWidgets;
   94               if (actions != null) {
   95                   for (ModelScreenAction screenOperation: actions) {
   96                       if (screenOperation instanceof ModelScreenAction.Service) {
   97                           String serviceName = ((ModelScreenAction.Service) screenOperation).serviceNameExdr.getOriginal();
   98                           if (UtilValidate.isNotEmpty(serviceName)) allServiceNamesUsed.add(serviceName);
   99                       }
  100                   }
  101               }
  102               if (subWidgets != null) {
  103                   for (ModelScreenWidget widget: subWidgets) {
  104                       findServiceNamesUsedInWidget(widget, allServiceNamesUsed);
  105                   }
  106               }
  107               if (failWidgets != null) {
  108                   for (ModelScreenWidget widget: failWidgets) {
  109                       findServiceNamesUsedInWidget(widget, allServiceNamesUsed);
  110                   }
  111               }
  112           } else if (currentWidget instanceof ModelScreenWidget.DecoratorSection) {
  113               ModelScreenWidget.DecoratorSection decoratorSection = (ModelScreenWidget.DecoratorSection)currentWidget;
  114               if (decoratorSection.subWidgets != null) {
  115                   for (ModelScreenWidget widget: decoratorSection.subWidgets) {
  116                       findServiceNamesUsedInWidget(widget, allServiceNamesUsed);
  117                   }
  118               }
  119           } else if (currentWidget instanceof ModelScreenWidget.DecoratorScreen) {
  120               ModelScreenWidget.DecoratorScreen decoratorScreen = (ModelScreenWidget.DecoratorScreen)currentWidget;
  121               if (decoratorScreen.sectionMap != null) {
  122                   Collection<ModelScreenWidget.DecoratorSection> sections = decoratorScreen.sectionMap.values();
  123                   for (ModelScreenWidget section: sections) {
  124                       findServiceNamesUsedInWidget(section, allServiceNamesUsed);
  125                   }
  126               }
  127           } else if (currentWidget instanceof ModelScreenWidget.Container) {
  128               ModelScreenWidget.Container container = (ModelScreenWidget.Container)currentWidget;
  129               if (container.subWidgets != null) {
  130                   for (ModelScreenWidget widget: container.subWidgets) {
  131                       findServiceNamesUsedInWidget(widget, allServiceNamesUsed);
  132                   }
  133               }
  134           } else if (currentWidget instanceof ModelScreenWidget.Screenlet) {
  135               ModelScreenWidget.Screenlet screenlet = (ModelScreenWidget.Screenlet)currentWidget;
  136               if (screenlet.subWidgets != null) {
  137                   for (ModelScreenWidget widget: screenlet.subWidgets) {
  138                       findServiceNamesUsedInWidget(widget, allServiceNamesUsed);
  139                   }
  140               }
  141           }
  142       }
  143       public Set<String> getAllEntityNamesUsed() {
  144           Set<String> allEntityNamesUsed = FastSet.newInstance();
  145           findEntityNamesUsedInWidget(this.section, allEntityNamesUsed);
  146           return allEntityNamesUsed;
  147       }
  148       protected static void findEntityNamesUsedInWidget(ModelScreenWidget currentWidget, Set<String> allEntityNamesUsed) {
  149           if (currentWidget instanceof ModelScreenWidget.Section) {
  150               List<ModelScreenAction> actions = ((ModelScreenWidget.Section)currentWidget).actions;
  151               List<ModelScreenWidget> subWidgets = ((ModelScreenWidget.Section)currentWidget).subWidgets;
  152               List<ModelScreenWidget> failWidgets = ((ModelScreenWidget.Section)currentWidget).failWidgets;
  153               if (actions != null) {
  154                   for (ModelScreenAction screenOperation: actions) {
  155                       if (screenOperation instanceof ModelScreenAction.EntityOne) {
  156                           String entName = ((ModelScreenAction.EntityOne) screenOperation).finder.getEntityName();
  157                           if (UtilValidate.isNotEmpty(entName)) allEntityNamesUsed.add(entName);
  158                       } else if (screenOperation instanceof ModelScreenAction.EntityAnd) {
  159                           String entName = ((ModelScreenAction.EntityAnd) screenOperation).finder.getEntityName();
  160                           if (UtilValidate.isNotEmpty(entName)) allEntityNamesUsed.add(entName);
  161                       } else if (screenOperation instanceof ModelScreenAction.EntityCondition) {
  162                           String entName = ((ModelScreenAction.EntityCondition) screenOperation).finder.getEntityName();
  163                           if (UtilValidate.isNotEmpty(entName)) allEntityNamesUsed.add(entName);
  164                       } else if (screenOperation instanceof ModelScreenAction.GetRelated) {
  165                           String relationName = ((ModelScreenAction.GetRelated) screenOperation).relationName;
  166                           if (UtilValidate.isNotEmpty(relationName)) allEntityNamesUsed.add(relationName);
  167                       } else if (screenOperation instanceof ModelScreenAction.GetRelatedOne) {
  168                           String relationName = ((ModelScreenAction.GetRelatedOne) screenOperation).relationName;
  169                           if (UtilValidate.isNotEmpty(relationName)) allEntityNamesUsed.add(relationName);
  170                       }
  171                   }
  172               }
  173               if (subWidgets != null) {
  174                   for (ModelScreenWidget widget: subWidgets) {
  175                       findEntityNamesUsedInWidget(widget, allEntityNamesUsed);
  176                   }
  177               }
  178               if (failWidgets != null) {
  179                   for (ModelScreenWidget widget: failWidgets) {
  180                       findEntityNamesUsedInWidget(widget, allEntityNamesUsed);
  181                   }
  182               }
  183           } else if (currentWidget instanceof ModelScreenWidget.DecoratorSection) {
  184               ModelScreenWidget.DecoratorSection decoratorSection = (ModelScreenWidget.DecoratorSection)currentWidget;
  185               if (decoratorSection.subWidgets != null) {
  186                   for (ModelScreenWidget widget: decoratorSection.subWidgets) {
  187                       findEntityNamesUsedInWidget(widget, allEntityNamesUsed);
  188                   }
  189               }
  190           } else if (currentWidget instanceof ModelScreenWidget.DecoratorScreen) {
  191               ModelScreenWidget.DecoratorScreen decoratorScreen = (ModelScreenWidget.DecoratorScreen)currentWidget;
  192               if (decoratorScreen.sectionMap != null) {
  193                   Collection<ModelScreenWidget.DecoratorSection> sections = decoratorScreen.sectionMap.values();
  194                   for (ModelScreenWidget section: sections) {
  195                       findEntityNamesUsedInWidget(section, allEntityNamesUsed);
  196                   }
  197               }
  198           } else if (currentWidget instanceof ModelScreenWidget.Container) {
  199               ModelScreenWidget.Container container = (ModelScreenWidget.Container)currentWidget;
  200               if (container.subWidgets != null) {
  201                   for (ModelScreenWidget widget: container.subWidgets) {
  202                       findEntityNamesUsedInWidget(widget, allEntityNamesUsed);
  203                   }
  204               }
  205           } else if (currentWidget instanceof ModelScreenWidget.Screenlet) {
  206               ModelScreenWidget.Screenlet screenlet = (ModelScreenWidget.Screenlet)currentWidget;
  207               if (screenlet.subWidgets != null) {
  208                   for (ModelScreenWidget widget: screenlet.subWidgets) {
  209                       findEntityNamesUsedInWidget(widget, allEntityNamesUsed);
  210                   }
  211               }
  212           }
  213       }
  214       public Set<String> getAllFormNamesIncluded() {
  215           Set<String> allFormNamesIncluded = FastSet.newInstance();
  216           findFormNamesIncludedInWidget(this.section, allFormNamesIncluded);
  217           return allFormNamesIncluded;
  218       }
  219       protected static void findFormNamesIncludedInWidget(ModelScreenWidget currentWidget, Set<String> allFormNamesIncluded) {
  220           if (currentWidget instanceof ModelScreenWidget.Form) {
  221               ModelScreenWidget.Form form = (ModelScreenWidget.Form) currentWidget;
  222               allFormNamesIncluded.add(form.locationExdr.getOriginal() + "#" + form.nameExdr.getOriginal());
  223           } else if (currentWidget instanceof ModelScreenWidget.Section) {
  224               ModelScreenWidget.Section section = (ModelScreenWidget.Section) currentWidget;
  225               if (section.subWidgets != null) {
  226                   for (ModelScreenWidget widget: section.subWidgets) {
  227                       findFormNamesIncludedInWidget(widget, allFormNamesIncluded);
  228                   }
  229               }
  230               if (section.failWidgets != null) {
  231                   for (ModelScreenWidget widget: section.failWidgets) {
  232                       findFormNamesIncludedInWidget(widget, allFormNamesIncluded);
  233                   }
  234               }
  235           } else if (currentWidget instanceof ModelScreenWidget.DecoratorSection) {
  236               ModelScreenWidget.DecoratorSection decoratorSection = (ModelScreenWidget.DecoratorSection) currentWidget;
  237               if (decoratorSection.subWidgets != null) {
  238                   for (ModelScreenWidget widget: decoratorSection.subWidgets) {
  239                       findFormNamesIncludedInWidget(widget, allFormNamesIncluded);
  240                   }
  241               }
  242           } else if (currentWidget instanceof ModelScreenWidget.DecoratorScreen) {
  243               ModelScreenWidget.DecoratorScreen decoratorScreen = (ModelScreenWidget.DecoratorScreen) currentWidget;
  244               if (decoratorScreen.sectionMap != null) {
  245                   Collection<ModelScreenWidget.DecoratorSection> sections = decoratorScreen.sectionMap.values();
  246                   for (ModelScreenWidget section: sections) {
  247                       findFormNamesIncludedInWidget(section, allFormNamesIncluded);
  248                   }
  249               }
  250           } else if (currentWidget instanceof ModelScreenWidget.Container) {
  251               ModelScreenWidget.Container container = (ModelScreenWidget.Container) currentWidget;
  252               if (container.subWidgets != null) {
  253                   for (ModelScreenWidget widget: container.subWidgets) {
  254                       findFormNamesIncludedInWidget(widget, allFormNamesIncluded);
  255                   }
  256               }
  257           } else if (currentWidget instanceof ModelScreenWidget.Screenlet) {
  258               ModelScreenWidget.Screenlet screenlet = (ModelScreenWidget.Screenlet) currentWidget;
  259               if (screenlet.subWidgets != null) {
  260                   for (ModelScreenWidget widget: screenlet.subWidgets) {
  261                       findFormNamesIncludedInWidget(widget, allFormNamesIncluded);
  262                   }
  263               }
  264           }
  265       }
  266   
  267       public Set<String> getAllRequestsLocationAndUri() throws GeneralException {
  268           Set<String> allRequestNamesIncluded = FastSet.newInstance();
  269           findRequestNamesLinkedtoInWidget(this.section, allRequestNamesIncluded);
  270           return allRequestNamesIncluded;
  271       }
  272       protected static void findRequestNamesLinkedtoInWidget(ModelScreenWidget currentWidget, Set<String> allRequestNamesIncluded) throws GeneralException {
  273           if (currentWidget instanceof ModelScreenWidget.Link) {
  274               ModelScreenWidget.Link link = (ModelScreenWidget.Link) currentWidget;
  275               String target = link.getTarget(null);
  276               String urlMode = link.getUrlMode();
  277               // Debug.logInfo("In findRequestNamesLinkedtoInWidget found link [" + link.rawString() + "] with target [" + target + "]", module);
  278   
  279               Set<String> controllerLocAndRequestSet = ConfigXMLReader.findControllerRequestUniqueForTargetType(target, urlMode);
  280               if (controllerLocAndRequestSet == null) return;
  281               allRequestNamesIncluded.addAll(controllerLocAndRequestSet);
  282           } else if (currentWidget instanceof ModelScreenWidget.Section) {
  283               ModelScreenWidget.Section section = (ModelScreenWidget.Section) currentWidget;
  284               if (section.subWidgets != null) {
  285                   for (ModelScreenWidget widget: section.subWidgets) {
  286                       findRequestNamesLinkedtoInWidget(widget, allRequestNamesIncluded);
  287                   }
  288               }
  289               if (section.failWidgets != null) {
  290                   for (ModelScreenWidget widget: section.failWidgets) {
  291                       findRequestNamesLinkedtoInWidget(widget, allRequestNamesIncluded);
  292                   }
  293               }
  294           } else if (currentWidget instanceof ModelScreenWidget.DecoratorSection) {
  295               ModelScreenWidget.DecoratorSection decoratorSection = (ModelScreenWidget.DecoratorSection) currentWidget;
  296               if (decoratorSection.subWidgets != null) {
  297                   for (ModelScreenWidget widget: decoratorSection.subWidgets) {
  298                       findRequestNamesLinkedtoInWidget(widget, allRequestNamesIncluded);
  299                   }
  300               }
  301           } else if (currentWidget instanceof ModelScreenWidget.DecoratorScreen) {
  302               ModelScreenWidget.DecoratorScreen decoratorScreen = (ModelScreenWidget.DecoratorScreen) currentWidget;
  303               if (decoratorScreen.sectionMap != null) {
  304                   Collection<ModelScreenWidget.DecoratorSection> sections = decoratorScreen.sectionMap.values();
  305                   for (ModelScreenWidget section: sections) {
  306                       findRequestNamesLinkedtoInWidget(section, allRequestNamesIncluded);
  307                   }
  308               }
  309           } else if (currentWidget instanceof ModelScreenWidget.Container) {
  310               ModelScreenWidget.Container container = (ModelScreenWidget.Container) currentWidget;
  311               if (container.subWidgets != null) {
  312                   for (ModelScreenWidget widget: container.subWidgets) {
  313                       findRequestNamesLinkedtoInWidget(widget, allRequestNamesIncluded);
  314                   }
  315               }
  316           } else if (currentWidget instanceof ModelScreenWidget.Screenlet) {
  317               ModelScreenWidget.Screenlet screenlet = (ModelScreenWidget.Screenlet) currentWidget;
  318               if (screenlet.subWidgets != null) {
  319                   for (ModelScreenWidget widget: screenlet.subWidgets) {
  320                       findRequestNamesLinkedtoInWidget(widget, allRequestNamesIncluded);
  321                   }
  322               }
  323           }
  324       }
  325   
  326   
  327       /**
  328        * Renders this screen to a String, i.e. in a text format, as defined with the
  329        * ScreenStringRenderer implementation.
  330        *
  331        * @param writer The Writer that the screen text will be written to
  332        * @param context Map containing the screen context; the following are
  333        *   reserved words in this context:
  334        *    - parameters (contains any special initial parameters coming in)
  335        *    - userLogin (if a user is logged in)
  336        *    - autoUserLogin (if a user is automatically logged in, ie no password has been entered)
  337        *    - formStringRenderer
  338        *    - request, response, session, application (special case, only in HTML contexts, etc)
  339        *    - delegator, dispatcher, security
  340        *    - null (represents a null field value for entity operations)
  341        *    - sections (used for decorators to reference the sections to be decorated and render them)
  342        * @param screenStringRenderer An implementation of the ScreenStringRenderer
  343        *   interface that is responsible for the actual text generation for
  344        *   different screen elements; implementing your own makes it possible to
  345        *   use the same screen definitions for many types of screen UIs
  346        */
  347       public void renderScreenString(Appendable writer, Map<String, Object> context, ScreenStringRenderer screenStringRenderer) throws ScreenRenderException {
  348           // make sure the "null" object is in there for entity ops
  349           context.put("null", GenericEntity.NULL_FIELD);
  350   
  351           setWidgetBoundaryComments(context);
  352   
  353           // wrap the whole screen rendering in a transaction, should improve performance in querying and such
  354           Map parameters = (Map) context.get("parameters");
  355           boolean beganTransaction = false;
  356           int transactionTimeout = -1;
  357           if (parameters != null) {
  358               String transactionTimeoutPar = (String) parameters.get("TRANSACTION_TIMEOUT");
  359               if (transactionTimeoutPar != null) {
  360                   try {
  361                       transactionTimeout = Integer.parseInt(transactionTimeoutPar);
  362                   } catch (NumberFormatException nfe) {
  363                       String msg = "TRANSACTION_TIMEOUT parameter for screen [" + this.sourceLocation + "#" + this.name + "] is invalid and it will be ignored: " + nfe.toString();
  364                       Debug.logWarning(msg, module);
  365                   }
  366               }
  367           }
  368   
  369           if (transactionTimeout < 0 && !transactionTimeoutExdr.isEmpty()) {
  370               // no TRANSACTION_TIMEOUT parameter, check screen attribute
  371               String transactionTimeoutStr = transactionTimeoutExdr.expandString(context);
  372               if (UtilValidate.isNotEmpty(transactionTimeoutStr)) {
  373                   try {
  374                       transactionTimeout = Integer.parseInt(transactionTimeoutStr);
  375                   } catch (NumberFormatException e) {
  376                       Debug.logWarning(e, "Could not parse transaction-timeout value, original=[" + transactionTimeoutExdr + "], expanded=[" + transactionTimeoutStr + "]", module);
  377                   }
  378               }
  379           }
  380   
  381           try {
  382               // If transaction timeout is not present (i.e. is equal to -1), the default transaction timeout is used
  383               // If transaction timeout is present, use it to start the transaction
  384               // If transaction timeout is set to zero, no transaction is started
  385               if (transactionTimeout < 0) {
  386                   beganTransaction = TransactionUtil.begin();
  387               }
  388               if (transactionTimeout > 0) {
  389                   beganTransaction = TransactionUtil.begin(transactionTimeout);
  390               }
  391   
  392               // render the screen, starting with the top-level section
  393               this.section.renderWidgetString(writer, context, screenStringRenderer);
  394           } catch (ScreenRenderException e) {
  395               throw e;
  396           } catch (RuntimeException e) {
  397               String errMsg = "Error rendering screen [" + this.sourceLocation + "#" + this.name + "]: " + e.toString();
  398               Debug.logError(errMsg + ". Rolling back transaction.", module);
  399               try {
  400                   // only rollback the transaction if we started one...
  401                   TransactionUtil.rollback(beganTransaction, errMsg, e);
  402               } catch (GenericEntityException e2) {
  403                   Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
  404               }
  405               // after rolling back, rethrow the exception
  406               throw new ScreenRenderException(errMsg, e);
  407           } catch (Exception e) {
  408               String errMsg = "Error rendering screen [" + this.sourceLocation + "#" + this.name + "]: " + e.toString();
  409               Debug.logError(errMsg + ". Rolling back transaction.", module);
  410               try {
  411                   // only rollback the transaction if we started one...
  412                   TransactionUtil.rollback(beganTransaction, errMsg, e);
  413               } catch (GenericEntityException e2) {
  414                   Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
  415               }
  416   
  417               // throw nested exception, don't need to log details here: Debug.logError(e, errMsg, module);
  418   
  419               // after rolling back, rethrow the exception
  420               throw new ScreenRenderException(errMsg, e);
  421           } finally {
  422               // only commit the transaction if we started one... this will throw an exception if it fails
  423               try {
  424                   TransactionUtil.commit(beganTransaction);
  425               } catch (GenericEntityException e2) {
  426                   Debug.logError(e2, "Could not commit transaction: " + e2.toString(), module);
  427               }
  428           }
  429       }
  430   
  431       public LocalDispatcher getDispatcher(Map<String, Object> context) {
  432           LocalDispatcher dispatcher = (LocalDispatcher) context.get("dispatcher");
  433           return dispatcher;
  434       }
  435   
  436       public GenericDelegator getDelegator(Map<String, Object> context) {
  437           GenericDelegator delegator = (GenericDelegator) context.get("delegator");
  438           return delegator;
  439       }
  440   }
  441   
  442   

Save This Page
Home » apache-ofbiz-09.04 » org.ofbiz.widget.screen » [javadoc | source]