Skip to content

Commit 5f22349

Browse files
committed
QuerySaveRowsCommand
1 parent eaee99c commit 5f22349

File tree

2 files changed

+346
-0
lines changed

2 files changed

+346
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
package org.labkey.remoteapi.query;
2+
3+
import org.json.JSONObject;
4+
import org.labkey.remoteapi.PostCommand;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class QuerySaveRowsCommand extends PostCommand<QuerySaveRowsResponse>
11+
{
12+
private final List<Command> _commands = new ArrayList<>();
13+
private Map<String, Object> _extraContext;
14+
private Boolean _transacted;
15+
private Boolean _validateOnly;
16+
17+
/**
18+
* Constructs a new SaveRowsActualCommand given a controller and action name.
19+
*
20+
* @param commands The commands.
21+
*/
22+
public QuerySaveRowsCommand(Command... commands)
23+
{
24+
super("query", "saveRows.api");
25+
26+
addCommand(commands);
27+
}
28+
29+
public Map<String, Object> getExtraContext()
30+
{
31+
return _extraContext;
32+
}
33+
34+
public void setExtraContext(Map<String, Object> extraContext)
35+
{
36+
_extraContext = extraContext;
37+
}
38+
39+
public QuerySaveRowsCommand addCommand(Command... commands)
40+
{
41+
for (Command command : commands)
42+
{
43+
if (command != null)
44+
_commands.add(command);
45+
}
46+
47+
return this;
48+
}
49+
50+
public List<Command> getCommands()
51+
{
52+
return _commands;
53+
}
54+
55+
public Boolean isTransacted()
56+
{
57+
return _transacted;
58+
}
59+
60+
public void setTransacted(Boolean transacted)
61+
{
62+
_transacted = transacted;
63+
}
64+
65+
public Boolean isValidateOnly()
66+
{
67+
return _validateOnly;
68+
}
69+
70+
public void setValidateOnly(Boolean validateOnly)
71+
{
72+
_validateOnly = validateOnly;
73+
}
74+
75+
@Override
76+
public JSONObject getJsonObject()
77+
{
78+
JSONObject json = new JSONObject();
79+
80+
List<JSONObject> commands = new ArrayList<>();
81+
for (Command command : getCommands())
82+
commands.add(command.getJsonObject());
83+
json.put("commands", commands);
84+
85+
if (getExtraContext() != null && !getExtraContext().isEmpty())
86+
json.put("extraContext", getExtraContext());
87+
88+
if (isTransacted() != null)
89+
json.put("transacted", isTransacted());
90+
91+
if (isValidateOnly() != null)
92+
json.put("validateOnly", isValidateOnly());
93+
94+
return json;
95+
}
96+
97+
@Override
98+
protected QuerySaveRowsResponse createResponse(String text, int status, String contentType, JSONObject json)
99+
{
100+
return new QuerySaveRowsResponse(text, status, contentType, json);
101+
}
102+
103+
public enum CommandType
104+
{
105+
Insert,
106+
Update,
107+
Delete
108+
}
109+
110+
public static class Command
111+
{
112+
SaveRowsCommand.AuditBehavior _auditBehavior;
113+
String _auditUserComment;
114+
final CommandType _commandType;
115+
String _containerPath;
116+
Map<String, Object> _extraContext;
117+
List<Map<String, Object>> _rows;
118+
final String _queryName;
119+
final String _schemaName;
120+
Boolean _skipReselectRows;
121+
122+
public Command(CommandType commandType, String schemaName, String queryName, List<Map<String, Object>> rows)
123+
{
124+
assert null != commandType;
125+
assert null != schemaName && !schemaName.isEmpty();
126+
assert null != queryName && !queryName.isEmpty();
127+
128+
_commandType = commandType;
129+
_schemaName = schemaName;
130+
_queryName = queryName;
131+
_rows = rows;
132+
}
133+
134+
public JSONObject getJsonObject()
135+
{
136+
JSONObject json = new JSONObject();
137+
138+
json.put("command", getCommandType().name().toLowerCase());
139+
json.put("schemaName", getSchemaName());
140+
json.put("queryName", getQueryName());
141+
json.put("rows", getRows());
142+
143+
if (getAuditBehavior() != null)
144+
json.put("auditBehavior", getAuditBehavior());
145+
146+
if (getAuditUserComment() != null && !getAuditUserComment().isEmpty())
147+
json.put("auditUserComment", getAuditUserComment());
148+
149+
if (getContainerPath() != null && !getContainerPath().isEmpty())
150+
json.put("containerPath", getContainerPath());
151+
152+
if (getExtraContext() != null && !getExtraContext().isEmpty())
153+
json.put("extraContext", getExtraContext());
154+
155+
if (isSkipReselectRows() != null)
156+
json.put("skipReselectRows", isSkipReselectRows());
157+
158+
return json;
159+
}
160+
161+
public SaveRowsCommand.AuditBehavior getAuditBehavior()
162+
{
163+
return _auditBehavior;
164+
}
165+
166+
public void setAuditBehavior(SaveRowsCommand.AuditBehavior auditBehavior)
167+
{
168+
_auditBehavior = auditBehavior;
169+
}
170+
171+
public String getAuditUserComment()
172+
{
173+
return _auditUserComment;
174+
}
175+
176+
public void setAuditUserComment(String auditUserComment)
177+
{
178+
_auditUserComment = auditUserComment;
179+
}
180+
181+
public CommandType getCommandType()
182+
{
183+
return _commandType;
184+
}
185+
186+
public String getContainerPath()
187+
{
188+
return _containerPath;
189+
}
190+
191+
public void setContainerPath(String containerPath)
192+
{
193+
_containerPath = containerPath;
194+
}
195+
196+
public Map<String, Object> getExtraContext()
197+
{
198+
return _extraContext;
199+
}
200+
201+
public void setExtraContext(Map<String, Object> extraContext)
202+
{
203+
_extraContext = extraContext;
204+
}
205+
206+
public String getQueryName()
207+
{
208+
return _queryName;
209+
}
210+
211+
public String getSchemaName()
212+
{
213+
return _schemaName;
214+
}
215+
216+
public List<Map<String, Object>> getRows()
217+
{
218+
return _rows;
219+
}
220+
221+
public void setRows(List<Map<String, Object>> rows)
222+
{
223+
_rows = rows;
224+
}
225+
226+
public Boolean isSkipReselectRows()
227+
{
228+
return _skipReselectRows;
229+
}
230+
231+
public void setSkipReselectRows(Boolean skipReselectRows)
232+
{
233+
_skipReselectRows = skipReselectRows;
234+
}
235+
}
236+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.labkey.remoteapi.query;
2+
3+
import org.json.JSONObject;
4+
import org.labkey.remoteapi.CommandResponse;
5+
import org.labkey.remoteapi.collections.CaseInsensitiveHashMap;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class QuerySaveRowsResponse extends CommandResponse
13+
{
14+
private final boolean _committed;
15+
private final int _errorCount;
16+
private final List<Result> _results;
17+
18+
public QuerySaveRowsResponse(String text, int statusCode, String contentType, JSONObject json)
19+
{
20+
super(text, statusCode, contentType, json);
21+
22+
_committed = json.optBoolean("committed", false);
23+
_errorCount = json.optInt("errorCount", 0);
24+
25+
List<Result> results = new ArrayList<>();
26+
if (json.has("result"))
27+
{
28+
for (Object resultJson : json.getJSONArray("result"))
29+
results.add(new Result((JSONObject) resultJson));
30+
}
31+
_results = Collections.unmodifiableList(results);
32+
}
33+
34+
public boolean isCommitted()
35+
{
36+
return _committed;
37+
}
38+
39+
public int getErrorCount()
40+
{
41+
return _errorCount;
42+
}
43+
44+
public List<Result> getResults()
45+
{
46+
return _results;
47+
}
48+
49+
public static class Result
50+
{
51+
private final String _command;
52+
private final String _containerPath;
53+
private final String _queryName;
54+
private final List<Map<String, Object>> _rows = new ArrayList<>();
55+
private final int _rowsAffected;
56+
private final String _schemaName;
57+
private final int _transactionAuditId;
58+
59+
private Result(JSONObject json)
60+
{
61+
_command = json.optString("command", null);
62+
_containerPath = json.optString("containerPath", null);
63+
_queryName = json.optString("queryName", null);
64+
_rowsAffected = json.optInt("rowsAffected", 0);
65+
_schemaName = json.optString("schemaName", null);
66+
_transactionAuditId = json.optInt("transactionAuditId", 0);
67+
68+
if (json.has("rows"))
69+
{
70+
for (Object rowJson : json.getJSONArray("rows"))
71+
_rows.add(new CaseInsensitiveHashMap<>(((JSONObject) rowJson).toMap()));
72+
}
73+
}
74+
75+
public String getCommand()
76+
{
77+
return _command;
78+
}
79+
80+
public String getContainerPath()
81+
{
82+
return _containerPath;
83+
}
84+
85+
public String getQueryName()
86+
{
87+
return _queryName;
88+
}
89+
90+
public List<Map<String, Object>> getRows()
91+
{
92+
return _rows;
93+
}
94+
95+
public int getRowsAffected()
96+
{
97+
return _rowsAffected;
98+
}
99+
100+
public String getSchemaName()
101+
{
102+
return _schemaName;
103+
}
104+
105+
public int getTransactionAuditId()
106+
{
107+
return _transactionAuditId;
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)