forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolParameterDescriptor.java
More file actions
267 lines (229 loc) · 8.52 KB
/
ToolParameterDescriptor.java
File metadata and controls
267 lines (229 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright (c) 2015 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.sequenceanalysis.pipeline;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.labkey.api.data.ConvertHelper;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* User: bimber
* Date: 6/13/2014
* Time: 1:28 PM
*/
public class ToolParameterDescriptor
{
private final CommandLineParam _ca;
private String _name;
private String _label;
private String _description;
private final String _fieldXtype;
protected JSONObject _additionalExtConfig;
private final Object _defaultValue;
public ToolParameterDescriptor(CommandLineParam ca, String name, String label, String description, String fieldXtype, @Nullable Object defaultValue, @Nullable JSONObject additionalExtConfig)
{
_ca = ca;
_name = name;
_label = label;
_description = description;
_fieldXtype = fieldXtype;
_defaultValue = defaultValue;
_additionalExtConfig = additionalExtConfig;
}
public static ToolParameterDescriptor create(String name, String label, String description, String fieldXtype, @Nullable JSONObject additionalExtConfig, @Nullable Object defaultValue)
{
return new ToolParameterDescriptor(null, name, label, description, fieldXtype, defaultValue, additionalExtConfig);
}
public static ToolParameterDescriptor createExpDataParam(String name, String label, String description, String fieldXtype, @Nullable JSONObject additionalExtConfig, @Nullable Object defaultValue)
{
return new ExpDataToolParameterDescriptor(null, name, label, description, fieldXtype, defaultValue, additionalExtConfig);
}
public static ToolParameterDescriptor createCommandLineParam(CommandLineParam ca, String name, String label, String description, String fieldXtype, @Nullable JSONObject additionalExtConfig, @Nullable Object defaultValue)
{
return new ToolParameterDescriptor(ca, name, label, description, fieldXtype, defaultValue, additionalExtConfig);
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public String getLabel()
{
return _label;
}
public void setLabel(String label)
{
_label = label;
}
public String getDescription()
{
return _description;
}
public void setDescription(String description)
{
_description = description;
}
public CommandLineParam getCommandLineParam()
{
return _ca;
}
public String getFieldXtype()
{
return _fieldXtype;
}
public JSONObject getAdditionalExtConfig()
{
return _additionalExtConfig;
}
public Object getDefaultValue()
{
return _defaultValue;
}
public JSONObject toJSON()
{
JSONObject ret = new JSONObject();
ret.put("name", _name);
ret.put("label", _label);
ret.put("description", _description);
ret.put("fieldXtype", _fieldXtype);
ret.put("additionalExtConfig", _additionalExtConfig);
ret.put("defaultValue", _defaultValue);
ret.put("commandLineParam", _ca == null ? null : _ca.getArgName());
return ret;
}
private String getJsonParamName(PipelineStepProvider<?> provider, int stepIdx)
{
String typeName = SequencePipelineService.get().getParamNameForStepType(provider.getStepClass());
return typeName + "." + provider.getName() + "." + getName() + (stepIdx == 0 ? "" : "." + stepIdx);
}
public String extractValue(PipelineJob job, PipelineStepProvider<?> provider, int stepIdx)
{
return extractValue(job, provider, stepIdx, String.class);
}
public List<Object> extractAllValues(PipelineJob job, PipelineStepProvider<?> provider)
{
List<Object> ret = new ArrayList<>();
String prefix = getJsonParamName(provider, 0);
JSONObject jobParams;
if (job instanceof HasJobParams)
{
jobParams = ((HasJobParams)job).getParameterJson();
}
else
{
jobParams = new JSONObject(job.getParameters());
}
for (Object key : jobParams.keySet())
{
if (key.toString().startsWith(prefix))
{
ret.add(jobParams.get(key.toString()));
}
}
return ret;
}
/**
* Returns the value that will actually be added to the command line. Can be overridden in subclasses
* to perform last minute transforms, such as converted a boolean (which is better in the UI as a checkbox)
* to 1 or 0
*/
public String extractValueForCommandLine(PipelineJob job, PipelineStepProvider<?> provider, int stepIdx) throws PipelineJobException
{
return extractValue(job, provider, stepIdx);
}
public <ParamType> ParamType extractValue(PipelineJob job, PipelineStepProvider<?> provider, int stepIdx, Class<ParamType> clazz)
{
return this.extractValue(job, provider, stepIdx, clazz, null);
}
public boolean hasValueInJson(PipelineJob job, PipelineStepProvider<?> provider, int stepIdx)
{
String key = getJsonParamName(provider, stepIdx);
JSONObject jobParams;
if (job instanceof HasJobParams)
{
jobParams = ((HasJobParams)job).getParameterJson();
}
else
{
jobParams = new JSONObject(job.getParameters());
}
return jobParams.has(key);
}
public <ParamType> ParamType extractValue(PipelineJob job, PipelineStepProvider<?> provider, int stepIdx, Class<ParamType> clazz, @Nullable ParamType defaultValue)
{
String key = getJsonParamName(provider, stepIdx);
JSONObject jobParams;
if (job instanceof HasJobParams)
{
jobParams = ((HasJobParams)job).getParameterJson();
}
else
{
jobParams = new JSONObject(job.getParameters());
}
if (jobParams.has(key) && !jobParams.isNull(key))
{
Object val = jobParams.get(key);
if (val == JSONObject.NULL) {
val = null;
}
return ConvertHelper.convert(val, clazz);
}
return defaultValue;
}
public interface CachableParam
{
void doCache(PipelineJob job, Object value, SequenceAnalysisJobSupport support) throws PipelineJobException;
}
public static class ExpDataToolParameterDescriptor extends ToolParameterDescriptor implements CachableParam
{
public ExpDataToolParameterDescriptor(CommandLineParam ca, String name, String label, String description, String fieldXtype, @Nullable Object defaultValue, @Nullable JSONObject additionalExtConfig)
{
super(ca, name, label, description, fieldXtype, defaultValue, additionalExtConfig);
}
@Override
public void doCache(PipelineJob job, Object value, SequenceAnalysisJobSupport support) throws PipelineJobException
{
if (value != null && !StringUtils.isEmpty(String.valueOf(value)))
{
job.getLogger().debug("caching expData: " + value);
try
{
Integer dataId = ConvertHelper.convert(value, Integer.class);
ExpData d = ExperimentService.get().getExpData(dataId);
if (d != null)
{
support.cacheExpData(d);
}
}
catch (ConversionException e)
{
throw new PipelineJobException("Unable to convert to integer: [" + value + "]", e);
}
}
}
}
}