View Javadoc

1   package org.lsst.ccs.config;
2   
3   import java.io.InputStream;
4   import java.io.Serializable;
5   import java.util.Collection;
6   import java.util.List;
7   import java.util.Scanner;
8   import java.util.Set;
9   
10  /**
11   * A set of static methods to be used on Configuration service client's side : they are hiding some implementation details to the "outside"
12   * world.
13   * To be used in conjunction with <TT>ConfigurationFacade</TT> (but
14   * these codes may be called locally without referring to a server)
15   * @author bamade
16   */
17  // Date: 05/06/12
18  
19  public class Factories {
20      ////////// creation of "raw" subsystemDescriptions
21      //TODO: normally the creation of all descriptions should be made from the text file
22      // so no more public factories for these ?
23  
24      /**
25       * Creates a new SubsystemDescription without any ParameterDescription creation. The object is not registered
26       * in the database since it is to be populated by <TT>ParameterDescription</TT> objects.
27       * <p/>
28       * To populate the descriptions get the base descriptions from the Subsystemdescription by calling a version
29       * of <TT>getBaseParameters</TT> or <TT>getParameterDescriptions</TT> and generate the descriptions from these bases.
30       *
31       * @param subsystemName
32       * @param tag
33       * @param user
34       * @param version
35       * @param configurationData
36       * @param dataFlavour
37       * @return
38       */
39      public static SubsystemDescription createRawSubsystemDescription(String subsystemName, String tag, String user, String version,
40                                                                Serializable configurationData, DataFlavour dataFlavour) {
41          return new ASubsystemDescription(subsystemName, tag, user, version, configurationData, dataFlavour);
42      }
43  
44      /**
45       * creates a "raw" subsystem description by reading a groovy text from file.
46       * @param subsystemName
47       * @param tag
48       * @param user
49       * @param version
50       * @param inputStream
51       * @param charSetName
52       * @return
53       */
54      public static SubsystemDescription createRawSubsystemDescription(String subsystemName, String tag, String user, String version,
55                                                                       InputStream inputStream, String charSetName) {
56  
57          // horrible hack: one liner for creating a String!
58          String text = new Scanner(inputStream,charSetName).useDelimiter("\\A").next() ;
59          return new ASubsystemDescription(subsystemName, tag, user, version,text, DataFlavour.TREE_FROM_SOURCE) ;
60      }
61  
62      /**
63       * creates a new SubsystemDescription populated with "empty" <TT>ParameterDescriptions</TT>.
64       * The object is not in the database since the list of descriptions should be modified first.
65       * <P>
66       *     TODO: this method will be modified to populate with parameterDescriptions that are not necessarily empty.
67       *
68       * @param subsystemName
69       * @param tag
70       * @param user
71       * @param version
72       * @param configurationData
73       * @param dataFlavour
74       * @param filter
75       * @return
76       */
77      public static SubsystemDescription createSubsystemDescription(String subsystemName, String tag, String user, String version,
78                                                             Serializable configurationData, DataFlavour dataFlavour, ParameterFilter filter) {
79          ASubsystemDescription res = new ASubsystemDescription(subsystemName, tag, user, version, configurationData, dataFlavour);
80          Collection<ParameterBase> bases = res.getBaseParameters(filter);
81          for (ParameterBase base : bases) {
82              AParameterDescription desc = new AParameterDescription(base, "", "", "", 10);
83              res.addParameterDescriptions(desc);
84          }
85          return res;
86      }
87  
88  
89      /**
90       * creates a new SubsystemDescription from another. The other one could be active of deprecated data:
91       * this method is meant to create another subsystemdescription were only the ParameterDescription are to be changed.
92       * not that the list of ParameterDescription may be populated (if the model have some) and then that this set
93       * is to be modified (the resulting object is not yet persisted in the database).
94       *
95       * @param desc
96       * @return
97       */
98      public static SubsystemDescription createSubsystemDescriptionCopy(SubsystemDescription desc) {
99          return new ASubsystemDescription(desc);
100     }
101 
102 
103     /**
104      * factory method to create a new ParameterDescription. To be used by tools such as GUI
105      *
106      * @param parameterBase
107      * @param description
108      * @param simpleName
109      * @param constraints
110      * @param level
111      * @return
112      */
113     public static ParameterDescription createParameterDescription(ParameterBase parameterBase, String description, String simpleName, String constraints, int level) {
114         return new AParameterDescription(parameterBase, description, simpleName, constraints, level);
115     }
116 
117     /**
118      * factory method to create a new ParameterDescription from another one.
119      *
120      * @param other
121      * @return
122      */
123     public static ParameterDescription createParameterDescription(ParameterDescription other) {
124         return new AParameterDescription(other);
125     }
126 
127     /**
128      * tries to copy <TT>ParameterDescription</TT> from a model to a new subsystem description.
129      * each <TT>ParameterDescription</TT> is checked against the configData (if not compatible it is rejected
130      * and a Listener is notified).
131      * <p/>
132      * NOT IMPLEMENTED YET
133      * </P>
134      *
135      * @param newDescription   a subsystem description <B>without</B> any <TT>ParameterDescription</TT>
136      * @param model
137      * @param mismatchListener optional code that will be warned when inconsistencies occur
138      */
139     public static void tryCopyParameters(SubsystemDescription newDescription, SubsystemDescription model,
140                                   DescriptionMismatchListener mismatchListener) {
141         // generate a bogus ParameterBase list for newDescription
142         // test if type is the same ?(default value may be changed)
143         // new defaultValue tested against "constraints"
144         // level set to old level
145         System.err.println("Try copy Parameters not implemented yet");
146     }
147 
148 
149     ////////////// creation of "raw" configProfiles
150 
151     /**
152      * Creates a new ConfigProfile that has no ParameterConfiguration (and so is not ready
153      * to be persisted).
154      *
155      * @param subsystemDesc should  be read from the database
156      * @param name
157      * @param tag
158      * @param userName
159      * @param level
160      * @return
161      * @throws IllegalArgumentException if subsystemdescription not in database
162      */
163     public static ConfigProfile createRawConfigProfile(SubsystemDescription subsystemDesc, String name, String tag, String userName, int level) {
164         if (!(subsystemDesc instanceof ASubsystemDescription)) {
165             throw new IllegalArgumentException("deprecated Description");
166         }
167         return createRawConfigProfile((ASubsystemDescription) subsystemDesc, name, tag, userName, level);
168     }
169 
170     static AConfigProfile createRawConfigProfile(ASubsystemDescription subsystemDesc, String name, String tag, String userName, int level) {
171         //TODO: check for preconditions
172         return new AConfigProfile(subsystemDesc, name, tag, userName, level);
173     }
174 
175     /// possible parameters is a method of A configProfile
176 
177     /**
178      * Creates a new ConfigProfile with ParameterConfiguration where values
179      * are just copied from the Description default value.
180      *
181      * @param subsystemDesc should be already in the database
182      * @param name
183      * @param tag
184      * @param userName
185      * @param level
186      * @return
187      * @throws IllegalArgumentException if subsystemdescription not in database
188      */
189     public static ConfigProfile createConfigProfile(SubsystemDescription subsystemDesc, String name, String tag, String userName, int level) {
190         if (!(subsystemDesc instanceof ASubsystemDescription)) {
191             throw new IllegalArgumentException("deprecated Description");
192         }
193 
194         return createConfigProfile((ASubsystemDescription) subsystemDesc, name, tag, userName, level);
195     }
196 
197     static AConfigProfile createConfigProfile(ASubsystemDescription subsystemDesc, String name, String tag, String userName, int level) {
198         //TODO: check for preconditions
199         AConfigProfile res = new AConfigProfile(subsystemDesc, name, tag, userName, level);
200         Set<AParameterDescription> descriptionSet = res.getPossibleParameters();
201         for (AParameterDescription parmDesc : descriptionSet) {
202             AParameterConfiguration conf = new AParameterConfiguration(parmDesc);
203             res.addParameterConfigurations(conf);
204         }
205         return res;
206     }
207 
208     /**
209      * to be used to create a copy of configuration with different name , tag, etc.
210      * This is used mostly to go back and forth in Engineering mode: for instance start
211      * an Engineering mode with modification of parameters "on the fly"
212      * <p/>
213      * when this is used to create an "engineering mode" profile it is mandatory that the profile
214      * to be copied is an active one (not a deprecated one)
215      *
216      * @param toBeCopied    for the moment should be an "active" ConfigProfile (not a deprecated one)
217      * @param newName
218      * @param newTag
219      * @param newUserName
220      * @param newLevel
221      * @param toEngineering
222      * @return
223      */
224     public static ConfigProfile copyProfile(ConfigProfile toBeCopied, String newName, String newTag,
225                                      String newUserName, int newLevel, boolean toEngineering) {
226         if (toBeCopied instanceof AConfigProfile) {
227             return new AConfigProfile((AConfigProfile) toBeCopied, newName, newTag,
228                     newUserName, newLevel, toEngineering);
229         }
230         if (toEngineering) {
231             throw new IllegalArgumentException("cannot create an engineering profile from a deprecated one");
232         }
233         throw new UnsupportedOperationException("copy with deprecated profile not yet implemented");
234     }
235 
236     /**
237      * factory method to create a ParameterConfiguration object
238      *
239      * @param description
240      * @param value
241      * @return
242      */
243     public static ParameterConfiguration createParameterConfiguration(ParameterDescription description, String value) {
244         if (!(description instanceof AParameterDescription)) {
245             throw new IllegalArgumentException("deprecated description");
246         }
247         if(description.getId() == 0L) {
248             throw new IllegalArgumentException("ParameterDescription not registered in base" + description) ;
249         }
250         return new AParameterConfiguration((AParameterDescription) description, value);
251     }
252 
253     /**
254      * factory method to create a ParameterConfiguration object
255      *
256      * @param description
257      * @return
258      */
259     public static ParameterConfiguration createParameterConfiguration(ParameterDescription description) {
260         if (!(description instanceof AParameterDescription)) {
261             throw new IllegalArgumentException("deprecated description");
262         }
263         if(description.getId() == 0L) {
264             throw new IllegalArgumentException("ParameterDescription not registered in base" + description) ;
265         }
266         return new AParameterConfiguration((AParameterDescription) description);
267     }
268 
269     /**
270      * tries to create a new ConfigProfile from an old one. The new one is matched to an active
271      * subsystemDescription
272      * <p/>
273      * NOT IMPLEMENTED YET
274      * </P>
275      *
276      * @param oldProfile
277      * @param newDescription   an active subsystemDescription already registered in the databse
278      * @param mismatchListener
279      * @return
280      */
281     public static ConfigProfile repair(ConfigProfile oldProfile, SubsystemDescription newDescription,
282                                 ConfigurationMismatchListener mismatchListener) {
283         return null;
284     }
285 
286 
287 }