/
ComponentUtils.java
622 lines (577 loc) · 19.1 KB
/
ComponentUtils.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
/*
* Copyright 2007-2011 the original author or authors.
*
* Licensed 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.shept.org.springframework.web.servlet.mvc.delegation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.shept.org.springframework.beans.support.CommandWrapper;
import org.shept.org.springframework.beans.support.MultiChoice;
import org.shept.org.springframework.beans.support.PageableList;
import org.shept.org.springframework.web.servlet.mvc.delegation.configuration.ChainConfiguration;
import org.shept.org.springframework.web.servlet.mvc.delegation.configuration.DataGridConfiguration;
import org.shept.org.springframework.web.servlet.mvc.delegation.configuration.SegmentConfiguration;
import org.shept.org.springframework.web.servlet.mvc.delegation.configuration.SheptBean;
import org.shept.org.springframework.web.servlet.mvc.delegation.configuration.TargetConfiguration;
import org.shept.org.springframework.web.servlet.mvc.support.InfoItem;
import org.shept.org.springframework.web.servlet.mvc.support.ModelUtils;
import org.shept.org.springframework.web.servlet.mvc.support.RequestValueUtils;
import org.shept.org.springframework.web.servlet.mvc.support.RequestValueUtils.ChainParameter;
import org.shept.util.SheptBeanFactoryUtils;
import org.shept.util.StringUtilsExtended;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.BindingResultUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
* @version $$Id: ComponentUtils.java 119 2011-02-21 17:28:18Z aha $$
*
* @author Andi
*
*/
public abstract class ComponentUtils {
/** Logger that is available to subclasses */
protected static Log logger = LogFactory.getLog(ComponentUtils.class);
public static String DEFAULT_SELECTOR = "";
/**
*
* @param
* @return
*
* @param token
* @return
*/
public static Object getModel(ComponentToken token) {
if (token == null) {
return null;
}
Object model = null;
Object unwrapped = ModelUtils.unwrapIfNecessary(token.getComponent());
if (null != AnnotationUtils.findAnnotation(unwrapped.getClass(),Entity.class)) {
model = unwrapped;
}
if (model == null) {
model = ModelUtils.unwrapIfNecessary(getModelFromList(token));
}
return model;
}
/**
*
* @param
* @return
*
* @param token
* @return
*/
public static Object getModelFromList(ComponentToken token) {
Object model = null;
String val = token.getToken().getValue();
Integer index = -1;
try { // checking for a direct index first
index = Integer.valueOf(val);
} catch (Exception e) {
// if not a direct index then check for submission parameter
ChainParameter jp = RequestValueUtils.getChainParameter(val);
if (jp.isIndexed()) {
index = jp.getIndex();
} else {
logger.info("Trying to find an object model index but there is no index specified in token " + token.toString());
}
}
if (index == -1) {
return null; // no index found so far
}
PageableList<?> pagedList = null;
if (PageableList.class.isAssignableFrom(token.getComponent().getClass())) {
pagedList = (PageableList<?>) token.getComponent();
if (pagedList.isVisible(index)) {
model = pagedList.getSource().get(index);
} else {
if (logger.isErrorEnabled()) {
logger.error("The index " + index.toString() + " is not visible within the boundaries of the pagedList (" +
String.valueOf(pagedList.getFirstElementOnPage()) + "-" + String.valueOf(pagedList.getLastElementOnPage()) + ")" );
}
return null;
}
} else {
if (logger.isErrorEnabled()) {
logger.error("The token component does not implement PageableList so we cannot extract a row object from list. Token is: " + token.toString());
}
}
return model;
}
/**
*
* @param
* @return
*
* @param token
* @return
*/
public static List<?> getModelsSelected(ComponentToken token) {
if (! (token.getComponent() instanceof MultiChoice)) return Collections.EMPTY_LIST;
return ((MultiChoice<?>) token.getComponent()).getSelectedItems();
}
/**
* Looking up the token's component index in the command list
*
* @param token
* @return
*/
public static int lookupComponentIndex(ComponentToken token) {
if (!(token.getCommand() instanceof SubCommandProvider)) {
return -1;
}
SubCommandProvider scp = (SubCommandProvider) token.getCommand();
for (int i = 0; i < scp.getChildren().size(); i++) {
CommandWrapper cmdWr = scp.getChildren().get(i);
if (cmdWr.getCommand().equals(token.getComponent())) {
return i;
}
}
return 0;
}
/**
* Looking up the token's component path
*
* @param token
* @return
*/
public static String lookupComponentPath(ComponentToken token) {
if (!(token.getCommand() instanceof SubCommandProvider)) {
return "";
}
SubCommandProvider scp = (SubCommandProvider) token.getCommand();
for (Entry<String, Object> pathAndCommand : scp.getSubCommands().entrySet()) {
if (pathAndCommand.getValue().equals(token.getComponent())) {
return pathAndCommand.getKey();
}
}
return "";
}
/**
* Return a map contaning bind path as key and CommandWrapper as value
*
* @param token
* @return
*/
public static Map<String, CommandWrapper> getComponentPathMap (SubCommandProvider command) {
Map<String, Object> cmdMap = command.getSubCommands();
String[] cmdPath = cmdMap.keySet().toArray(new String[0]);
Map<String, CommandWrapper> pathMap = new LinkedHashMap<String, CommandWrapper>();
for (int i = 0; i < cmdMap.size(); i++) {
pathMap.put(cmdPath[i], command.getChildren().get(i));
}
return pathMap;
}
/**
* Looking up the token's component wrapper
*
* @param token
* @return
*/
public static CommandWrapper lookupComponentWrapper(ComponentToken token) {
return lookupComponentWrapper(token, 0);
}
/**
* Looking up the token's component wrapper
*
* @param token
* @return
*/
public static CommandWrapper lookupComponentWrapper(ComponentToken token, int offset) {
if (!(token.getCommand() instanceof SubCommandProvider)) {
return null;
}
SubCommandProvider scp = (SubCommandProvider) token.getCommand();
Integer idx = lookupComponentIndex(token);
return scp.getChildren().get(idx + offset);
}
/**
* Remove all components after the specified index. If no index is specified
* then the passed component is taken as the last component
*
* @param token
* @param index
* @return
*/
public static int removeComponentsAfterIndex(ComponentToken token, Integer index) {
if (!(token.getCommand() instanceof SubCommandProvider)) {
return -1;
}
// jump to the fragment we just added (identified by token)
SubCommandProvider scp = (SubCommandProvider) token.getCommand();
List<CommandWrapper> children = scp.getChildren();
Integer idx = index;
if (index == null) {
idx = lookupComponentIndex(token);
}
idx = Math.min(idx, children.size() - 1);
while (children.size() - 1 > idx) {
scp.getChildren().remove(children.size() - 1);
}
return idx;
}
/**
* Add the Component wrapper after the position specified by token
*
* @param token
* @param wrapper
* @param index
*/
public static int addComponent (ComponentToken token, CommandWrapper wrapper) {
return addComponent(token, wrapper, null);
}
/**
* Add the Component wrapper after the position specified by token
* If theres a valid index argument then add the compoent after the index
*
* @param token
* @param wrapper
* @param index
*/
public static int addComponent (ComponentToken token, CommandWrapper wrapper, Integer index) {
if (!(token.getCommand() instanceof SubCommandProvider)) {
return -1;
}
SubCommandProvider scp = (SubCommandProvider) token.getCommand();
List<CommandWrapper> children = scp.getChildren();
Integer idx = index;
if (index == null) {
idx = lookupComponentIndex(token);
}
idx = Math.min(idx, children.size() - 1) +1;
scp.getChildren().add(idx, wrapper);
return idx;
}
/**
*
* @param
* @return
*
* @param ctx
* @param tagName
* @return
*/
public static SegmentConfiguration getConfiguration(ApplicationContext ctx, String tagName) {
SegmentConfiguration config = null;
try {
config = ctx.getBean(tagName,SegmentConfiguration.class);
} catch (BeansException ex) {
// no configuration found, this is ok
}
return config;
}
/**
*
* @param
* @return
*
* @param ctx
* @param wrapper
*/
public static void applyConfiguration(CommandWrapper wrapper, ApplicationContext ctx) {
// fill in values from the configuration
SegmentConfiguration config = ComponentUtils.getConfiguration(ctx, wrapper.getTagName());
if (config instanceof DataGridConfiguration &&
wrapper.getCommand() instanceof PageableList) {
PageableList<?> listHolder = (PageableList<?>) wrapper.getCommand();
DataGridConfiguration listConfig = (DataGridConfiguration) config;
if (listConfig.getNewModelSize() != null &&
listHolder.getNewModelSize() == PageableList.DEFAULT_NEW_MODEL_SIZE ) {
listHolder.setNewModelSize(listConfig.getNewModelSize());
}
if (listConfig.getPageSize() != null &&
listHolder.getPageSize() == PageableList.DEFAULT_PAGE_SIZE) {
listHolder.setPageSize(listConfig.getPageSize());
}
}
}
/**
*
* @param
* @return
*
* @param wrapper
* @param ctx
* @param model
* @param selector
* @return
*/
public static String getComponentInfo(HttpServletRequest request, InfoItem item, Object model) {
if (item == null) return null;
if (item.getCode() == null) return null;
WebApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
Locale locale = RequestContextUtils.getLocale(request);
String arg = null;
Method mth;
if (model != null) {
if (StringUtils.hasText(item.getSelector())) {
mth = ReflectionUtils.findMethod(model.getClass(), StringUtilsExtended.getReadAccessor(item.getSelector()));
if (mth != null) {
arg = (String) ReflectionUtils.invokeMethod(mth, model);
}
}
}
if (StringUtils.hasText(arg)) {
return ctx.getMessage(item.getCode(), new String[]{arg}, "???", locale );
} else {
return ctx.getMessage(item.getCode(), null, "???", locale );
}
}
/**
*
* @param request
* @param cc
* @param model
* @return
*/
public static String getComponentInfo(HttpServletRequest request, TargetConfiguration cc, Object model) {
return getComponentInfo(request, cc.getInfo(), model);
}
/**
*
*/
public static SegmentConfiguration getConfiguration(ApplicationContext ctx, CommandWrapper wrapper) {
return ComponentUtils.getConfiguration(ctx, wrapper.getTagName());
}
/**
*
*/
public static SegmentConfiguration getConfiguration(ApplicationContext ctx, ComponentToken token) {
CommandWrapper wrapper = lookupComponentWrapper(token);
if (wrapper == null) return null;
return getConfiguration(ctx, wrapper);
}
/**
*
*/
public static SegmentConfiguration getConfiguration(HttpServletRequest request, CommandWrapper wrapper) {
WebApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
if (ctx == null) return null;
return ComponentUtils.getConfiguration(ctx, wrapper);
}
/**
*
*/
public static SegmentConfiguration getConfiguration(HttpServletRequest request, ComponentToken token) {
CommandWrapper wrapper = lookupComponentWrapper(token);
if (wrapper == null) return null;
return getConfiguration(request, wrapper);
}
/**
*
*/
public static TargetConfiguration getChainConfiguration(HttpServletRequest request, ComponentToken token) {
WebApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
if (ctx == null) return null;
return getChainConfiguration(ctx, token);
}
/**
*
*/
@SuppressWarnings("unused")
public static TargetConfiguration getChainConfiguration(ApplicationContext ctx, ComponentToken token) {
ChainParameter jp = RequestValueUtils.getChainParameter(token.getToken().getValue());
String linkName = jp.getName();
String sourceName = lookupComponentWrapper(token).getTagName();
TargetConfiguration tc = getChainConfiguration(ctx, linkName, sourceName);
if (tc != null) {
return tc;
}
String parentName = SheptBeanFactoryUtils.getParentBeanName(ctx, sourceName);
while (parentName != null) {
tc = getChainConfiguration(ctx, linkName, parentName);
if (tc != null) {
return tc;
}
parentName = SheptBeanFactoryUtils.getParentBeanName(ctx, parentName);
}
String errStr = "No chain configuration found for link: '"
+ linkName + "' from source: '" + sourceName + "'";
if (parentName != null) {
errStr = errStr + " parent: '" + parentName + "'";
}
logger.error(errStr);
return null;
}
/**
* @param ctx
* @param linkName
* @param sourceName
* @return
*/
private static TargetConfiguration getChainConfiguration(
ApplicationContext ctx, String linkName, String sourceName) {
// TODO introduce a ChainConfigurationHolder instead
// ChainConfigurationHolder should do initialization checks (duplicate names ...)
List<TargetConfiguration> chains = getChainConfigurations(ctx, sourceName);
// check configuration bean names
for (TargetConfiguration cc : chains) {
if (StringUtils.hasText(cc.getBeanName())) {
if (cc.getBeanName().equals(linkName)) {
logger.info("Chain configuration found for (chain-)bean named '" + linkName + "' from source '" + sourceName + "'");
return cc;
}
}
}
// check relation values (w getter)
String relName = linkName;
if (! relName.startsWith("get")) {
relName = "get" + StringUtils.capitalize(relName);
}
for (TargetConfiguration cc : chains) {
if (cc instanceof ChainConfiguration) {
String rel = ((ChainConfiguration)cc).getRelation();
if (StringUtils.hasText(rel)) {
if (rel.equals(relName)) {
logger.info("Chain configuration found for relation named '" + linkName + "' from source '" + sourceName + "'");
return cc;
}
}
}
}
// last but not least check for destination name
for (TargetConfiguration cc : chains) {
if (StringUtils.hasText(cc.getTo().getBeanName())) {
if (cc.getTo().getBeanName().equals(linkName)) {
logger.info("Chain configuration found for target bean named '" + linkName + "' from source '" + sourceName + "'");
return cc;
}
}
}
return null;
}
/**
* @param ctx
* @param sourceName
* @return
*/
protected static List<TargetConfiguration> getChainConfigurations(
ApplicationContext ctx, String sourceName) {
// select all configurations from the given source segment
@SuppressWarnings("unchecked")
List<TargetConfiguration> allChains = (List<TargetConfiguration>) ctx.getBean(SheptBean.CHAINS.getBeanName());
List<TargetConfiguration> chains = new ArrayList<TargetConfiguration>();
for (TargetConfiguration tC : allChains) {
if ((tC instanceof ChainConfiguration) && (((ChainConfiguration) tC).getFrom().getBeanName().equals(sourceName))) {
chains.add(tC);
}
}
return chains;
}
/**
*
*/
public static String getChainConfigurationErrorMessage(HttpServletRequest request, ComponentToken token) {
WebApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
if (ctx == null) return "";
return getChainConfigurationErrorMessage(ctx, token);
}
/**
*
*/
public static String getChainConfigurationErrorMessage(ApplicationContext ctx, ComponentToken token) {
SegmentConfiguration config = ComponentUtils.getConfiguration(ctx, token);
ChainParameter jp = RequestValueUtils.getChainParameter(token.getToken().getValue());
String linkName = jp.getName();
return "Missing chain configuration in '" + config.getBeanName() + "' for '" + linkName + "'";
}
/**
*
* @param
* @return the prefixed path name and append always the NESTED_PROPERTY_SEPARATOR to the end
*
* @param path
* @return
*/
public static String getPropertyPathPrefix(String pathName) {
if (StringUtils.hasLength(pathName) && ! pathName.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)){
pathName = pathName + PropertyAccessor.NESTED_PROPERTY_SEPARATOR;
}
return pathName;
}
/**
*
* @param
* @return
*
* @param target
* @param pathName
* @return
*/
public static Object getComponent(Object target, String pathName) {
Object component = null;
if (target instanceof SubCommandProvider) {
Map<String, CommandWrapper> pathMap = getComponentPathMap((SubCommandProvider) target);
CommandWrapper wrapper = pathMap.get(pathName);
component = wrapper.getCommand();
}
return component;
}
/**
*
* @param
* @return
*
* @param modelAndView
* @return
*/
public static SubCommandProvider getCommand (ModelAndView modelAndView) {
String prefix = BindingResult.MODEL_KEY_PREFIX;
if (modelAndView == null) {
return null;
}
for (String name : modelAndView.getModel().keySet()) {
if (name.startsWith(prefix)) {
BindingResult res = BindingResultUtils.getBindingResult(modelAndView.getModel(),
name.substring(prefix.length()));
if (res != null && res.getTarget() instanceof SubCommandProvider) {
return (SubCommandProvider) res.getTarget();
}
}
}
return null;
}
/**
*
* @param
* @return
*
* @param modelAndView
* @param pathName
* @return
*/
public static Object getComponent (ModelAndView modelAndView, String pathName) {
SubCommandProvider command = getCommand(modelAndView);
return getComponent(command, pathName);
}
}