Refactored client class to accomodate new WS interface and reduce the complexity
[jabaws.git] / webservices / compbio / ws / client / MetadataHelper.java
1 package compbio.ws.client;\r
2 \r
3 import static compbio.ws.client.Constraints.pseparator;\r
4 \r
5 import java.util.ArrayList;\r
6 import java.util.List;\r
7 \r
8 import compbio.data.msa.Metadata;\r
9 import compbio.metadata.Limit;\r
10 import compbio.metadata.LimitsManager;\r
11 import compbio.metadata.Option;\r
12 import compbio.metadata.Preset;\r
13 import compbio.metadata.PresetManager;\r
14 import compbio.metadata.RunnerConfig;\r
15 import compbio.metadata.WrongParameterException;\r
16 \r
17 public class MetadataHelper {\r
18 \r
19         /**\r
20          * Returns a list of options supported by web service\r
21          * \r
22          * @param <T>\r
23          *            web service type\r
24          * @param msaws\r
25          *            web service proxy\r
26          * @return List of options supported by a web service\r
27          */\r
28         static <T> List<Option<T>> getParametersList(Metadata<T> msaws) {\r
29                 assert msaws != null;\r
30                 return msaws.getRunnerOptions().getArguments();\r
31         }\r
32 \r
33         /**\r
34          * Returns an objects from which the list of presets supported by web\r
35          * service <T> can be obtained\r
36          * \r
37          * @param <T>\r
38          *            web service type\r
39          * @param msaws\r
40          *            web service proxy\r
41          * @return PresetManager, object which operates on presets\r
42          */\r
43         static <T> PresetManager<T> getPresetList(Metadata<T> msaws) {\r
44                 assert msaws != null;\r
45                 PresetManager<T> presetman = msaws.getPresets();\r
46                 return presetman;\r
47         }\r
48 \r
49         /**\r
50          * Returns a list of limits supported by web service Each limit correspond\r
51          * to a particular preset.\r
52          * \r
53          * @param <T>\r
54          *            web service type\r
55          * @param msaws\r
56          *            web service proxy\r
57          * @return List of limits supported by a web service\r
58          */\r
59         static <T> List<Limit<T>> getLimits(Metadata<T> msaws) {\r
60                 assert msaws != null;\r
61                 LimitsManager<T> lmanger = msaws.getLimits();\r
62 \r
63                 return lmanger != null ? lmanger.getLimits() : null;\r
64         }\r
65 \r
66         /**\r
67          * Returns {@code Preset} by its name\r
68          * \r
69          * @see Preset\r
70          * @param <T>\r
71          * @param msaws\r
72          * @param presetName\r
73          * @return Return a Preset by its optionName\r
74          */\r
75         static <T> Preset<T> getPreset(Metadata<T> msaws, String presetName) {\r
76                 assert presetName != null;\r
77                 PresetManager<T> presets = MetadataHelper.getPresetList(msaws);\r
78                 if (presets == null) {\r
79                         System.out\r
80                                         .println("No presets are supported by the service! Ignoring -r directive!");\r
81                         return null;\r
82                 }\r
83                 Preset<T> pre = presets.getPresetByName(presetName);\r
84                 if (pre == null) {\r
85                         System.out.println("Cannot find preset: " + presetName\r
86                                         + " WARN: ignoring -r directive!");\r
87                 }\r
88                 return pre;\r
89         }\r
90 \r
91         /**\r
92          * Converts options supplied via parameters file into {@code Option} objects\r
93          * \r
94          * @param <T>\r
95          *            web service type\r
96          * @param params\r
97          * @param options\r
98          * @return List of Options of type T\r
99          */\r
100         static <T> List<Option<T>> processParameters(List<String> params,\r
101                         RunnerConfig<T> options) {\r
102                 List<Option<T>> chosenOptions = new ArrayList<Option<T>>();\r
103                 for (String param : params) {\r
104                         String oname = null;\r
105                         if (isParameter(param)) {\r
106                                 oname = getParamName(param);\r
107                         } else {\r
108                                 oname = param;\r
109                         }\r
110                         Option<T> o = options.getArgumentByOptionName(oname);\r
111                         if (o == null) {\r
112                                 System.out.println("WARN ignoring unsuppoted parameter: "\r
113                                                 + oname);\r
114                                 continue;\r
115                         }\r
116                         if (isParameter(param)) {\r
117                                 try {\r
118                                         o.setValue(getParamValue(param));\r
119                                 } catch (WrongParameterException e) {\r
120                                         System.out\r
121                                                         .println("Problem setting value for the parameter: "\r
122                                                                         + param);\r
123                                         e.printStackTrace();\r
124                                 }\r
125                         }\r
126                         chosenOptions.add(o);\r
127                 }\r
128                 return chosenOptions;\r
129         }\r
130 \r
131         static String getParamName(String fullName) {\r
132                 assert isParameter(fullName);\r
133                 return fullName.substring(0, fullName.indexOf(pseparator));\r
134         }\r
135 \r
136         static String getParamValue(String fullName) {\r
137                 assert isParameter(fullName);\r
138                 return fullName.substring(fullName.indexOf(pseparator) + 1);\r
139         }\r
140 \r
141         static boolean isParameter(String param) {\r
142                 return param.contains(pseparator);\r
143         }\r
144 \r
145 }\r