1 package fr.ifremer.reefdb.dto.enums;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
37
38 public enum ControlElementValues {
39
40
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
57
58
59
60 public String getLabel() {
61 return t(this.keyLabel);
62 }
63
64
65
66
67
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
79
80
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
92
93
94
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
106
107
108
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
120
121
122
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 }