View Javadoc
1   package fr.ifremer.dali.dto.enums;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.core.dao.system.rule.FunctionId;
27  import fr.ifremer.dali.dto.FunctionDTO;
28  import fr.ifremer.dali.dto.DaliBeanFactory;
29  
30  import static org.nuiton.i18n.I18n.n;
31  import static org.nuiton.i18n.I18n.t;
32  
33  /**
34   * ControlFunctionValues enum.
35   */
36  public enum ControlFunctionValues {
37  
38      IS_EMPTY(FunctionId.EMPTY, n("dali.core.enums.functionControlValues.isEmpty")),
39      IS_AMONG(FunctionId.IN, n("dali.core.enums.functionControlValues.isAmong")),
40      MIN_MAX(FunctionId.MIN_MAX, n("dali.core.enums.functionControlValues.minMax")),
41      MIN_MAX_DATE(FunctionId.MIN_MAX_DATE, n("dali.core.enums.functionControlValues.minMaxDate")),
42      NOT_EMPTY(FunctionId.NOT_EMPTY, n("dali.core.enums.functionControlValues.notEmpty")),
43      PRECONDITION(FunctionId.PRECONDITION_QUALITATIVE, n("dali.core.enums.functionControlValues.precondition"))
44      ;
45  
46      private final FunctionId function;
47      private final String keyLabel;
48  
49      ControlFunctionValues(FunctionId functionId, final String key) {
50          this.function = functionId;
51          this.keyLabel = key;
52      }
53  
54      /**
55       * <p>getFunctionId.</p>
56       *
57       * @return a int.
58       */
59      public int getFunctionId() {
60          return this.function.getValue();
61      }
62  
63      /**
64       * <p>getLabel.</p>
65       *
66       * @return a {@link java.lang.String} object.
67       */
68      public String getLabel() {
69          return t(this.keyLabel);
70      }
71  
72      /**
73       * <p>toFunctionDTO.</p>
74       *
75       * @return a {@link fr.ifremer.dali.dto.FunctionDTO} object.
76       */
77      public FunctionDTO toFunctionDTO() {
78          FunctionDTO dto = DaliBeanFactory.newFunctionDTO();
79          dto.setId(getFunctionId());
80          dto.setName(getLabel());
81          return dto;
82      }
83  
84      /**
85       * <p>equals.</p>
86       *
87       * @param function a {@link fr.ifremer.dali.dto.FunctionDTO} object.
88       * @return a boolean.
89       */
90      public boolean equals(FunctionDTO function) {
91          return function != null && getFunctionId() == function.getId();
92      }
93  
94      /**
95       * <p>toFunctionDTO.</p>
96       *
97       * @param functionId a {@link java.lang.Integer} object.
98       * @return a {@link fr.ifremer.dali.dto.FunctionDTO} object.
99       */
100     public static FunctionDTO toFunctionDTO(Integer functionId) {
101         ControlFunctionValues value = getFunctionValue(functionId);
102         if (value != null) {
103             return value.toFunctionDTO();
104         }
105         return null;
106     }
107 
108     /**
109      * <p>getFunctionValue.</p>
110      *
111      * @param functionId a {@link java.lang.Integer} object.
112      * @return a {@link ControlFunctionValues} object.
113      */
114     public static ControlFunctionValues getFunctionValue(final Integer functionId) {
115         if (functionId != null) {
116             for (final ControlFunctionValues functionValue : ControlFunctionValues.values()) {
117                 if (functionId.equals(functionValue.getFunctionId())) {
118                     return functionValue;
119                 }
120             }
121         }
122         return null;
123     }
124 }