Refactoring of all SequenceAnnotation web services
[jabaws.git] / webservices / compbio / ws / server / SAService.java
1 package compbio.ws.server;\r
2 \r
3 import java.util.List;\r
4 \r
5 import org.apache.log4j.Logger;\r
6 \r
7 import compbio.data.sequence.FastaSequence;\r
8 import compbio.data.sequence.ScoreManager;\r
9 import compbio.engine.Configurator;\r
10 import compbio.engine.client.ConfiguredExecutable;\r
11 import compbio.engine.client.Executable;\r
12 import compbio.engine.client.SkeletalExecutable;\r
13 import compbio.metadata.ChunkHolder;\r
14 import compbio.metadata.JobStatus;\r
15 import compbio.metadata.JobSubmissionException;\r
16 import compbio.metadata.Limit;\r
17 import compbio.metadata.LimitExceededException;\r
18 import compbio.metadata.LimitsManager;\r
19 import compbio.metadata.Option;\r
20 import compbio.metadata.Preset;\r
21 import compbio.metadata.PresetManager;\r
22 import compbio.metadata.ResultNotAvailableException;\r
23 import compbio.metadata.RunnerConfig;\r
24 import compbio.metadata.UnsupportedRuntimeException;\r
25 import compbio.metadata.WrongParameterException;\r
26 import compbio.runner.Util;\r
27 import compbio.runner.conservation.AACon;\r
28 \r
29 /**\r
30  * Common methods for all SequenceAnnotation web services\r
31  * \r
32  * @author pvtroshin\r
33  * \r
34  * @param <T>\r
35  * \r
36  * @version 1.0 June 2011\r
37  * @since 2.0\r
38  */\r
39 public abstract class SAService<T> {\r
40 \r
41         private final RunnerConfig<T> aaconOptions;\r
42         private final PresetManager<T> aaconPresets;\r
43         private final LimitsManager<T> limitMan;\r
44         private SkeletalExecutable<T> exec;\r
45         private final Logger log;\r
46 \r
47         /*\r
48          * FIXME - instances of the Runner (?) and their types should be defined in\r
49          * Executable IF\r
50          */\r
51         SAService(SkeletalExecutable<T> exec, Logger log) {\r
52                 assert log != null;\r
53                 assert exec != null;\r
54                 this.log = log;\r
55                 this.exec = exec;\r
56                 this.limitMan = compbio.engine.client.Util.getLimits(exec.getType());\r
57                 this.aaconOptions = Util\r
58                                 .getSupportedOptions((Class<? extends Executable<T>>) exec\r
59                                                 .getType());\r
60                 this.aaconPresets = Util\r
61                                 .getPresets((Class<? extends Executable<T>>) exec.getType());\r
62         }\r
63 \r
64         ConfiguredExecutable<T> init(List<FastaSequence> sequences)\r
65                         throws JobSubmissionException {\r
66                 // FIXME\r
67                 try {\r
68                         exec = (SkeletalExecutable<T>) exec.getType().newInstance();\r
69                 } catch (InstantiationException e) {\r
70                         log.error(e.getLocalizedMessage(), e);\r
71                         throw new JobSubmissionException(e.getLocalizedMessage(), e);\r
72                 } catch (IllegalAccessException e) {\r
73                         log.error(e.getLocalizedMessage(), e);\r
74                         throw new JobSubmissionException(e.getLocalizedMessage(), e);\r
75                 }\r
76                 exec.setInput(SkeletalExecutable.INPUT).setOutput(\r
77                                 SkeletalExecutable.OUTPUT);\r
78                 return Configurator.configureExecutable(exec, sequences);\r
79         }\r
80 \r
81         public ScoreManager getAnnotation(String jobId)\r
82                         throws ResultNotAvailableException {\r
83                 return WSUtil.getAnnotation(jobId, log);\r
84         }\r
85 \r
86         public boolean cancelJob(String jobId) {\r
87                 WSUtil.validateJobId(jobId);\r
88                 return WSUtil.cancelJob(jobId);\r
89         }\r
90 \r
91         public JobStatus getJobStatus(String jobId) {\r
92                 WSUtil.validateJobId(jobId);\r
93                 return WSUtil.getJobStatus(jobId);\r
94         }\r
95 \r
96         public Limit<T> getLimit(String presetName) {\r
97                 if (limitMan == null) {\r
98                         // Limit is not defined\r
99                         return null;\r
100                 }\r
101                 return limitMan.getLimitByName(presetName);\r
102         }\r
103 \r
104         public LimitsManager<T> getLimits() {\r
105                 return limitMan;\r
106         }\r
107 \r
108         public PresetManager<T> getPresets() {\r
109                 return aaconPresets;\r
110         }\r
111 \r
112         public RunnerConfig<T> getRunnerOptions() {\r
113                 return aaconOptions;\r
114         }\r
115 \r
116         public String analize(List<FastaSequence> sequences)\r
117                         throws UnsupportedRuntimeException, LimitExceededException,\r
118                         JobSubmissionException {\r
119                 WSUtil.validateFastaInput(sequences);\r
120                 ConfiguredExecutable<T> confIUPred = init(sequences);\r
121                 return WSUtil.analize(sequences, confIUPred, log, "analize",\r
122                                 getLimit(""));\r
123         }\r
124 \r
125         public String customAnalize(List<FastaSequence> sequences,\r
126                         List<Option<T>> options) throws UnsupportedRuntimeException,\r
127                         LimitExceededException, JobSubmissionException,\r
128                         WrongParameterException {\r
129                 WSUtil.validateAAConInput(sequences);\r
130                 ConfiguredExecutable<T> confAACon = init(sequences);\r
131                 // Could not do that! Space separated values\r
132                 // will all be treated as keys! thus duplicates removed\r
133                 // String params = cbuilder.getCommand();\r
134                 List<String> params = WSUtil.getCommands(options,\r
135                                 AACon.KEY_VALUE_SEPARATOR);\r
136                 confAACon.addParameters(params);\r
137                 return WSUtil.analize(sequences, confAACon, log, "customAnalize",\r
138                                 getLimit(""));\r
139         }\r
140 \r
141         public String presetAnalize(List<FastaSequence> sequences, Preset<T> preset)\r
142                         throws UnsupportedRuntimeException, LimitExceededException,\r
143                         JobSubmissionException, WrongParameterException {\r
144                 WSUtil.validateAAConInput(sequences);\r
145                 if (preset == null) {\r
146                         throw new WrongParameterException("Preset must be provided!");\r
147                 }\r
148                 ConfiguredExecutable<T> confAAcon = init(sequences);\r
149                 confAAcon.addParameters(preset.getOptions());\r
150                 Limit<T> limit = getLimit(preset.getName());\r
151                 return WSUtil\r
152                                 .analize(sequences, confAAcon, log, "presetAnalize", limit);\r
153         }\r
154 \r
155         /**\r
156          * Assume statistics is not supported\r
157          * \r
158          * @param jobId\r
159          * @param position\r
160          * @return\r
161          */\r
162         public ChunkHolder pullExecStatistics(String jobId, long position) {\r
163                 // Execution stat is not supported\r
164                 return new ChunkHolder("", -1);\r
165         }\r
166 \r
167 }\r