JWS-17 re-design behaviour of Option.toString
[jabaws.git] / webservices / compbio / ws / client / MetadataHelper.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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 package compbio.ws.client;\r
19 \r
20 import static compbio.ws.client.Constraints.pseparator;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.Collections;\r
24 import java.util.List;\r
25 import java.net.MalformedURLException;\r
26 import java.net.URL;\r
27 \r
28 import compbio.data.msa.Metadata;\r
29 import compbio.metadata.Limit;\r
30 import compbio.metadata.LimitsManager;\r
31 import compbio.metadata.Option;\r
32 import compbio.metadata.Preset;\r
33 import compbio.metadata.PresetManager;\r
34 import compbio.metadata.RunnerConfig;\r
35 import compbio.metadata.WrongParameterException;\r
36 \r
37 public class MetadataHelper {\r
38 \r
39         /**\r
40          * Returns a list of options supported by web service\r
41          * \r
42          * @param <T>\r
43          *            web service type\r
44          * @param msaws\r
45          *            web service proxy\r
46          * @return List of options supported by a web service\r
47          */\r
48         static <T> List<Option<T>> getParametersList(Metadata<T> msaws) {\r
49                 assert msaws != null;\r
50                 RunnerConfig<T> config = msaws.getRunnerOptions();\r
51                 if (config == null) {\r
52                         return Collections.emptyList();\r
53                 }\r
54                 return config.getArguments();\r
55         }\r
56 \r
57         /**\r
58          * Returns a list of options supported by web service\r
59          * \r
60          * @param <T>\r
61          *            web service type\r
62          * @param msaws\r
63          *            web service proxy\r
64          * @param host\r
65          *            the server URL, as a string\r
66          * @return List of options supported by a web service\r
67          */\r
68         static <T> List<Option<T>> getParametersList(Metadata<T> msaws, String host) throws MalformedURLException {\r
69                 assert msaws != null;\r
70                 RunnerConfig<T> config = msaws.getRunnerOptions();\r
71                 if (config == null) {\r
72                         return Collections.emptyList();\r
73                 }\r
74                 List<Option<T>> opts = config.getArguments();\r
75                 for (Option<T> o : opts) {\r
76                         o.setBasicURL(new URL(host + "/"));\r
77                 }\r
78                 return opts;\r
79         }\r
80 \r
81         /**\r
82          * Returns an objects from which the list of presets supported by web\r
83          * service <T> can be obtained\r
84          * \r
85          * @param <T>\r
86          *            web service type\r
87          * @param msaws\r
88          *            web service proxy\r
89          * @return PresetManager, object which operates on presets\r
90          */\r
91         static <T> PresetManager<T> getPresetList(Metadata<T> msaws) {\r
92                 assert msaws != null;\r
93                 PresetManager<T> presetman = msaws.getPresets();\r
94                 return presetman;\r
95         }\r
96 \r
97         /**\r
98          * Returns a list of limits supported by web service Each limit correspond\r
99          * to a particular preset.\r
100          * \r
101          * @param <T>\r
102          *            web service type\r
103          * @param msaws\r
104          *            web service proxy\r
105          * @return List of limits supported by a web service\r
106          */\r
107         static <T> List<Limit<T>> getLimits(Metadata<T> msaws) {\r
108                 assert msaws != null;\r
109                 LimitsManager<T> lmanger = msaws.getLimits();\r
110 \r
111                 return lmanger != null ? lmanger.getLimits() : null;\r
112         }\r
113 \r
114         /**\r
115          * Returns {@code Preset} by its name\r
116          * \r
117          * @see Preset\r
118          * @param <T>\r
119          * @param msaws\r
120          * @param presetName\r
121          * @return Return a Preset by its optionName\r
122          */\r
123         static <T> Preset<T> getPreset(Metadata<T> msaws, String presetName) {\r
124                 assert presetName != null;\r
125                 PresetManager<T> presets = MetadataHelper.getPresetList(msaws);\r
126                 if (presets == null) {\r
127                         System.out.println("No presets are supported by the service! Ignoring -r directive!");\r
128                         return null;\r
129                 }\r
130                 Preset<T> pre = presets.getPresetByName(presetName);\r
131                 if (pre == null) {\r
132                         System.out.println("Cannot find preset: " + presetName + " WARN: ignoring -r directive!");\r
133                 }\r
134                 return pre;\r
135         }\r
136 \r
137         /**\r
138          * Converts options supplied via parameters file into {@code Option} objects\r
139          * \r
140          * @param <T>\r
141          *            web service type\r
142          * @param params\r
143          * @param options\r
144          * @return List of Options of type T\r
145          */\r
146         static <T> List<Option<T>> processParameters(List<String> params,\r
147                         RunnerConfig<T> options) {\r
148                 List<Option<T>> chosenOptions = new ArrayList<Option<T>>();\r
149                 for (String param : params) {\r
150                         String oname = null;\r
151                         if (isParameter(param)) {\r
152                                 oname = getParamName(param);\r
153                         } else {\r
154                                 oname = param;\r
155                         }\r
156                         Option<T> o = options.getArgumentByOptionName(oname);\r
157                         if (o == null) {\r
158                                 System.out.println("WARN ignoring unsuppoted parameter: " + oname);\r
159                                 continue;\r
160                         }\r
161                         if (isParameter(param)) {\r
162                                 try {\r
163                                         o.setValue(getParamValue(param));\r
164                                 } catch (WrongParameterException e) {\r
165                                         System.out.println("Problem setting value for the parameter: " + param);\r
166                                         e.printStackTrace();\r
167                                 }\r
168                         }\r
169                         chosenOptions.add(o);\r
170                 }\r
171                 return chosenOptions;\r
172         }\r
173 \r
174         static String getParamName(String fullName) {\r
175                 assert isParameter(fullName);\r
176                 return fullName.substring(0, fullName.indexOf(pseparator));\r
177         }\r
178 \r
179         static String getParamValue(String fullName) {\r
180                 assert isParameter(fullName);\r
181                 return fullName.substring(fullName.indexOf(pseparator) + 1);\r
182         }\r
183 \r
184         static boolean isParameter(String param) {\r
185                 return param.contains(pseparator);\r
186         }\r
187 \r
188 }\r