• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #55

02 Oct 2024 07:40PM UTC coverage: 60.548% (+0.7%) from 59.836%
#55

push

github

web-flow
Release prep for 3.2.1 (#103)

4156 of 6864 relevant lines covered (60.55%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.59
/src/main/java/com/smartsheet/api/internal/SheetRowResourcesImpl.java
1
/*
2
 * Copyright (C) 2024 Smartsheet
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.smartsheet.api.internal;
18

19
import com.smartsheet.api.AuthorizationException;
20
import com.smartsheet.api.InvalidRequestException;
21
import com.smartsheet.api.ResourceNotFoundException;
22
import com.smartsheet.api.RowAttachmentResources;
23
import com.smartsheet.api.RowColumnResources;
24
import com.smartsheet.api.RowDiscussionResources;
25
import com.smartsheet.api.ServiceUnavailableException;
26
import com.smartsheet.api.SheetRowResources;
27
import com.smartsheet.api.SmartsheetException;
28
import com.smartsheet.api.SmartsheetRestException;
29
import com.smartsheet.api.internal.http.HttpEntity;
30
import com.smartsheet.api.internal.http.HttpMethod;
31
import com.smartsheet.api.internal.http.HttpRequest;
32
import com.smartsheet.api.internal.http.HttpResponse;
33
import com.smartsheet.api.internal.util.QueryUtil;
34
import com.smartsheet.api.internal.util.Util;
35
import com.smartsheet.api.models.BulkItemFailure;
36
import com.smartsheet.api.models.BulkItemResult;
37
import com.smartsheet.api.models.BulkRowFailedItem;
38
import com.smartsheet.api.models.Cell;
39
import com.smartsheet.api.models.CopyOrMoveRowDirective;
40
import com.smartsheet.api.models.CopyOrMoveRowResult;
41
import com.smartsheet.api.models.MultiRowEmail;
42
import com.smartsheet.api.models.PartialRowUpdateResult;
43
import com.smartsheet.api.models.Row;
44
import com.smartsheet.api.models.RowEmail;
45
import com.smartsheet.api.models.Sheet;
46
import com.smartsheet.api.models.enums.ObjectExclusion;
47
import com.smartsheet.api.models.enums.RowCopyInclusion;
48
import com.smartsheet.api.models.enums.RowInclusion;
49
import com.smartsheet.api.models.enums.RowMoveInclusion;
50

51
import java.io.ByteArrayInputStream;
52
import java.io.ByteArrayOutputStream;
53
import java.util.ArrayList;
54
import java.util.EnumSet;
55
import java.util.HashMap;
56
import java.util.List;
57
import java.util.Map;
58
import java.util.Set;
59

60
/**
61
 * This is the implementation of the SheetRowResources.
62
 * <p>
63
 * Thread Safety: This class is thread safe because it is immutable and its base class is thread safe.
64
 */
65
public class SheetRowResourcesImpl extends AbstractResources implements SheetRowResources {
66
    RowAttachmentResources attachments;
67
    RowDiscussionResources discussions;
68
    RowColumnResources columns;
69

70
    private static final String IGNORE_ROWS_NOT_FOUND = "ignoreRowsNotFound";
71
    private static final String SHEETS_PATH = "sheets/";
72
    private static final String ROWS = "rows";
73
    private static final String INCLUDE = "include";
74
    private static final String EXCLUDE = "exclude";
75

76
    /**
77
     * Constructor.
78
     *
79
     * @param smartsheet the SmartsheetImpl
80
     * @throws IllegalArgumentException : if any argument is null
81
     */
82
    public SheetRowResourcesImpl(SmartsheetImpl smartsheet) {
83
        super(smartsheet);
1✔
84
        this.attachments = new RowAttachmentResourcesImpl(smartsheet);
1✔
85
        this.discussions = new RowDiscussionResourcesImpl(smartsheet);
1✔
86
        this.columns = new RowColumnResourcesImpl(smartsheet);
1✔
87
    }
1✔
88

89
    /**
90
     * Insert rows to a sheet.
91
     * <p>
92
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/rows
93
     *
94
     * @param sheetId the sheet id
95
     * @param rows    the list of rows to create
96
     * @return the created rows
97
     * @throws IllegalArgumentException    : if any argument is null
98
     * @throws InvalidRequestException     : if there is any problem with the REST API request
99
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
100
     * @throws ResourceNotFoundException   : if the resource can not be found
101
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
102
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
103
     * @throws SmartsheetException         : if there is any other error occurred during the operation
104
     */
105
    public List<Row> addRows(long sheetId, List<Row> rows) throws SmartsheetException {
106
        return this.addRows(sheetId, rows, null, null);
1✔
107
    }
108

109
    /**
110
     * Insert rows to a sheet.
111
     * <p>
112
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/rows
113
     *
114
     * @param sheetId  the sheet id
115
     * @param rows     the list of rows to create
116
     * @param includes optional objects to include
117
     * @param excludes optional objects to exclude
118
     * @return the created rows
119
     * @throws IllegalArgumentException    : if any argument is null
120
     * @throws InvalidRequestException     : if there is any problem with the REST API request
121
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
122
     * @throws ResourceNotFoundException   : if the resource can not be found
123
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
124
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
125
     * @throws SmartsheetException         : if there is any other error occurred during the operation
126
     */
127
    public List<Row> addRows(
128
            long sheetId,
129
            List<Row> rows,
130
            EnumSet<RowInclusion> includes,
131
            EnumSet<ObjectExclusion> excludes
132
    ) throws SmartsheetException {
133
        String path = SHEETS_PATH + sheetId + "/" + ROWS;
1✔
134

135
        Map<String, Object> parameters = new HashMap<>();
1✔
136

137
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
138
        parameters.put(EXCLUDE, QueryUtil.generateCommaSeparatedList(excludes));
1✔
139

140
        path += QueryUtil.generateUrl(null, parameters);
1✔
141
        return this.postAndReceiveList(path, rows, Row.class);
1✔
142
    }
143

144
    /**
145
     * Insert rows to a sheet, allowing partial success. If a row cannot be inserted, it will fail, while the others may succeed.
146
     * <p>
147
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{id}/rows
148
     *
149
     * @param sheetId the sheet id
150
     * @param rows    the list of rows to create
151
     * @return the list of created rows
152
     * @throws IllegalArgumentException    if any argument is null or empty string
153
     * @throws InvalidRequestException     if there is any problem with the REST API request
154
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
155
     * @throws ResourceNotFoundException   if the resource cannot be found
156
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
157
     * @throws SmartsheetException         if there is any other error during the operation
158
     */
159
    @Override
160
    public PartialRowUpdateResult addRowsAllowPartialSuccess(long sheetId, List<Row> rows) throws SmartsheetException {
161
        return doPartialRowOperation(sheetId, rows, null, null, HttpMethod.POST);
1✔
162
    }
163

164
    /**
165
     * Insert rows to a sheet, allowing partial success. If a row cannot be inserted, it will fail, while the others may succeed.
166
     * <p>
167
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{id}/rows
168
     *
169
     * @param sheetId  the sheet id
170
     * @param rows     the list of rows to create
171
     * @param includes optional objects to include
172
     * @param excludes optional objects to exclude
173
     * @return the list of created rows
174
     * @throws IllegalArgumentException    if any argument is null or empty string
175
     * @throws InvalidRequestException     if there is any problem with the REST API request
176
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
177
     * @throws ResourceNotFoundException   if the resource cannot be found
178
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
179
     * @throws SmartsheetException         if there is any other error during the operation
180
     */
181
    @Override
182
    public PartialRowUpdateResult addRowsAllowPartialSuccess(
183
            long sheetId,
184
            List<Row> rows,
185
            EnumSet<RowInclusion> includes,
186
            EnumSet<ObjectExclusion> excludes
187
    ) throws SmartsheetException {
188
        return doPartialRowOperation(sheetId, rows, includes, excludes, HttpMethod.POST);
1✔
189
    }
190

191
    /**
192
     * Get a row.
193
     * <p>
194
     * It mirrors to the following Smartsheet REST API method: GET /sheets/{sheetId}/rows/{rowId}
195
     *
196
     * @param sheetId  the id of the sheet
197
     * @param rowId    the id of the row
198
     * @param includes optional objects to include
199
     * @param excludes optional objects to exclude
200
     * @return the row (note that if there is no such resource, this method will throw ResourceNotFoundException rather
201
     * than returning null).
202
     * @throws InvalidRequestException     : if there is any problem with the REST API request
203
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
204
     * @throws ResourceNotFoundException   : if the resource can not be found
205
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
206
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
207
     * @throws SmartsheetException         : if there is any other error occurred during the operation
208
     */
209
    public Row getRow(
210
            long sheetId,
211
            long rowId,
212
            EnumSet<RowInclusion> includes,
213
            EnumSet<ObjectExclusion> excludes
214
    ) throws SmartsheetException {
215
        String path = SHEETS_PATH + sheetId + "/" + ROWS + "/" + rowId;
1✔
216

217
        Map<String, Object> parameters = new HashMap<>();
1✔
218

219
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
220
        parameters.put(EXCLUDE, QueryUtil.generateCommaSeparatedList(excludes));
1✔
221

222
        path += QueryUtil.generateUrl(null, parameters);
1✔
223
        return this.getResource(path, Row.class);
1✔
224
    }
225

226
    /**
227
     * Delete a row.
228
     * <p>
229
     * It mirrors to the following Smartsheet REST API method: DELETE /sheets/{sheetId}/rows/{rowId}
230
     *
231
     * @param sheetId the sheet id
232
     * @param rowId   the ID of the row
233
     * @throws InvalidRequestException     : if there is any problem with the REST API request
234
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
235
     * @throws ResourceNotFoundException   : if the resource can not be found
236
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
237
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
238
     * @throws SmartsheetException         : if there is any other error occurred during the operation
239
     * @deprecated as of API 2.0.2 release, replaced by {@link #deleteRows(long, Set, boolean)}
240
     */
241
    @Deprecated(since = "2.0.2", forRemoval = true)
242
    public void deleteRow(long sheetId, long rowId) throws SmartsheetException {
243
        this.deleteResource(SHEETS_PATH + sheetId + "/" + ROWS + "/" + rowId, Row.class);
×
244
    }
×
245

246
    /**
247
     * Deletes one or more row(s) from the Sheet specified in the URL.
248
     * <p>
249
     * It mirrors to the following Smartsheet REST API method: DELETE /sheets/{sheetId}/rows/{rowId}
250
     *
251
     * @param sheetId            the sheet id
252
     * @param rowIds             the row ids
253
     * @param ignoreRowsNotFound boolean for ignoring row ids not found
254
     * @return a list of deleted rows
255
     * @throws InvalidRequestException     : if there is any problem with the REST API request
256
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
257
     * @throws ResourceNotFoundException   : if the resource can not be found
258
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
259
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
260
     * @throws SmartsheetException         : if there is any other error occurred during the operation
261
     */
262
    public List<Long> deleteRows(long sheetId, Set<Long> rowIds, boolean ignoreRowsNotFound) throws SmartsheetException {
263
        Util.throwIfNull(rowIds);
1✔
264
        Map<String, Object> parameters = new HashMap<>();
1✔
265
        String path = SHEETS_PATH + sheetId + "/" + ROWS + "/";
1✔
266
        parameters.put("ids", QueryUtil.generateCommaSeparatedList(rowIds));
1✔
267
        parameters.put(IGNORE_ROWS_NOT_FOUND, ignoreRowsNotFound);
1✔
268

269
        path += QueryUtil.generateUrl(null, parameters);
1✔
270

271
        return this.deleteListResources(path, Long.class);
1✔
272
    }
273

274
    /**
275
     * Send a row via email to the designated recipients.
276
     * <p>
277
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/{rowId}/emails
278
     *
279
     * @param sheetId the id of the sheet
280
     * @param rowId   the id of the row
281
     * @param email   the row email
282
     * @throws IllegalArgumentException    : if any argument is null
283
     * @throws InvalidRequestException     : if there is any problem with the REST API request
284
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
285
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
286
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
287
     * @throws SmartsheetException         : if there is any other error occurred during the operation
288
     * @deprecated as of API V2.0.2, replaced by {@link #sendRows(long, MultiRowEmail)}
289
     */
290
    @Deprecated(since = "2.0.2", forRemoval = true)
291
    public void sendRow(long sheetId, long rowId, RowEmail email) throws SmartsheetException {
292
        this.createResource(SHEETS_PATH + sheetId + "/" + ROWS + "/" + rowId + "/emails", RowEmail.class, email);
×
293
    }
×
294

295
    /**
296
     * Send a row via email to the designated recipients.
297
     * <p>
298
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/emails
299
     *
300
     * @param sheetId the id of the sheet
301
     * @param email   the multi row email
302
     * @throws IllegalArgumentException    : if any argument is null
303
     * @throws InvalidRequestException     : if there is any problem with the REST API request
304
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
305
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
306
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
307
     * @throws SmartsheetException         : if there is any other error occurred during the operation
308
     */
309
    public void sendRows(long sheetId, MultiRowEmail email) throws SmartsheetException {
310
        this.createResource(SHEETS_PATH + sheetId + "/rows/emails", MultiRowEmail.class, email);
1✔
311
    }
1✔
312

313
    /**
314
     * Update rows.
315
     * <p>
316
     * It mirrors to the following Smartsheet REST API method: PUT /sheets/{sheetId}/rows
317
     *
318
     * @param sheetId the id of the sheet
319
     * @param rows    the list of rows
320
     * @return a list of rows
321
     * @throws IllegalArgumentException    : if any argument is null
322
     * @throws InvalidRequestException     : if there is any problem with the REST API request
323
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
324
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
325
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
326
     * @throws SmartsheetException         : if there is any other error occurred during the operation
327
     */
328
    public List<Row> updateRows(long sheetId, List<Row> rows) throws SmartsheetException {
329
        return this.updateRows(sheetId, rows, null, null);
1✔
330
    }
331

332
    /**
333
     * Update rows.
334
     * <p>
335
     * It mirrors to the following Smartsheet REST API method: PUT /sheets/{sheetId}/rows
336
     *
337
     * @param sheetId  the id of the sheet
338
     * @param rows     the list of rows
339
     * @param includes optional objects to include
340
     * @param excludes optional objects to exclude
341
     * @return a list of rows
342
     * @throws IllegalArgumentException    : if any argument is null
343
     * @throws InvalidRequestException     : if there is any problem with the REST API request
344
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
345
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
346
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
347
     * @throws SmartsheetException         : if there is any other error occurred during the operation
348
     */
349
    public List<Row> updateRows(
350
            long sheetId,
351
            List<Row> rows,
352
            EnumSet<RowInclusion> includes,
353
            EnumSet<ObjectExclusion> excludes
354
    ) throws SmartsheetException {
355
        String path = SHEETS_PATH + sheetId + "/" + ROWS;
1✔
356

357
        Map<String, Object> parameters = new HashMap<>();
1✔
358

359
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
360
        parameters.put(EXCLUDE, QueryUtil.generateCommaSeparatedList(excludes));
1✔
361

362
        path += QueryUtil.generateUrl(null, parameters);
1✔
363
        return this.putAndReceiveList(path, rows, Row.class);
1✔
364
    }
365

366
    /**
367
     * <p>Helper method: Update a single cell</p>
368
     *
369
     * @param sheetId the sheet ID the cell should be written to
370
     * @param cell    the cell object to be written. Must include a rowId and columnId
371
     * @return The returned Row object from the api
372
     * @throws SmartsheetException the smartsheet exception
373
     */
374
    public Row updateCell(long sheetId, Cell cell) throws SmartsheetException {
375
        if (cell.getRowId() == null || cell.getColumnId() == null) {
×
376
            throw new SmartsheetException("Cell must include rowId and columnId");
×
377
        }
378

379
        Row updateRow = new Row();
×
380
        updateRow.setCells(List.of(cell));
×
381
        updateRow.setId(cell.getRowId());
×
382

383
        List<Row> rows = updateRows(sheetId, List.of(updateRow));
×
384
        return rows.isEmpty() ? null : rows.get(0);
×
385
    }
386

387
    /**
388
     * <p>Helper method: Update a single with a string value</p>
389
     * <p>NOTE: This method internally fetches the sheet. To avoid this step, fetch the sheet in
390
     * advance and use the method by the same name</p>
391
     *
392
     * @param sheetId  the sheet ID the cell should be written to
393
     * @param rowIdx   the row index of the cell (base 1 indexed)
394
     * @param colIdx   the column index of the cell (base 1 indexed)
395
     * @param newValue the new value of the cell
396
     * @return The returned Row object from the api
397
     * @throws SmartsheetException the smartsheet exception
398
     */
399
    public Row updateCell(long sheetId, int rowIdx, int colIdx, String newValue) throws SmartsheetException {
400
        Sheet sheet = smartsheet.sheetResources().getSheet(sheetId);
×
401
        return updateCell(sheet, colIdx, rowIdx, newValue);
×
402
    }
403

404
    /**
405
     * <p>Helper method: Update a single with a string value</p>
406
     *
407
     * @param sheet    The sheet to update the cell in. Must include rowId and cell information
408
     * @param rowIdx   The row index of the cell (base 1 indexed)
409
     * @param colIdx   The column index of the cell (base 1 indexed)
410
     * @param newValue The new value of the cell
411
     * @return The returned Row object from the api
412
     * @throws SmartsheetException the smartsheet exception
413
     */
414
    public Row updateCell(Sheet sheet, int rowIdx, int colIdx, String newValue) throws SmartsheetException {
415
        Row row = sheet.getRowByRowNumber(rowIdx);
×
416
        if (row == null) {
×
417
            throw new SmartsheetException("Sheet does not contain row at index" + rowIdx);
×
418
        }
419
        long rowId = row.getId();
×
420
        if (row.getCells().size() < colIdx) {
×
421
            throw new SmartsheetException("Sheet does not contain column at index" + colIdx);
×
422
        }
423
        long colId = row.getCells().get(colIdx - 1).getColumnId();
×
424

425
        Cell cell = new Cell();
×
426
        cell.setColumnId(colId);
×
427
        cell.setRowId(rowId);
×
428
        cell.setValue(newValue);
×
429
        return updateCell(sheet.getId(), cell);
×
430
    }
431

432
    /**
433
     * Update rows, but allow partial success. The PartialRowUpdateResult will contain the successful
434
     * rows and those that failed, with specific messages for each.
435
     * <p>
436
     * It mirrors to the following Smartsheet REST API method: PUT /sheets/{sheetId}/rows
437
     *
438
     * @param sheetId the id of the sheet
439
     * @param rows    the list of rows
440
     * @return a list of rows
441
     * @throws IllegalArgumentException    : if any argument is null
442
     * @throws InvalidRequestException     : if there is any problem with the REST API request
443
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
444
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
445
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
446
     * @throws SmartsheetException         : if there is any other error occurred during the operation
447
     */
448
    @Override
449
    public PartialRowUpdateResult updateRowsAllowPartialSuccess(long sheetId, List<Row> rows) throws SmartsheetException {
450
        return doPartialRowOperation(sheetId, rows, null, null, HttpMethod.PUT);
1✔
451
    }
452

453
    /**
454
     * Update rows, but allow partial success. The PartialRowUpdateResult will contain the successful
455
     * rows and those that failed, with specific messages for each.
456
     * <p>
457
     * It mirrors to the following Smartsheet REST API method: PUT /sheets/{sheetId}/rows
458
     *
459
     * @param sheetId  the id of the sheet
460
     * @param rows     the list of rows
461
     * @param includes optional objects to include
462
     * @param excludes optional objects to exclude
463
     * @return a list of rows
464
     * @throws IllegalArgumentException    : if any argument is null
465
     * @throws InvalidRequestException     : if there is any problem with the REST API request
466
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
467
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
468
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
469
     * @throws SmartsheetException         : if there is any other error occurred during the operation
470
     */
471
    @Override
472
    public PartialRowUpdateResult updateRowsAllowPartialSuccess(
473
            long sheetId,
474
            List<Row> rows,
475
            EnumSet<RowInclusion> includes,
476
            EnumSet<ObjectExclusion> excludes
477
    ) throws SmartsheetException {
478
        return doPartialRowOperation(sheetId, rows, includes, excludes, HttpMethod.PUT);
1✔
479
    }
480

481
    private PartialRowUpdateResult doPartialRowOperation(
482
            long sheetId,
483
            List<Row> rows,
484
            EnumSet<RowInclusion> includes,
485
            EnumSet<ObjectExclusion> excludes,
486
            HttpMethod method
487
    ) throws SmartsheetException {
488
        Util.throwIfNull(rows, method);
1✔
489
        if (method != HttpMethod.POST && method != HttpMethod.PUT) {
1✔
490
            throw new IllegalArgumentException();
×
491
        }
492

493
        String path = SHEETS_PATH + sheetId + "/" + ROWS;
1✔
494
        Map<String, Object> parameters = new HashMap<>();
1✔
495
        parameters.put("allowPartialSuccess", "true");
1✔
496
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
497
        parameters.put(EXCLUDE, QueryUtil.generateCommaSeparatedList(excludes));
1✔
498

499
        path = QueryUtil.generateUrl(path, parameters);
1✔
500

501
        HttpRequest request;
502
        request = createHttpRequest(smartsheet.getBaseURI().resolve(path), method);
1✔
503

504
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
1✔
505

506
        this.smartsheet.getJsonSerializer().serialize(rows, baos);
1✔
507

508
        HttpEntity entity = new HttpEntity();
1✔
509
        entity.setContentType("application/json");
1✔
510
        entity.setContent(new ByteArrayInputStream(baos.toByteArray()));
1✔
511
        entity.setContentLength(baos.size());
1✔
512
        request.setEntity(entity);
1✔
513

514
        HttpResponse response = this.smartsheet.getHttpClient().request(request);
1✔
515

516
        PartialRowUpdateResult result = null;
1✔
517
        switch (response.getStatusCode()) {
1✔
518
            case 200:
519
                BulkItemResult<Row> bulkItemResult;
520
                bulkItemResult = this.smartsheet.getJsonSerializer().deserializeBulkItemResult(Row.class,
1✔
521
                        response.getEntity().getContent());
1✔
522
                result = new PartialRowUpdateResult();
1✔
523
                result.setResult(bulkItemResult.getResult());
1✔
524
                result.setResultCode(bulkItemResult.getResultCode());
1✔
525
                result.setMessage(bulkItemResult.getMessage());
1✔
526
                result.setVersion(bulkItemResult.getVersion());
1✔
527
                if (bulkItemResult.getFailedItems() != null) {
1✔
528
                    List<BulkRowFailedItem> failedItems = new ArrayList<>();
1✔
529
                    for (BulkItemFailure bulkItemFailure : bulkItemResult.getFailedItems()) {
1✔
530
                        BulkRowFailedItem bulkRowFailedItem = new BulkRowFailedItem();
1✔
531
                        bulkRowFailedItem.setError(bulkItemFailure.getError());
1✔
532
                        bulkRowFailedItem.setIndex(bulkItemFailure.getIndex());
1✔
533
                        bulkRowFailedItem.setRowId(bulkItemFailure.getRowId());
1✔
534
                        failedItems.add(bulkRowFailedItem);
1✔
535
                    }
1✔
536
                    result.setFailedItems(failedItems);
1✔
537
                }
1✔
538
                break;
539
            default:
540
                handleError(response);
×
541
        }
542

543
        smartsheet.getHttpClient().releaseConnection();
1✔
544

545
        return result;
1✔
546
    }
547

548
    /**
549
     * Moves Row(s) from the Sheet specified in the URL to (the bottom of) another sheet.
550
     * <p>
551
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/move
552
     *
553
     * @param sheetId            the sheet ID to move
554
     * @param includes           the parameters to include
555
     * @param ignoreRowsNotFound optional,specifying row Ids that do not exist within the source sheet
556
     * @param moveParameters     CopyOrMoveRowDirective object
557
     * @return the result object
558
     * @throws IllegalArgumentException    : if any argument is null, or path is empty string
559
     * @throws InvalidRequestException     : if there is any problem with the REST API request
560
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
561
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
562
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
563
     * @throws SmartsheetException         : if there is any other error occurred during the operation
564
     */
565
    public CopyOrMoveRowResult moveRows(
566
            Long sheetId,
567
            EnumSet<RowMoveInclusion> includes,
568
            Boolean ignoreRowsNotFound,
569
            CopyOrMoveRowDirective moveParameters
570
    ) throws SmartsheetException {
571
        String path = SHEETS_PATH + sheetId + "/rows/move";
1✔
572
        Map<String, Object> parameters = new HashMap<>();
1✔
573
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
574

575
        if (ignoreRowsNotFound != null) {
1✔
576
            parameters.put(IGNORE_ROWS_NOT_FOUND, ignoreRowsNotFound.toString());
1✔
577
        }
578

579
        path += QueryUtil.generateUrl(null, parameters);
1✔
580
        return this.postAndReceiveRowObject(path, moveParameters);
1✔
581
    }
582

583
    /**
584
     * Copies Row(s) from the Sheet specified in the URL to (the bottom of) another sheet.
585
     * <p>
586
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/copy
587
     *
588
     * @param sheetId            the sheet ID to move
589
     * @param includes           the parameters to include
590
     * @param ignoreRowsNotFound optional,specifying row Ids that do not exist within the source sheet
591
     * @param copyParameters     CopyOrMoveRowDirective object
592
     * @return the result object
593
     * @throws IllegalArgumentException    : if any argument is null, or path is empty string
594
     * @throws InvalidRequestException     : if there is any problem with the REST API request
595
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
596
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
597
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
598
     * @throws SmartsheetException         : if there is any other error occurred during the operation
599
     */
600
    public CopyOrMoveRowResult copyRows(
601
            Long sheetId,
602
            EnumSet<RowCopyInclusion> includes,
603
            Boolean ignoreRowsNotFound,
604
            CopyOrMoveRowDirective copyParameters
605
    ) throws SmartsheetException {
606
        String path = SHEETS_PATH + sheetId + "/rows/copy";
1✔
607
        Map<String, Object> parameters = new HashMap<>();
1✔
608
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
609

610
        if (ignoreRowsNotFound != null) {
1✔
611
            parameters.put(IGNORE_ROWS_NOT_FOUND, ignoreRowsNotFound.toString());
1✔
612
        }
613

614
        path += QueryUtil.generateUrl(null, parameters);
1✔
615
        return this.postAndReceiveRowObject(path, copyParameters);
1✔
616
    }
617

618
    /**
619
     * /**
620
     * Update the values of the Cells in a Row.
621
     * <p>
622
     * It mirrors to the following Smartsheet REST API method: PUT /row/{id}/cells
623
     *
624
     * @param rowId the row id
625
     * @param cells the cells to update (Cells must have the following attributes set: *
626
     *              columnId * value * strict (optional)
627
     * @return the updated cells (note that if there is no such resource, this method will throw
628
     * ResourceNotFoundException rather than returning null).
629
     * @throws IllegalArgumentException    : if any argument is null
630
     * @throws InvalidRequestException     : if there is any problem with the REST API request
631
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
632
     * @throws ResourceNotFoundException   : if the resource can not be found
633
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
634
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
635
     * @throws SmartsheetException         : if there is any other error occurred during the operation
636
     * @deprecated replaced by {@link #updateRows(long, List)}
637
     */
638
    @Deprecated(since = "2.0.0", forRemoval = true)
639
    public List<Cell> updateCells(long rowId, List<Cell> cells) throws SmartsheetException {
640
        return this.putAndReceiveList("row/" + rowId + "/cells", cells, Cell.class);
×
641
    }
642

643
    /**
644
     * Creates an object of RowAttachmentResources.
645
     *
646
     * @return the created RowAttachmentResources object
647
     */
648
    public RowAttachmentResources attachmentResources() {
649
        return attachments;
×
650
    }
651

652
    /**
653
     * Creates an object of RowDiscussionResources.
654
     *
655
     * @return the created RowDiscussionResources object
656
     */
657
    public RowDiscussionResources discussionResources() {
658
        return discussions;
×
659
    }
660

661
    /**
662
     * Creates an object of RowColumnResources.
663
     *
664
     * @return the created RowColumnResources object
665
     */
666
    public RowColumnResources cellResources() {
667
        return columns;
×
668
    }
669
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc