/
MacroFormRenderer.java
2401 lines (2292 loc) · 109 KB
/
MacroFormRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.apache.ofbiz.widget.renderer.macro;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilCodec;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilHttp;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.string.FlexibleStringExpander;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.security.CsrfUtil;
import org.apache.ofbiz.webapp.control.RequestHandler;
import org.apache.ofbiz.webapp.taglib.ContentUrlTag;
import org.apache.ofbiz.widget.WidgetWorker;
import org.apache.ofbiz.widget.content.StaticContentUrlProvider;
import org.apache.ofbiz.widget.model.CommonWidgetModels;
import org.apache.ofbiz.widget.model.FieldInfo;
import org.apache.ofbiz.widget.model.ModelForm;
import org.apache.ofbiz.widget.model.ModelFormField;
import org.apache.ofbiz.widget.model.ModelFormField.CheckField;
import org.apache.ofbiz.widget.model.ModelFormField.ContainerField;
import org.apache.ofbiz.widget.model.ModelFormField.DateFindField;
import org.apache.ofbiz.widget.model.ModelFormField.DateTimeField;
import org.apache.ofbiz.widget.model.ModelFormField.DisplayEntityField;
import org.apache.ofbiz.widget.model.ModelFormField.DisplayField;
import org.apache.ofbiz.widget.model.ModelFormField.DropDownField;
import org.apache.ofbiz.widget.model.ModelFormField.FileField;
import org.apache.ofbiz.widget.model.ModelFormField.HiddenField;
import org.apache.ofbiz.widget.model.ModelFormField.HyperlinkField;
import org.apache.ofbiz.widget.model.ModelFormField.IgnoredField;
import org.apache.ofbiz.widget.model.ModelFormField.ImageField;
import org.apache.ofbiz.widget.model.ModelFormField.LookupField;
import org.apache.ofbiz.widget.model.ModelFormField.MenuField;
import org.apache.ofbiz.widget.model.ModelFormField.PasswordField;
import org.apache.ofbiz.widget.model.ModelFormField.RadioField;
import org.apache.ofbiz.widget.model.ModelFormField.RangeFindField;
import org.apache.ofbiz.widget.model.ModelFormField.ResetField;
import org.apache.ofbiz.widget.model.ModelFormField.SubmitField;
import org.apache.ofbiz.widget.model.ModelFormField.TextField;
import org.apache.ofbiz.widget.model.ModelFormField.TextFindField;
import org.apache.ofbiz.widget.model.ModelFormField.TextareaField;
import org.apache.ofbiz.widget.model.ModelScreenWidget;
import org.apache.ofbiz.widget.model.ModelSingleForm;
import org.apache.ofbiz.widget.model.ModelTheme;
import org.apache.ofbiz.widget.model.ModelWidget;
import org.apache.ofbiz.widget.model.ThemeFactory;
import org.apache.ofbiz.widget.renderer.FormRenderer;
import org.apache.ofbiz.widget.renderer.FormStringRenderer;
import org.apache.ofbiz.widget.renderer.Paginator;
import org.apache.ofbiz.widget.renderer.UtilHelpText;
import org.apache.ofbiz.widget.renderer.VisualTheme;
import org.apache.ofbiz.widget.renderer.macro.renderable.RenderableFtl;
import org.apache.ofbiz.widget.renderer.macro.renderable.RenderableFtlMacroCall;
import org.jsoup.nodes.Element;
/**
* Widget Library - Form Renderer implementation based on Freemarker macros
*/
public final class MacroFormRenderer implements FormStringRenderer {
private static final String MODULE = MacroFormRenderer.class.getName();
private final UtilCodec.SimpleEncoder internalEncoder;
private final RequestHandler rh;
private final HttpServletRequest request;
private final HttpServletResponse response;
private final RenderableFtlFormElementsBuilder renderableFtlFormElementsBuilder;
private final boolean javaScriptEnabled;
private final VisualTheme visualTheme;
private final FtlWriter ftlWriter;
private boolean renderPagination = true;
private boolean widgetCommentsEnabled = false;
public MacroFormRenderer(String macroLibraryPath, HttpServletRequest request, HttpServletResponse response)
throws IOException {
this(macroLibraryPath, request, response, null, null);
}
public MacroFormRenderer(String macroLibraryPath, HttpServletRequest request, HttpServletResponse response,
FtlWriter ftlWriter, RenderableFtlFormElementsBuilder renderableFtlFormElementsBuilder)
throws IOException {
this.request = request;
this.response = response;
this.visualTheme = ThemeFactory.resolveVisualTheme(request);
this.rh = RequestHandler.from(request);
this.javaScriptEnabled = UtilHttp.isJavaScriptEnabled(request);
internalEncoder = UtilCodec.getEncoder("string");
this.ftlWriter = ftlWriter != null ? ftlWriter : new FtlWriter(macroLibraryPath, this.visualTheme);
final StaticContentUrlProvider staticContentUrlProvider = new StaticContentUrlProvider(request);
this.renderableFtlFormElementsBuilder = renderableFtlFormElementsBuilder != null
? renderableFtlFormElementsBuilder
: new RenderableFtlFormElementsBuilder(this.visualTheme, rh, request, response, staticContentUrlProvider);
}
private static String encodeDoubleQuotes(String htmlString) {
return htmlString.replace("\"", "\\\"");
}
public boolean getRenderPagination() {
return this.renderPagination;
}
public void setRenderPagination(boolean renderPagination) {
this.renderPagination = renderPagination;
}
public void writeFtlElement(final Appendable writer, final RenderableFtl renderableFtl) {
ftlWriter.processFtl(writer, renderableFtl);
}
private void executeMacro(Appendable writer, String macro) {
ftlWriter.processFtlString(writer, null, macro);
}
/**
* Make locale available before executing macro
* @param writer
* @param locale
* @param macro
*/
private void executeMacro(Appendable writer, Locale locale, String macro) {
ftlWriter.processFtlString(writer, locale, macro);
}
private String encode(String value, ModelFormField modelFormField, Map<String, Object> context) {
if (UtilValidate.isEmpty(value)) {
return value;
}
UtilCodec.SimpleEncoder encoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder");
if (modelFormField.getEncodeOutput() && encoder != null) {
value = encoder.encode(value);
} else {
value = internalEncoder.encode(value);
}
return value;
}
public void renderLabel(Appendable writer, Map<String, Object> context, ModelScreenWidget.Label label) {
final RenderableFtl renderableFtl = renderableFtlFormElementsBuilder.label(context, label);
writeFtlElement(writer, renderableFtl);
}
@Override
public void renderDisplayField(Appendable writer, Map<String, Object> context, DisplayField displayField)
throws IOException {
if (displayField instanceof DisplayEntityField
&& ((DisplayEntityField) displayField).needConvertAsHyperlink(context)) {
// When we have a subHyperlink on a display entity, display all as a hyperlink
renderHyperlinkField(writer, context, ((DisplayEntityField) displayField).asHyperlink(context));
} else {
writeFtlElement(writer,
renderableFtlFormElementsBuilder.displayField(context, displayField, this.javaScriptEnabled));
if (displayField instanceof DisplayEntityField) {
writeFtlElement(writer,
renderableFtlFormElementsBuilder.makeHyperlinkString(((DisplayEntityField) displayField).getSubHyperlink(),
context));
}
}
final ModelFormField modelFormField = displayField.getModelFormField();
appendTooltip(writer, context, modelFormField);
}
@Override
public void renderHyperlinkField(Appendable writer, Map<String, Object> context, HyperlinkField hyperlinkField)
throws IOException {
this.request.setAttribute("image", hyperlinkField.getImageLocation(context));
ModelFormField modelFormField = hyperlinkField.getModelFormField();
String encodedAlternate = encode(hyperlinkField.getAlternate(context), modelFormField, context);
String encodedImageTitle = encode(hyperlinkField.getImageTitle(context), modelFormField, context);
this.request.setAttribute("alternate", encodedAlternate);
this.request.setAttribute("imageTitle", encodedImageTitle);
this.request.setAttribute("descriptionSize", hyperlinkField.getSize());
this.request.setAttribute("id", UtilValidate.isNotEmpty(hyperlinkField.getId(context)) ? hyperlinkField.getId(context)
: modelFormField.getCurrentContainerId(context));
this.request.setAttribute("title", hyperlinkField.getTitle());
this.request.setAttribute("width", hyperlinkField.getWidth());
this.request.setAttribute("height", hyperlinkField.getHeight());
this.request.setAttribute("text", hyperlinkField.getText(context));
makeHyperlinkByType(writer, hyperlinkField.getLinkType(), modelFormField.getWidgetStyle(), hyperlinkField.getUrlMode(),
hyperlinkField.getTarget(context), hyperlinkField.getParameterMap(context, modelFormField.getEntityName(),
modelFormField.getServiceName()), hyperlinkField.getDescription(context), hyperlinkField.getTargetWindow(context),
hyperlinkField.getConfirmation(context), modelFormField, this.request, this.response, context);
this.appendTooltip(writer, context, modelFormField);
this.request.removeAttribute("image");
this.request.removeAttribute("descriptionSize");
}
@Override
public void renderMenuField(Appendable writer, Map<String, Object> context, MenuField menuField) throws IOException {
menuField.renderFieldString(writer, context, null);
}
@Override
public void renderTextField(Appendable writer, Map<String, Object> context, TextField textField) {
writeFtlElement(writer, renderableFtlFormElementsBuilder.textField(context, textField, javaScriptEnabled));
writeFtlElement(writer, renderableFtlFormElementsBuilder.makeHyperlinkString(textField.getSubHyperlink(), context));
final ModelFormField modelFormField = textField.getModelFormField();
this.addAsterisks(writer, context, modelFormField);
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderTextareaField(Appendable writer, Map<String, Object> context, TextareaField textareaField) {
writeFtlElement(writer, renderableFtlFormElementsBuilder.textArea(context, textareaField));
final ModelFormField modelFormField = textareaField.getModelFormField();
this.addAsterisks(writer, context, modelFormField);
this.appendTooltip(writer, context, modelFormField);
}
private boolean shouldApplyRequiredField(ModelFormField modelFormField) {
return ("single".equals(modelFormField.getModelForm().getType())
|| "upload".equals(modelFormField.getModelForm().getType()))
&& modelFormField.getRequiredField();
}
@Override
public void renderDateTimeField(Appendable writer, Map<String, Object> context, DateTimeField dateTimeField) {
writeFtlElement(writer, renderableFtlFormElementsBuilder.dateTime(context, dateTimeField));
final ModelFormField modelFormField = dateTimeField.getModelFormField();
this.addAsterisks(writer, context, modelFormField);
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderDropDownField(Appendable writer, Map<String, Object> context, DropDownField dropDownField) throws IOException {
writeFtlElement(writer, renderableFtlFormElementsBuilder.dropDownField(context, dropDownField, this.javaScriptEnabled));
ModelFormField.SubHyperlink subHyperlink = dropDownField.getSubHyperlink();
if (subHyperlink != null && subHyperlink.shouldUse(context)) {
makeHyperlinkString(writer, subHyperlink, context);
}
ModelFormField modelFormField = dropDownField.getModelFormField();
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderCheckField(Appendable writer, Map<String, Object> context, CheckField checkField) throws IOException {
ModelFormField modelFormField = checkField.getModelFormField();
String currentValue = modelFormField.getEntry(context);
String conditionGroup = modelFormField.getConditionGroup();
Boolean allChecked = checkField.isAllChecked(context);
boolean disabled = modelFormField.getDisabled(context);
String id = modelFormField.getCurrentContainerId(context);
String className = "";
String alert = "false";
String name = modelFormField.getParameterName(context);
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
StringBuilder items = new StringBuilder();
String checkBox = checkField.getModelFormField().getAttributeName();
List<String> checkedByDefault = new ArrayList<String>();
if (context.containsKey(checkBox) && !context.get(checkBox).getClass().equals(String.class)) {
checkedByDefault = context.containsKey(checkBox) ? StringUtil.toList(context.get(checkBox).toString())
: List.of();
}
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
String tabindex = modelFormField.getTabindex();
List<String> currentValueList = null;
if (UtilValidate.isNotEmpty(currentValue)) {
if (currentValue.startsWith("[")) {
currentValueList = StringUtil.toList(currentValue);
} else {
currentValueList = UtilMisc.toList(currentValue);
}
}
List<ModelFormField.OptionValue> allOptionValues = checkField.getAllOptionValues(context, WidgetWorker.getDelegator(context));
items.append("[");
for (ModelFormField.OptionValue optionValue : allOptionValues) {
boolean checked;
if (UtilValidate.isNotEmpty(currentValueList)) {
checked = currentValueList.contains(optionValue.getKey());
} else {
if (UtilValidate.isNotEmpty(checkedByDefault)) {
checked = checkedByDefault.contains(optionValue.getKey());
} else checked = allChecked;
}
String data = String.format(
"{'value':'%s', 'description':'%s', 'checked':'%s'}",
optionValue.getKey(),
encode(optionValue.getDescription(), modelFormField, context),
checked);
items.append(data);
items.append(",");
}
if (items.length() > 0) {
items.deleteCharAt(items.length() - 1);
}
items.append("]");
StringWriter sr = new StringWriter();
sr.append("<@renderCheckField ");
sr.append("items=");
sr.append(items.toString());
sr.append(" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" id=\"");
sr.append(id);
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" currentValue=\"");
sr.append(currentValue);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" disabled=");
sr.append(Boolean.toString(disabled));
sr.append(" />");
executeMacro(writer, sr.toString());
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderRadioField(Appendable writer, Map<String, Object> context, RadioField radioField) throws IOException {
ModelFormField modelFormField = radioField.getModelFormField();
List<ModelFormField.OptionValue> allOptionValues = radioField.getAllOptionValues(context, WidgetWorker.getDelegator(context));
String currentValue = modelFormField.getEntry(context);
String conditionGroup = modelFormField.getConditionGroup();
boolean disabled = modelFormField.getDisabled(context);
String className = "";
String alert = "false";
String name = modelFormField.getParameterName(context);
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
StringBuilder items = new StringBuilder();
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
String noCurrentSelectedKey = radioField.getNoCurrentSelectedKey(context);
String tabindex = modelFormField.getTabindex();
items.append("[");
for (ModelFormField.OptionValue optionValue : allOptionValues) {
if (items.length() > 1) {
items.append(",");
}
items.append("{'key':'");
items.append(optionValue.getKey());
items.append("', 'description':'" + encode(optionValue.getDescription(), modelFormField, context));
items.append("'}");
}
items.append("]");
StringWriter sr = new StringWriter();
sr.append("<@renderRadioField ");
sr.append("items=");
sr.append(items.toString());
sr.append(" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" currentValue=\"");
sr.append(currentValue);
sr.append("\" noCurrentSelectedKey=\"");
sr.append(noCurrentSelectedKey);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" disabled=");
sr.append(Boolean.toString(disabled));
sr.append(" />");
executeMacro(writer, sr.toString());
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderSubmitField(Appendable writer, Map<String, Object> context, SubmitField submitField) throws IOException {
ModelFormField modelFormField = submitField.getModelFormField();
ModelForm modelForm = modelFormField.getModelForm();
String id = modelFormField.getCurrentContainerId(context);
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
String title = modelFormField.getTitle(context);
String name = modelFormField.getParameterName(context);
String buttonType = submitField.getButtonType();
String formName = FormRenderer.getCurrentFormName(modelForm, context);
String imgSrc = submitField.getImageLocation(context);
String confirmation = submitField.getConfirmation(context);
String className = "";
String alert = "false";
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
String formId = FormRenderer.getCurrentContainerId(modelForm, context);
List<ModelForm.UpdateArea> updateAreas = modelForm.getOnSubmitUpdateAreas();
// This is here for backwards compatibility. Use on-event-update-area
// elements instead.
String backgroundSubmitRefreshTarget = submitField.getBackgroundSubmitRefreshTarget(context);
ModelForm.UpdateArea jwtCallback = ModelForm.UpdateArea.fromJwtToken(context);
if (UtilValidate.isNotEmpty(backgroundSubmitRefreshTarget)) {
if (updateAreas == null) {
updateAreas = new LinkedList<>();
}
updateAreas.add(new ModelForm.UpdateArea("submit", formId, backgroundSubmitRefreshTarget));
}
// In context a callback is present and no other update area to call after the submit, so trigger it.
if (UtilValidate.isEmpty(updateAreas) && jwtCallback != null) {
updateAreas = UtilMisc.toList(jwtCallback);
}
boolean ajaxEnabled = UtilValidate.isNotEmpty(updateAreas) && this.javaScriptEnabled;
String ajaxUrl = "";
if (ajaxEnabled) {
Map<String, Object> extraParams = CommonWidgetModels.propagateCallbackInParameterMap(context,
submitField.getPropagateCallback(), jwtCallback);
ajaxUrl = MacroCommonRenderer.createAjaxParamsFromUpdateAreas(updateAreas, extraParams, modelForm, "", context);
}
String tabindex = modelFormField.getTabindex();
boolean disabled = modelFormField.getDisabled(context);
StringWriter sr = new StringWriter();
sr.append("<@renderSubmitField ");
sr.append("buttonType=\"");
sr.append(buttonType);
sr.append("\" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" title=\"");
sr.append(encode(title, modelFormField, context));
sr.append("\" name=\"");
sr.append(name);
sr.append("\" id=\"");
sr.append(id);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" imgSrc=\"");
sr.append(imgSrc);
sr.append("\" containerId=\"");
if (ajaxEnabled) {
sr.append(formId);
}
sr.append("\" confirmation =\"");
sr.append(confirmation);
sr.append("\" ajaxUrl=\"");
if (ajaxEnabled) {
sr.append(ajaxUrl);
}
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" disabled=");
sr.append(Boolean.toString(disabled));
sr.append(" closeOnSubmit=\"");
sr.append(String.valueOf(!submitField.getPropagateCallback()));
sr.append("\" />");
executeMacro(writer, sr.toString());
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderResetField(Appendable writer, Map<String, Object> context, ResetField resetField) throws IOException {
ModelFormField modelFormField = resetField.getModelFormField();
String name = modelFormField.getParameterName(context);
String className = "";
String alert = "false";
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
String title = modelFormField.getTitle(context);
StringWriter sr = new StringWriter();
sr.append("<@renderResetField ");
sr.append(" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" title=\"");
sr.append(title);
sr.append("\" />");
executeMacro(writer, sr.toString());
this.appendTooltip(writer, context, modelFormField);
}
@Override
public void renderHiddenField(Appendable writer, Map<String, Object> context, HiddenField hiddenField) throws IOException {
ModelFormField modelFormField = hiddenField.getModelFormField();
String value = hiddenField.getValue(context);
this.renderHiddenField(writer, context, modelFormField, value);
}
@Override
public void renderHiddenField(Appendable writer, Map<String, Object> context, ModelFormField modelFormField, String value) throws IOException {
String name = modelFormField.getParameterName(context);
String action = modelFormField.getAction(context);
String conditionGroup = modelFormField.getConditionGroup();
String event = modelFormField.getEvent();
String id = modelFormField.getCurrentContainerId(context);
boolean disabled = modelFormField.getDisabled(context);
StringWriter sr = new StringWriter();
sr.append("<@renderHiddenField ");
sr.append(" name=\"");
sr.append(name);
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" value=\"");
sr.append(value);
sr.append("\" id=\"");
sr.append(id);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" disabled=");
sr.append(Boolean.toString(disabled));
sr.append(" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderIgnoredField(Appendable writer, Map<String, Object> context, IgnoredField ignoredField) {
// do nothing, it's an ignored field; could add a comment or something if we wanted to
Debug.logVerbose("do nothing, it's an ignored field; could add a comment or something if we wanted to", MODULE);
}
@Override
public void renderFieldTitle(Appendable writer, Map<String, Object> context, ModelFormField modelFormField) throws IOException {
String titleText = modelFormField.getTitle(context);
String style = modelFormField.getTitleStyle();
String id = modelFormField.getCurrentContainerId(context);
StringBuilder sb = new StringBuilder();
if (UtilValidate.isNotEmpty(titleText)) {
if (" ".equals(titleText)) {
executeMacro(writer, "<@renderFormatEmptySpace />");
} else {
titleText = UtilHttp.encodeAmpersands(titleText);
titleText = encode(titleText, modelFormField, context);
if (UtilValidate.isNotEmpty(modelFormField.getHeaderLink())) {
StringBuilder targetBuffer = new StringBuilder();
FlexibleStringExpander target = FlexibleStringExpander.getInstance(modelFormField.getHeaderLink());
String fullTarget = target.expandString(context);
targetBuffer.append(fullTarget);
String targetType = CommonWidgetModels.Link.DEFAULT_URL_MODE;
if (UtilValidate.isNotEmpty(targetBuffer.toString()) && targetBuffer.toString().toLowerCase(Locale
.getDefault()).startsWith("javascript:")) {
targetType = "plain";
}
StringWriter sr = new StringWriter();
makeHyperlinkString(sr, modelFormField.getHeaderLinkStyle(), targetType, targetBuffer.toString(), null, titleText, "",
modelFormField, this.request, this.response, context, "");
String title = sr.toString().replace("\"", "\'");
sr = new StringWriter();
sr.append("<@renderHyperlinkTitle ");
sr.append(" name=\"");
sr.append(modelFormField.getModelForm().getName());
sr.append("\" title=\"");
sr.append(encodeDoubleQuotes(title));
sr.append("\" />");
executeMacro(writer, sr.toString());
} else if (modelFormField.isSortField()) {
renderSortField(writer, context, modelFormField, titleText);
} else if (modelFormField.isRowSubmit()) {
StringWriter sr = new StringWriter();
sr.append("<@renderHyperlinkTitle ");
sr.append(" name=\"");
sr.append(modelFormField.getModelForm().getName());
sr.append("\" title=\"");
sr.append(titleText);
sr.append("\" showSelectAll=\"Y\"/>");
executeMacro(writer, sr.toString());
} else {
sb.append(titleText);
}
}
}
if (!sb.toString().isEmpty()) {
//check for required field style on single forms
if (shouldApplyRequiredField(modelFormField)) {
String requiredStyle = modelFormField.getRequiredFieldStyle();
if (UtilValidate.isNotEmpty(requiredStyle)) {
style = requiredStyle;
}
}
StringWriter sr = new StringWriter();
sr.append("<@renderFieldTitle ");
sr.append(" style=\"");
sr.append(style);
String displayHelpText = UtilProperties.getPropertyValue("widget", "widget.form.displayhelpText");
if ("Y".equals(displayHelpText)) {
Delegator delegator = WidgetWorker.getDelegator(context);
Locale locale = (Locale) context.get("locale");
String entityName = modelFormField.getEntityName();
String fieldName = modelFormField.getFieldName();
String helpText = UtilHelpText.getEntityFieldDescription(entityName, fieldName, delegator, locale);
sr.append("\" fieldHelpText=\"");
sr.append(encodeDoubleQuotes(helpText));
}
sr.append("\" title=\"");
sr.append(sb.toString());
if (UtilValidate.isNotEmpty(id)) {
sr.append("\" id=\"");
sr.append(id);
sr.append("_title");
// Render "for"
sr.append("\" for=\"");
sr.append(id);
}
sr.append("\" />");
executeMacro(writer, sr.toString());
}
}
@Override
public void renderSingleFormFieldTitle(Appendable writer, Map<String, Object> context, ModelFormField modelFormField) throws IOException {
renderFieldTitle(writer, context, modelFormField);
}
@Override
public void renderFormOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
this.widgetCommentsEnabled = ModelWidget.widgetBoundaryCommentsEnabled(context);
if (modelForm instanceof ModelSingleForm) {
renderBeginningBoundaryComment(writer, "Form Widget - Form Element", modelForm);
} else {
renderBeginningBoundaryComment(writer, "Grid Widget - Grid Element", modelForm);
}
String targetType = modelForm.getTargetType();
String targ = modelForm.getTarget(context, targetType);
StringBuilder linkUrl = new StringBuilder();
if (UtilValidate.isNotEmpty(targ)) {
final URI linkUri = WidgetWorker.buildHyperlinkUri(targ, targetType, null, null, false, false, true,
request, response);
linkUrl.append(linkUri.toString());
}
String formType = modelForm.getType();
String targetWindow = modelForm.getTargetWindow(context);
String containerId = FormRenderer.getCurrentContainerId(modelForm, context);
String containerStyle = modelForm.getContainerStyle();
String autocomplete = "";
String name = FormRenderer.getCurrentFormName(modelForm, context);
String viewIndexField = modelForm.getMultiPaginateIndexField(context);
String viewSizeField = modelForm.getMultiPaginateSizeField(context);
int viewIndex = Paginator.getViewIndex(modelForm, context);
int viewSize = Paginator.getViewSize(modelForm, context);
boolean useRowSubmit = modelForm.getUseRowSubmit();
if (!modelForm.getClientAutocompleteFields()) {
autocomplete = "off";
}
String hasRequiredField = "";
for (ModelFormField formField : modelForm.getFieldList()) {
if (formField.getRequiredField()) {
hasRequiredField = "Y";
break;
}
}
String focusFieldName = modelForm.getFocusFieldName();
// Generate CSRF name & value for form
String csrfNameValue = CsrfUtil.getTokenNameNonAjax() + " " + CsrfUtil.generateTokenForNonAjax(request, targ);
StringWriter sr = new StringWriter();
sr.append("<@renderFormOpen ");
sr.append(" linkUrl=\"");
sr.append(linkUrl);
sr.append("\" formType=\"");
sr.append(formType);
sr.append("\" targetWindow=\"");
sr.append(targetWindow);
sr.append("\" containerId=\"");
sr.append(containerId);
sr.append("\" containerStyle=\"");
sr.append(containerStyle);
sr.append("\" autocomplete=\"");
sr.append(autocomplete);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" focusFieldName=\"");
sr.append(focusFieldName);
sr.append("\" hasRequiredField=\"");
sr.append(hasRequiredField);
sr.append("\" viewIndexField=\"");
sr.append(viewIndexField);
sr.append("\" viewSizeField=\"");
sr.append(viewSizeField);
sr.append("\" viewIndex=\"");
sr.append(Integer.toString(viewIndex));
sr.append("\" viewSize=\"");
sr.append(Integer.toString(viewSize));
sr.append("\" useRowSubmit=");
sr.append(Boolean.toString(useRowSubmit));
sr.append(" csrfNameValue=\"");
sr.append(csrfNameValue);
sr.append("\" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormClose />");
executeMacro(writer, sr.toString());
if (modelForm instanceof ModelSingleForm) {
renderEndingBoundaryComment(writer, "Form Widget - Form Element", modelForm);
} else {
renderEndingBoundaryComment(writer, "Grid Widget - Grid Element", modelForm);
}
}
@Override
public void renderMultiFormClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
//FIXME copy from HtmlFormRenderer.java (except for the closing form tag itself, that is now converted)
Iterator<ModelFormField> submitFields = modelForm.getMultiSubmitFields().iterator();
while (submitFields.hasNext()) {
ModelFormField submitField = submitFields.next();
if (submitField != null && submitField.shouldUse(context)) {
// Threw this in that as a hack to keep the submit button from expanding the first field
// Needs a more rugged solution
// WARNING: this method (renderMultiFormClose) must be called after the
// table that contains the list has been closed (to avoid validation errors) so
// we cannot call here the methods renderFormatItemRowCell*: for this reason
// they are now commented.
// this.renderFormatItemRowCellOpen(writer, context, modelForm, submitField);
// this.renderFormatItemRowCellClose(writer, context, modelForm, submitField);
// this.renderFormatItemRowCellOpen(writer, context, modelForm, submitField);
submitField.renderFieldString(writer, context, this);
// this.renderFormatItemRowCellClose(writer, context, modelForm, submitField);
}
}
StringWriter sr = new StringWriter();
sr.append("<@renderMultiFormClose />");
executeMacro(writer, sr.toString());
// see if there is anything that needs to be added outside of the multi-form
Map<String, Object> wholeFormContext = UtilGenerics.cast(context.get("wholeFormContext"));
Appendable postMultiFormWriter = wholeFormContext != null ? (Appendable) wholeFormContext.get("postMultiFormWriter") : null;
if (postMultiFormWriter != null) {
writer.append(postMultiFormWriter.toString());
}
if (modelForm instanceof ModelSingleForm) {
renderEndingBoundaryComment(writer, "Form Widget - Form Element", modelForm);
} else {
renderEndingBoundaryComment(writer, "Grid Widget - Grid Element", modelForm);
}
}
@Override
public void renderFormatListWrapperOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
Map<String, Object> inputFields = UtilGenerics.cast(context.get("requestParameters"));
Object obj = context.get("queryStringMap");
Map<String, Object> queryStringMap = (obj instanceof Map) ? UtilGenerics.cast(obj) : null;
if (UtilValidate.isNotEmpty(queryStringMap)) {
inputFields.putAll(queryStringMap);
}
if ("multi".equals(modelForm.getType())) {
inputFields = UtilHttp.removeMultiFormParameters(inputFields);
}
String queryString = UtilHttp.urlEncodeArgs(inputFields);
context.put("_QBESTRING_", queryString);
if (modelForm instanceof ModelSingleForm) {
renderBeginningBoundaryComment(writer, "Form Widget - Form Element", modelForm);
} else {
renderBeginningBoundaryComment(writer, "Grid Widget - Grid Element", modelForm);
}
if (this.renderPagination) {
this.renderNextPrev(writer, context, modelForm);
}
List<ModelFormField> childFieldList = modelForm.getFieldList();
List<String> columnStyleList = new LinkedList<>();
List<String> fieldNameList = new LinkedList<>();
for (ModelFormField childField : childFieldList) {
int childFieldType = childField.getFieldInfo().getFieldType();
if (childFieldType == FieldInfo.HIDDEN || childFieldType == FieldInfo.IGNORED) {
continue;
}
String areaStyle = childField.getTitleAreaStyle();
if (UtilValidate.isEmpty(areaStyle)) {
areaStyle = "";
}
if (fieldNameList.contains(childField.getName())) {
if (UtilValidate.isNotEmpty(areaStyle)) {
columnStyleList.set(fieldNameList.indexOf(childField.getName()), areaStyle);
}
} else {
columnStyleList.add(areaStyle);
fieldNameList.add(childField.getName());
}
}
String columnStyleListString =
columnStyleList.stream().map(str -> "'" + str + "'").collect(Collectors.joining(", "));
StringWriter sr = new StringWriter();
sr.append("<@renderFormatListWrapperOpen ");
sr.append(" formName=\"");
sr.append(modelForm.getName());
sr.append("\" style=\"");
sr.append(FlexibleStringExpander.expandString(modelForm.getDefaultTableStyle(), context));
sr.append("\" columnStyles=[");
if (UtilValidate.isNotEmpty(columnStyleListString)) {
// this is a fix for forms with no fields
sr.append(columnStyleListString);
}
sr.append("] />");
executeMacro(writer, sr.toString());
}
@Override
public void renderEmptyFormDataMessage(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderEmptyFormDataMessage");
sr.append(" message=\"");
sr.append(modelForm.getEmptyFormDataMessage(context));
sr.append("\" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatListWrapperClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormatListWrapperClose");
sr.append(" formName=\"");
sr.append(modelForm.getName());
sr.append("\" />");
executeMacro(writer, sr.toString());
if (this.renderPagination) {
this.renderNextPrev(writer, context, modelForm);
}
if (modelForm instanceof ModelSingleForm) {
renderEndingBoundaryComment(writer, "Form Widget - Form Element", modelForm);
} else {
renderEndingBoundaryComment(writer, "Grid Widget - Grid Element", modelForm);
}
}
@Override
public void renderFormatHeaderOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderOpen ");
sr.append(" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderClose");
sr.append(" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
String headerStyle = FlexibleStringExpander.expandString(modelForm.getHeaderRowStyle(), context);
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowOpen ");
sr.append(" style=\"");
sr.append(headerStyle);
sr.append("\" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowClose />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowCellOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField,
int positionSpan) throws IOException {
String areaStyle = modelFormField.getTitleAreaStyle();
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowCellOpen ");
sr.append(" style=\"");
sr.append(areaStyle);
sr.append("\" positionSpan=");
sr.append(Integer.toString(positionSpan));
sr.append(" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowCellClose(Appendable writer, Map<String, Object> context, ModelForm modelForm, ModelFormField modelFormField)
throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowCellClose />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowFormCellOpen(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
String areaStyle = modelForm.getFormTitleAreaStyle();
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowFormCellOpen ");
sr.append(" style=\"");
sr.append(areaStyle);
sr.append("\" />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowFormCellClose(Appendable writer, Map<String, Object> context, ModelForm modelForm) throws IOException {
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowFormCellClose />");
executeMacro(writer, sr.toString());
}
@Override
public void renderFormatHeaderRowFormCellTitleSeparator(Appendable writer, Map<String, Object> context, ModelForm modelForm,
ModelFormField modelFormField, boolean isLast) throws IOException {
String titleStyle = modelFormField.getTitleStyle();
StringWriter sr = new StringWriter();
sr.append("<@renderFormatHeaderRowFormCellTitleSeparator ");
sr.append(" style=\"");
sr.append(titleStyle);
sr.append("\" isLast=");
sr.append(Boolean.toString(isLast));