Skip to content

Commit 607eb34

Browse files
committed
Added basic support for adding @URLQueryParam annotated fields to multiple mappings
1 parent 808966f commit 607eb34

1 file changed

Lines changed: 58 additions & 53 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/config/annotation/PrettyAnnotationHandler.java‎

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void processClass(Class clazz)
119119
// loop over fields to find URLQueryParameter annotations
120120
for (Field field : clazz.getDeclaredFields())
121121
{
122-
processFieldAnnotations(field, classMappingId);
122+
processFieldAnnotations(field, new String[] { classMappingId } );
123123
}
124124

125125
}
@@ -275,9 +275,9 @@ private void processMethodAnnotations(Method method, String[] classMappingIds)
275275
* Searches for {@link URLQueryParameter} annotations on a single field.
276276
*
277277
* @param field Field to scan
278-
* @param classMappingId The mapping ID of the class this method belongs to
278+
* @param classMappingIds The mapping IDs of the class this method belongs to
279279
*/
280-
private void processFieldAnnotations(Field field, String classMappingId)
280+
private void processFieldAnnotations(Field field, String[] classMappingIds)
281281
{
282282
// Is there a @URLQueryParameter annotation?
283283
URLQueryParameter queryParamAnnotation = field.getAnnotation(URLQueryParameter.class);
@@ -295,14 +295,13 @@ private void processFieldAnnotations(Field field, String classMappingId)
295295
// check which mapping the action belongs to
296296
if (!isBlank(queryParamAnnotation.mappingId()))
297297
{
298-
// action belongs to the mapping mentioned with mappingId
299-
// attribute
300-
queryParam.setMappingId(queryParamAnnotation.mappingId().trim());
298+
// action belongs to the mapping mentioned with mappingId attribute
299+
queryParam.setMappingIds(new String[] { queryParamAnnotation.mappingId().trim() });
301300
}
302-
else if (!isBlank(classMappingId))
301+
else if (classMappingIds != null && classMappingIds.length > 0)
303302
{
304-
// use the mapping found on the class
305-
queryParam.setMappingId(classMappingId.trim());
303+
// use the mappings found on the class
304+
queryParam.setMappingIds(classMappingIds);
306305
}
307306
else
308307
{
@@ -448,46 +447,52 @@ public void build(PrettyConfigBuilder builder)
448447
for (QueryParamSpec queryParamSpec : queryParamList)
449448
{
450449

451-
// Get the mapping references by the query param
452-
UrlMapping mapping = urlMappings.get(queryParamSpec.getMappingId());
453-
454-
// fail for unresolved mappings
455-
if (mapping == null)
450+
// create a query param for each referenced mapping
451+
for (String mappingId : queryParamSpec.getMappingIds())
456452
{
457-
throw new IllegalArgumentException("Unable to find the mapping '" + queryParamSpec.getMappingId()
458-
+ "' referenced at field '" + queryParamSpec.getFieldName() + "' in class '"
459-
+ queryParamSpec.getOwnerClass().getName() + "'.");
460-
}
461453

462-
// build UrlMapping
463-
QueryParameter queryParam = new QueryParameter();
464-
queryParam.setName(queryParamSpec.getName());
465-
queryParam.setOnError(queryParamSpec.getOnError());
466-
queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " "));
467-
queryParam.setOnPostback(queryParamSpec.isOnPostback());
454+
// Get the mapping references by the query param
455+
UrlMapping mapping = urlMappings.get(mappingId);
468456

469-
// optional validator method
470-
if (!isBlank(queryParamSpec.getValidator()))
471-
{
472-
queryParam.setValidatorExpression(new ConstantExpression(queryParamSpec.getValidator()));
473-
}
457+
// fail for unresolved mappings
458+
if (mapping == null)
459+
{
460+
throw new IllegalArgumentException("Unable to find the mapping '" + mappingId
461+
+ "' referenced at field '" + queryParamSpec.getFieldName() + "' in class '"
462+
+ queryParamSpec.getOwnerClass().getName() + "'.");
463+
}
474464

475-
// try to get bean name
476-
Class<?> clazz = queryParamSpec.getOwnerClass();
465+
// build UrlMapping
466+
QueryParameter queryParam = new QueryParameter();
467+
queryParam.setName(queryParamSpec.getName());
468+
queryParam.setOnError(queryParamSpec.getOnError());
469+
queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " "));
470+
queryParam.setOnPostback(queryParamSpec.isOnPostback());
477471

478-
// build expression
479-
PrettyExpression expression = buildPrettyExpression(clazz, queryParamSpec.getFieldName());
480-
queryParam.setExpression(expression);
472+
// optional validator method
473+
if (!isBlank(queryParamSpec.getValidator()))
474+
{
475+
queryParam.setValidatorExpression(new ConstantExpression(queryParamSpec.getValidator()));
476+
}
481477

482-
// trace
483-
if (log.isTraceEnabled())
484-
{
485-
log.trace("Registered query-param '" + queryParam.getName() + "' to '" + expression + "' in mapping: "
486-
+ mapping.getId());
487-
}
478+
// try to get bean name
479+
Class<?> clazz = queryParamSpec.getOwnerClass();
488480

489-
// register this action
490-
mapping.addQueryParam(queryParam);
481+
// build expression
482+
PrettyExpression expression = buildPrettyExpression(clazz, queryParamSpec.getFieldName());
483+
queryParam.setExpression(expression);
484+
485+
// trace
486+
if (log.isTraceEnabled())
487+
{
488+
log.trace("Registered query-param '" + queryParam.getName() + "' to '" + expression + "' in mapping: "
489+
+ mapping.getId());
490+
}
491+
492+
// register this action
493+
mapping.addQueryParam(queryParam);
494+
495+
}
491496

492497
}
493498

@@ -632,7 +637,7 @@ private static class QueryParamSpec
632637
{
633638
private String fieldName;
634639
private Class<?> ownerClass;
635-
private String mappingId;
640+
private String[] mappingIds;
636641
private String name;
637642
private String onError;
638643
private String[] validatorIds = {};
@@ -649,16 +654,6 @@ public void setValidator(String validator)
649654
this.validator = validator;
650655
}
651656

652-
public String getMappingId()
653-
{
654-
return mappingId;
655-
}
656-
657-
public void setMappingId(String mappingId)
658-
{
659-
this.mappingId = mappingId;
660-
}
661-
662657
public String getName()
663658
{
664659
return name;
@@ -718,6 +713,16 @@ public void setOnPostback(boolean onPostback)
718713
{
719714
this.onPostback = onPostback;
720715
}
716+
717+
public String[] getMappingIds()
718+
{
719+
return mappingIds;
720+
}
721+
722+
public void setMappingIds(String[] mappingIds)
723+
{
724+
this.mappingIds = mappingIds;
725+
}
721726
}
722727

723728
}

0 commit comments

Comments
 (0)