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 javax.validation.ConstraintValidatorContext;
22  import org.apache.syncope.common.lib.SyncopeConstants;
23  import org.apache.syncope.common.lib.types.EntityViolationType;
24  import org.apache.syncope.core.persistence.api.entity.Remediation;
25  
26  public class RemediationValidator extends AbstractValidator<RemediationCheck, Remediation> {
27  
28      @Override
29      public boolean isValid(final Remediation remediation, final ConstraintValidatorContext context) {
30          boolean isValid = true;
31  
32          switch (remediation.getOperation()) {
33              case CREATE:
34                  if (remediation.getPayloadAsCR(remediation.getAnyType().getKind().getCRClass()) == null) {
35                      context.disableDefaultConstraintViolation();
36                      context.buildConstraintViolationWithTemplate(
37                              getTemplate(EntityViolationType.InvalidRemediation,
38                                      "Expected " + remediation.getAnyType().getKind().getTOClass().getName())).
39                              addPropertyNode("payload").addConstraintViolation();
40  
41                      isValid = false;
42                  }
43                  break;
44  
45              case UPDATE:
46                  if (remediation.getPayloadAsUR(remediation.getAnyType().getKind().getURClass()) == null) {
47                      context.disableDefaultConstraintViolation();
48                      context.buildConstraintViolationWithTemplate(
49                              getTemplate(EntityViolationType.InvalidRemediation,
50                                      "Expected " + remediation.getAnyType().getKind().getURClass().getName())).
51                              addPropertyNode("payload").addConstraintViolation();
52  
53                      isValid = false;
54                  }
55                  break;
56  
57              case DELETE:
58                  if (!SyncopeConstants.UUID_PATTERN.matcher(remediation.getPayloadAsKey()).matches()) {
59                      context.disableDefaultConstraintViolation();
60                      context.buildConstraintViolationWithTemplate(
61                              getTemplate(EntityViolationType.InvalidRemediation, "Expected UUID")).
62                              addPropertyNode("payload").addConstraintViolation();
63  
64                      isValid = false;
65                  }
66                  break;
67  
68              case NONE:
69              default:
70                  context.disableDefaultConstraintViolation();
71                  context.buildConstraintViolationWithTemplate(
72                          getTemplate(EntityViolationType.InvalidRemediation, "NONE is not allowed")).
73                          addPropertyNode("operation").addConstraintViolation();
74  
75                  isValid = false;
76          }
77  
78          return isValid;
79      }
80  }