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