Class SaveRowsCommand
- All Implemented Interfaces:
HasRequiredVersion
- Direct Known Subclasses:
DeleteRowsCommand
,InsertRowsCommand
,MoveRowsCommand
,UpdateRowsCommand
UpdateRowsCommand
,
InsertRowsCommand
or DeleteRowsCommand
and not this class directly.
All three of these subclasses post similar JSON to the server, so this class
does all the common work. The client must supply three things: the schemaName,
the queryName and an array of 'rows' (i.e. Maps). The rows are added via
the addRow(Map)
or setRows(List)
methods.
All data exposed from the LabKey Server is organized into a set of queries contained in a set of schemas. A schema is simply a group of queries, identified by a name (e.g., 'lists' or 'study'). A query is particular table or view within that schema (e.g., 'People' or 'Peptides'). Currently, clients may update rows in base tables only, and not in joined views. Therefore the query name must be the name of a table in the schema.
To view the schemas and queries exposed in a given folder, add a Query web part to your portal page and choose the option "Show the list of tables in this schema" in the part configuration page. Alternatively, if it is exposed, click on the Query tab across the top of the main part of the page.
Examples:
// May need to add CONTEXT_PATH for dev instances
Connection cn = new Connection("http://localhost:8080", user, password);
//Insert Rows Command
InsertRowsCommand cmd = new InsertRowsCommand("lists", "People");
Map<String, Object> row = new HashMap<String, Object>();
row.put("FirstName", "Insert");
row.put("LastName", "Test");
cmd.addRow(row); //can add multiple rows to insert many at once
SaveRowsResponse resp = cmd.execute(cn, "PROJECT_NAME");
//get the newly-assigned primary key value from the first return row
int newKey = resp.getRows().get(0).get("Key");
//Update Rows Command
UpdateRowsCommand cmdUpd = new UpdateRowsCommand("lists", "People");
row = new HashMap<String, Object>();
row.put("Key", newKey);
row.put("LastName", "Test UPDATED");
cmdUpd.addRow(row);
resp = cmdUpd.execute(cn, "PROJECT_NAME");
//Delete Rows Command
DeleteRowsCommand cmdDel = new DeleteRowsCommand("lists", "People");
row = new HashMap<String, Object>();
row.put("Key", newKey);
cmdDel.addRow(row);
resp = cmdDel.execute(cn, "PROJECT_NAME");
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.labkey.remoteapi.Command
Command.CommonParameters, Command.Response
-
Field Summary
Fields inherited from class org.labkey.remoteapi.Command
CONTENT_TYPE_JSON
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SaveRowsCommand
(String schemaName, String queryName, String actionName) Constructs a new SaveRowsCommand for a given schema, query and action name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a row to the list of rows to be sent to the server.protected SaveRowsResponse
createResponse
(String text, int status, String contentType, org.json.JSONObject json) Creates an instance of the response class, initialized with the response text, the HTTP status code, and parsed JSONObject.Gets the additional extra context.org.json.JSONObject
Dynamically builds the JSON object to send based on the current schema name, query name and rows list.Returns the query namegetRows()
Returns the current list of 'rows' (i.e., Maps) that will be sent to the server.Returns the schema name.void
setAuditBehavior
(SaveRowsCommand.AuditBehavior auditBehavior) Used to override the audit behavior for the schema/query.void
setAuditUserComment
(String auditUserComment) Used to provide a comment that will be attached to certain detailed audit log recordsvoid
setExtraContext
(Map<String, Object> extraContext) Sets the additional extra context.void
setQueryName
(String queryName) Sets a new query name to updatevoid
Sets the list of 'rows' (i.e., Maps) to be sent to the server.void
setSchemaName
(String schemaName) Sets the schema nameMethods inherited from class org.labkey.remoteapi.PostCommand
createRequest
Methods inherited from class org.labkey.remoteapi.Command
_execute, createParameterMap, execute, getActionName, getControllerName, getHttpRequest, getParameters, getParamValueAsString, getRequiredVersion, getTimeout, setRequiredVersion, setTimeout
-
Constructor Details
-
SaveRowsCommand
Constructs a new SaveRowsCommand for a given schema, query and action name.- Parameters:
schemaName
- The schema name.queryName
- The query name.actionName
- The action name to call (supplied by the derived class).
-
-
Method Details
-
getSchemaName
Returns the schema name.- Returns:
- The schema name.
-
setSchemaName
Sets the schema name- Parameters:
schemaName
- The new schema name.
-
getQueryName
Returns the query name- Returns:
- the query name.
-
setQueryName
Sets a new query name to update- Parameters:
queryName
- the query name.
-
getExtraContext
Gets the additional extra context.- Returns:
- the extra context.
-
setExtraContext
Sets the additional extra context.- Parameters:
extraContext
- The extra context.
-
getRows
Returns the current list of 'rows' (i.e., Maps) that will be sent to the server.- Returns:
- The list of rows.
-
setRows
Sets the list of 'rows' (i.e., Maps) to be sent to the server.- Parameters:
rows
- The rows to send
-
addRow
Adds a row to the list of rows to be sent to the server.- Parameters:
row
- The row to add
-
getAuditBehavior
-
setAuditBehavior
Used to override the audit behavior for the schema/query. Note that any audit behavior type that is configured via an XML file for the given schema/query will take precedence over this value. See TableInfo.getAuditBehavior() for more details.- Parameters:
auditBehavior
- Valid values include "NONE", "SUMMARY", and "DETAILED"
-
getAuditUserComment
-
setAuditUserComment
Used to provide a comment that will be attached to certain detailed audit log records- Parameters:
auditUserComment
- The comment to attach to the detailed audit log records
-
getJsonObject
public org.json.JSONObject getJsonObject()Dynamically builds the JSON object to send based on the current schema name, query name and rows list.- Overrides:
getJsonObject
in classPostCommand<SaveRowsResponse>
- Returns:
- The JSON object to send.
-
createResponse
protected SaveRowsResponse createResponse(String text, int status, String contentType, org.json.JSONObject json) Description copied from class:Command
Creates an instance of the response class, initialized with the response text, the HTTP status code, and parsed JSONObject.Override this method to create an instance of a different class that extends CommandResponse
- Overrides:
createResponse
in classCommand<SaveRowsResponse,
org.apache.hc.client5.http.classic.methods.HttpPost> - Parameters:
text
- The response text from the server.status
- The HTTP status code.contentType
- The Content-Type header value.json
- The parsed JSONObject (or null if no JSON was returned).- Returns:
- An instance of the response object.
-