Changes from JWS2 branch merged, mostly javadoc
[jabaws.git] / datamodel / compbio / metadata / PresetManager.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0  \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.metadata;\r
20 \r
21 import java.util.List;\r
22 \r
23 import javax.xml.bind.ValidationException;\r
24 import javax.xml.bind.annotation.XmlAccessType;\r
25 import javax.xml.bind.annotation.XmlAccessorType;\r
26 import javax.xml.bind.annotation.XmlElement;\r
27 import javax.xml.bind.annotation.XmlRootElement;\r
28 \r
29 import compbio.util.Util;\r
30 \r
31 /**\r
32  * Collection of presets and methods to manipulate them @see {@link Preset}\r
33  * \r
34  * @author pvtroshin\r
35  * \r
36  * @version 1.0 December 2009\r
37  * @param <T>\r
38  *            type of executable.\r
39  */\r
40 @XmlRootElement(name = "presets")\r
41 @XmlAccessorType(XmlAccessType.FIELD)\r
42 public class PresetManager<T> {\r
43 \r
44         @XmlElement(required = true)\r
45         String runnerClassName;\r
46 \r
47         @XmlElement(required = true)\r
48         List<Preset<T>> preset;\r
49 \r
50         public static final String LOCAL_ENGINE_LIMIT_PRESET = "# LocalEngineExecutionLimit #";\r
51 \r
52         public List<Preset<T>> getPresets() {\r
53                 return preset;\r
54         }\r
55 \r
56         public void setPresets(List<Preset<T>> presets) {\r
57                 this.preset = presets;\r
58         }\r
59 \r
60         /**\r
61          * \r
62          * @return fully qualified class name of type T\r
63          */\r
64         public String getRunnerClassName() {\r
65                 return runnerClassName;\r
66         }\r
67 \r
68         public void setRunnerClassName(String runnerClassName) {\r
69                 this.runnerClassName = runnerClassName;\r
70         }\r
71 \r
72         /**\r
73          * \r
74          * @param presetName\r
75          * @return preset by its name, null if no preset found\r
76          */\r
77         public Preset<T> getPresetByName(String presetName) {\r
78                 for (Preset<T> p : preset) {\r
79                         if (p.getName().equalsIgnoreCase(presetName)) {\r
80                                 return p;\r
81                         }\r
82                 }\r
83                 return null;\r
84         }\r
85 \r
86         boolean isComposite(String value) {\r
87                 assert value != null;\r
88                 return value.contains(" ");\r
89         }\r
90 \r
91         boolean containsValue(List<String> values, String value) {\r
92                 assert !Util.isEmpty(value);\r
93                 for (String v : values) {\r
94                         if (v.equals(value)) {\r
95                                 return true;\r
96                         }\r
97                 }\r
98                 return false;\r
99         }\r
100 \r
101         boolean isNumeric(String value) {\r
102                 assert value != null;\r
103                 try {\r
104                         Double.parseDouble(value);\r
105                         return true;\r
106                 } catch (NumberFormatException e) {\r
107                         // ignore\r
108                         return false;\r
109                 }\r
110         }\r
111 \r
112         /**\r
113          * Checks whether preset option and parameter are defined in RunnerConfig\r
114          * object.\r
115          * \r
116          * TODO handle parameters with values properly!\r
117          * \r
118          * @throws ValidationException\r
119          *             if preset is found to be invalid.\r
120          * \r
121          */\r
122         public void validate(RunnerConfig<T> options) throws ValidationException {\r
123                 for (Preset<T> p : preset) {\r
124                         if (Util.isEmpty(p.name)) {\r
125                                 throw new ValidationException("Preset name must not be empty!");\r
126                         }\r
127                         List<String> optionNames = p.getOptions();\r
128                         if (optionNames == null || optionNames.size() == 0) {\r
129                                 throw new ValidationException(\r
130                                                 "At lease one option must be defined for a preset!");\r
131                         }\r
132                         for (String oName : optionNames) {\r
133                                 if (isComposite(oName)) {\r
134                                         String name = oName.split(" ")[0];\r
135                                         Argument<T> arg = getArgument(options, name);\r
136                                         String value = oName.split(" ")[1];\r
137                                         // Ignore numeric values\r
138                                         if (!isNumeric(value)\r
139                                                         && !containsValue(arg.getPossibleValues(), value)) {\r
140                                                 throw new ValidationException("Value " + value\r
141                                                                 + " is not found in the option " + name);\r
142                                         }\r
143                                 } else {\r
144                                         getArgument(options, oName);\r
145                                 }\r
146                         }\r
147                 }\r
148         }\r
149 \r
150         Argument<T> getArgument(RunnerConfig<T> options, String optionName)\r
151                         throws ValidationException {\r
152                 Argument<T> arg = options.getArgumentByOptionName(optionName);\r
153                 if (arg == null) {\r
154                         throw new ValidationException(\r
155                                         "Option "\r
156                                                         + optionName\r
157                                                         + " is not found in the option list (<RunnerName>Parameter.xml file)");\r
158                 }\r
159                 return arg;\r
160         }\r
161 \r
162         @Override\r
163         public String toString() {\r
164                 String value = "Runner: " + this.runnerClassName;\r
165                 for (Preset<T> p : preset) {\r
166                         value += p.toString() + "\n";\r
167                 }\r
168                 return value;\r
169         }\r
170 \r
171 }\r