View Javadoc
1   package fr.ifremer.reefdb.dto.enums;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.RuleControlEntity;
27  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
28  import fr.ifremer.reefdb.dto.configuration.control.ControlElementDTO;
29  
30  import java.util.Objects;
31  
32  import static org.nuiton.i18n.I18n.n;
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * ElementControl enum.
37   */
38  public enum ControlElementValues {
39  
40      // FIXME uniformiser les enum dans le modèle
41      SURVEY(RuleControlEntity.SURVEY, n("reefdb.core.enums.elementControlValues.observation"), RuleControlEntity.SURVEY_REEF_DB),
42      SAMPLING_OPERATION(RuleControlEntity.SAMPLING_OPERATION, n("reefdb.core.enums.elementControlValues.samplingOperation"), RuleControlEntity.SAMPLING_OPERATION_REEF_DB),
43      MEASUREMENT(RuleControlEntity.MEASUREMENT, n("reefdb.core.enums.elementControlValues.measurement"), RuleControlEntity.MEASUREMENT_REEF_DB);
44  
45      private final RuleControlEntity ruleControlEntity;
46      private final String keyLabel;
47      private final RuleControlEntity oldRuleControlEntity;
48  
49      ControlElementValues(RuleControlEntity ruleControlEntity, String keyLabel, RuleControlEntity oldRuleControlEntity) {
50          this.ruleControlEntity = ruleControlEntity;
51          this.keyLabel = keyLabel;
52          this.oldRuleControlEntity = oldRuleControlEntity;
53      }
54  
55      /**
56       * <p>getLabel.</p>
57       *
58       * @return a {@link java.lang.String} object.
59       */
60      public String getLabel() {
61          return t(this.keyLabel);
62      }
63  
64      /**
65       * <p>Getter for the field <code>code</code>.</p>
66       *
67       * @return a {@link java.lang.String} object.
68       */
69      public String getCode() {
70          return ruleControlEntity.getValue();
71      }
72  
73      public String getOldCode() {
74          return oldRuleControlEntity != null ? oldRuleControlEntity.getValue() : null;
75      }
76  
77      /**
78       * <p>toControlElementDTO.</p>
79       *
80       * @return a {@link fr.ifremer.reefdb.dto.configuration.control.ControlElementDTO} object.
81       */
82      public ControlElementDTO toControlElementDTO() {
83          ControlElementDTO dto = ReefDbBeanFactory.newControlElementDTO();
84          dto.setId(ordinal());
85          dto.setCode(getCode());
86          dto.setName(getLabel());
87          return dto;
88      }
89  
90      /**
91       * <p>equals.</p>
92       *
93       * @param controlElement a {@link fr.ifremer.reefdb.dto.configuration.control.ControlElementDTO} object.
94       * @return a boolean.
95       */
96      public boolean equals(ControlElementDTO controlElement) {
97          return controlElement != null && (getCode().equals(controlElement.getCode()) || Objects.equals(getOldCode(), controlElement.getCode()));
98      }
99  
100     public boolean equals(String controlElementCode) {
101         return controlElementCode != null && (getCode().equals(controlElementCode) || Objects.equals(getOldCode(), controlElementCode));
102     }
103 
104     /**
105      * <p>toControlElementDTO.</p>
106      *
107      * @param code a {@link java.lang.String} object.
108      * @return a {@link fr.ifremer.reefdb.dto.configuration.control.ControlElementDTO} object.
109      */
110     public static ControlElementDTO toControlElementDTO(String code) {
111         ControlElementValues value = getByCode(code);
112         if (value != null) {
113             return value.toControlElementDTO();
114         }
115         return null;
116     }
117 
118     /**
119      * <p>getByCode.</p>
120      *
121      * @param code a {@link java.lang.String} object.
122      * @return a {@link fr.ifremer.reefdb.dto.enums.ControlElementValues} object.
123      */
124     public static ControlElementValues getByCode(String code) {
125         for (ControlElementValues value : values()) {
126             if (value.getCode().equals(code) || Objects.equals(value.getOldCode(), code)) {
127                 return value;
128             }
129         }
130         return null;
131     }
132 }