/
EntityWidgetBuilder.java
566 lines (443 loc) · 22.8 KB
/
EntityWidgetBuilder.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.forge.scaffold.faces.metawidget.widgetbuilder;
import static org.jboss.forge.scaffold.faces.metawidget.inspector.ForgeInspectionResultConstants.*;
import static org.metawidget.inspector.InspectionResultConstants.*;
import static org.metawidget.inspector.faces.StaticFacesInspectionResultConstants.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.metawidget.statically.BaseStaticXmlWidget;
import org.metawidget.statically.StaticWidget;
import org.metawidget.statically.StaticXmlMetawidget;
import org.metawidget.statically.StaticXmlStub;
import org.metawidget.statically.StaticXmlWidget;
import org.metawidget.statically.faces.StaticFacesUtils;
import org.metawidget.statically.faces.component.StaticUIMetawidget;
import org.metawidget.statically.faces.component.ValueHolder;
import org.metawidget.statically.faces.component.html.StaticHtmlMetawidget;
import org.metawidget.statically.faces.component.html.layout.HtmlMessage;
import org.metawidget.statically.faces.component.html.layout.HtmlPanelGrid;
import org.metawidget.statically.faces.component.html.layout.HtmlPanelGroup;
import org.metawidget.statically.faces.component.html.widgetbuilder.FaceletsParam;
import org.metawidget.statically.faces.component.html.widgetbuilder.Facet;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlColumn;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlCommandLink;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlDataTable;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlOutcomeTargetLink;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlOutputText;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlSelectOneMenu;
import org.metawidget.statically.faces.component.html.widgetbuilder.HtmlWidgetBuilder;
import org.metawidget.statically.faces.component.html.widgetbuilder.Param;
import org.metawidget.statically.faces.component.widgetprocessor.ReadableIdProcessor;
import org.metawidget.statically.faces.component.widgetprocessor.StandardBindingProcessor;
import org.metawidget.statically.layout.SimpleLayout;
import org.metawidget.util.ClassUtils;
import org.metawidget.util.CollectionUtils;
import org.metawidget.util.WidgetBuilderUtils;
import org.metawidget.util.XmlUtils;
import org.metawidget.util.simple.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Builds widgets with Forge-specific behaviours (such as links to other scaffolding pages).
*
* @author Richard Kennard
*/
public class EntityWidgetBuilder
extends HtmlWidgetBuilder
{
//
// Private statics
//
private static final String COLLECTION_VAR = "_collection";
/**
* When expanding OneToOne or Embedded types in data table rows, we must point the row link to the original type, not
* the type being expanded
*/
private static final String TOP_LEVEL_PARAMETERIZED_TYPE = "top-level-parameterized-type";
//
// Public methods
//
@Override
public StaticXmlWidget buildWidget(String elementName, Map<String, String> attributes, StaticXmlMetawidget metawidget)
{
// Suppress nested INVERSE ONE_TO_ONE, to avoid recursion
if (TRUE.equals(attributes.get(ONE_TO_ONE)) && attributes.containsKey(INVERSE_RELATIONSHIP)
&& metawidget.getParent() != null)
{
return new StaticXmlStub();
}
String type = WidgetBuilderUtils.getActualClassOrType(attributes);
if (WidgetBuilderUtils.isReadOnly(attributes))
{
// Render read-only FACES_LOOKUP as a link
if (attributes.containsKey(FACES_LOOKUP))
{
// Cleaner to stop using a Converter for a read-only FACES_LOOKUP, than to make every Converter consider
// whether it's really a UIInput (and should therefore use .toString instead of .getId())
attributes.remove(FACES_CONVERTER_ID);
// (unless parent is *already* a link, such as inside a table row)
if (metawidget.getParent() instanceof HtmlOutcomeTargetLink)
{
return null;
}
String controllerName = ClassUtils.getSimpleName(WidgetBuilderUtils.getActualClassOrType(attributes));
controllerName = StringUtils.decapitalize(controllerName);
HtmlOutcomeTargetLink link = new HtmlOutcomeTargetLink();
link.putAttribute("outcome", "/scaffold/" + controllerName + "/view");
StandardBindingProcessor bindingProcessor = metawidget.getWidgetProcessor(StandardBindingProcessor.class);
if (bindingProcessor != null)
{
bindingProcessor.processWidget(link, elementName, attributes,
(StaticUIMetawidget) metawidget);
}
Param param = new Param();
param.putAttribute("name", "id");
param.putAttribute("value",
StaticFacesUtils.wrapExpression(StaticFacesUtils.unwrapExpression(link.getValue()) + ".id"));
link.getChildren().add(param);
return link;
}
Class<?> clazz = ClassUtils.niceForName(type);
if (clazz != null)
{
// Render read-only booleans as graphics
if (boolean.class.equals(clazz))
{
HtmlOutputText outputText = new HtmlOutputText();
StandardBindingProcessor bindingProcessor = metawidget
.getWidgetProcessor(StandardBindingProcessor.class);
if (bindingProcessor != null)
{
bindingProcessor.processWidget(outputText, elementName, attributes, (StaticUIMetawidget) metawidget);
}
String styleClassEl = StaticFacesUtils.unwrapExpression(outputText.getValue())
+ " ? 'boolean-true' : 'boolean-false'";
outputText.putAttribute("styleClass", StaticFacesUtils.wrapExpression(styleClassEl));
outputText.setValue("");
return outputText;
}
}
}
// Render collection tables with links
if (type != null)
{
// Render non-optional ONE_TO_ONE with a button
if (TRUE.equals(attributes.get(ONE_TO_ONE)) && !TRUE.equals(attributes.get(REQUIRED)))
{
// (we are about to create a nestedMetawidget, so we must prevent recursion)
if (ENTITY.equals(elementName))
{
return null;
}
// Create nestedMetawidget with conditional 'rendered' attribute
StaticHtmlMetawidget nestedMetawidget = new StaticHtmlMetawidget();
metawidget.initNestedMetawidget(nestedMetawidget, attributes);
String unwrappedExpression = StaticFacesUtils.unwrapExpression(nestedMetawidget.getValue());
nestedMetawidget.putAttribute("rendered",
StaticFacesUtils.wrapExpression("!empty " + unwrappedExpression));
// If read-only we're done
if (WidgetBuilderUtils.isReadOnly(attributes))
{
return nestedMetawidget;
}
// Otherwise, further wrap it with a button
int lastIndexOf = unwrappedExpression.lastIndexOf('.');
String parentExpression = unwrappedExpression.substring(0, lastIndexOf);
String childExpression = unwrappedExpression.substring(lastIndexOf + 1);
HtmlCommandLink commandLink = new HtmlCommandLink();
commandLink.setValue("Create New " + StringUtils.uncamelCase(childExpression));
commandLink.putAttribute(
"action",
StaticFacesUtils.wrapExpression(parentExpression + ".new"
+ StringUtils.capitalize(childExpression)));
commandLink.putAttribute("rendered", StaticFacesUtils.wrapExpression("empty " + unwrappedExpression));
HtmlPanelGroup panelGroup = new HtmlPanelGroup();
panelGroup.getChildren().add(commandLink);
panelGroup.getChildren().add(nestedMetawidget);
return panelGroup;
}
Class<?> clazz = ClassUtils.niceForName(type);
if (clazz != null)
{
if (Collection.class.isAssignableFrom(clazz))
{
return createDataTableComponent(elementName, attributes, metawidget);
}
}
}
// Delegate to next WidgetBuilder in the chain
return null;
}
//
// Protected methods
//
/**
* Overridden to add row creation/deletion.
*/
@Override
protected StaticXmlWidget createDataTableComponent(String elementName, Map<String, String> attributes,
StaticXmlMetawidget metawidget)
{
// Create the normal table
StaticXmlWidget dataTable = super.createDataTableComponent(elementName, attributes, metawidget);
dataTable.putAttribute("styleClass", "data-table");
// Process the binding and id early, so we can use them below
StandardBindingProcessor bindingProcessor = metawidget.getWidgetProcessor(StandardBindingProcessor.class);
if (bindingProcessor != null)
{
bindingProcessor.processWidget(dataTable, elementName, attributes, (StaticUIMetawidget) metawidget);
}
ReadableIdProcessor readableIdProcessor = metawidget.getWidgetProcessor(ReadableIdProcessor.class);
if (readableIdProcessor != null)
{
readableIdProcessor.processWidget(dataTable, elementName, attributes, metawidget);
}
ValueHolder valueHolderTable = (ValueHolder) dataTable;
String tableValueExpression = valueHolderTable.getValue();
// Special support for non-Lists
Class<?> clazz = ClassUtils.niceForName(WidgetBuilderUtils.getActualClassOrType(attributes));
if (!List.class.isAssignableFrom(clazz))
{
String asListValueExpression = "forgeview:asList(" + StaticFacesUtils.unwrapExpression(tableValueExpression)
+ ")";
valueHolderTable.setValue(StaticFacesUtils.wrapExpression(asListValueExpression));
((BaseStaticXmlWidget) dataTable).putAdditionalNamespaceURI("forgeview", "http://jboss.org/forge/view");
}
// Add row creation/deletion for OneToMany and ManyToMany
if (!TRUE.equals(attributes.get(N_TO_MANY)) || metawidget.isReadOnly())
{
return dataTable;
}
String componentType = WidgetBuilderUtils.getComponentType(attributes);
if (componentType == null)
{
return dataTable;
}
// Hack until https://issues.apache.org/jira/browse/MYFACES-3410 is resolved: move the Collection into a temporary
// variable
HtmlPanelGroup panelGroup = new HtmlPanelGroup();
FaceletsParam param = new FaceletsParam();
param.putAttribute("name", COLLECTION_VAR);
param.putAttribute("value", tableValueExpression);
panelGroup.getChildren().add(param);
// Special support for non-Lists
if (!List.class.isAssignableFrom(clazz))
{
valueHolderTable.setValue(StaticFacesUtils.wrapExpression("forgeview:asList(" + COLLECTION_VAR + ")"));
}
else
{
valueHolderTable.setValue(StaticFacesUtils.wrapExpression(COLLECTION_VAR));
}
panelGroup.getChildren().add(dataTable);
// If not bidirectional, create an 'Add' section (bidirectional does it 'in place')
if (!attributes.containsKey(INVERSE_RELATIONSHIP))
{
HtmlPanelGrid panelGrid = new HtmlPanelGrid();
panelGrid.putAttribute("styleClass", "data-table-footer");
panelGrid.putAttribute("columns", "2");
panelGrid.putAttribute("columnClasses", ",remove-column");
// Select menu at bottom
HtmlSelectOneMenu select = new HtmlSelectOneMenu();
String selectId = dataTable.getAttribute("id") + "Select";
select.putAttribute("id", selectId);
String requestScopedValue = "requestScope['" + selectId + "']";
select.setValue(StaticFacesUtils.wrapExpression(requestScopedValue));
String simpleComponentType = ClassUtils.getSimpleName(componentType);
String controllerName = StringUtils.decapitalize(simpleComponentType);
select.setConverter(StaticFacesUtils.wrapExpression(controllerName + "Bean.converter"));
Map<String, String> emptyAttributes = CollectionUtils.newHashMap();
addSelectItems(select, StaticFacesUtils.wrapExpression(controllerName + "Bean.all"), emptyAttributes);
panelGrid.getChildren().add(select);
// Create 'Add' button
HtmlCommandLink addLink = new HtmlCommandLink();
addLink.putAttribute("styleClass", "add-button");
String addExpression = COLLECTION_VAR + ".add(" + requestScopedValue + ")";
addLink.putAttribute("action", StaticFacesUtils.wrapExpression(addExpression));
addLink.putAttribute("onclick", "if (document.getElementById(document.forms[0].id+':" + selectId
+ "').selectedIndex < 1) { alert('Must select a " + StringUtils.uncamelCase(simpleComponentType)
+ "'); return false; }");
// (id is useful for unit tests)
addLink.putAttribute("id", dataTable.getAttribute("id") + "Add");
panelGrid.getChildren().add(addLink);
panelGroup.getChildren().add(panelGrid);
}
return panelGroup;
}
/**
* Overridden to add a 'remove' column.
*/
@Override
protected void addColumnComponents(HtmlDataTable dataTable, Map<String, String> attributes, NodeList elements,
StaticXmlMetawidget metawidget)
{
super.addColumnComponents(dataTable, attributes, elements, metawidget);
if (dataTable.getChildren().isEmpty())
{
return;
}
if (!attributes.containsKey(N_TO_MANY) || metawidget.isReadOnly())
{
return;
}
HtmlCommandLink removeLink = new HtmlCommandLink();
removeLink.putAttribute("styleClass", "remove-button");
String removeExpression = COLLECTION_VAR + ".remove(" + dataTable.getAttribute("var") + ")";
removeLink.putAttribute("action", StaticFacesUtils.wrapExpression(removeExpression));
HtmlColumn column = new HtmlColumn();
column.putAttribute("headerClass", "remove-column");
column.putAttribute("footerClass", "remove-column");
column.getChildren().add(removeLink);
dataTable.getChildren().add(column);
// If bidirectional, an 'Add' button too
String inverseRelationship = attributes.get(INVERSE_RELATIONSHIP);
if (inverseRelationship != null)
{
String componentType = WidgetBuilderUtils.getComponentType(attributes);
if (componentType != null)
{
String controllerName = StringUtils.decapitalize(ClassUtils.getSimpleName(componentType));
HtmlCommandLink addLink = new HtmlCommandLink();
addLink.putAttribute("styleClass", "add-button");
String addExpression = COLLECTION_VAR + ".add(" + controllerName + "Bean.added)";
addLink.putAttribute("action", StaticFacesUtils.wrapExpression(addExpression));
// Use a f:setPropertyActionListener to initialize the bidirectional relationship
SetPropertyActionListener setPropertyActionListener = new SetPropertyActionListener();
setPropertyActionListener.putAttribute(
"target",
StaticFacesUtils.wrapExpression(controllerName + "Bean.add." + inverseRelationship));
StandardBindingProcessor bindingProcessor = metawidget.getWidgetProcessor(StandardBindingProcessor.class);
if (bindingProcessor != null)
{
bindingProcessor.processWidget(setPropertyActionListener, ENTITY, attributes,
(StaticUIMetawidget) metawidget);
}
addLink.getChildren().add(setPropertyActionListener);
// (id is useful for unit tests)
String id = StaticFacesUtils.unwrapExpression(setPropertyActionListener.getValue())
+ StringUtils.SEPARATOR_DOT_CHAR + attributes.get(NAME) + StringUtils.SEPARATOR_DOT_CHAR + "Add";
addLink.putAttribute("id", StringUtils.camelCase(id, StringUtils.SEPARATOR_DOT_CHAR));
Facet footerFacet = new Facet();
footerFacet.putAttribute("name", "footer");
footerFacet.getChildren().add(addLink);
column.getChildren().add(footerFacet);
}
}
}
/**
* Overridden to replace original column text with an <tt>h:link</tt>, in those cases we can determine the dataType.
*/
@Override
protected void addColumnComponent(HtmlDataTable dataTable, Map<String, String> tableAttributes, String elementName,
Map<String, String> columnAttributes,
StaticXmlMetawidget metawidget)
{
// Suppress columns that show Collection values. Their toString is never very nice, and nested tables look awful!
//
// Note: we don't just do N_TO_MANY values, as sometimes Collections are not annotated
String type = WidgetBuilderUtils.getActualClassOrType(columnAttributes);
if (type != null)
{
Class<?> clazz = ClassUtils.niceForName(type);
if (clazz != null && (Collection.class.isAssignableFrom(clazz)))
{
return;
}
}
// FORGE-446: Expand columns that show one-to-one values
String componentType = WidgetBuilderUtils.getComponentType(tableAttributes);
if (TRUE.equals(columnAttributes.get(ONE_TO_ONE)))
{
String columnType = columnAttributes.get(TYPE);
String inspectedType = metawidget.inspect(null, columnType);
if (inspectedType != null)
{
Element root = XmlUtils.documentFromString(inspectedType).getDocumentElement();
NodeList elements = root.getFirstChild().getChildNodes();
Map<String, String> embeddedAttributes = CollectionUtils.newHashMap();
embeddedAttributes.put(TOP_LEVEL_PARAMETERIZED_TYPE, componentType);
embeddedAttributes.put(PARAMETERIZED_TYPE, columnType);
addColumnComponents(dataTable, embeddedAttributes, elements, metawidget);
return;
}
}
// FORGE-448: Don't display "owner" when showing relationships
String columnName = columnAttributes.get(NAME);
if (columnName.equals(tableAttributes.get(INVERSE_RELATIONSHIP)))
{
return;
}
// Create the column
super.addColumnComponent(dataTable, tableAttributes, elementName, columnAttributes, metawidget);
List<StaticWidget> columns = dataTable.getChildren();
HtmlColumn column = (HtmlColumn) columns.get(columns.size() - 1);
// If we can determine the componentType, wrap it with a link
if (tableAttributes.get(TOP_LEVEL_PARAMETERIZED_TYPE) != null)
{
componentType = tableAttributes.get(TOP_LEVEL_PARAMETERIZED_TYPE);
}
if (componentType != null)
{
String controllerName = StringUtils.decapitalize(ClassUtils.getSimpleName(componentType));
// Create a link...
HtmlOutcomeTargetLink link = new HtmlOutcomeTargetLink();
link.putAttribute("outcome", "/scaffold/" + controllerName + "/view");
// ...pointing to the id
Param param = new Param();
param.putAttribute("name", "id");
param.putAttribute("value", StaticFacesUtils.wrapExpression(dataTable.getAttribute("var") + ".id"));
link.getChildren().add(param);
link.getChildren().add(column.getChildren().remove(1));
column.getChildren().add(link);
// If bidirectional, add a footer facet
if (tableAttributes.containsKey(INVERSE_RELATIONSHIP) && !metawidget.isReadOnly())
{
// If it's an inverse relationship, we really should have been able to determine sub-properties, so we
// should never be at 'entity' level *unless* componentType couldn't resolve to an actual type
if (!ENTITY.equals(elementName))
{
StaticHtmlMetawidget footerMetawidget = new StaticHtmlMetawidget();
Map<String, String> footerAttributes = CollectionUtils.newHashMap();
metawidget.initNestedMetawidget(footerMetawidget, footerAttributes);
footerMetawidget.setValue(StaticFacesUtils.wrapExpression(controllerName + "Bean.add." + columnName));
footerMetawidget.setPath(componentType + StringUtils.SEPARATOR_FORWARD_SLASH_CHAR + columnName);
footerMetawidget.setLayout(new SimpleLayout());
Facet footerFacet = new Facet();
footerFacet.putAttribute("name", "footer");
footerFacet.getChildren().add(footerMetawidget);
ReadableIdProcessor readableIdProcessor = metawidget.getWidgetProcessor(ReadableIdProcessor.class);
if (readableIdProcessor != null)
{
readableIdProcessor.processWidget(footerMetawidget, elementName, columnAttributes, metawidget);
}
HtmlMessage message = new HtmlMessage();
message.putAttribute("for", footerMetawidget.getAttribute("id"));
message.putAttribute("styleClass", "error");
footerFacet.getChildren().add(message);
column.getChildren().add(footerFacet);
}
}
}
}
}