|
27 | 27 | import com.ocpsoft.pretty.faces.annotation.URLAction.PhaseId; |
28 | 28 | import com.ocpsoft.pretty.faces.annotation.URLActions; |
29 | 29 | import com.ocpsoft.pretty.faces.annotation.URLBeanName; |
| 30 | +import com.ocpsoft.pretty.faces.annotation.URLConverter; |
30 | 31 | import com.ocpsoft.pretty.faces.annotation.URLMapping; |
31 | 32 | import com.ocpsoft.pretty.faces.annotation.URLMappings; |
32 | 33 | import com.ocpsoft.pretty.faces.annotation.URLQueryParameter; |
33 | 34 | import com.ocpsoft.pretty.faces.annotation.URLValidator; |
34 | 35 | import com.ocpsoft.pretty.faces.config.PrettyConfigBuilder; |
| 36 | +import com.ocpsoft.pretty.faces.config.mapping.PathConverter; |
35 | 37 | import com.ocpsoft.pretty.faces.config.mapping.PathValidator; |
36 | 38 | import com.ocpsoft.pretty.faces.config.mapping.QueryParameter; |
37 | 39 | import com.ocpsoft.pretty.faces.config.mapping.UrlAction; |
|
40 | 42 | import com.ocpsoft.pretty.faces.el.LazyBeanNameFinder; |
41 | 43 | import com.ocpsoft.pretty.faces.el.LazyExpression; |
42 | 44 | import com.ocpsoft.pretty.faces.el.PrettyExpression; |
| 45 | +import com.ocpsoft.pretty.faces.util.StringUtils; |
43 | 46 |
|
44 | | -@SuppressWarnings("unchecked") |
| 47 | +@SuppressWarnings({"unchecked", "rawtypes"}) |
45 | 48 | public class PrettyAnnotationHandler |
46 | 49 | { |
47 | 50 |
|
@@ -239,6 +242,28 @@ private String processPrettyMappingAnnotation(final Class clazz, final URLMappin |
239 | 242 | mapping.getPathValidators().add(pathValidator); |
240 | 243 |
|
241 | 244 | } |
| 245 | + |
| 246 | + // process converters |
| 247 | + for (URLConverter converterAnnotation : mappingAnnotation.converter()) |
| 248 | + { |
| 249 | + |
| 250 | + // index attribute is required in this case |
| 251 | + if (converterAnnotation.index() < 0) |
| 252 | + { |
| 253 | + throw new IllegalArgumentException( |
| 254 | + "Please set the index of the path parameter you want to convert with the @URLConverter specified on mapping: " |
| 255 | + + mapping.getId()); |
| 256 | + } |
| 257 | + |
| 258 | + // prepare PathValidator |
| 259 | + PathConverter pathConverter = new PathConverter(); |
| 260 | + pathConverter.setIndex(converterAnnotation.index()); |
| 261 | + pathConverter.setConverterId(StringUtils.trimToNull(converterAnnotation.converterId())); |
| 262 | + |
| 263 | + // add PathValidator to the mapping |
| 264 | + mapping.addPathConverter(pathConverter); |
| 265 | + |
| 266 | + } |
242 | 267 |
|
243 | 268 | // return mapping id |
244 | 269 | return mapping.getId().trim(); |
@@ -354,6 +379,12 @@ else if ((classMappingIds != null) && (classMappingIds.length > 0)) |
354 | 379 | queryParam.setValidator(validationAnnotation.validator()); |
355 | 380 |
|
356 | 381 | } |
| 382 | + |
| 383 | + // check if there is also a converter annotation placed on the field |
| 384 | + URLConverter converterAnnotation = field.getAnnotation(URLConverter.class); |
| 385 | + if (converterAnnotation != null) { |
| 386 | + queryParam.setConverterId(converterAnnotation.converterId().trim()); |
| 387 | + } |
357 | 388 |
|
358 | 389 | // add the new spec object to the list of specs |
359 | 390 | queryParamList.add(queryParam); |
@@ -494,6 +525,7 @@ public void build(final PrettyConfigBuilder builder) |
494 | 525 | QueryParameter queryParam = new QueryParameter(); |
495 | 526 | queryParam.setName(queryParamSpec.getName()); |
496 | 527 | queryParam.setOnError(queryParamSpec.getOnError()); |
| 528 | + queryParam.setConverterId(StringUtils.trimToNull(queryParamSpec.getConverterId())); |
497 | 529 | queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " ")); |
498 | 530 | queryParam.setOnPostback(queryParamSpec.isOnPostback()); |
499 | 531 |
|
@@ -668,6 +700,7 @@ private static class QueryParamSpec |
668 | 700 | private String[] mappingIds; |
669 | 701 | private String name; |
670 | 702 | private String onError; |
| 703 | + private String converterId; |
671 | 704 | private String[] validatorIds = {}; |
672 | 705 | private String validator; |
673 | 706 | private boolean onPostback; |
@@ -751,6 +784,16 @@ public void setMappingIds(final String[] mappingIds) |
751 | 784 | { |
752 | 785 | this.mappingIds = mappingIds; |
753 | 786 | } |
| 787 | + |
| 788 | + public String getConverterId() |
| 789 | + { |
| 790 | + return converterId; |
| 791 | + } |
| 792 | + |
| 793 | + public void setConverterId(String converterId) |
| 794 | + { |
| 795 | + this.converterId = converterId; |
| 796 | + } |
754 | 797 | } |
755 | 798 |
|
756 | 799 | } |
0 commit comments