View Javadoc

1   package org.lsst.ccs.config;
2   
3   import javax.persistence.MappedSuperclass;
4   import java.io.Serializable;
5   
6   /**
7    * The base class for all Parameter descriptions
8    */
9   @MappedSuperclass
10  public abstract class ParameterDescription implements Serializable, PathObject{
11  
12      private  /*@NotNull*/ ParameterBase parameterBase ;
13      /**
14       * possible details. Immutable in Ghost data
15       */
16      protected /*@Nullable*/ String description ;
17      /**
18       * a user-friendly name: it's up to the config "designer" to make sure it's a unique name
19       * otherwise the "pathName" of the parameterBase will be used. Immutable in ghost Data
20       */
21      private /*@NotNull*/ String simpleName ;
22      /**
23       * Added constraints to the type, may be null. Immutable in ghost data
24       */
25      private /*@Nullable*/ String constraints ;
26  
27      //TODO:  startDate, endDate, user, tag are normally part of SubsystemDescription do we duplicate?
28      /**
29       * used for configuration "rights"; a designer may have
30       * the right to modify a parameter and an end-user not.
31       * when automatically generated from a subsytemdescription
32       * the level of all modifiable parameters is set to 10,
33       * a parameter which is modifiable by an end-user is  less than 5.
34       * Immutable in Ghost Data.
35       *
36       */
37      private int level = PackCst.DESIGNER_LEVEL;
38  
39      /**
40       * "static" parameter: it can only be set for a constructor but cannot be
41       * changed dynamically at runtime.
42       */
43      private boolean notModifiableAtRuntime;
44  
45      ////////////////////////////// CONSTRUCTORS
46  
47      protected ParameterDescription() {
48  
49      }
50  
51      /**
52       * builds a default Description out of a ParameterBase which is described in the original setup.
53       * @param parameterBase
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       * copy constructor.
67       * Beware: the id is copied so do not use to make a new Parameterdescription that is to be registered later.
68       * @param other
69       */
70      protected ParameterDescription(ParameterDescription other) {
71          //this(other.getParameterBase()); no will not work!
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      //////////////////////////// ACCESSORS/MUTATORS
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      * this data is not essential to the logic so it is modifiable anyway.
107      * @param description
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     //////////////// IDENT METHODS
154     // TODO: inclusion of id?
155 
156     /**
157      * equals is special: it works with any Path Object!
158      * @param o
159      * @return
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      * creates a String to be included in a .preperties file
217      * @param value default value to be included in the text
218      * @return
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         //System.out.println(" -> -> " +builder );
248         return builder.toString() ;
249     }
250 
251     /**
252      * default value is included in the text that describes the parameter as a property
253      * @return
254      */
255     public String toPropertyString() {
256         return toPropertyString(getDefaultValue(), true) ;
257     }
258 }