JWS-17: fix problem with undefined URL for the furtherDetails tag
[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          * @throws MalformedURLException \r
48          */\r
49         static <T> List<Option<T>> getParametersList(Metadata<T> msaws, String host) throws MalformedURLException {\r
50                 assert msaws != null;\r
51                 RunnerConfig<T> config = msaws.getRunnerOptions();\r
52                 if (config == null) {\r
53                         return Collections.emptyList();\r
54                 }\r
55                 List<Option<T>> opts = config.getArguments();\r
56                 for (Option<T> o : opts) {\r
57                         o.setBasicURL(new URL(host + "/"));\r
58                 }\r
59                 return opts;\r
60         }\r
61 \r
62         static <T> List<Option<T>> getParametersList(Metadata<T> msaws) throws MalformedURLException {\r
63                 return getParametersList (msaws, "http://unknown.jabaws.server.ac.uk");\r
64         }\r
65         \r
66         \r
67         /**\r
68          * Returns an objects from which the list of presets supported by web\r
69          * service <T> can be obtained\r
70          * \r
71          * @param <T>\r
72          *            web service type\r
73          * @param msaws\r
74          *            web service proxy\r
75          * @return PresetManager, object which operates on presets\r
76          */\r
77         static <T> PresetManager<T> getPresetList(Metadata<T> msaws) {\r
78                 assert msaws != null;\r
79                 PresetManager<T> presetman = msaws.getPresets();\r
80                 return presetman;\r
81         }\r
82 \r
83         /**\r
84          * Returns a list of limits supported by web service Each limit correspond\r
85          * to a particular preset.\r
86          * \r
87          * @param <T>\r
88          *            web service type\r
89          * @param msaws\r
90          *            web service proxy\r
91          * @return List of limits supported by a web service\r
92          */\r
93         static <T> List<Limit<T>> getLimits(Metadata<T> msaws) {\r
94                 assert msaws != null;\r
95                 LimitsManager<T> lmanger = msaws.getLimits();\r
96 \r
97                 return lmanger != null ? lmanger.getLimits() : null;\r
98         }\r
99 \r
100         /**\r
101          * Returns {@code Preset} by its name\r
102          * \r
103          * @see Preset\r
104          * @param <T>\r
105          * @param msaws\r
106          * @param presetName\r
107          * @return Return a Preset by its optionName\r
108          */\r
109         static <T> Preset<T> getPreset(Metadata<T> msaws, String presetName) {\r
110                 assert presetName != null;\r
111                 PresetManager<T> presets = MetadataHelper.getPresetList(msaws);\r
112                 if (presets == null) {\r
113                         System.out.println("No presets are supported by the service! Ignoring -r directive!");\r
114                         return null;\r
115                 }\r
116                 Preset<T> pre = presets.getPresetByName(presetName);\r
117                 if (pre == null) {\r
118                         System.out.println("Cannot find preset: " + presetName + " WARN: ignoring -r directive!");\r
119                 }\r
120                 return pre;\r
121         }\r
122 \r
123         /**\r
124          * Converts options supplied via parameters file into {@code Option} objects\r
125          * \r
126          * @param <T>\r
127          *            web service type\r
128          * @param params\r
129          * @param options\r
130          * @return List of Options of type T\r
131          */\r
132         static <T> List<Option<T>> processParameters(List<String> params,\r
133                         RunnerConfig<T> options) {\r
134                 List<Option<T>> chosenOptions = new ArrayList<Option<T>>();\r
135                 for (String param : params) {\r
136                         String oname = null;\r
137                         if (isParameter(param)) {\r
138                                 oname = getParamName(param);\r
139                         } else {\r
140                                 oname = param;\r
141                         }\r
142                         Option<T> o = options.getArgumentByOptionName(oname);\r
143                         if (o == null) {\r
144                                 System.out.println("WARN ignoring unsuppoted parameter: " + oname);\r
145                                 continue;\r
146                         }\r
147                         if (isParameter(param)) {\r
148                                 try {\r
149                                         o.setValue(getParamValue(param));\r
150                                 } catch (WrongParameterException e) {\r
151                                         System.out.println("Problem setting value for the parameter: " + param);\r
152                                         e.printStackTrace();\r
153                                 }\r
154                         }\r
155                         chosenOptions.add(o);\r
156                 }\r
157                 return chosenOptions;\r
158         }\r
159 \r
160         static String getParamName(String fullName) {\r
161                 assert isParameter(fullName);\r
162                 return fullName.substring(0, fullName.indexOf(pseparator));\r
163         }\r
164 \r
165         static String getParamValue(String fullName) {\r
166                 assert isParameter(fullName);\r
167                 return fullName.substring(fullName.indexOf(pseparator) + 1);\r
168         }\r
169 \r
170         static boolean isParameter(String param) {\r
171                 return param.contains(pseparator);\r
172         }\r
173 \r
174 }\r