|
| 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 | +} |
0 commit comments