1 24 package org.ofbiz.widget.form; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.ListIterator ; 32 import java.util.Map ; 33 import java.util.NoSuchElementException ; 34 import java.util.Set ; 35 import java.util.TreeSet ; 36 37 import javolution.util.FastList; 38 39 import bsh.EvalError; 40 import bsh.Interpreter; 41 42 import org.ofbiz.base.util.BshUtil; 43 import org.ofbiz.base.util.Debug; 44 import org.ofbiz.base.util.UtilValidate; 45 import org.ofbiz.base.util.UtilXml; 46 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 47 import org.ofbiz.base.util.string.FlexibleStringExpander; 48 import org.ofbiz.entity.GenericDelegator; 49 import org.ofbiz.entity.GenericEntityException; 50 import org.ofbiz.entity.model.ModelEntity; 51 import org.ofbiz.entity.model.ModelField; 52 import org.ofbiz.entity.util.EntityListIterator; 53 import org.ofbiz.service.GenericServiceException; 54 import org.ofbiz.service.LocalDispatcher; 55 import org.ofbiz.service.ModelParam; 56 import org.ofbiz.service.ModelService; 57 58 import org.w3c.dom.Element ; 59 60 68 public class ModelForm { 69 70 public static final String module = ModelForm.class.getName(); 71 public static final String DEFAULT_FORM_RESULT_LIST_NAME = "defaultFormResultList"; 72 73 protected GenericDelegator delegator; 74 protected LocalDispatcher dispatcher; 75 76 protected String name; 77 protected String type; 78 protected FlexibleStringExpander target; 79 protected String targetType; 80 protected String title; 81 protected String tooltip; 82 protected String listName; 83 protected String listEntryName; 84 protected FlexibleMapAccessor defaultMapName; 85 protected String defaultEntityName; 86 protected String defaultServiceName; 87 protected String formTitleAreaStyle; 88 protected String formWidgetAreaStyle; 89 protected String defaultTitleAreaStyle; 90 protected String defaultWidgetAreaStyle; 91 protected String defaultTitleStyle; 92 protected String defaultWidgetStyle; 93 protected String defaultTooltipStyle; 94 protected String itemIndexSeparator; 95 protected FlexibleStringExpander paginateTarget; 96 protected FlexibleStringExpander paginateIndexField; 97 protected FlexibleStringExpander paginateSizeField; 98 protected FlexibleStringExpander paginatePreviousLabel; 99 protected FlexibleStringExpander paginateNextLabel; 100 protected String paginateTargetAnchor; 101 protected String paginatePreviousStyle; 102 protected String paginateNextStyle; 103 protected boolean separateColumns = false; 104 protected String listIteratorName; 105 protected boolean paginate = true; 106 protected boolean cssStyling = false; 107 protected boolean useRowSubmit = false; 108 protected FlexibleStringExpander targetWindowExdr; 109 protected String defaultRequiredFieldStyle; 110 protected String oddRowStyle; 111 protected String evenRowStyle; 112 protected String defaultTableStyle; 113 protected String headerRowStyle; 114 protected boolean skipStart = false; 115 protected boolean skipEnd = false; 116 protected boolean hideHeader = false; 117 118 protected List altTargets = new LinkedList (); 119 protected List autoFieldsServices = new LinkedList (); 120 protected List autoFieldsEntities = new LinkedList (); 121 protected List sortOrderFields = new LinkedList (); 122 123 132 protected List fieldList = new LinkedList (); 133 134 138 protected Map fieldMap = new HashMap (); 139 140 143 protected List fieldGroupList = new ArrayList (); 144 145 148 protected Map fieldGroupMap = new HashMap (); 149 150 153 protected FieldGroup defaultFieldGroup; 154 155 156 public static String DEFAULT_TARGET_TYPE = "intra-app"; 157 158 159 public static int DEFAULT_PAGE_SIZE = 100; 160 protected int viewIndex = 0; 161 protected int viewSize = DEFAULT_PAGE_SIZE; 162 protected int lowIndex = -1; 163 protected int highIndex = -1; 164 protected int listSize = 0; 165 protected int actualPageSize = 0; 166 public static String DEFAULT_PAG_INDEX_FIELD = "viewIndex"; 167 public static String DEFAULT_PAG_SIZE_FIELD = "viewSize"; 168 public static String DEFAULT_PAG_PREV_LABEL = "Previous"; 169 public static String DEFAULT_PAG_NEXT_LABEL = "Next"; 170 public static String DEFAULT_PAG_PREV_STYLE = "buttontext"; 171 public static String DEFAULT_PAG_NEXT_STYLE = "buttontext"; 172 173 protected List actions; 174 protected List rowActions; 175 protected FlexibleStringExpander rowCountExdr; 176 protected ModelFormField multiSubmitField; 177 protected int rowCount = 0; 178 179 181 public ModelForm() {} 182 183 184 public ModelForm(Element formElement, GenericDelegator delegator, LocalDispatcher dispatcher) { 185 this.delegator = delegator; 186 this.dispatcher = dispatcher; 187 initForm(formElement); 188 } 189 190 public ModelForm(Element formElement) { 191 initForm(formElement); 192 } 193 194 public void initForm(Element formElement) { 195 196 String parentResource = formElement.getAttribute("extends-resource"); 198 String parentForm = formElement.getAttribute("extends"); 199 if (parentForm.length() > 0 && !parentForm.equals(formElement.getAttribute("name"))) { 201 ModelForm parent = null; 202 if (parentResource.length() > 0) { 204 try { 205 parent = FormFactory.getFormFromLocation(parentResource, parentForm, delegator, dispatcher); 206 } catch (Exception e) { 207 Debug.logError(e, "Failed to load parent form definition '" + parentForm + "' at resource '" + parentResource + "'", module); 208 } 209 } else { 210 Element rootElement = formElement.getOwnerDocument().getDocumentElement(); 212 List formElements = UtilXml.childElementList(rootElement, "form"); 213 Iterator formElementIter = formElements.iterator(); 216 while (formElementIter.hasNext()) { 217 Element formElementEntry = (Element ) formElementIter.next(); 218 if (formElementEntry.getAttribute("name").equals(parentForm)) { 219 parent = new ModelForm(formElementEntry, delegator, dispatcher); 220 break; 221 } 222 } 223 if (parent == null) { 224 Debug.logError("Failed to find parent form defenition '" + parentForm + "' in same document.", module); 225 } 226 } 227 228 if (parent != null) { 229 this.type = parent.type; 230 this.target = parent.target; 231 this.title = parent.title; 232 this.tooltip = parent.tooltip; 233 this.listName = parent.listName; 234 this.listEntryName = parent.listEntryName; 235 this.tooltip = parent.tooltip; 236 this.defaultEntityName = parent.defaultEntityName; 237 this.defaultServiceName = parent.defaultServiceName; 238 this.formTitleAreaStyle = parent.formTitleAreaStyle; 239 this.formWidgetAreaStyle = parent.formWidgetAreaStyle; 240 this.defaultTitleAreaStyle = parent.defaultTitleAreaStyle; 241 this.defaultWidgetAreaStyle = parent.defaultWidgetAreaStyle; 242 this.oddRowStyle = parent.oddRowStyle; 243 this.evenRowStyle = parent.evenRowStyle; 244 this.defaultTableStyle = parent.defaultTableStyle; 245 this.headerRowStyle = parent.headerRowStyle; 246 this.defaultTitleStyle = parent.defaultTitleStyle; 247 this.defaultWidgetStyle = parent.defaultWidgetStyle; 248 this.defaultTooltipStyle = parent.defaultTooltipStyle; 249 this.itemIndexSeparator = parent.itemIndexSeparator; 250 this.separateColumns = parent.separateColumns; 253 this.targetType = parent.targetType; 254 this.defaultMapName = parent.defaultMapName; 255 this.targetWindowExdr = parent.targetWindowExdr; 256 this.hideHeader = parent.hideHeader; 257 258 259 Iterator fieldListIter = parent.fieldList.iterator(); 261 while (fieldListIter.hasNext()) { 262 ModelFormField parentChildField = (ModelFormField)fieldListIter.next(); 263 ModelFormField childField = new ModelFormField(this); 264 childField.mergeOverrideModelFormField(parentChildField); 265 this.fieldList.add(childField); 266 this.fieldMap.put(childField.getName(), childField); 267 } 268 } 269 } 270 271 this.name = formElement.getAttribute("name"); 272 if (this.type == null || formElement.hasAttribute("type")) 273 this.type = formElement.getAttribute("type"); 274 if (this.target == null || formElement.hasAttribute("target")) 275 setTarget( formElement.getAttribute("target") ); 276 if (this.targetWindowExdr == null || formElement.hasAttribute("target-window")) 277 setTargetWindow(formElement.getAttribute("target-window")); 278 if (this.title == null || formElement.hasAttribute("title")) 279 this.title = formElement.getAttribute("title"); 280 if (this.tooltip == null || formElement.hasAttribute("tooltip")) 281 this.tooltip = formElement.getAttribute("tooltip"); 282 if (this.listName == null || formElement.hasAttribute("list-name")) 283 this.listName = formElement.getAttribute("list-name"); 284 if (this.listEntryName == null || formElement.hasAttribute("list-entry-name")) 285 this.listEntryName = formElement.getAttribute("list-entry-name"); 286 if (this.listIteratorName == null || formElement.hasAttribute("list-iterator-name")) 287 this.listIteratorName = formElement.getAttribute("list-iterator-name"); 288 if (this.defaultMapName == null || formElement.hasAttribute("default-map-name")) 289 this.setDefaultMapName(formElement.getAttribute("default-map-name")); 290 if (this.defaultServiceName == null || formElement.hasAttribute("default-service-name")) 291 this.defaultServiceName = formElement.getAttribute("default-service-name"); 292 if (this.defaultEntityName == null || formElement.hasAttribute("default-entity-name")) 293 this.defaultEntityName = formElement.getAttribute("default-entity-name"); 294 295 if (this.formTitleAreaStyle == null || formElement.hasAttribute("form-title-area-style")) 296 this.formTitleAreaStyle = formElement.getAttribute("form-title-area-style"); 297 if (this.formWidgetAreaStyle == null || formElement.hasAttribute("form-widget-area-style")) 298 this.formWidgetAreaStyle = formElement.getAttribute("form-widget-area-style"); 299 300 if (this.defaultTitleAreaStyle == null || formElement.hasAttribute("default-title-area-style")) 301 this.defaultTitleAreaStyle = formElement.getAttribute("default-title-area-style"); 302 if (this.defaultWidgetAreaStyle == null || formElement.hasAttribute("default-widget-area-style")) 303 this.defaultWidgetAreaStyle = formElement.getAttribute("default-widget-area-style"); 304 if (this.oddRowStyle == null || formElement.hasAttribute("odd-row-style")) 305 this.oddRowStyle = formElement.getAttribute("odd-row-style"); 306 if (this.evenRowStyle == null || formElement.hasAttribute("even-row-style")) 307 this.evenRowStyle = formElement.getAttribute("even-row-style"); 308 if (this.defaultTableStyle == null || formElement.hasAttribute("default-table-style")) 309 this.defaultTableStyle = formElement.getAttribute("default-table-style"); 310 if (this.headerRowStyle == null || formElement.hasAttribute("header-row-style")) 311 this.headerRowStyle = formElement.getAttribute("header-row-style"); 312 if (this.defaultTitleStyle == null || formElement.hasAttribute("header-row-style")) 313 this.defaultTitleStyle = formElement.getAttribute("default-title-style"); 314 if (this.defaultWidgetStyle == null || formElement.hasAttribute("default-widget-style")) 315 this.defaultWidgetStyle = formElement.getAttribute("default-widget-style"); 316 if (this.defaultTooltipStyle == null || formElement.hasAttribute("default-tooltip-style")) 317 this.defaultTooltipStyle = formElement.getAttribute("default-tooltip-style"); 318 if (this.itemIndexSeparator == null || formElement.hasAttribute("item-index-separator")) 319 this.itemIndexSeparator = formElement.getAttribute("item-index-separator"); 320 if (this.targetType == null || formElement.hasAttribute("target-type")) 321 this.targetType = formElement.getAttribute("target-type"); 322 if (this.defaultRequiredFieldStyle == null || formElement.hasAttribute("default-required-field-style")) 323 this.defaultRequiredFieldStyle = formElement.getAttribute("default-required-field-style"); 324 325 if (this.paginateTarget == null || formElement.hasAttribute("paginate-target")) 327 setPaginateTarget(formElement.getAttribute("paginate-target")); 328 if (this.paginateTargetAnchor == null || formElement.hasAttribute("paginate-target-anchor")) 329 this.paginateTargetAnchor = formElement.getAttribute("paginate-target-anchor"); 330 if (this.paginateIndexField == null || formElement.hasAttribute("paginate-index-field")) 331 setPaginateIndexField(formElement.getAttribute("paginate-index-field")); 332 if (this.paginateSizeField == null || formElement.hasAttribute("paginate-size-field")) 333 setPaginateSizeField(formElement.getAttribute("paginate-size-field")); 334 if (this.paginatePreviousLabel == null || formElement.hasAttribute("paginate-previous-label")) 335 this.paginatePreviousLabel = new FlexibleStringExpander(formElement.getAttribute("paginate-previous-label")); 336 if (this.paginateNextLabel == null || formElement.hasAttribute("paginate-next-label")) 337 this.paginateNextLabel = new FlexibleStringExpander(formElement.getAttribute("paginate-next-label")); 338 if (this.paginatePreviousStyle == null || formElement.hasAttribute("paginate-previous-style")) 339 setPaginatePreviousStyle(formElement.getAttribute("paginate-previous-style")); 340 if (this.paginateNextStyle == null || formElement.hasAttribute("paginate-next-style")) 341 setPaginateNextStyle(formElement.getAttribute("paginate-next-style")); 342 343 this.paginate = "true".equals(formElement.getAttribute("paginate")); 344 this.cssStyling = "true".equals(formElement.getAttribute("css-styling")); 345 this.skipStart = "true".equals(formElement.getAttribute("skip-start")); 346 this.skipEnd = "true".equals(formElement.getAttribute("skip-end")); 347 this.hideHeader = "true".equals(formElement.getAttribute("hide-header")); 348 if (formElement.hasAttribute("separate-columns")) { 349 String sepColumns = formElement.getAttribute("separate-columns"); 350 if (sepColumns != null && sepColumns.equalsIgnoreCase("true")) 351 separateColumns = true; 352 } 353 if (formElement.hasAttribute("use-row-submit")) { 354 String rowSubmit = formElement.getAttribute("use-row-submit"); 355 if (rowSubmit != null && rowSubmit.equalsIgnoreCase("true")) 356 useRowSubmit = true; 357 } 358 if (formElement.hasAttribute("view-size")) 359 setViewSize(formElement.getAttribute("view-size")); 360 if (this.rowCountExdr == null || formElement.hasAttribute("row-count")) 361 this.rowCountExdr = new FlexibleStringExpander(formElement.getAttribute("row-count")); 362 363 List altTargetElements = UtilXml.childElementList(formElement, "alt-target"); 365 Iterator altTargetElementIter = altTargetElements.iterator(); 366 while (altTargetElementIter.hasNext()) { 367 Element altTargetElement = (Element ) altTargetElementIter.next(); 368 AltTarget altTarget = new AltTarget(altTargetElement); 369 this.addAltTarget(altTarget); 370 } 371 372 List autoFieldsServiceElements = UtilXml.childElementList(formElement, "auto-fields-service"); 374 Iterator autoFieldsServiceElementIter = autoFieldsServiceElements.iterator(); 375 while (autoFieldsServiceElementIter.hasNext()) { 376 Element autoFieldsServiceElement = (Element ) autoFieldsServiceElementIter.next(); 377 AutoFieldsService autoFieldsService = new AutoFieldsService(autoFieldsServiceElement); 378 this.addAutoFieldsFromService(autoFieldsService, dispatcher); 379 } 380 381 List autoFieldsEntityElements = UtilXml.childElementList(formElement, "auto-fields-entity"); 383 Iterator autoFieldsEntityElementIter = autoFieldsEntityElements.iterator(); 384 while (autoFieldsEntityElementIter.hasNext()) { 385 Element autoFieldsEntityElement = (Element ) autoFieldsEntityElementIter.next(); 386 AutoFieldsEntity autoFieldsEntity = new AutoFieldsEntity(autoFieldsEntityElement); 387 this.addAutoFieldsFromEntity(autoFieldsEntity, delegator); 388 } 389 390 List fieldElements = UtilXml.childElementList(formElement, "field"); 392 Iterator fieldElementIter = fieldElements.iterator(); 393 String thisType = this.getType(); 394 while (fieldElementIter.hasNext()) { 395 Element fieldElement = (Element ) fieldElementIter.next(); 396 ModelFormField modelFormField = new ModelFormField(fieldElement, this); 397 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 398 if (thisType.equals("multi") && fieldInfo instanceof ModelFormField.SubmitField) { 399 multiSubmitField = modelFormField; 400 } else { 401 modelFormField = this.addUpdateField(modelFormField); 402 } 403 } 405 406 defaultFieldGroup = new FieldGroup(null, this); 408 Element sortOrderElement = UtilXml.firstChildElement(formElement, "sort-order"); 410 if (sortOrderElement != null) { 411 FieldGroup lastFieldGroup = new FieldGroup(null, this); 412 this.fieldGroupList.add(lastFieldGroup); 413 List sortFieldElements = UtilXml.childElementList(sortOrderElement); 415 Iterator sortFieldElementIter = sortFieldElements.iterator(); 416 while (sortFieldElementIter.hasNext()) { 417 Element sortFieldElement = (Element ) sortFieldElementIter.next(); 418 String tagName = sortFieldElement.getTagName(); 419 if (tagName.equals("sort-field")) { 420 String fieldName = sortFieldElement.getAttribute("name"); 421 this.sortOrderFields.add(fieldName ); 422 this.fieldGroupMap.put(fieldName, lastFieldGroup); 423 } else if (tagName.equals("banner")) { 424 Banner thisBanner = new Banner(sortFieldElement, this); 425 this.fieldGroupList.add(thisBanner); 426 427 lastFieldGroup = new FieldGroup(null, this); 428 this.fieldGroupList.add(lastFieldGroup); 429 } else if (tagName.equals("field-group")) { 430 FieldGroup thisFieldGroup = new FieldGroup(sortFieldElement, this); 431 this.fieldGroupList.add(thisFieldGroup); 432 433 lastFieldGroup = new FieldGroup(null, this); 434 this.fieldGroupList.add(lastFieldGroup); 435 } 436 } 437 } 438 439 if (sortOrderFields.size() > 0) { 441 List sortedFields = new ArrayList (this.fieldList.size()); 442 Iterator sortOrderFieldIter = this.sortOrderFields.iterator(); 443 while (sortOrderFieldIter.hasNext()) { 444 String fieldName = (String ) sortOrderFieldIter.next(); 445 if (UtilValidate.isEmpty(fieldName)) { 446 continue; 447 } 448 449 Iterator fieldIter = this.fieldList.iterator(); 451 while (fieldIter.hasNext()) { 452 ModelFormField modelFormField = (ModelFormField) fieldIter.next(); 453 if (fieldName.equals(modelFormField.getName())) { 454 fieldIter.remove(); 456 sortedFields.add(modelFormField); 457 } 458 } 459 } 460 sortedFields.addAll(this.fieldList); 462 this.fieldList = sortedFields; 464 } 465 466 Element actionsElement = UtilXml.firstChildElement(formElement, "actions"); 468 if (actionsElement != null) { 469 this.actions = ModelFormAction.readSubActions(this, actionsElement); 470 } 471 472 Element rowActionsElement = UtilXml.firstChildElement(formElement, "row-actions"); 474 if (rowActionsElement != null) { 475 this.rowActions = ModelFormAction.readSubActions(this, rowActionsElement); 476 } 477 } 478 479 484 public ModelFormField addUpdateField(ModelFormField modelFormField) { 485 if (!modelFormField.isUseWhenEmpty()) { 486 boolean inserted = false; 489 for (int i = 0; i < this.fieldList.size(); i++) { 490 ModelFormField curField = (ModelFormField) this.fieldList.get(i); 491 if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { 492 this.fieldList.add(i, modelFormField); 493 inserted = true; 494 break; 495 } 496 } 497 if (!inserted) { 498 this.fieldList.add(modelFormField); 499 } 500 return modelFormField; 501 } else { 502 503 ModelFormField existingField = (ModelFormField) this.fieldMap.get(modelFormField.getName()); 505 if (existingField != null) { 506 existingField.mergeOverrideModelFormField(modelFormField); 508 return existingField; 509 } else { 510 this.fieldList.add(modelFormField); 512 this.fieldMap.put(modelFormField.getName(), modelFormField); 513 return modelFormField; 514 } 515 } 516 } 517 518 public void addAltTarget(AltTarget altTarget) { 519 altTargets.add(altTarget); 520 } 521 522 public void addAutoFieldsFromService(AutoFieldsService autoFieldsService, LocalDispatcher dispatcher) { 523 autoFieldsServices.add(autoFieldsService); 524 525 ModelService modelService = null; 527 try { 528 modelService = dispatcher.getDispatchContext().getModelService(autoFieldsService.serviceName); 529 } catch (GenericServiceException e) { 530 String errmsg = "Error finding Service with name " + autoFieldsService.serviceName + " for auto-fields-service in a form widget"; 531 Debug.logError(e, errmsg, module); 532 throw new IllegalArgumentException (errmsg); 533 } 534 535 List modelParams = modelService.getInModelParamList(); 536 Iterator modelParamIter = modelParams.iterator(); 537 while (modelParamIter.hasNext()) { 538 ModelParam modelParam = (ModelParam) modelParamIter.next(); 539 if ("userLogin".equals(modelParam.name) || "locale".equals(modelParam.name)) { 541 continue; 542 } 543 if (modelParam.formDisplay) { 544 if (UtilValidate.isNotEmpty(modelParam.entityName) && UtilValidate.isNotEmpty(modelParam.fieldName)) { 545 ModelEntity modelEntity = delegator.getModelEntity(modelParam.entityName); 546 if (modelEntity != null) { 547 ModelField modelField = modelEntity.getField(modelParam.fieldName); 548 if (modelField != null) { 549 ModelFormField modelFormField = this.addFieldFromEntityField(modelEntity, modelField, autoFieldsService.defaultFieldType); 551 if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) { 552 modelFormField.setMapName(autoFieldsService.mapName); 553 } 554 555 continue; 557 } 558 } 559 } 560 561 ModelFormField modelFormField = this.addFieldFromServiceParam(modelService, modelParam, autoFieldsService.defaultFieldType); 562 if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) { 563 modelFormField.setMapName(autoFieldsService.mapName); 564 } 565 } 566 } 567 } 568 569 public ModelFormField addFieldFromServiceParam(ModelService modelService, ModelParam modelParam, String defaultFieldType) { 570 ModelFormField newFormField = new ModelFormField(this); 572 newFormField.setName(modelParam.name); 573 newFormField.setServiceName(modelService.name); 574 newFormField.setAttributeName(modelParam.name); 575 newFormField.setTitle(modelParam.formLabel); 576 newFormField.induceFieldInfoFromServiceParam(modelService, modelParam, defaultFieldType); 577 return this.addUpdateField(newFormField); 578 } 579 580 public void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity, GenericDelegator delegator) { 581 autoFieldsEntities.add(autoFieldsEntity); 582 ModelEntity modelEntity = delegator.getModelEntity(autoFieldsEntity.entityName); 584 if (modelEntity == null) { 585 throw new IllegalArgumentException ("Error finding Entity with name " + autoFieldsEntity.entityName + " for auto-fields-entity in a form widget"); 586 } 587 588 Iterator modelFieldIter = modelEntity.getFieldsIterator(); 589 while (modelFieldIter.hasNext()) { 590 ModelField modelField = (ModelField) modelFieldIter.next(); 591 if (modelField.getIsAutoCreatedInternal()) { 592 continue; 594 } 595 ModelFormField modelFormField = this.addFieldFromEntityField(modelEntity, modelField, autoFieldsEntity.defaultFieldType); 596 if (UtilValidate.isNotEmpty(autoFieldsEntity.mapName)) { 597 modelFormField.setMapName(autoFieldsEntity.mapName); 598 } 599 } 600 } 601 602 public ModelFormField addFieldFromEntityField(ModelEntity modelEntity, ModelField modelField, String defaultFieldType) { 603 ModelFormField newFormField = new ModelFormField(this); 605 newFormField.setName(modelField.getName()); 606 newFormField.setEntityName(modelEntity.getEntityName()); 607 newFormField.setFieldName(modelField.getName()); 608 newFormField.induceFieldInfoFromEntityField(modelEntity, modelField, defaultFieldType); 609 return this.addUpdateField(newFormField); 610 } 611 612 627 public void renderFormString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer) { 628 ModelFormAction.runSubActions(this.actions, context); 629 630 if ("list".equals(this.type) || "multi".equals(this.type)) { 632 context.put("useRequestParameters", Boolean.FALSE); 633 } 634 635 int positions = 1; 637 Iterator fieldIter = this.fieldList.iterator(); 638 while (fieldIter.hasNext()) { 639 ModelFormField modelFormField = (ModelFormField) fieldIter.next(); 640 int curPos = modelFormField.getPosition(); 641 if (curPos > positions) { 642 positions = curPos; 643 } 644 ModelFormField.FieldInfo currentFieldInfo = modelFormField.getFieldInfo(); 645 if (currentFieldInfo != null) { 646 ModelFormField fieldInfoFormField = currentFieldInfo.getModelFormField(); 647 if (fieldInfoFormField != null) { 648 fieldInfoFormField.setModelForm(this); 649 } 650 } else { 651 throw new IllegalArgumentException ("Error rendering form, a field has no FieldInfo, ie no sub-element for the type of field for field named: " + modelFormField.getName()); 652 } 653 } 654 655 if ("single".equals(this.type)) { 656 this.renderSingleFormString(buffer, context, formStringRenderer, positions); 657 } else if ("list".equals(this.type)) { 658 this.renderListFormString(buffer, context, formStringRenderer, positions); 659 } else if ("multi".equals(this.type)) { 660 this.renderMultiFormString(buffer, context, formStringRenderer, positions); 661 } else if ("upload".equals(this.type)) { 662 this.renderSingleFormString(buffer, context, formStringRenderer, positions); 663 } else { 664 throw new IllegalArgumentException ("The type " + this.getType() + " is not supported for form with name " + this.getName()); 665 } 666 } 667 668 public void renderSingleFormString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer, int positions) { 669 List tempFieldList = FastList.newInstance(); 670 tempFieldList.addAll(this.fieldList); 671 672 for (int j = 0; j < tempFieldList.size(); j++) { 674 ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j); 675 if (!modelFormField.isUseWhenEmpty()) { 676 boolean shouldUse1 = modelFormField.shouldUse(context); 677 for (int i = j+1; i < tempFieldList.size(); i++) { 678 ModelFormField curField = (ModelFormField) tempFieldList.get(i); 679 if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { 680 boolean shouldUse2 = curField.shouldUse(context); 681 if (shouldUse1 == shouldUse2) { 682 tempFieldList.remove(i--); 683 } 684 } else { 685 continue; 686 } 687 } 688 } 689 } 690 691 Set alreadyRendered = new TreeSet (); 692 FieldGroup lastFieldGroup = null; 693 if (!skipStart) formStringRenderer.renderFormOpen(buffer, context, this); 695 696 this.renderHiddenIgnoredFields(buffer, context, formStringRenderer, alreadyRendered); 698 699 703 Iterator fieldIter = tempFieldList.iterator(); 705 ModelFormField lastFormField = null; 706 ModelFormField currentFormField = null; 707 ModelFormField nextFormField = null; 708 if (fieldIter.hasNext()) { 709 currentFormField = (ModelFormField) fieldIter.next(); 710 } 711 if (fieldIter.hasNext()) { 712 nextFormField = (ModelFormField) fieldIter.next(); 713 } 714 715 FieldGroup currentFieldGroup = null; 716 String currentFieldGroupName = null; 717 String lastFieldGroupName = null; 718 if (currentFormField != null) { 719 currentFieldGroup = (FieldGroup)fieldGroupMap.get(currentFormField.getFieldName()); 720 if (currentFieldGroup == null) { 721 currentFieldGroup = defaultFieldGroup; 722 } 723 if (currentFieldGroup != null) { 724 currentFieldGroupName = currentFieldGroup.getId(); 725 } 726 } 727 728 729 boolean isFirstPass = true; 730 boolean haveRenderedOpenFieldRow = false; 731 while (currentFormField != null) { 732 if (isFirstPass) { 735 isFirstPass = false; 736 List inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); 737 Iterator iter = inbetweenList.iterator(); 738 while (iter.hasNext()) { 739 Object obj = iter.next(); 740 if (obj instanceof ModelForm.Banner) { 741 ((ModelForm.Banner) obj).renderString(buffer, context, formStringRenderer); 742 } else { 743 formStringRenderer.renderFieldGroupOpen(buffer, context, (FieldGroup) obj); 745 formStringRenderer.renderFieldGroupClose(buffer, context, (FieldGroup) obj); 746 } 747 } 748 if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) { 749 currentFieldGroup.renderStartString(buffer, context, formStringRenderer); 750 lastFieldGroup = currentFieldGroup; 751 } 752 } else { 753 if (fieldIter.hasNext()) { 754 lastFormField = currentFormField; 756 currentFormField = nextFormField; 757 nextFormField = (ModelFormField) fieldIter.next(); 758 } else if (nextFormField != null) { 759 lastFormField = currentFormField; 761 currentFormField = nextFormField; 762 nextFormField = null; 763 } else { 764 lastFormField = currentFormField; 766 currentFormField = null; 767 break; 769 } 770 currentFieldGroup = null; 771 if (currentFormField != null) { 772 currentFieldGroup = (FieldGroup) fieldGroupMap.get(currentFormField.getName()); 773 } 774 if (currentFieldGroup == null) { 775 currentFieldGroup = defaultFieldGroup; 776 } 777 currentFieldGroupName = currentFieldGroup.getId(); 778 779 if (lastFieldGroup != null ) { 780 lastFieldGroupName = lastFieldGroup.getId(); 781 if (!lastFieldGroupName.equals(currentFieldGroupName)) { 782 lastFieldGroup.renderEndString(buffer, context, formStringRenderer); 783 784 List inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); 785 Iterator iter = inbetweenList.iterator(); 786 while (iter.hasNext()) { 787 Object obj = iter.next(); 788 if (obj instanceof ModelForm.Banner) { 789 ((ModelForm.Banner) obj).renderString(buffer, context, formStringRenderer); 790 } else { 791 formStringRenderer.renderFieldGroupOpen(buffer, context, (FieldGroup) obj); 793 formStringRenderer.renderFieldGroupClose(buffer, context, (FieldGroup) obj); 794 } 795 } 796 } 797 } 798 799 if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) { 800 currentFieldGroup.renderStartString(buffer, context, formStringRenderer); 801 lastFieldGroup = currentFieldGroup; 802 } 803 } 804 805 ModelFormField.FieldInfo fieldInfo = currentFormField.getFieldInfo(); 806 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 807 continue; 808 } 809 if (alreadyRendered.contains(currentFormField.getName())) { 810 continue; 811 } 812 if (!currentFormField.shouldUse(context)) { 814 continue; 815 } 816 alreadyRendered.add(currentFormField.getName()); 817 818 boolean stayingOnRow = false; 819 if (lastFormField != null) { 820 if (lastFormField.getPosition() >= currentFormField.getPosition()) { 821 stayingOnRow = false; 823 } else { 824 stayingOnRow = true; 826 } 827 } 828 829 int positionSpan = 1; 830 Integer nextPositionInRow = null; 831 if (nextFormField != null) { 832 if (nextFormField.getPosition() > currentFormField.getPosition()) { 833 positionSpan = nextFormField.getPosition() - currentFormField.getPosition() - 1; 834 nextPositionInRow = new Integer (nextFormField.getPosition()); 835 } else { 836 positionSpan = positions - currentFormField.getPosition(); 837 if (!stayingOnRow && nextFormField.getPosition() > 1) { 838 } 842 } 843 } 844 845 if (stayingOnRow) { 846 } else { 849 if (haveRenderedOpenFieldRow) { 850 formStringRenderer.renderFormatFieldRowClose(buffer, context, this); 852 haveRenderedOpenFieldRow = false; 853 } 854 855 formStringRenderer.renderFormatFieldRowOpen(buffer, context, this); 857 haveRenderedOpenFieldRow = true; 858 } 859 860 formStringRenderer.renderFormatFieldRowTitleCellOpen(buffer, context, currentFormField); 862 863 if (fieldInfo.getFieldType() != ModelFormField.FieldInfo.SUBMIT && fieldInfo.getFieldType() != ModelFormField.FieldInfo.RESET) { 865 formStringRenderer.renderFieldTitle(buffer, context, currentFormField); 866 } else { 867 formStringRenderer.renderFormatEmptySpace(buffer, context, this); 868 } 869 870 formStringRenderer.renderFormatFieldRowTitleCellClose(buffer, context, currentFormField); 872 873 formStringRenderer.renderFormatFieldRowSpacerCell(buffer, context, currentFormField); 875 876 formStringRenderer.renderFormatFieldRowWidgetCellOpen(buffer, context, currentFormField, positions, positionSpan, nextPositionInRow); 878 879 currentFormField.renderFieldString(buffer, context, formStringRenderer); 881 882 formStringRenderer.renderFormatFieldRowWidgetCellClose(buffer, context, currentFormField, positions, positionSpan, nextPositionInRow); 884 885 } 886 formStringRenderer.renderFormatFieldRowClose(buffer, context, this); 888 889 if (lastFieldGroup != null) { 890 lastFieldGroup.renderEndString(buffer, context, formStringRenderer); 891 } 892 896 if (!skipEnd) formStringRenderer.renderFormClose(buffer, context, this); 898 } 899 900 public void renderListFormString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer, int positions) { 901 903 formStringRenderer.renderFormatListWrapperOpen(buffer, context, this); 905 906 if (!getHideHeader()) { 908 this.renderHeaderRow(buffer, context, formStringRenderer); 909 } 910 911 this.renderItemRows(buffer, context, formStringRenderer, true); 913 914 formStringRenderer.renderFormatListWrapperClose(buffer, context, this); 916 } 917 918 public void renderMultiFormString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer, int positions) { 919 formStringRenderer.renderFormOpen(buffer, context, this); 920 921 formStringRenderer.renderFormatListWrapperOpen(buffer, context, this); 923 924 this.renderHeaderRow(buffer, context, formStringRenderer); 926 927 this.renderItemRows(buffer, context, formStringRenderer, false); 929 930 formStringRenderer.renderFormatListWrapperClose(buffer, context, this); 931 932 formStringRenderer.renderMultiFormClose(buffer, context, this); 933 } 934 935 public void renderHeaderRow(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer) { 936 formStringRenderer.renderFormatHeaderRowOpen(buffer, context, this); 937 938 940 946 Iterator displayHyperlinkFieldIter = this.fieldList.iterator(); 948 ModelFormField previousModelFormField = null; 949 while (displayHyperlinkFieldIter.hasNext()) { 950 ModelFormField modelFormField = (ModelFormField) displayHyperlinkFieldIter.next(); 951 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 952 953 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 955 continue; 956 } 957 958 if (previousModelFormField != null && previousModelFormField.getTitle(context).equals(modelFormField.getTitle(context)) && 960 !(previousModelFormField.isUseWhenEmpty() && modelFormField.isUseWhenEmpty())) { 961 continue; 962 } 963 964 if (fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY_ENTITY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.HYPERLINK) { 965 break; 967 } 968 969 971 formStringRenderer.renderFormatHeaderRowCellOpen(buffer, context, this, modelFormField); 972 973 formStringRenderer.renderFieldTitle(buffer, context, modelFormField); 974 975 formStringRenderer.renderFormatHeaderRowCellClose(buffer, context, this, modelFormField); 976 977 previousModelFormField = modelFormField; 979 } 980 981 List headerFormFields = new LinkedList (); 982 Iterator formFieldIter = this.fieldList.iterator(); 983 while (formFieldIter.hasNext()) { 985 ModelFormField modelFormField = (ModelFormField) formFieldIter.next(); 986 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 987 988 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 990 continue; 991 } 992 993 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.DISPLAY || fieldInfo.getFieldType() == ModelFormField.FieldInfo.DISPLAY_ENTITY || fieldInfo.getFieldType() == ModelFormField.FieldInfo.HYPERLINK) { 995 continue; 996 } 997 998 if (!modelFormField.shouldUse(context)) { 999 continue; 1000 } 1001 1002 headerFormFields.add(modelFormField); 1003 } 1004 1005 formStringRenderer.renderFormatHeaderRowFormCellOpen(buffer, context, this); 1007 1008 Iterator headerFormFieldIter = headerFormFields.iterator(); 1009 while (headerFormFieldIter.hasNext()) { 1010 ModelFormField modelFormField = (ModelFormField) headerFormFieldIter.next(); 1011 1013 if (separateColumns || modelFormField.getSeparateColumn()) 1014 formStringRenderer.renderFormatItemRowCellOpen(buffer, context, this, modelFormField); 1015 1016 formStringRenderer.renderFieldTitle(buffer, context, modelFormField); 1018 1019 if (separateColumns || modelFormField.getSeparateColumn()) 1020 formStringRenderer.renderFormatItemRowCellClose(buffer, context, this, modelFormField); 1021 1022 if (headerFormFieldIter.hasNext()) { 1023 if (!separateColumns && !modelFormField.getSeparateColumn()) 1025 formStringRenderer.renderFormatHeaderRowFormCellTitleSeparator(buffer, context, this, modelFormField, false); 1026 } 1027 } 1028 1029 formStringRenderer.renderFormatHeaderRowFormCellClose(buffer, context, this); 1030 1031 while (displayHyperlinkFieldIter.hasNext()) { 1033 ModelFormField modelFormField = (ModelFormField) displayHyperlinkFieldIter.next(); 1034 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 1035 1036 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 1038 continue; 1039 } 1040 1041 if (fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY_ENTITY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.HYPERLINK) { 1043 continue; 1044 } 1045 1046 if (!modelFormField.shouldUse(context)) { 1047 continue; 1048 } 1049 1050 formStringRenderer.renderFormatHeaderRowCellOpen(buffer, context, this, modelFormField); 1051 1052 formStringRenderer.renderFieldTitle(buffer, context, modelFormField); 1053 1054 formStringRenderer.renderFormatHeaderRowCellClose(buffer, context, this, modelFormField); 1055 } 1056 1057 formStringRenderer.renderFormatHeaderRowClose(buffer, context, this); 1058 } 1059 1060 protected Object safeNext(Iterator iterator) { 1061 try { 1062 return iterator.next(); 1063 } catch (NoSuchElementException e) { 1064 return null; 1065 } 1066 } 1067 1068 public void renderItemRows(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer, boolean formPerItem) { 1069 this.rowCount = 0; 1070 String lookupName = getListIteratorName(); 1071 if (UtilValidate.isEmpty(lookupName)) { 1072 lookupName = getListName(); 1073 } 1074 if (UtilValidate.isEmpty(lookupName)) { 1075 Debug.logError("No value for list or iterator name found.", module); 1076 return; 1077 } 1078 Object obj = context.get(lookupName); 1079 if (obj == null) { 1080 Debug.logError("No object for list or iterator name:" + lookupName + " found.", module); 1081 return; 1082 } 1083 Iterator iter = null; 1085 List items = null; 1086 if (obj instanceof Iterator ) { 1087 iter = (Iterator ) obj; 1088 setPaginate(true); 1089 } else if (obj instanceof List ) { 1090 items = (List ) obj; 1091 iter = items.listIterator(); 1092 setPaginate(true); 1093 } 1094 1096 getListLimits(context, obj); 1097 1098 if (iter != null) { 1099 int itemIndex = -1; 1101 Object item = null; 1102 while ((item = this.safeNext(iter)) != null) { 1103 itemIndex++; 1104 if (itemIndex >= highIndex) { 1105 break; 1106 } 1107 1108 if (itemIndex < lowIndex) { 1110 continue; 1111 } 1112 1113 Map localContext = new HashMap (context); 1114 if (UtilValidate.isNotEmpty(this.getListEntryName())) { 1115 localContext.put(this.getListEntryName(), item); 1116 } else { 1117 Map itemMap = (Map ) item; 1118 localContext.putAll(itemMap); 1119 } 1120 1121 ModelFormAction.runSubActions(this.rowActions, localContext); 1122 1123 localContext.put("itemIndex", new Integer (itemIndex - lowIndex)); 1124 this.resetBshInterpreter(localContext); 1125 this.rowCount++; 1126 1127 if (Debug.verboseOn()) Debug.logVerbose("In form got another row, context is: " + localContext, module); 1128 1129 for (int j = 0; j < this.fieldList.size(); j++) { 1131 ModelFormField modelFormField = (ModelFormField) this.fieldList.get(j); 1132 if (!modelFormField.isUseWhenEmpty()) { 1133 boolean shouldUse1 = modelFormField.shouldUse(localContext); 1134 for (int i = j+1; i < this.fieldList.size(); i++) { 1135 ModelFormField curField = (ModelFormField) this.fieldList.get(i); 1136 if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { 1137 boolean shouldUse2 = curField.shouldUse(localContext); 1138 if (shouldUse1 == shouldUse2) { 1139 this.fieldList.remove(i--); 1140 } 1141 } else { 1142 continue; 1143 } 1144 } 1145 } 1146 } 1147 1148 formStringRenderer.renderFormatItemRowOpen(buffer, localContext, this); 1150 1151 Iterator innerDisplayHyperlinkFieldIter = this.fieldList.iterator(); 1153 while (innerDisplayHyperlinkFieldIter.hasNext()) { 1154 ModelFormField modelFormField = (ModelFormField) innerDisplayHyperlinkFieldIter.next(); 1155 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 1156 1157 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 1159 continue; 1160 } 1161 1162 if (fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY_ENTITY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.HYPERLINK) { 1163 break; 1165 } 1166 1167 if (!modelFormField.shouldUse(localContext)) { 1168 continue; 1169 } 1170 1171 formStringRenderer.renderFormatItemRowCellOpen(buffer, localContext, this, modelFormField); 1172 1173 modelFormField.renderFieldString(buffer, localContext, formStringRenderer); 1174 1175 formStringRenderer.renderFormatItemRowCellClose(buffer, localContext, this, modelFormField); 1176 } 1177 1178 formStringRenderer.renderFormatItemRowFormCellOpen(buffer, localContext, this); 1180 1181 if (formPerItem) { 1182 formStringRenderer.renderFormOpen(buffer, localContext, this); 1183 } 1184 1185 this.renderHiddenIgnoredFields(buffer, localContext, formStringRenderer, null); 1187 1188 Iterator innerFormFieldIter = this.fieldList.iterator(); 1189 while (innerFormFieldIter.hasNext()) { 1190 ModelFormField modelFormField = (ModelFormField) innerFormFieldIter.next(); 1191 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 1192 1193 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 1195 continue; 1196 } 1197 1198 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.DISPLAY || fieldInfo.getFieldType() == ModelFormField.FieldInfo.DISPLAY_ENTITY || fieldInfo.getFieldType() == ModelFormField.FieldInfo.HYPERLINK) { 1200 continue; 1201 } 1202 1203 if (!modelFormField.shouldUse(localContext)) { 1204 continue; 1205 } 1206 1207 if (separateColumns || modelFormField.getSeparateColumn()) 1208 formStringRenderer.renderFormatItemRowCellOpen(buffer, localContext, this, modelFormField); 1209 modelFormField.renderFieldString(buffer, localContext, formStringRenderer); 1211 1212 if (separateColumns || modelFormField.getSeparateColumn()) 1213 formStringRenderer.renderFormatItemRowCellClose(buffer, localContext, this, modelFormField); 1214 } 1215 1216 if (formPerItem) { 1217 formStringRenderer.renderFormClose(buffer, localContext, this); 1218 } 1219 1220 formStringRenderer.renderFormatItemRowFormCellClose(buffer, localContext, this); 1221 1222 while (innerDisplayHyperlinkFieldIter.hasNext()) { 1224 ModelFormField modelFormField = (ModelFormField) innerDisplayHyperlinkFieldIter.next(); 1225 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 1226 1227 if (fieldInfo.getFieldType() == ModelFormField.FieldInfo.HIDDEN || fieldInfo.getFieldType() == ModelFormField.FieldInfo.IGNORED) { 1229 continue; 1230 } 1231 1232 if (fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.DISPLAY_ENTITY && fieldInfo.getFieldType() != ModelFormField.FieldInfo.HYPERLINK) { 1234 continue; 1235 } 1236 1237 if (!modelFormField.shouldUse(localContext)) { 1238 continue; 1239 } 1240 1241 formStringRenderer.renderFormatItemRowCellOpen(buffer, localContext, this, modelFormField); 1242 1243 modelFormField.renderFieldString(buffer, localContext, formStringRenderer); 1244 1245 formStringRenderer.renderFormatItemRowCellClose(buffer, localContext, this, modelFormField); 1246 } 1247 1248 formStringRenderer.renderFormatItemRowClose(buffer, localContext, this); 1250 } 1251 if ((itemIndex + 1) < highIndex) { 1252 setHighIndex(itemIndex + 1); 1253 } 1254 setActualPageSize(highIndex - lowIndex); 1255 1256 if (iter instanceof EntityListIterator) { 1257 try { 1258 ((EntityListIterator) iter).close(); 1259 } catch(GenericEntityException e) { 1260 Debug.logError(e, "Error closing list form render EntityListIterator: " + e.toString(), module); 1261 } 1262 } 1263 } 1268 } 1269 1270 public void renderHiddenIgnoredFields(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer, Set alreadyRendered) { 1271 Iterator fieldIter = this.fieldList.iterator(); 1272 while (fieldIter.hasNext()) { 1273 ModelFormField modelFormField = (ModelFormField) fieldIter.next(); 1274 ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); 1275 1276 switch (fieldInfo.getFieldType()) { 1278 case ModelFormField.FieldInfo.HIDDEN : 1279 case ModelFormField.FieldInfo.IGNORED : 1280 if (modelFormField.shouldUse(context)) { 1281 modelFormField.renderFieldString(buffer, context, formStringRenderer); 1282 if (alreadyRendered != null) 1283 alreadyRendered.add(modelFormField.getName()); 1284 } 1285 break; 1286 1287 case ModelFormField.FieldInfo.DISPLAY : 1288 case ModelFormField.FieldInfo.DISPLAY_ENTITY : 1289 ModelFormField.DisplayField displayField = (ModelFormField.DisplayField) fieldInfo; 1290 if (displayField.getAlsoHidden() && modelFormField.shouldUse(context)) { 1291 formStringRenderer.renderHiddenField(buffer, context, modelFormField, modelFormField.getEntry(context)); 1292 } 1294 break; 1295 1296 case ModelFormField.FieldInfo.HYPERLINK : 1297 ModelFormField.HyperlinkField hyperlinkField = (ModelFormField.HyperlinkField) fieldInfo; 1298 if (hyperlinkField.getAlsoHidden() && modelFormField.shouldUse(context)) { 1299 formStringRenderer.renderHiddenField(buffer, context, modelFormField, modelFormField.getEntry(context)); 1300 } 1302 break; 1303 } 1304 } 1305 } 1306 1307 public LocalDispatcher getDispacher() { 1308 return this.dispatcher; 1309 } 1310 1311 public GenericDelegator getDelegator() { 1312 return this.delegator; 1313 } 1314 1315 1316 public LocalDispatcher getDispatcher(Map context) { 1317 LocalDispatcher dispatcher = (LocalDispatcher) context.get("dispatcher"); 1318 return dispatcher; 1319 } 1320 1321 public GenericDelegator getDelegator(Map context) { 1322 GenericDelegator delegator = (GenericDelegator) context.get("delegator"); 1323 return delegator; 1324 } 1325 1326 public String getTargetType() { 1327 return this.targetType; 1328 } 1329 1330 1333 public String getDefaultEntityName() { 1334 return this.defaultEntityName; 1335 } 1336 1337 1340 public String getDefaultMapName() { 1341 return this.defaultMapName.getOriginalName(); 1342 } 1343 1344 public Map getDefaultMap(Map context) { 1345 return (Map ) this.defaultMapName.get(context); 1346 } 1347 1348 1351 public String getDefaultRequiredFieldStyle() { 1352 return this.defaultRequiredFieldStyle; 1353 } 1354 1355 1356 1359 public String getDefaultServiceName() { 1360 return this.defaultServiceName; 1361 } 1362 1363 1366 public String getFormTitleAreaStyle() { 1367 return this.formTitleAreaStyle; 1368 } 1369 1370 1373 public String getFormWidgetAreaStyle() { 1374 return this.formWidgetAreaStyle; 1375 } 1376 1377 1380 public String getDefaultTitleAreaStyle() { 1381 return this.defaultTitleAreaStyle; 1382 } 1383 1384 1387 public String getDefaultWidgetAreaStyle() { 1388 return this.defaultWidgetAreaStyle; 1389 } 1390 1391 1394 public String getOddRowStyle() { 1395 return this.oddRowStyle; 1396 } 1397 1398 1401 public String getEvenRowStyle() { 1402 return this.evenRowStyle; 1403 } 1404 1405 1408 public String getDefaultTableStyle() { 1409 return this.defaultTableStyle; 1410 } 1411 1412 1415 public String getHeaderRowStyle() { 1416 return this.headerRowStyle; 1417 } 1418 1419 1422 public String getDefaultTitleStyle() { 1423 return this.defaultTitleStyle; 1424 } 1425 1426 1429 public String getDefaultWidgetStyle() { 1430 return this.defaultWidgetStyle; 1431 } 1432 1433 1436 public String getDefaultTooltipStyle() { 1437 return this.defaultTooltipStyle; 1438 } 1439 1440 1443 public String getItemIndexSeparator() { 1444 if (UtilValidate.isNotEmpty(this.itemIndexSeparator)) { 1445 return this.itemIndexSeparator; 1446 } else { 1447 return "_o_"; 1448 } 1449 } 1450 1451 1454 public String getListEntryName() { 1455 return this.listEntryName; 1456 } 1457 1458 1461 public String getListName() { 1462 String lstNm = this.listName; 1463 if (UtilValidate.isEmpty(lstNm)) { 1464 lstNm = DEFAULT_FORM_RESULT_LIST_NAME; 1465 } 1466 return lstNm; 1467 } 1468 1469 1472 public void setListIteratorName(String string) { 1473 this.listIteratorName = string; 1474 } 1475 1476 1479 public String getListIteratorName() { 1480 String listIterName = this.listIteratorName; 1481 if (UtilValidate.isEmpty(listIterName)) { 1482 listIterName = this.getListName(); 1483 } 1484 return listIterName; 1485 } 1486 1487 public ListIterator getListIterator(Map context) { 1488 String name = getListIteratorName(); 1489 ListIterator iter = null; 1490 if (UtilValidate.isNotEmpty(name)) { 1491 iter = (ListIterator )context.get(name); 1492 } 1493 return iter; 1494 } 1495 1498 public String getName() { 1499 return this.name; 1500 } 1501 1502 public String getCurrentFormName(Map context) { 1503 Integer itemIndex = (Integer ) context.get("itemIndex"); 1504 String formName = (String ) context.get("formName"); 1505 if (UtilValidate.isEmpty(formName)) { 1506 formName = this.getName(); 1507 } 1508 1509 if (itemIndex != null && "list".equals(this.getType())) { 1510 return formName + this.getItemIndexSeparator() + itemIndex.intValue(); 1511 } else { 1512 return formName; 1513 } 1514 } 1515 1516 1519 public String getTarget(Map context) { 1520 try { 1521 Interpreter bsh = this.getBshInterpreter(context); 1523 Iterator altTargetIter = this.altTargets.iterator(); 1524 while (altTargetIter.hasNext()) { 1525 AltTarget altTarget = (AltTarget) altTargetIter.next(); 1526 Object retVal = bsh.eval(altTarget.useWhen); 1527 boolean condTrue = false; 1528 if (retVal instanceof Boolean ) { 1530 Boolean boolVal = (Boolean ) retVal; 1531 condTrue = boolVal.booleanValue(); 1532 } else { 1533 throw new IllegalArgumentException ( 1534 "Return value from target condition eval was not a Boolean: " + retVal.getClass().getName() + " [" + retVal + "] of form " + this.name); 1535 } 1536 1537 if (condTrue) { 1538 return altTarget.target; 1539 } 1540 } 1541 } catch (EvalError e) { 1542 String errmsg = "Error evaluating BeanShell target conditions on form " + this.name; 1543 Debug.logError(e, errmsg, module); 1544 throw new IllegalArgumentException (errmsg); 1545 } 1546 1547 return target.expandString(context); 1548 } 1549 1550 1553 public String getTitle() { 1554 return this.title; 1555 } 1556 1557 1560 public String getTooltip() { 1561 return this.tooltip; 1562 } 1563 1564 1567 public String getType() { 1568 return this.type; 1569 } 1570 1571 public void resetBshInterpreter(Map context) { 1572 context.remove("bshInterpreter"); 1573 } 1574 1575 public Interpreter getBshInterpreter(Map context) throws EvalError { 1576 Interpreter bsh = (Interpreter) context.get("bshInterpreter"); 1577 if (bsh == null) { 1578 bsh = BshUtil.makeInterpreter(context); 1579 context.put("bshInterpreter", bsh); 1580 } 1581 return bsh; 1582 } 1583 1584 1587 public void setDefaultEntityName(String string) { 1588 this.defaultEntityName = string; 1589 } 1590 1591 1594 public void setDefaultMapName(String string) { 1595 this.defaultMapName = new FlexibleMapAccessor(string); 1596 } 1597 1598 1601 public void setDefaultServiceName(String string) { 1602 this.defaultServiceName = string; 1603 } 1604 1605 1608 public void setFormTitleAreaStyle(String string) { 1609 this.formTitleAreaStyle = string; 1610 } 1611 1612 1615 public void setFormWidgetAreaStyle(String string) { 1616 this.formWidgetAreaStyle = string; 1617 } 1618 1619 1622 public void setDefaultTitleAreaStyle(String string) { 1623 this.defaultTitleAreaStyle = string; 1624 } 1625 1626 1629 public void setDefaultWidgetAreaStyle(String string) { 1630 this.defaultWidgetAreaStyle = string; 1631 } 1632 1633 1636 public void setOddRowStyle(String string) { 1637 this.oddRowStyle = string; 1638 } 1639 1640 1643 public void setEvenRowStyle(String string) { 1644 this.evenRowStyle = string; 1645 } 1646 1647 1650 public void setDefaultTableStyle(String string) { 1651 this.defaultTableStyle = string; 1652 } 1653 1654 1657 public void setHeaderRowStyle(String string) { 1658 this.headerRowStyle = string; 1659 } 1660 1661 1664 public void setDefaultTitleStyle(String string) { 1665 this.defaultTitleStyle = string; 1666 } 1667 1668 1671 public void setDefaultWidgetStyle(String string) { 1672 this.defaultWidgetStyle = string; 1673 } 1674 1675 1678 public void setDefaultTooltipStyle(String string) { 1679 this.defaultTooltipStyle = string; 1680 } 1681 1682 1685 public void setItemIndexSeparator(String string) { 1686 this.itemIndexSeparator = string; 1687 } 1688 1689 1692 public void setListEntryName(String string) { 1693 this.listEntryName = string; 1694 } 1695 1696 1699 public void setListName(String string) { 1700 this.listName = string; 1701 } 1702 1703 1706 public void setName(String string) { 1707 this.name = string; 1708 } 1709 1710 1713 public void setTarget(String string) { 1714 this.target = new FlexibleStringExpander(string); 1715 } 1716 1717 1720 public void setTitle(String string) { 1721 this.title = string; 1722 } 1723 1724 1727 public void setTooltip(String string) { 1728 this.tooltip = string; 1729 } 1730 1731 1734 public void setType(String string) { 1735 this.type = string; 1736 } 1737 1738 1741 public String getPaginateTarget(Map context) { 1742 String targ = this.paginateTarget.expandString(context); 1743 if (UtilValidate.isEmpty(targ)) { 1744 targ = getTarget(context); 1745 } 1746 1747 return targ; 1748 } 1749 1750 public String getPaginateTargetAnchor() { 1751 return this.paginateTargetAnchor; 1752 } 1753 1754 public String getPaginateIndexField(Map context) { 1755 String field = this.paginateIndexField.expandString(context); 1756 if (UtilValidate.isEmpty(field)) { 1757 field = DEFAULT_PAG_INDEX_FIELD; 1758 } 1759 return field; 1760 } 1761 1762 public String getPaginateSizeField(Map context) { 1763 String field = this.paginateSizeField.expandString(context); 1764 if (UtilValidate.isEmpty(field)) { 1765 field = DEFAULT_PAG_SIZE_FIELD; 1766 } 1767 return field; 1768 } 1769 1770 public String getPaginatePreviousLabel(Map context) { 1771 String field = this.paginatePreviousLabel.expandString(context); 1772 if (UtilValidate.isEmpty(field)) { 1773 field = DEFAULT_PAG_PREV_LABEL; 1774 } 1775 return field; 1776 } 1777 1778 public String getPaginateNextLabel(Map context) { 1779 String field = this.paginateNextLabel.expandString(context); 1780 if (UtilValidate.isEmpty(field)) { 1781 field = DEFAULT_PAG_NEXT_LABEL; 1782 } 1783 return field; 1784 } 1785 1786 public String getPaginatePreviousStyle() { 1787 return this.paginatePreviousStyle; 1788 } 1789 1790 public String getPaginateNextStyle() { 1791 return this.paginateNextStyle; 1792 } 1793 1794 public String getTargetWindow(Map context) { 1795 return this.targetWindowExdr.expandString(context); 1796 } 1797 1798 public void setTargetWindow( String val ) { 1799 this.targetWindowExdr = new FlexibleStringExpander(val); 1800 } 1801 1802 1805 public boolean getSeparateColumns() { 1806 return this.separateColumns; 1807 } 1808 1809 public boolean getPaginate() { 1810 return this.paginate; 1811 } 1812 1813 public boolean getCssStyling() { 1814 return this.cssStyling; 1815 } 1816 1817 public boolean getSkipStart() { 1818 return this.skipStart; 1819 } 1820 1821 public boolean getSkipEnd() { 1822 return this.skipEnd; 1823 } 1824 1825 public void setSkipStart(boolean val) { 1826 this.skipStart = val; 1827 } 1828 1829 public void setSkipEnd(boolean val) { 1830 this.skipEnd = val; 1831 } 1832 1833 public boolean getHideHeader() { 1834 return this.hideHeader; 1835 } 1836 1837 public void setPaginate(boolean val) { 1838 paginate = val; 1839 } 1840 1841 1844 public void setPaginateTarget(String string) { 1845 this.paginateTarget = new FlexibleStringExpander(string); 1846 } 1847 1848 public void setPaginateIndexField(String string) { 1849 this.paginateIndexField = new FlexibleStringExpander(string); 1850 } 1851 1852 public void setPaginateSizeField(String string) { 1853 this.paginateSizeField = new FlexibleStringExpander(string); 1854 } 1855 1856 public void setPaginatePreviousStyle(String string) { 1857 this.paginatePreviousStyle = (UtilValidate.isEmpty(string) ? DEFAULT_PAG_PREV_STYLE : string); 1858 } 1859 1860 public void setPaginateNextStyle(String string) { 1861 this.paginateNextStyle = (UtilValidate.isEmpty(string) ? DEFAULT_PAG_NEXT_STYLE : string); 1862 } 1863 1864 public void setViewIndex(int val) { 1865 viewIndex = val; 1866 } 1867 1868 public void setViewSize(int val) { 1869 viewSize = val; 1870 } 1871 1872 public void setViewSize(String val) { 1873 try { 1874 Integer sz = new Integer (val); 1875 viewSize = sz.intValue(); 1876 } catch(NumberFormatException e) { 1877 viewSize = DEFAULT_PAGE_SIZE; 1878 } 1879 } 1880 1881 public void setListSize(int val) { 1882 listSize = val; 1883 } 1884 1885 public void setLowIndex(int val) { 1886 lowIndex = val; 1887 } 1888 1889 public void setHighIndex(int val) { 1890 highIndex = val; 1891 } 1892 public void setActualPageSize(int val) { 1893 actualPageSize = val; 1894 } 1895 1896 public int getViewIndex() { 1897 return viewIndex; 1898 } 1899 1900 public int getViewSize() { 1901 return viewSize; 1902 } 1903 1904 public int getListSize() { 1905 return listSize; 1906 } 1907 1908 public int getLowIndex() { 1909 return lowIndex; 1910 } 1911 1912 public int getHighIndex() { 1913 return highIndex; 1914 } 1915 1916 public int getActualPageSize() { 1917 return actualPageSize; 1918 } 1919 1920 public void getListLimits(Map context, Object obj) { 1921 ListIterator iter = null; 1922 List items = null; 1923 if (obj instanceof ListIterator ) { 1924 iter = (ListIterator )obj; 1925 try { 1926 ((EntityListIterator)iter).last(); 1927 listSize = ((EntityListIterator)iter).currentIndex(); 1928 ((EntityListIterator)iter).beforeFirst(); 1929 } catch (GenericEntityException e2) { 1930 Debug.logError(e2, "Error getting list size", module); 1931 listSize = 0; 1932 } 1933 } else if (obj instanceof List ) { 1934 items = (List )obj; 1935 listSize = items.size(); 1936 } 1937 1938 if (paginate) { 1939 viewIndex = 0; 1940 viewSize = DEFAULT_PAGE_SIZE; 1941 try { 1942 Object value = context.get(getPaginateIndexField(context)); 1943 if (value instanceof Integer ) 1944 viewIndex = ((Integer ) value).intValue(); 1945 else if (value instanceof String ) 1946 viewIndex = Integer.parseInt((String ) value); 1947 } catch (Exception e) { 1948 } 1949 try { 1950 Object value = context.get(getPaginateSizeField(context)); 1951 if (value instanceof Integer ) 1952 viewSize = ((Integer ) value).intValue(); 1953 else if (value instanceof String ) 1954 viewSize = Integer.parseInt((String ) value); 1955 } catch (Exception e) { 1956 } 1957 lowIndex = viewIndex * viewSize; 1958 highIndex = (viewIndex + 1) * viewSize; 1959 } else { 1960 viewIndex = 0; 1961 viewSize = DEFAULT_PAGE_SIZE; 1962 lowIndex = 0; 1963 highIndex = DEFAULT_PAGE_SIZE; 1964 } 1965 } 1966 1967 public String getPassedRowCount(Map context) { 1968 return rowCountExdr.expandString(context); 1969 } 1970 1971 public int getRowCount() { 1972 return this.rowCount; 1973 } 1974 1975 public boolean getUseRowSubmit() { 1976 return this.useRowSubmit; 1977 } 1978 1979 public ModelFormField getMultiSubmitField() { 1980 return this.multiSubmitField; 1981 } 1982 1983 public List getInbetweenList(FieldGroup startFieldGroup, FieldGroup endFieldGroup) { 1984 ArrayList inbetweenList = new ArrayList (); 1985 boolean firstFound = false; 1986 String startFieldGroupId = null; 1987 String endFieldGroupId = null; 1988 if (endFieldGroup != null) { 1989 endFieldGroupId = endFieldGroup.getId(); 1990 } 1991 if (startFieldGroup == null) { 1992 firstFound = true; 1993 } else { 1994 startFieldGroupId = startFieldGroup.getId(); 1995 } 1996 Iterator iter = fieldGroupList.iterator(); 1997 while (iter.hasNext()) { 1998 Object obj = iter.next(); 1999 if (obj instanceof ModelForm.Banner) { 2000 if (firstFound) inbetweenList.add(obj); 2001 } else { 2002 FieldGroup fieldGroup = (FieldGroup)obj; 2003 String fieldGroupId = fieldGroup.getId(); 2004 if (!firstFound) { 2005 if (fieldGroupId.equals(startFieldGroupId)) { 2006 firstFound = true; 2007 continue; 2008 } 2009 } 2010 if (firstFound) { 2011 if (fieldGroupId.equals(endFieldGroupId)) { 2012 break; 2013 } else { 2014 inbetweenList.add(fieldGroup); 2015 } 2016 } 2017 } 2018 } 2019 return inbetweenList; 2020 } 2021 2022 public static class AltTarget { 2023 public String useWhen; 2024 public String target; 2025 public AltTarget(Element altTargetElement) { 2026 this.useWhen = altTargetElement.getAttribute("use-when"); 2027 this.target = altTargetElement.getAttribute("target"); 2028 } 2029 } 2030 2031 public static class AutoFieldsService { 2032 public String serviceName; 2033 public String mapName; 2034 public String defaultFieldType; 2035 public AutoFieldsService(Element element) { 2036 this.serviceName = element.getAttribute("service-name"); 2037 this.mapName = element.getAttribute("map-name"); 2038 this.defaultFieldType = element.getAttribute("default-field-type"); 2039 } 2040 } 2041 2042 public static class AutoFieldsEntity { 2043 public String entityName; 2044 public String mapName; 2045 public String defaultFieldType; 2046 public AutoFieldsEntity(Element element) { 2047 this.entityName = element.getAttribute("entity-name"); 2048 this.mapName = element.getAttribute("map-name"); 2049 this.defaultFieldType = element.getAttribute("default-field-type"); 2050 } 2051 } 2052 2053 public static class FieldGroup { 2054 public String id; 2055 public String style; 2056 protected ModelForm modelForm; 2057 protected static int baseSeqNo = 0; 2058 protected static String baseId = "_G"; 2059 public FieldGroup(Element sortOrderElement, ModelForm modelForm) { 2060 2061 this.modelForm = modelForm; 2062 if (sortOrderElement != null) { 2063 this.id = sortOrderElement.getAttribute("id"); 2064 if (UtilValidate.isEmpty(this.id)) { 2065 String lastGroupId = baseId + baseSeqNo++ + "_"; 2066 this.setId(lastGroupId); 2067 } 2068 this.style = sortOrderElement.getAttribute("style"); 2069 List sortFieldElements = UtilXml.childElementList(sortOrderElement, "sort-field"); 2070 Iterator sortFieldElementIter = sortFieldElements.iterator(); 2071 while (sortFieldElementIter.hasNext()) { 2072 Element sortFieldElement = (Element ) sortFieldElementIter.next(); 2073 modelForm.sortOrderFields.add(sortFieldElement.getAttribute("name")); 2074 modelForm.fieldGroupMap.put(sortFieldElement.getAttribute("name"), this); 2075 } 2076 } else { 2077 String lastGroupId = baseId + baseSeqNo++ + "_"; 2078 this.setId(lastGroupId); 2079 } 2080 } 2081 2082 public String getId() { 2083 return this.id; 2084 } 2085 2086 public void setId( String id) { 2087 this.id = id; 2088 } 2089 2090 public String getStyle() { 2091 return this.style; 2092 } 2093 2094 public void renderStartString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer) { 2095 formStringRenderer.renderFieldGroupOpen(buffer, context, this); 2096 formStringRenderer.renderFormatSingleWrapperOpen(buffer, context, modelForm); 2097 } 2098 2099 public void renderEndString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer) { 2100 formStringRenderer.renderFormatSingleWrapperClose(buffer, context, modelForm); 2101 formStringRenderer.renderFieldGroupClose(buffer, context, this); 2102 } 2103 } 2104 2105 public static class Banner { 2106 protected ModelForm modelForm; 2107 public FlexibleStringExpander style; 2108 public FlexibleStringExpander text; 2109 public FlexibleStringExpander textStyle; 2110 public FlexibleStringExpander leftText; 2111 public FlexibleStringExpander leftTextStyle; 2112 public FlexibleStringExpander rightText; 2113 public FlexibleStringExpander rightTextStyle; 2114 2115 public Banner(Element sortOrderElement, ModelForm modelForm) { 2116 2117 this.modelForm = modelForm; 2118 this.style = new FlexibleStringExpander(sortOrderElement.getAttribute("style")); 2119 this.text = new FlexibleStringExpander(sortOrderElement.getAttribute("text")); 2120 this.textStyle = new FlexibleStringExpander(sortOrderElement.getAttribute("text-style")); 2121 this.leftText = new FlexibleStringExpander(sortOrderElement.getAttribute("left-text")); 2122 this.leftTextStyle = new FlexibleStringExpander(sortOrderElement.getAttribute("left-text-style")); 2123 this.rightText = new FlexibleStringExpander(sortOrderElement.getAttribute("right-text")); 2124 this.rightTextStyle = new FlexibleStringExpander(sortOrderElement.getAttribute("right-text-style")); 2125 } 2126 2127 2128 public String getStyle(Map context) { return this.style.expandString(context); } 2129 public String getText(Map context) { return this.text.expandString(context); } 2130 public String getTextStyle(Map context) { return this.textStyle.expandString(context); } 2131 public String getLeftText(Map context) { return this.leftText.expandString(context); } 2132 public String getLeftTextStyle(Map context) { return this.leftTextStyle.expandString(context); } 2133 public String getRightText(Map context) { return this.rightText.expandString(context); } 2134 public String getRightTextStyle(Map context) { return this.rightTextStyle.expandString(context); } 2135 2136 public void renderString(StringBuffer buffer, Map context, FormStringRenderer formStringRenderer) { 2137 formStringRenderer.renderBanner(buffer, context, this); 2138 } 2139 } 2140 2141} 2142 | Popular Tags |