org.labkey.remoteapi.query.SaveRowsCommand Maven / Gradle / Ivy
Show all versions of labkey-client-api Show documentation
/*
* Copyright (c) 2008-2016 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.remoteapi.query;
import org.json.JSONArray;
import org.json.JSONObject;
import org.labkey.remoteapi.PostCommand;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Base class for commands that make changes to rows exposed from a given
* query in a given schema. Clients should use {@link UpdateRowsCommand},
* {@link InsertRowsCommand} or {@link DeleteRowsCommand} and not this class directly.
*
* All three of these sub-classes 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 {@link #addRow(Map)} or {@link #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");
*
*/
public abstract class SaveRowsCommand extends PostCommand
{
private String _schemaName;
private String _queryName;
private Map _extraContext;
private List