View Javadoc
1   package fr.ifremer.tutti.service.validator;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.opensymphony.xwork2.validator.ValidationException;
28  import fr.ifremer.tutti.persistence.entities.referential.Status;
29  import fr.ifremer.tutti.persistence.entities.referential.TuttiReferentialEntity;
30  import org.nuiton.validator.xwork2.field.NuitonFieldValidatorSupport;
31  
32  import java.util.Collection;
33  
34  /**
35   * Created on 7/9/14.
36   *
37   * @author Tony Chemit - chemit@codelutin.com
38   * @since 3.6
39   */
40  public class TemporaryReferentialFieldValidator extends NuitonFieldValidatorSupport {
41  
42      @Override
43      protected void validateWhenNotSkip(Object object) throws ValidationException {
44  
45          String fieldName = getFieldName();
46  
47          Object fieldValue = getFieldValue(fieldName, object);
48  
49          boolean valid = true;
50  
51          if (fieldValue != null) {
52  
53              if (fieldValue instanceof TuttiReferentialEntity) {
54  
55                  valid = validateReference((TuttiReferentialEntity) fieldValue);
56  
57              } else if (fieldValue instanceof Collection) {
58  
59                  Collection<TuttiReferentialEntity> value = (Collection<TuttiReferentialEntity>) fieldValue;
60  
61                  for (TuttiReferentialEntity referentialEntity : value) {
62                      valid = validateReference(referentialEntity);
63                      if (!valid) {
64                          break;
65                      }
66                  }
67  
68              } else {
69  
70                  throw new IllegalStateException(
71                          "validator " + this +
72                          " must be used on a referential or a collection of referential field, but was uses on " +
73                          fieldValue);
74  
75              }
76  
77          }
78  
79  
80          if (!valid) {
81  
82              addFieldError(fieldName, object);
83  
84          }
85  
86      }
87  
88      protected boolean validateReference(TuttiReferentialEntity value) {
89          if (value == null) {
90              // no value defined, can not validate it
91              return true;
92          }
93  
94          Status status = value.getStatus();
95  
96          return status.getIdAsInt() != 2;
97      }
98  
99      @Override
100     public String getValidatorType() {
101         return "temporaryReferential";
102     }
103 
104 }