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.syncope.core.persistence.api.attrvalue.validation;
20  
21  import java.util.EnumSet;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Optional;
25  import java.util.Set;
26  import java.util.stream.Collectors;
27  import javax.validation.ConstraintViolation;
28  import javax.validation.ValidationException;
29  import org.apache.commons.lang3.StringUtils;
30  import org.apache.syncope.common.lib.types.EntityViolationType;
31  
32  /**
33   * Exception thrown when any JPA entity fails bean validation.
34   */
35  public class InvalidEntityException extends ValidationException {
36  
37      private static final long serialVersionUID = 3249297275444409691L;
38  
39      private final String entityClassSimpleName;
40  
41      private final Map<Class<?>, Set<EntityViolationType>> violations = new HashMap<>();
42  
43      /**
44       * Constructs a singleton map of violations from given parameters.
45       *
46       * @param entityClass class of invalid entity
47       * @param entityViolationType type of violation found
48       * @param message message to be associated to the violation
49       */
50      public InvalidEntityException(
51              final Class<?> entityClass,
52              final EntityViolationType entityViolationType,
53              final String message) {
54  
55          super();
56  
57          this.entityClassSimpleName = entityClass.getSimpleName();
58  
59          entityViolationType.setMessage(Optional.ofNullable(message).map(String::trim).orElse(StringUtils.EMPTY));
60  
61          this.violations.put(entityClass, EnumSet.noneOf(EntityViolationType.class));
62          this.violations.get(entityClass).add(entityViolationType);
63      }
64  
65      /**
66       * Constructs a map of violations out of given {@code ConstraintViolation} set.
67       *
68       * @param entityClassSimpleName simple class name of invalid entity
69       * @param violations as returned by bean validation
70       */
71      public InvalidEntityException(
72              final String entityClassSimpleName,
73              final Set<ConstraintViolation<Object>> violations) {
74  
75          super();
76  
77          this.entityClassSimpleName = entityClassSimpleName;
78  
79          violations.forEach(violation -> {
80              String key = StringUtils.substringBefore(violation.getMessageTemplate(), ";").trim();
81              String message = StringUtils.substringAfter(violation.getMessageTemplate(), ";").trim();
82  
83              EntityViolationType entityViolationType;
84              try {
85                  entityViolationType = EntityViolationType.valueOf(key);
86              } catch (IllegalArgumentException e) {
87                  entityViolationType = EntityViolationType.Standard;
88              }
89              entityViolationType.setMessage(message);
90              entityViolationType.setPropertyPath(violation.getPropertyPath().toString());
91              entityViolationType.setInvalidValue(violation.getInvalidValue());
92  
93              if (!this.violations.containsKey(violation.getLeafBean().getClass())) {
94                  this.violations.put(violation.getLeafBean().getClass(), EnumSet.noneOf(EntityViolationType.class));
95              }
96              this.violations.get(violation.getLeafBean().getClass()).add(entityViolationType);
97          });
98      }
99  
100     public final boolean hasViolation(final EntityViolationType type) {
101         return violations.keySet().stream().anyMatch(entity -> violations.get(entity).contains(type));
102     }
103 
104     public String getEntityClassSimpleName() {
105         return entityClassSimpleName;
106     }
107 
108     public final Map<Class<?>, Set<EntityViolationType>> getViolations() {
109         return violations;
110     }
111 
112     @Override
113     public String getMessage() {
114         return violations.entrySet().stream().
115                 map(entry -> entry.getKey().getSimpleName() + " " + entry.getValue().toString()).
116                 collect(Collectors.joining(","));
117     }
118 }