View Javadoc

1   package org.lsst.ccs.config;
2   
3   import javax.persistence.*;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   /**
8    * Actual parameter configuration.
9    * TODO: when setting up the value this data will be checked for constraints and type against
10   * its ParameterDescription.
11   * <P>
12   *  All values are represented as String.
13   *  The type in ParameterDescription should have a static <TT>valueOf(String val)</TT>
14   *  method. This is the case for the standard "wrapper" classes.
15   *  Other non-standard classes may be provided with the same feature (including classes that
16   *  may represent "structures" such as arrays, lists or maps).
17   *  If the value is generated then it will be checked with the constraints type (to be specified).
18   *
19   * @author bamade
20   */
21  // Date: 11/04/12
22  
23      @Entity
24      //@Immutable no more
25  //@Table(name="ParmConfiguration")
26  class AParameterConfiguration extends ParameterConfiguration implements Cloneable{
27      private static final long serialVersionUID = 8134633715625475333L;
28      @Id
29      @GeneratedValue
30      private long id ; //generated
31      /*
32      Beware: constraint: configuration should be deleted BEFORE any pointed description
33      but in that case
34       */
35      //TODO: EAGER not obvious
36      @ManyToOne (fetch=FetchType.EAGER)
37      //@NaturalId not true! ther can be copy of the same
38      private /*@NotNull*/ AParameterDescription parameterDescription ;
39  
40      private boolean copy ;
41  
42      @OneToMany (cascade = CascadeType.ALL, fetch=FetchType.EAGER)
43      List<AValueEvent> valueEvents ;
44  
45      ////////////////////////////// CONSTRUCTORS
46  
47  
48       AParameterConfiguration() {
49  
50       }
51  
52      /**
53       * create an empty parameter configuration whose value is to be modified
54       * @param parameterDescription
55       */
56      public AParameterConfiguration(AParameterDescription parameterDescription) {
57          super(parameterDescription.getParameterBase().getDefaultValue()) ;
58          this.parameterDescription = parameterDescription;
59      }
60  
61      /**
62       *
63       * @param parameterDescription
64       * @param value
65       * @throws IllegalArgumentException if value is not coherent with type and constraints
66       */
67      public AParameterConfiguration(AParameterDescription parameterDescription, String value) {
68          super();
69          this.parameterDescription = parameterDescription;
70          modifyValue(value);
71      }
72  
73      public static AParameterConfiguration copyWithoutEvents(ParameterConfiguration other) {
74          // if other is Past -> description is ghost
75          // if ghost still valid -> get RealDescription
76          //       else Exception no valid description
77         throw new UnsupportedOperationException("copy without events (general)")  ;
78      }
79      //TODO: copy from any parameterConfiguration (very complex due to ParameterDescription status)
80      public static AParameterConfiguration copyWithoutEvents(AParameterConfiguration another) {
81          //incorrect if another is Ghost!
82          AParameterDescription descr = another.getParameterDescription() ;
83          AParameterConfiguration res = new AParameterConfiguration(descr, another.getValue())  ;
84          return res ;
85      }
86  
87      ////////////////////////////// ACCESSORS/MUTATORS
88      @Override
89      public long getId() {
90          return id;
91      }
92  
93      @Override
94      protected void setId(long id) {
95          this.id = id ;
96      }
97  
98      @Override
99      public ParameterDescription getDescription() {
100         return parameterDescription;
101     }
102 
103     @Override
104     public List<? extends ValueEvent> getValueEvents() {
105         return this.valueEvents ;
106     }
107 
108     void setValueEvents(List<AValueEvent> valueEventsList) {
109         this.valueEvents = valueEventsList ;
110     }
111 
112     /**
113      * this method should be called from a ConfigProfile where property "beenInEngineeringMode" is set.
114      * this is not checked in the body of this current method.
115      * @param event a newly created ValueEvent
116      */
117     //@Override
118     public void addValueEvent(ValueEvent event) {
119         if(! (event instanceof AValueEvent)) {
120             throw new IllegalArgumentException("trying to add a deprecated ValueEvent");
121         }
122         AValueEvent anEvent = (AValueEvent) event ;
123         if(valueEvents == null) {
124           valueEvents = new ArrayList<AValueEvent>()   ;
125         }
126         if(checkValue(event.getValue()) != null) {
127             valueEvents.add(anEvent) ;
128         } else {
129             throw new IllegalArgumentException("non compatible value :" + value) ;
130         }
131     }
132 
133 
134     AParameterDescription getParameterDescription() {
135         return parameterDescription;
136     }
137 
138      void setParameterDescription(AParameterDescription parameterDescription) {
139         this.parameterDescription = parameterDescription;
140     }
141 
142     public boolean isCopy() {
143         return copy;
144     }
145 
146      void setCopy(boolean copy) {
147         this.copy = copy;
148     }
149 
150     public AParameterConfiguration clone() {
151         AParameterConfiguration res = null ;
152         try {
153             res = (AParameterConfiguration) super.clone();
154         } catch (CloneNotSupportedException e) {/*IGNORE*/ }
155         return res ;
156     }
157 }