Skip to content

Commit 961d667

Browse files
committed
Remove extension of BaseRowsCommand
1 parent 80c0352 commit 961d667

File tree

2 files changed

+109
-65
lines changed

2 files changed

+109
-65
lines changed

src/org/labkey/remoteapi/query/BaseRowsCommand.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -239,48 +239,64 @@ public JSONObject getJsonObject()
239239
json.put("extraContext", getExtraContext());
240240
if (getAuditBehavior() != null)
241241
json.put("auditBehavior", getAuditBehavior());
242-
if (getAuditUserComment() != null)
243-
json.put("auditUserComment", getAuditUserComment());
244242

243+
stringToJson(json, "auditUserComment", getAuditUserComment());
244+
json.put("rows", rowsToJson(getRows()));
245+
246+
return json;
247+
}
248+
249+
@Override
250+
protected RowsResponse createResponse(String text, int status, String contentType, JSONObject json)
251+
{
252+
return new RowsResponse(text, status, contentType, json, this);
253+
}
254+
255+
static void stringToJson(JSONObject json, String prop, String value)
256+
{
257+
if (value != null && !value.isEmpty())
258+
{
259+
String trimmed = value.trim();
260+
if (!trimmed.isEmpty())
261+
json.put(prop, trimmed);
262+
}
263+
}
264+
265+
static JSONArray rowsToJson(List<Map<String, Object>> rows)
266+
{
245267
//unfortunately, JSON simple is so simple that it doesn't
246268
//encode maps into JSON objects on the fly,
247269
//nor dates into property JSON format
248270
JSONArray jsonRows = new JSONArray();
249-
if(null != getRows())
271+
if (null != rows && !rows.isEmpty())
250272
{
251-
SimpleDateFormat fmt = new SimpleDateFormat("d MMM yyyy HH:mm:ss Z");
252-
for(Map<String, Object> row : getRows())
273+
SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy HH:mm:ss Z");
274+
for (Map<String, Object> row : rows)
253275
{
254-
JSONObject jsonRow;
255-
if (row instanceof JSONObject jo) //optimization
276+
if (row instanceof JSONObject jo)
256277
{
257-
jsonRow = jo;
278+
jsonRows.put(jo);
258279
}
259280
else
260281
{
261-
jsonRow = new JSONObject();
262-
//row map entries must be scalar values (no embedded maps or arrays)
263-
for(Map.Entry<String, Object> entry : row.entrySet())
282+
JSONObject jsonRow = new JSONObject();
283+
// Row map entries must be scalar values (no embedded maps or arrays)
284+
for (Map.Entry<String, Object> entry : row.entrySet())
264285
{
265286
Object value = entry.getValue();
266287

267-
if(value instanceof Date)
268-
value = fmt.format((Date)value);
288+
if (value instanceof Date dateValue)
289+
value = dateFormat.format(dateValue);
269290

270291
// JSONObject.wrap allows us to save 'null' values.
271292
jsonRow.put(entry.getKey(), JSONObject.wrap(value));
272293
}
294+
295+
jsonRows.put(jsonRow);
273296
}
274-
jsonRows.put(jsonRow);
275297
}
276298
}
277-
json.put("rows", jsonRows);
278-
return json;
279-
}
280299

281-
@Override
282-
protected RowsResponse createResponse(String text, int status, String contentType, JSONObject json)
283-
{
284-
return new RowsResponse(text, status, contentType, json, this);
300+
return jsonRows;
285301
}
286302
}

src/org/labkey/remoteapi/query/SaveRowsCommand.java

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package org.labkey.remoteapi.query;
22

33
import org.json.JSONObject;
4-
import org.labkey.remoteapi.CommandException;
5-
import org.labkey.remoteapi.Connection;
64
import org.labkey.remoteapi.PostCommand;
75

8-
import java.io.IOException;
96
import java.util.ArrayList;
107
import java.util.List;
118
import java.util.Map;
@@ -49,7 +46,7 @@
4946
* )));
5047
* updateCmd.setAuditBehavior(SaveRowsCommand.AuditBehavior.DETAILED);
5148
* updateCmd.setAuditUserComment("Updated promoter region coordinates based on new assembly");
52-
* saveCmd.addCommand(updateCmd);
49+
* saveCmd.addCommands(updateCmd);
5350
*
5451
* // Delete obsolete annotation
5552
* saveCmd.addCommand(new Command(CommandType.Delete, "genome", "GeneAnnotations",
@@ -156,105 +153,136 @@ public enum CommandType
156153
Delete
157154
}
158155

159-
public static class Command extends BaseRowsCommand
156+
// N.B. You may be inclined to have this share implementation with BaseRowsCommand; however, I would caution
157+
// against doing so. This class does not represent a command like a PostCommand or a GetCommand but rather
158+
// aligns with the "commands" made on a request to the save rows endpoint.
159+
public static class Command
160160
{
161+
BaseRowsCommand.AuditBehavior _auditBehavior;
162+
String _auditUserComment;
161163
final CommandType _commandType;
162164
String _containerPath;
165+
Map<String, Object> _extraContext;
166+
List<Map<String, Object>> _rows;
167+
final String _queryName;
168+
final String _schemaName;
163169
Boolean _skipReselectRows;
164170

165171
public Command(CommandType commandType, String schemaName, String queryName, List<Map<String, Object>> rows)
166172
{
167-
super(schemaName, queryName, null);
168173
assert null != commandType;
174+
assert null != schemaName && !schemaName.isEmpty();
175+
assert null != queryName && !queryName.isEmpty();
176+
169177
_commandType = commandType;
170-
setRows(rows);
178+
_schemaName = schemaName;
179+
_queryName = queryName;
180+
_rows = rows;
171181
}
172182

173183
public JSONObject getJsonObject()
174184
{
175-
JSONObject json = super.getJsonObject();
185+
JSONObject json = new JSONObject();
186+
176187
json.put("command", getCommandType().name().toLowerCase());
188+
json.put("schemaName", getSchemaName());
189+
json.put("queryName", getQueryName());
190+
json.put("rows", BaseRowsCommand.rowsToJson(getRows()));
191+
192+
if (getAuditBehavior() != null)
193+
json.put("auditBehavior", getAuditBehavior());
194+
195+
BaseRowsCommand.stringToJson(json, "auditUserComment", getAuditUserComment());
196+
BaseRowsCommand.stringToJson(json, "containerPath", getContainerPath());
177197

178-
if (getContainerPath() != null && !getContainerPath().isEmpty())
179-
json.put("containerPath", getContainerPath());
198+
if (getExtraContext() != null && !getExtraContext().isEmpty())
199+
json.put("extraContext", getExtraContext());
180200

181201
if (isSkipReselectRows() != null)
182202
json.put("skipReselectRows", isSkipReselectRows());
183203

184204
return json;
185205
}
186206

187-
public CommandType getCommandType()
207+
public BaseRowsCommand.AuditBehavior getAuditBehavior()
188208
{
189-
return _commandType;
209+
return _auditBehavior;
190210
}
191211

192-
public String getContainerPath()
212+
public Command setAuditBehavior(BaseRowsCommand.AuditBehavior auditBehavior)
193213
{
194-
return _containerPath;
214+
_auditBehavior = auditBehavior;
215+
return this;
195216
}
196217

197-
public void setContainerPath(String containerPath)
218+
public String getAuditUserComment()
198219
{
199-
_containerPath = containerPath;
220+
return _auditUserComment;
200221
}
201222

202-
public Boolean isSkipReselectRows()
223+
public Command setAuditUserComment(String auditUserComment)
203224
{
204-
return _skipReselectRows;
225+
_auditUserComment = auditUserComment;
226+
return this;
205227
}
206228

207-
public void setSkipReselectRows(Boolean skipReselectRows)
229+
public CommandType getCommandType()
208230
{
209-
_skipReselectRows = skipReselectRows;
231+
return _commandType;
210232
}
211233

212-
@Override
213-
public String getActionName()
234+
public String getContainerPath()
214235
{
215-
throw new UnsupportedOperationException(unsupportedMethodMessage());
236+
return _containerPath;
216237
}
217238

218-
@Override
219-
public String getControllerName()
239+
public Command setContainerPath(String containerPath)
220240
{
221-
throw new UnsupportedOperationException(unsupportedMethodMessage());
241+
_containerPath = containerPath;
242+
return this;
222243
}
223244

224-
@Override
225-
public RowsResponse execute(Connection connection, String folderPath) throws IOException, CommandException
245+
public Map<String, Object> getExtraContext()
226246
{
227-
throw new UnsupportedOperationException(unsupportedMethodMessage());
247+
return _extraContext;
228248
}
229249

230-
@Override
231-
public double getRequiredVersion()
250+
public Command setExtraContext(Map<String, Object> extraContext)
232251
{
233-
throw new UnsupportedOperationException(unsupportedMethodMessage());
252+
_extraContext = extraContext;
253+
return this;
234254
}
235255

236-
@Override
237-
public void setRequiredVersion(double requiredVersion)
256+
public String getQueryName()
238257
{
239-
throw new UnsupportedOperationException(unsupportedMethodMessage());
258+
return _queryName;
240259
}
241260

242-
@Override
243-
public Integer getTimeout()
261+
public String getSchemaName()
244262
{
245-
throw new UnsupportedOperationException(unsupportedMethodMessage());
263+
return _schemaName;
246264
}
247265

248-
@Override
249-
public void setTimeout(Integer timeout)
266+
public List<Map<String, Object>> getRows()
250267
{
251-
throw new UnsupportedOperationException(unsupportedMethodMessage());
268+
return _rows;
252269
}
253270

254-
private String unsupportedMethodMessage()
271+
public Command setRows(List<Map<String, Object>> rows)
255272
{
256-
// "Command does not support methodName()."
257-
return Command.class.getSimpleName() + " does not support " + new Throwable().getStackTrace()[1].getMethodName() + "().";
273+
_rows = rows;
274+
return this;
275+
}
276+
277+
public Boolean isSkipReselectRows()
278+
{
279+
return _skipReselectRows;
280+
}
281+
282+
public Command setSkipReselectRows(Boolean skipReselectRows)
283+
{
284+
_skipReselectRows = skipReselectRows;
285+
return this;
258286
}
259287
}
260288
}

0 commit comments

Comments
 (0)