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.jpa.validation.entity;
20  
21  import java.util.Set;
22  import javax.persistence.PrePersist;
23  import javax.persistence.PreUpdate;
24  import javax.validation.ConstraintViolation;
25  import javax.validation.Validator;
26  import org.apache.commons.lang3.ClassUtils;
27  import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
28  import org.apache.syncope.core.persistence.api.entity.Any;
29  import org.apache.syncope.core.persistence.api.entity.DynMembership;
30  import org.apache.syncope.core.persistence.api.entity.Entity;
31  import org.apache.syncope.core.persistence.api.entity.GroupableRelatable;
32  import org.apache.syncope.core.persistence.api.entity.ProvidedKeyEntity;
33  import org.apache.syncope.core.persistence.api.entity.Schema;
34  import org.apache.syncope.core.persistence.api.entity.policy.Policy;
35  import org.apache.syncope.core.persistence.api.entity.task.Task;
36  import org.apache.syncope.core.spring.ApplicationContextProvider;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * JPA validation listener implementing bean validation.
42   */
43  public class EntityValidationListener {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(EntityValidationListener.class);
46  
47      @PrePersist
48      @PreUpdate
49      public void validate(final Object object) {
50          final Validator validator = ApplicationContextProvider.getBeanFactory().getBean(Validator.class);
51          Set<ConstraintViolation<Object>> violations = validator.validate(object);
52          if (!violations.isEmpty()) {
53              LOG.warn("Bean validation errors found: {}", violations);
54  
55              Class<?> entityInt = null;
56              for (Class<?> interf : ClassUtils.getAllInterfaces(object.getClass())) {
57                  if (!Entity.class.equals(interf)
58                          && !ProvidedKeyEntity.class.equals(interf)
59                          && !Schema.class.equals(interf)
60                          && !Task.class.equals(interf)
61                          && !Policy.class.equals(interf)
62                          && !GroupableRelatable.class.equals(interf)
63                          && !Any.class.equals(interf)
64                          && !DynMembership.class.equals(interf)
65                          && Entity.class.isAssignableFrom(interf)) {
66  
67                      entityInt = interf;
68                  }
69              }
70  
71              throw new InvalidEntityException(entityInt == null
72                      ? "Entity" : entityInt.getSimpleName(), violations);
73          }
74      }
75  }