1 package org.lsst.ccs.config;
2
3 import org.lsst.gruth.jutils.ComponentNode;
4
5 import javax.persistence.MappedSuperclass;
6 import java.io.PrintWriter;
7 import java.io.Serializable;
8 import java.util.*;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 @MappedSuperclass
25 public abstract class ConfigProfile implements Serializable {
26
27
28
29 private long startTimestamp ;
30
31
32
33 private long endTimestamp = PackCst.STILL_VALID ;
34
35
36
37
38 private String name ;
39
40
41
42
43 private String tag ;
44
45
46
47
48 private String userName ;
49
50
51
52
53 private boolean beenInEngineeringMode ;
54
55
56
57
58
59
60 private int level = PackCst.END_USER_LEVEL ;
61
62
63
64
65 private String remarks ;
66
67
68
69
70 private long previousConfigID ;
71
72
73
74
75 protected ConfigProfile() {
76 }
77
78 protected ConfigProfile(String name, String tag, String userName, int level) {
79 this.name = name ;
80 this.tag = tag ;
81 this.userName = userName;
82 this.level = level;
83 }
84
85
86
87 protected abstract long getId() ;
88 protected abstract void setId(long id) ;
89
90 public abstract SubsystemDescription getSubsystemDescription() ;
91
92 public abstract Set<? extends ParameterConfiguration> getModifiedParameters() ;
93
94 public abstract void temporaryChangeConfigurationValue(ParameterConfiguration parameter, long time, String value) ;
95
96
97 public abstract ParameterConfiguration temporaryChangeConfigurationValue(String parameterPath, long time, String value) ;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public boolean isReadOnly() {
113 return getId() != 0L ;
114 }
115
116 public boolean isBeenInEngineeringMode() {
117 return beenInEngineeringMode;
118 }
119
120 void setBeenInEngineeringMode(boolean beenInEngineeringMode) {
121 this.beenInEngineeringMode = beenInEngineeringMode;
122 }
123
124 public String getName() {
125 return name;
126 }
127
128 void setName(String name) {
129 this.name = name;
130 }
131
132
133 public String getSubsystemName() {
134 return getSubsystemDescription().getSubsystemName() ;
135 }
136
137 public String getTag() {
138 return tag;
139 }
140
141 void setTag(String tag) {
142 this.tag = tag;
143 }
144
145
146 public long getStartTimestamp() {
147 return startTimestamp;
148 }
149
150 protected void setStartTimestamp(long startTimestamp) {
151 this.startTimestamp = startTimestamp;
152 }
153
154 public long getEndTimestamp() {
155 return endTimestamp;
156 }
157
158 void setEndTimestamp(long endTimestamp) {
159 this.endTimestamp = endTimestamp;
160 }
161
162 public String getUserName() {
163 return userName;
164 }
165
166 void setUserName(String userName) {
167 this.userName = userName;
168 }
169
170 public int getLevel() {
171 return level;
172 }
173
174 void setLevel(int level) {
175 this.level = level;
176 }
177
178 public String getRemarks() {
179 return remarks;
180 }
181
182
183
184
185
186 public void setRemarks(String remarks) {
187 this.remarks = remarks;
188 }
189
190
191
192
193
194 public long getPreviousConfigID() {
195 return previousConfigID;
196 }
197
198 void setPreviousConfigID(long previousConfigID) {
199 this.previousConfigID = previousConfigID;
200 }
201
202
203
204 @Override
205 public boolean equals(Object o) {
206 if (this == o) return true;
207 if (!(o instanceof ConfigProfile)) return false;
208
209 ConfigProfile that = (ConfigProfile) o;
210 if (getId() != that.getId()) return false;
211 if (!getName().equals(that.getName())) return false;
212 String tag = getTag() ;
213 if (tag != null ? !tag.equals(that.getTag()) : that.getTag() != null) return false;
214
215 return true;
216 }
217
218 @Override
219 public int hashCode() {
220 long id = getId() ;
221 int result = (int) (id ^ (id >>> 32));
222 result = 31 * result + getName().hashCode();
223 String tag = getTag() ;
224 result = 31 * result + (tag != null ? tag.hashCode() : 0);
225 return result;
226 }
227 @Override
228 public String toString() {
229 return "{" +
230 "id=" + getId() +
231 ";configurations=" + this.getModifiedParameters() +
232 '}';
233 }
234
235
236 public ParameterConfiguration fetch(PathObject path) {
237 for(ParameterConfiguration config: this.getModifiedParameters()) {
238 if(config.getPath().equals(path.getPath())) {
239 return config ;
240 }
241 }
242 return null ;
243 }
244
245
246
247
248
249 public void generateConfigProperties(PrintWriter printer) {
250 List<ParameterConfiguration> list = new ArrayList<ParameterConfiguration>(this.getModifiedParameters());
251 Collections.sort(list, PathObject.COMPARATOR);
252 for(ParameterConfiguration config: list) {
253 boolean commentOut = false ;
254 if(config.getValue().equals(config.getDescription().getDefaultValue())) {
255 commentOut= true ;
256 }
257 printer.println(config.getDescription().toPropertyString(config.getValue(), commentOut));
258 }
259 }
260
261
262
263
264
265
266
267 public abstract void mergeProperties(Properties props) ;
268
269
270
271
272
273
274
275
276
277
278 public abstract void addParameterConfigurations(ParameterConfiguration... parameterConfigurations) ;
279
280
281
282
283
284
285 public abstract void removeParameterConfigurations(ParameterConfiguration... parameterConfigurations) ;
286
287
288
289
290
291
292 public abstract ComponentNode getModifiedConfigurationData() ;
293
294
295
296
297
298
299
300
301
302
303
304
305 public String getValueAt( String parameterPath, long date) {
306
307 if (parameterPath == null) return null;
308 ParameterPath path = ParameterPath.valueOf(parameterPath);
309 ParameterConfiguration parmConfig = this.fetch(path);
310 if (parmConfig == null) {
311 SubsystemDescription subsystemDescription = this.getSubsystemDescription();
312 ParameterDescription description = subsystemDescription.fetch(path);
313 if (description != null) {
314 return description.getParameterBase().getDefaultValue();
315 }
316 return null;
317 }
318 if (this.isBeenInEngineeringMode()) {
319 List<? extends ValueEvent> eventList = parmConfig.getValueEvents();
320 if (eventList == null) {
321 return parmConfig.value;
322 }
323
324 long begin = this.getStartTimestamp();
325 long end = this.getEndTimestamp();
326 String lastValue = parmConfig.value;
327 for (ValueEvent event : eventList) {
328 if (event.getTime() > date) {
329 return lastValue;
330 }
331 lastValue = event.getValue();
332 }
333 return null;
334 } else {
335 return parmConfig.value;
336 }
337 }
338
339
340
341 }