View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.extensions.validator.beanval;
20  
21  import org.apache.myfaces.extensions.validator.core.property.PropertyDetails;
22  import org.apache.myfaces.extensions.validator.core.property.PropertyInformation;
23  import org.apache.myfaces.extensions.validator.internal.UsageCategory;
24  import org.apache.myfaces.extensions.validator.internal.UsageInformation;
25  import org.apache.myfaces.extensions.validator.util.ExtValUtils;
26  import org.apache.myfaces.extensions.validator.util.ConstraintSourceUtils;
27  import org.apache.myfaces.extensions.validator.beanval.util.BeanValidationUtils;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import javax.validation.ConstraintViolation;
32  import javax.validation.groups.Default;
33  import javax.validation.metadata.ElementDescriptor;
34  import java.util.Collections;
35  import java.util.Set;
36  import java.util.logging.Logger;
37  
38  /**
39   * @since r4
40   */
41  @UsageInformation(UsageCategory.INTERNAL)
42  class MappedConstraintSourceBeanValidationModuleValidationInterceptorInternals
43  {
44      private Logger logger;
45      private BeanValidationModuleValidationInterceptorInternals bviUtils;
46  
47      MappedConstraintSourceBeanValidationModuleValidationInterceptorInternals(
48              Logger logger, BeanValidationModuleValidationInterceptorInternals bviUtils)
49      {
50          this.logger = logger;
51          this.bviUtils = bviUtils;
52      }
53  
54      void initComponentWithPropertyDetailsOfMappedConstraintSource(FacesContext facesContext,
55                                                                    UIComponent uiComponent,
56                                                                    PropertyDetails propertyDetails)
57      {
58          Class[] foundGroups = this.bviUtils.resolveGroups(facesContext, uiComponent);
59  
60          if (foundGroups == null)
61          {
62              return;
63          }
64          else if (foundGroups.length == 0)
65          {
66              foundGroups = new Class[]{Default.class};
67          }
68  
69          PropertyDetails constraintSourcePropertyDetails = resolveMappedConstraintSourceFor(
70                  propertyDetails.getKey(), propertyDetails.getBaseObject().getClass(), propertyDetails.getProperty());
71  
72          if(constraintSourcePropertyDetails == null)
73          {
74              return;
75          }
76  
77          ElementDescriptor elementDescriptor =
78                  BeanValidationUtils.getElementDescriptor((Class) constraintSourcePropertyDetails.getBaseObject(),
79                          constraintSourcePropertyDetails.getProperty());
80  
81          if (elementDescriptor == null)
82          {
83              return;
84          }
85  
86          this.bviUtils.processElementDescriptor(facesContext, uiComponent, foundGroups, elementDescriptor);
87      }
88  
89      Set<ConstraintViolation<Object>> validateMappedConstraintSource(FacesContext facesContext,
90                                                              UIComponent uiComponent,
91                                                              Object convertedObject,
92                                                              PropertyInformation propertyInformation,
93                                                              boolean cascadedValidation)
94      {
95          Class baseBeanClass = this.bviUtils.getBaseClassType(propertyInformation);
96          String propertyName = this.bviUtils.getPropertyToValidate(propertyInformation);
97          String originalKey = getKey(propertyInformation);
98  
99          PropertyDetails constraintSourcePropertyDetails =
100                 resolveMappedConstraintSourceFor(originalKey, baseBeanClass, propertyName);
101 
102         if(constraintSourcePropertyDetails == null)
103         {
104             return Collections.emptySet();
105         }
106 
107         baseBeanClass = (Class) constraintSourcePropertyDetails.getBaseObject();
108         propertyName = constraintSourcePropertyDetails.getProperty();
109 
110         Class[] groups = this.bviUtils.resolveGroups(facesContext, uiComponent);
111 
112         if (groups == null)
113         {
114             return null;
115         }
116 
117         return BeanValidationUtils.validate(baseBeanClass, propertyName, convertedObject, groups, cascadedValidation);
118     }
119 
120     private String getKey(PropertyInformation propertyInformation)
121     {
122         return ExtValUtils.getPropertyDetails(propertyInformation).getKey();
123     }
124 
125     PropertyDetails resolveMappedConstraintSourceFor(String originalKey, Class baseBeanClass, String property)
126     {
127         return ConstraintSourceUtils.resolveMappedConstraintSourceFor(originalKey, baseBeanClass, property);
128     }
129 }