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.RuleControlAttribute;
27  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
28  import fr.ifremer.reefdb.dto.configuration.control.ControlFeatureDTO;
29  import org.apache.commons.lang3.StringUtils;
30  
31  import java.util.Objects;
32  
33  import static org.nuiton.i18n.I18n.n;
34  import static org.nuiton.i18n.I18n.t;
35  
36  /**
37   * FeatureControlSampleValues enum.
38   */
39  public enum ControlFeatureSamplingOperationValues {
40  
41      COMMENT(RuleControlAttribute.COMMENT, n("reefdb.core.enums.featureControlValues.comment")),
42      TIME(RuleControlAttribute.TIME, n("reefdb.core.enums.featureControlValues.time")),
43      LATITUDE_REAL(RuleControlAttribute.LATITUDE_REAL, n("reefdb.core.enums.featureControlValues.latitudeReal")),
44      LONGITUDE_REAL(RuleControlAttribute.LONGITUDE_REAL, n("reefdb.core.enums.featureControlValues.longitudeReal")),
45      GEAR(RuleControlAttribute.GEAR, n("reefdb.core.enums.featureControlValues.gear"), RuleControlAttribute.GEAR_REEF_DB), // FIXME uniformiser l'énumération du modèle
46      NAME(RuleControlAttribute.NAME, n("reefdb.core.enums.featureControlValues.name"), RuleControlAttribute.NAME_REEF_DB), // FIXME uniformiser l'énumération du modèle
47      DEPARTMENT(RuleControlAttribute.SAMPLER_DEPARTMENT, n("reefdb.core.enums.featureControlValues.samplerDepartment")),
48      POSITIONING(RuleControlAttribute.POSITIONING, n("reefdb.core.enums.featureControlValues.positioning")),
49      POSITIONING_PRECISION(RuleControlAttribute.POSITIONING_PRECISION, n("reefdb.core.enums.featureControlValues.positioningPrecision")),
50      DEPTH(RuleControlAttribute.DEPTH, n("reefdb.core.enums.featureControlValues.depth")),
51      DEPTH_MAX(RuleControlAttribute.MAX_DEPTH, n("reefdb.core.enums.featureControlValues.depthMax")),
52      DEPTH_MIN(RuleControlAttribute.MIN_DEPTH, n("reefdb.core.enums.featureControlValues.depthMin")),
53      SIZE(RuleControlAttribute.SIZE, n("reefdb.core.enums.featureControlValues.size")),
54      SIZE_UNIT(RuleControlAttribute.SIZE_UNIT, n("reefdb.core.enums.featureControlValues.sizeUnit"));
55  
56      private final RuleControlAttribute ruleControlAttribute;
57      private final String keyLabel;
58      private final RuleControlAttribute oldRuleControlAttribute;
59  
60      ControlFeatureSamplingOperationValues(RuleControlAttribute ruleControlAttribute, String key) {
61          this(ruleControlAttribute, key, null);
62      }
63  
64      ControlFeatureSamplingOperationValues(RuleControlAttribute ruleControlAttribute, String key, RuleControlAttribute oldRuleControlAttribute) {
65          this.ruleControlAttribute = ruleControlAttribute;
66          this.keyLabel = key;
67          this.oldRuleControlAttribute = oldRuleControlAttribute;
68      }
69  
70      /**
71       * <p>getLabel.</p>
72       *
73       * @return a {@link java.lang.String} object.
74       */
75      public String getLabel() {
76          return t(this.keyLabel);
77      }
78  
79      /**
80       * <p>Getter for the field <code>code</code>.</p>
81       *
82       * @return a {@link java.lang.String} object.
83       */
84      public String getCode() {
85          return ruleControlAttribute.getValue();
86      }
87  
88      public String getOldCode() {
89          return oldRuleControlAttribute != null ? oldRuleControlAttribute.getValue() : null;
90      }
91  
92      /**
93       * <p>toControlFeatureDTO.</p>
94       *
95       * @return a {@link fr.ifremer.reefdb.dto.configuration.control.ControlFeatureDTO} object.
96       */
97      public ControlFeatureDTO toControlFeatureDTO() {
98          ControlFeatureDTO dto = ReefDbBeanFactory.newControlFeatureDTO();
99          dto.setId(ordinal());
100         dto.setCode(getCode());
101         dto.setName(getLabel());
102         return dto;
103     }
104 
105     /**
106      * <p>equals.</p>
107      *
108      * @param controlFeature a {@link fr.ifremer.reefdb.dto.configuration.control.ControlFeatureDTO} object.
109      * @return a boolean.
110      */
111     public boolean equals(ControlFeatureDTO controlFeature) {
112         return controlFeature != null && (getCode().equals(controlFeature.getCode()) || Objects.equals(getOldCode(), controlFeature.getCode()));
113     }
114 
115     /**
116      * <p>toControlFeatureDTO.</p>
117      *
118      * @param code a {@link java.lang.String} object.
119      * @return a {@link fr.ifremer.reefdb.dto.configuration.control.ControlFeatureDTO} object.
120      */
121     public static ControlFeatureDTO toControlFeatureDTO(String code) {
122         ControlFeatureSamplingOperationValues value = getByCode(code);
123         if (value != null) {
124             return value.toControlFeatureDTO();
125         }
126         return null;
127     }
128 
129     /**
130      * <p>getByCode.</p>
131      *
132      * @param code a {@link java.lang.String} object.
133      * @return a {@link fr.ifremer.reefdb.dto.enums.ControlFeatureSamplingOperationValues} object.
134      */
135     public static ControlFeatureSamplingOperationValues getByCode(String code) {
136         for (ControlFeatureSamplingOperationValues feature : ControlFeatureSamplingOperationValues.values()) {
137             if (StringUtils.isNotBlank(feature.getCode()) && (feature.getCode().equals(code) || Objects.equals(feature.getOldCode(), code))) {
138                 return feature;
139             }
140         }
141         return null;
142     }
143 }