1 package org.lsst.ccs.config;
2
3 import javax.persistence.MappedSuperclass;
4 import java.io.Serializable;
5
6
7
8
9 @MappedSuperclass
10 public abstract class ParameterDescription implements Serializable, PathObject{
11
12 private
13
14
15
16 protected
17
18
19
20
21 private
22
23
24
25 private
26
27
28
29
30
31
32
33
34
35
36
37 private int level = PackCst.DESIGNER_LEVEL;
38
39
40
41
42
43 private boolean notModifiableAtRuntime;
44
45
46
47 protected ParameterDescription() {
48
49 }
50
51
52
53
54
55 protected ParameterDescription(ParameterBase parameterBase) {
56 this.parameterBase = parameterBase;
57 String codeName = parameterBase.getCodeName() ;
58 if(codeName == null) codeName = "" ;
59 this.simpleName = String.format("%s/%s/%s",
60 parameterBase.getComponentName(),
61 codeName,
62 parameterBase.getParameterName()) ;
63 }
64
65
66
67
68
69
70 protected ParameterDescription(ParameterDescription other) {
71
72 this.setId(other.getId());
73 this.parameterBase = other.parameterBase ;
74 this.description = other.description ;
75 this.simpleName = other.simpleName;
76 this.constraints = other.constraints ;
77 this.level = other.level ;
78 this.notModifiableAtRuntime = other.notModifiableAtRuntime ;
79 }
80
81
82
83
84 public abstract long getId() ;
85
86 protected abstract void setId(long id) ;
87
88 public boolean isReadOnly() {
89 return this.getId() != 0L ;
90 }
91
92
93 public ParameterBase getParameterBase() {
94 return parameterBase;
95 }
96
97 protected void setParameterBase(ParameterBase parameterBase) {
98 this.parameterBase = parameterBase;
99 }
100
101 public String getDescription() {
102 return description;
103 }
104
105
106
107
108
109 public void setDescription(String description) {
110 this.description = description ;
111 }
112
113 public String getSimpleName() {
114 return simpleName;
115 }
116
117 public void setSimpleName(String simpleName) {
118 if(isReadOnly()) {
119 throw new ImmutableStateException("simple name read Only") ;
120 }
121 this.simpleName = simpleName;
122 }
123
124 public String getConstraints() {
125 return constraints;
126 }
127
128 public void setConstraints(String constraints) {
129 if(isReadOnly()) {
130 throw new ImmutableStateException("Constraints read Only") ;
131 }
132 this.constraints = constraints;
133 }
134
135 public int getLevel() {
136 return level;
137 }
138
139 public void setLevel(int level) {
140 if(isReadOnly()) {
141 throw new ImmutableStateException("Level read Only") ;
142 }
143 this.level = level;
144 }
145
146 public boolean isNotModifiableAtRuntime() {
147 return notModifiableAtRuntime;
148 }
149
150 public void setNotModifiableAtRuntime(boolean notModifiableAtRuntime) {
151 this.notModifiableAtRuntime = notModifiableAtRuntime;
152 }
153
154
155
156
157
158
159
160
161 @Override
162 public boolean equals(Object o) {
163 if (this == o) return true;
164 if (!(o instanceof PathObject)) return false;
165
166 PathObject that = (PathObject) o;
167
168 if (!parameterBase.getPath().equals(that.getPath())) return false;
169
170 return true;
171 }
172
173 @Override
174 public int hashCode() {
175 return parameterBase.getPath().hashCode();
176 }
177
178 @Override
179 public String toString() {
180 return "{" + getId() +
181 ": base=" + parameterBase +
182 '}';
183 }
184
185
186 public String getComponentName() {
187 return parameterBase.getComponentName();
188 }
189
190 public String getCodeName() {
191 return parameterBase.getCodeName();
192 }
193
194 public String getParameterName() {
195 return parameterBase.getParameterName();
196 }
197
198 public String getDefaultValue() {
199 return parameterBase.getDefaultValue() ;
200 }
201
202 @Override
203 public ParameterPath getPath() {
204 return parameterBase.getPath();
205 }
206
207 public String getTypeName() {
208 return parameterBase.getTypeName();
209 }
210
211 public Object checkValue(String value) {
212 return Constraints.check(this.getParameterBase().getTypeName(),
213 value, this.getConstraints()) ;
214 }
215
216
217
218
219
220 public String toPropertyString(String value, boolean commentOutValue) {
221 if(value == null) {value ="" ;}
222 StringBuilder builder = new StringBuilder() ;
223 String pathName = parameterBase.getPath().toString() ;
224 builder.append("\n#******** ").append(pathName) ;
225 if(description != null) {
226 String withPounds = description.replaceAll("\n","\n#") ;
227 builder.append('\n').append("#** ").append(withPounds) ;
228 }
229 if(isNotModifiableAtRuntime()) {
230 builder.append("\n#** static parameter (not modifiable at runtime)") ;
231 }
232 if(constraints!= null && !"".equals(constraints.trim())) {
233 builder.append("\n#** constraints : ").append(constraints).append(" ; type : ")
234 .append(TypeInfos.get( parameterBase.getTypeName())) ;
235 } else {
236 builder.append("\n#** type : " )
237 .append(TypeInfos.get( parameterBase.getTypeName())) ;
238 }
239 String propName = this.simpleName;
240 if( propName== null || "".equals(propName.trim())) {
241 propName = pathName ;
242 } else {
243 propName = propName.trim().replaceAll(" ", "\\\\ ");
244 }
245 builder.append("\n#**\n#********\n\n").append(commentOutValue?"#":"").append(propName).append(" = ").append(value).append('\n') ;
246
247
248 return builder.toString() ;
249 }
250
251
252
253
254
255 public String toPropertyString() {
256 return toPropertyString(getDefaultValue(), true) ;
257 }
258 }