• 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

66.48
/src/main/java/com/smartsheet/api/internal/SheetResourcesImpl.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.ServiceUnavailableException;
23
import com.smartsheet.api.ShareResources;
24
import com.smartsheet.api.SheetAttachmentResources;
25
import com.smartsheet.api.SheetAutomationRuleResources;
26
import com.smartsheet.api.SheetColumnResources;
27
import com.smartsheet.api.SheetCommentResources;
28
import com.smartsheet.api.SheetCrossSheetReferenceResources;
29
import com.smartsheet.api.SheetDiscussionResources;
30
import com.smartsheet.api.SheetFilterResources;
31
import com.smartsheet.api.SheetResources;
32
import com.smartsheet.api.SheetRowResources;
33
import com.smartsheet.api.SheetSummaryResources;
34
import com.smartsheet.api.SheetUpdateRequestResources;
35
import com.smartsheet.api.SmartsheetException;
36
import com.smartsheet.api.SmartsheetRestException;
37
import com.smartsheet.api.UserResources;
38
import com.smartsheet.api.internal.http.HttpEntity;
39
import com.smartsheet.api.internal.http.HttpMethod;
40
import com.smartsheet.api.internal.http.HttpRequest;
41
import com.smartsheet.api.internal.http.HttpResponse;
42
import com.smartsheet.api.internal.util.QueryUtil;
43
import com.smartsheet.api.internal.util.Util;
44
import com.smartsheet.api.models.ContainerDestination;
45
import com.smartsheet.api.models.MultiRowEmail;
46
import com.smartsheet.api.models.PagedResult;
47
import com.smartsheet.api.models.PaginationParameters;
48
import com.smartsheet.api.models.Sheet;
49
import com.smartsheet.api.models.SheetEmail;
50
import com.smartsheet.api.models.SheetPublish;
51
import com.smartsheet.api.models.SortSpecifier;
52
import com.smartsheet.api.models.UpdateRequest;
53
import com.smartsheet.api.models.enums.CopyExclusion;
54
import com.smartsheet.api.models.enums.ObjectExclusion;
55
import com.smartsheet.api.models.enums.PaperSize;
56
import com.smartsheet.api.models.enums.SheetCopyInclusion;
57
import com.smartsheet.api.models.enums.SheetInclusion;
58
import com.smartsheet.api.models.enums.SheetTemplateInclusion;
59
import com.smartsheet.api.models.enums.SourceInclusion;
60

61
import java.io.ByteArrayInputStream;
62
import java.io.ByteArrayOutputStream;
63
import java.io.File;
64
import java.io.FileInputStream;
65
import java.io.FileNotFoundException;
66
import java.io.IOException;
67
import java.io.InputStream;
68
import java.io.OutputStream;
69
import java.text.SimpleDateFormat;
70
import java.util.Date;
71
import java.util.EnumSet;
72
import java.util.HashMap;
73
import java.util.Map;
74
import java.util.Set;
75

76
/**
77
 * This is the implementation of the SheetResources.
78
 * <p>
79
 * Thread Safety: This class is thread safe because it is immutable and its base class is thread safe.
80
 */
81
public class SheetResourcesImpl extends AbstractResources implements SheetResources {
82

83
    /**
84
     * The Constant BUFFER_SIZE.
85
     */
86
    private static final int BUFFER_SIZE = 4098;
87

88
    private static final String SHEETS = "sheets";
89
    private static final String TEXT_CSV = "text/csv";
90
    private static final String FOLDERS = "folders";
91
    private static final String INCLUDE = "include";
92
    private static final String IMPORT = "import";
93
    private static final String WORKSPACES = "WORKSPACES";
94
    private static final String XLSX_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
95

96
    /**
97
     * Represents the ShareResources.
98
     * <p>
99
     * It will be initialized in constructor and will not change afterward.
100
     */
101
    private ShareResources shares;
102
    /**
103
     * Represents the SheetRowResources.
104
     * <p>
105
     * It will be initialized in constructor and will not change afterward.
106
     */
107
    private SheetRowResources rows;
108
    /**
109
     * Represents the SheetColumnResources.
110
     * <p>
111
     * It will be initialized in constructor and will not change afterward.
112
     */
113
    private SheetColumnResources columns;
114
    /**
115
     * Represents the AssociatedAttachmentResources.
116
     * <p>
117
     * It will be initialized in constructor and will not change afterward.
118
     */
119
    private SheetAttachmentResources attachments;
120
    /**
121
     * Represents the AssociatedDiscussionResources.
122
     * <p>
123
     * It will be initialized in constructor and will not change afterward.
124
     */
125
    private SheetDiscussionResources discussions;
126

127
    /**
128
     * Represents the SheetCommentResources.
129
     * <p>
130
     * It will be initialized in constructor and will not change afterward
131
     */
132
    private SheetCommentResources comments;
133

134
    /**
135
     * Represents the SheetUpdateRequestResources.
136
     * <p>
137
     * It will be initialized in constructor and will not change afterward
138
     */
139
    private SheetUpdateRequestResources updateRequests;
140

141
    /**
142
     * Represents the SheetFilterResources.
143
     * <p>
144
     * It will be initialized in constructor and will not change afterward
145
     */
146
    private SheetFilterResources filters;
147

148
    /**
149
     * Represents the AutomationRules.
150
     * <p>
151
     * It will be initialized in the constructor and will not change afterward
152
     */
153
    private SheetAutomationRuleResources automationRules;
154

155
    /**
156
     * Represents the CrossSheetReferences
157
     * <p>
158
     * It will be initialized in the constructor and will not change afterward
159
     */
160
    private SheetCrossSheetReferenceResources crossSheetReferences;
161

162
    /**
163
     * Represents the sheetSummary
164
     * <p>
165
     * It will be initialized in the constructor and will not change afterward
166
     */
167
    private SheetSummaryResources sheetSummary;
168

169
    /**
170
     * Constructor.
171
     *
172
     * @param smartsheet the smartsheet
173
     * @throws IllegalArgumentException : if any argument is null
174
     */
175
    public SheetResourcesImpl(SmartsheetImpl smartsheet) {
176
        super(smartsheet);
1✔
177
        this.shares = new ShareResourcesImpl(smartsheet, SHEETS);
1✔
178
        this.rows = new SheetRowResourcesImpl(smartsheet);
1✔
179
        this.columns = new SheetColumnResourcesImpl(smartsheet);
1✔
180
        this.attachments = new SheetAttachmentResourcesImpl(smartsheet);
1✔
181
        this.discussions = new SheetDiscussionResourcesImpl(smartsheet);
1✔
182
        this.comments = new SheetCommentResourcesImpl(smartsheet);
1✔
183
        this.updateRequests = new SheetUpdateRequestResourcesImpl(smartsheet);
1✔
184
        this.filters = new SheetFilterResourcesImpl(smartsheet);
1✔
185
        this.automationRules = new SheetAutomationRuleResourcesImpl(smartsheet);
1✔
186
        this.crossSheetReferences = new SheetCrossSheetReferenceResourcesImpl(smartsheet);
1✔
187
        this.sheetSummary = new SheetSummaryResourcesImpl(smartsheet);
1✔
188
    }
1✔
189

190
    /**
191
     * List all sheets.
192
     * <p>
193
     * It mirrors to the following Smartsheet REST API method: GET /sheets
194
     *
195
     * @return A list of all sheets (note that an empty list will be returned if there are none).
196
     * @throws IllegalArgumentException    if any argument is null or empty string
197
     * @throws InvalidRequestException     if there is any problem with the REST API request
198
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
199
     * @throws ResourceNotFoundException   if the resource cannot be found
200
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
201
     * @throws SmartsheetException         if there is any other error during the operation
202
     */
203
    public PagedResult<Sheet> listSheets() throws SmartsheetException {
204
        return this.listSheets(null, null, null);
×
205
    }
206

207
    /**
208
     * List all sheets.
209
     * <p>
210
     * It mirrors to the following Smartsheet REST API method: GET /sheets
211
     *
212
     * @param includes   the source inclusion
213
     * @param pagination the object containing the pagination parameters
214
     * @return A list of all sheets (note that an empty list will be returned if there are none).
215
     * @throws IllegalArgumentException    if any argument is null or empty string
216
     * @throws InvalidRequestException     if there is any problem with the REST API request
217
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
218
     * @throws ResourceNotFoundException   if the resource cannot be found
219
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
220
     * @throws SmartsheetException         if there is any other error during the operation
221
     */
222
    public PagedResult<Sheet> listSheets(EnumSet<SourceInclusion> includes, PaginationParameters pagination) throws SmartsheetException {
223
        return this.listSheets(includes, pagination, null);
×
224
    }
225

226
    /**
227
     * List Sheets
228
     */
229
    public PagedResult<Sheet> listSheets(
230
            EnumSet<SourceInclusion> includes,
231
            PaginationParameters pagination,
232
            Date modifiedSince
233
    ) throws SmartsheetException {
234
        String path = SHEETS;
1✔
235

236
        Map<String, Object> parameters = new HashMap<>();
1✔
237
        if (pagination != null) {
1✔
238
            parameters = pagination.toHashMap();
1✔
239
        }
240
        if (modifiedSince != null) {
1✔
241
            String isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(modifiedSince);
×
242
            parameters.put("modifiedSince", isoDate);
×
243
        }
244
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
245

246
        path += QueryUtil.generateUrl(null, parameters);
1✔
247
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
248
    }
249

250
    /**
251
     * List all sheets in the organization.
252
     * <p>
253
     * It mirrors to the following Smartsheet REST API method: GET /users/sheets
254
     *
255
     * @param parameters the object containing the pagination parameters
256
     * @return all sheets (note that empty list will be returned if there is none)
257
     * @throws InvalidRequestException     : if there is any problem with the REST API request
258
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
259
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
260
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
261
     * @throws SmartsheetException         : if there is any other error occurred during the operation
262
     * @deprecated As of release 2.0. Please use {@link UserResources} instead
263
     */
264
    @Deprecated(since = "2.0.0", forRemoval = true)
265
    public PagedResult<Sheet> listOrganizationSheets(PaginationParameters parameters) throws SmartsheetException {
266
        String path = "users/sheets";
1✔
267

268
        if (parameters != null) {
1✔
269
            path += parameters.toQueryString();
1✔
270
        }
271
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
272
    }
273

274
    /**
275
     * Get a sheet.
276
     * <p>
277
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
278
     *
279
     * @param id         the id
280
     * @param includes   used to specify the optional objects to include, currently DISCUSSIONS and ATTACHMENTS are supported.
281
     * @param columnIds  the column ids
282
     * @param excludes   the exclude parameters
283
     * @param page       the page number
284
     * @param pageSize   the page size
285
     * @param rowIds     the row ids
286
     * @param rowNumbers the row numbers
287
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
288
     * rather than returning null).
289
     * @throws InvalidRequestException     : if there is any problem with the REST API request
290
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
291
     * @throws ResourceNotFoundException   : if the resource can not be found
292
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
293
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
294
     * @throws SmartsheetException         : if there is any other error occurred during the operation
295
     */
296
    public Sheet getSheet(
297
            long id,
298
            EnumSet<SheetInclusion> includes,
299
            EnumSet<ObjectExclusion> excludes,
300
            Set<Long> rowIds,
301
            Set<Integer> rowNumbers,
302
            Set<Long> columnIds,
303
            Integer pageSize,
304
            Integer page
305
    ) throws SmartsheetException {
306
        return this.getSheet(id, includes, excludes, rowIds, rowNumbers, columnIds, pageSize, page, null, null);
1✔
307
    }
308

309
    /**
310
     * Get a sheet.
311
     * <p>
312
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
313
     *
314
     * @param id             the id
315
     * @param includes       used to specify the optional objects to include, currently DISCUSSIONS and ATTACHMENTS are supported.
316
     * @param columnIds      the column ids
317
     * @param excludes       the exclude parameters
318
     * @param page           the page number
319
     * @param pageSize       the page size
320
     * @param rowIds         the row ids
321
     * @param rowNumbers     the row numbers
322
     * @param ifVersionAfter only fetch Sheet if more recent version available
323
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
324
     * rather than returning null).
325
     * @throws InvalidRequestException     : if there is any problem with the REST API request
326
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
327
     * @throws ResourceNotFoundException   : if the resource can not be found
328
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
329
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
330
     * @throws SmartsheetException         : if there is any other error occurred during the operation
331
     */
332
    public Sheet getSheet(
333
            long id,
334
            EnumSet<SheetInclusion> includes,
335
            EnumSet<ObjectExclusion> excludes,
336
            Set<Long> rowIds,
337
            Set<Integer> rowNumbers,
338
            Set<Long> columnIds,
339
            Integer pageSize,
340
            Integer page,
341
            Integer ifVersionAfter
342
    ) throws SmartsheetException {
343
        return this.getSheet(id, includes, excludes, rowIds, rowNumbers, columnIds, pageSize, page, ifVersionAfter, null);
×
344
    }
345

346
    /**
347
     * Get a sheet.
348
     * <p>
349
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
350
     *
351
     * @param id             the id of the sheet
352
     * @param includes       used to specify the optional objects to include.
353
     * @param columnIds      the column ids
354
     * @param excludes       the exclude parameters
355
     * @param page           the page number
356
     * @param pageSize       the page size
357
     * @param rowIds         the row ids
358
     * @param rowNumbers     the row numbers
359
     * @param ifVersionAfter only fetch Sheet if more recent version available
360
     * @param level          compatibility level
361
     * @return the sheet resource (note that if there is no such resource, this method will throw
362
     * ResourceNotFoundException rather than returning null).
363
     * @throws IllegalArgumentException    if any argument is null or empty string
364
     * @throws InvalidRequestException     if there is any problem with the REST API request
365
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
366
     * @throws ResourceNotFoundException   if the resource cannot be found
367
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
368
     * @throws SmartsheetException         if there is any other error during the operation
369
     */
370
    public Sheet getSheet(
371
            long id,
372
            EnumSet<SheetInclusion> includes,
373
            EnumSet<ObjectExclusion> excludes,
374
            Set<Long> rowIds,
375
            Set<Integer> rowNumbers,
376
            Set<Long> columnIds,
377
            Integer pageSize,
378
            Integer page,
379
            Integer ifVersionAfter,
380
            Integer level
381
    ) throws SmartsheetException {
382

383
        String path = SHEETS + "/" + id;
1✔
384

385
        // Add the parameters to a map and build the query string at the end
386
        Map<String, Object> parameters = new HashMap<>();
1✔
387

388
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
389
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
1✔
390
        parameters.put("rowIds", QueryUtil.generateCommaSeparatedList(rowIds));
1✔
391
        parameters.put("rowNumbers", QueryUtil.generateCommaSeparatedList(rowNumbers));
1✔
392
        parameters.put("columnIds", QueryUtil.generateCommaSeparatedList(columnIds));
1✔
393
        parameters.put("pageSize", pageSize);
1✔
394
        parameters.put("page", page);
1✔
395
        parameters.put("ifVersionAfter", ifVersionAfter);
1✔
396
        parameters.put("level", level);
1✔
397

398
        // Iterate through the map of parameters and generate the query string
399
        path += QueryUtil.generateUrl(null, parameters);
1✔
400

401
        return this.getResource(path, Sheet.class);
1✔
402
    }
403

404
    /**
405
     * <p>Get sheet by ID. Load all row and column data</p>
406
     *
407
     * @param sheetId id of the sheet to fetch
408
     * @return the sheet resource (note that if there is no such resource, this method will throw
409
     * ResourceNotFoundException rather than returning null).
410
     * @throws IllegalArgumentException    if any argument is null or empty string
411
     * @throws InvalidRequestException     if there is any problem with the REST API request
412
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
413
     * @throws ResourceNotFoundException   if the resource cannot be found
414
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
415
     * @throws SmartsheetException         if there is any other error during the operation
416
     */
417
    public Sheet getSheet(long sheetId) throws SmartsheetException {
418
        return getSheet(sheetId, null, null, null, null, null, null, null, null, null);
1✔
419
    }
420

421
    /**
422
     * Get a sheet as an Excel file.
423
     * <p>
424
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/vnd.ms-excel" Accept
425
     * HTTP header
426
     *
427
     * @param id           the id
428
     * @param outputStream the OutputStream to which the Excel file will be written
429
     * @throws IllegalArgumentException    : if outputStream is null
430
     * @throws InvalidRequestException     : if there is any problem with the REST API request
431
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
432
     * @throws ResourceNotFoundException   : if the resource can not be found
433
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
434
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
435
     * @throws SmartsheetException         : if there is any other error occurred during the operation
436
     */
437
    public void getSheetAsExcel(long id, OutputStream outputStream) throws SmartsheetException {
438
        getSheetAsFile(id, null, outputStream, "application/vnd.ms-excel");
1✔
439
    }
1✔
440

441
    /**
442
     * Get a sheet as a PDF file.
443
     * <p>
444
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/pdf" Accept HTTP
445
     * header
446
     *
447
     * @param id           the id
448
     * @param outputStream the output stream to which the PDF file will be written.
449
     * @param paperSize    the optional paper size
450
     * @throws IllegalArgumentException    : if outputStream is null
451
     * @throws InvalidRequestException     : if there is any problem with the REST API request
452
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
453
     * @throws ResourceNotFoundException   : if the resource can not be found
454
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
455
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
456
     * @throws SmartsheetException         : if there is any other error occurred during the operation
457
     */
458
    public void getSheetAsPDF(long id, OutputStream outputStream, PaperSize paperSize) throws SmartsheetException {
459
        getSheetAsFile(id, paperSize, outputStream, "application/pdf");
1✔
460
    }
1✔
461

462
    /**
463
     * Create a sheet in default "Sheets" collection.
464
     * <p>
465
     * It mirrors to the following Smartsheet REST API method: POST /sheets
466
     *
467
     * @param sheet the sheet to create, limited to the following required attributes: * name (string) *
468
     *              columns (array of Column objects, limited to the following attributes) - title - primary - type - symbol -
469
     *              options
470
     * @return the created sheet
471
     * @throws IllegalArgumentException    : if any argument is null
472
     * @throws InvalidRequestException     : if there is any problem with the REST API request
473
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
474
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
475
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
476
     * @throws SmartsheetException         : if there is any other error occurred during the operation
477
     */
478
    public Sheet createSheet(Sheet sheet) throws SmartsheetException {
479
        return this.createResource(SHEETS, Sheet.class, sheet);
1✔
480
    }
481

482
    /**
483
     * Create a sheet (from existing sheet or template) in default "Sheets" collection.
484
     * <p>
485
     * It mirrors to the following Smartsheet REST API method: POST /sheets
486
     *
487
     * @param sheet    the sheet to create, limited to the following required attributes: * name (string) * fromId
488
     *                 (number): ID of the Sheet or Template from which to create the sheet.
489
     * @param includes used to specify the optional objects to include, currently DATA, DISCUSSIONS and ATTACHMENTS are supported.
490
     * @return the sheet
491
     * @throws IllegalArgumentException    : if any argument is null
492
     * @throws InvalidRequestException     : if there is any problem with the REST API request
493
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
494
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
495
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
496
     * @throws SmartsheetException         : if there is any other error occurred during the operation
497
     */
498
    public Sheet createSheetFromTemplate(Sheet sheet, EnumSet<SheetTemplateInclusion> includes) throws SmartsheetException {
499
        Map<String, Object> parameters = new HashMap<>();
1✔
500
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
501
        String path = QueryUtil.generateUrl(SHEETS, parameters);
1✔
502

503
        return this.createResource(path, Sheet.class, sheet);
1✔
504
    }
505

506
    /**
507
     * Imports a sheet.
508
     * <p>
509
     * It mirrors to the following Smartsheet REST API method: POST /sheets/import
510
     *
511
     * @param file               path to the CSV file
512
     * @param sheetName          destination sheet name
513
     * @param headerRowIndex     index (0 based) of row to be used for column names
514
     * @param primaryColumnIndex index (0 based) of primary column
515
     * @return the created sheet
516
     * @throws IllegalArgumentException    if any argument is null or empty string
517
     * @throws InvalidRequestException     if there is any problem with the REST API request
518
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
519
     * @throws ResourceNotFoundException   if the resource cannot be found
520
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
521
     * @throws SmartsheetException         if there is any other error during the operation
522
     */
523
    public Sheet importCsv(String file, String sheetName, Integer headerRowIndex, Integer primaryColumnIndex) throws SmartsheetException {
524
        return importFile("sheets/import", file, TEXT_CSV, sheetName, headerRowIndex, primaryColumnIndex);
×
525
    }
526

527
    /**
528
     * Imports a sheet.
529
     * <p>
530
     * It mirrors to the following Smartsheet REST API method: POST /sheets/import
531
     *
532
     * @param file               path to the XLSX file
533
     * @param sheetName          destination sheet name
534
     * @param headerRowIndex     index (0 based) of row to be used for column names
535
     * @param primaryColumnIndex index (0 based) of primary column
536
     * @return the created sheet
537
     * @throws IllegalArgumentException    if any argument is null or empty string
538
     * @throws InvalidRequestException     if there is any problem with the REST API request
539
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
540
     * @throws ResourceNotFoundException   if the resource cannot be found
541
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
542
     * @throws SmartsheetException         if there is any other error during the operation
543
     */
544
    public Sheet importXlsx(String file, String sheetName, Integer headerRowIndex, Integer primaryColumnIndex) throws SmartsheetException {
545
        return importFile("sheets/import", file, XLSX_CONTENT_TYPE,
×
546
                sheetName, headerRowIndex, primaryColumnIndex);
547
    }
548

549
    /**
550
     * Create a sheet in given folder.
551
     * <p>
552
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets
553
     *
554
     * @param folderId the folder id
555
     * @param sheet    the sheet to create, limited to the following required
556
     *                 attributes: * name (string) * columns (array of Column objects, limited to the following attributes) - title -
557
     *                 primary - type - symbol - options
558
     * @return the created sheet
559
     * @throws IllegalArgumentException    : if any argument is null
560
     * @throws InvalidRequestException     : if there is any problem with the REST API request
561
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
562
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
563
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
564
     * @throws SmartsheetException         : if there is any other error occurred during the operation
565
     */
566
    public Sheet createSheetInFolder(long folderId, Sheet sheet) throws SmartsheetException {
567

568
        return this.createResource(FOLDERS + "/" + folderId + "/" + SHEETS, Sheet.class, sheet);
1✔
569
    }
570

571
    /**
572
     * Create a sheet in given folder.
573
     * <p>
574
     * It mirrors to the following Smartsheet REST API method: POST /folder/{folderId}/sheets
575
     *
576
     * @param folderId the folder id
577
     * @param sheet    the sheet
578
     * @param includes the includes
579
     * @return the sheet to create, limited to the following required
580
     * attributes: * name (string) * columns (array of Column objects, limited to the following attributes) - title -
581
     * primary - type - symbol - options
582
     * @throws IllegalArgumentException    : if any argument is null
583
     * @throws InvalidRequestException     : if there is any problem with the REST API request
584
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
585
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
586
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
587
     * @throws SmartsheetException         : if there is any other error occurred during the operation
588
     */
589
    public Sheet createSheetInFolderFromTemplate(
590
            long folderId,
591
            Sheet sheet,
592
            EnumSet<SheetTemplateInclusion> includes
593
    ) throws SmartsheetException {
594
        Map<String, Object> parameters = new HashMap<>();
1✔
595
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
596
        String path = QueryUtil.generateUrl(FOLDERS + "/" + folderId + "/" + SHEETS, parameters);
1✔
597

598
        return this.createResource(path, Sheet.class, sheet);
1✔
599
    }
600

601
    /**
602
     * Imports a sheet in given folder.
603
     * <p>
604
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets/import
605
     *
606
     * @param folderId           the folder id
607
     * @param file               path to the CSV file
608
     * @param sheetName          destination sheet name
609
     * @param headerRowIndex     index (0 based) of row to be used for column names
610
     * @param primaryColumnIndex index (0 based) of primary column
611
     * @return the created sheet
612
     * @throws IllegalArgumentException    if any argument is null or empty string
613
     * @throws InvalidRequestException     if there is any problem with the REST API request
614
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
615
     * @throws ResourceNotFoundException   if the resource cannot be found
616
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
617
     * @throws SmartsheetException         if there is any other error during the operation
618
     */
619
    public Sheet importCsvInFolder(
620
            long folderId,
621
            String file,
622
            String sheetName,
623
            Integer headerRowIndex,
624
            Integer primaryColumnIndex
625
    ) throws SmartsheetException {
626
        return importFile(
×
627
                FOLDERS + "/" + folderId + "/" + SHEETS + "/" + IMPORT,
628
                file,
629
                TEXT_CSV,
630
                sheetName,
631
                headerRowIndex,
632
                primaryColumnIndex
633
        );
634
    }
635

636
    /**
637
     * Imports a sheet in given folder.
638
     * <p>
639
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets/import
640
     *
641
     * @param folderId           the folder id
642
     * @param file               path to the XLSX file
643
     * @param sheetName          destination sheet name
644
     * @param headerRowIndex     index (0 based) of row to be used for column names
645
     * @param primaryColumnIndex index (0 based) of primary column
646
     * @return the created sheet
647
     * @throws IllegalArgumentException    if any argument is null or empty string
648
     * @throws InvalidRequestException     if there is any problem with the REST API request
649
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
650
     * @throws ResourceNotFoundException   if the resource cannot be found
651
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
652
     * @throws SmartsheetException         if there is any other error during the operation
653
     */
654
    public Sheet importXlsxInFolder(
655
            long folderId,
656
            String file,
657
            String sheetName,
658
            Integer headerRowIndex,
659
            Integer primaryColumnIndex
660
    ) throws SmartsheetException {
661
        return importFile(
×
662
                FOLDERS + "/" + folderId + "/" + SHEETS + "/" + IMPORT,
663
                file,
664
                XLSX_CONTENT_TYPE,
665
                sheetName,
666
                headerRowIndex,
667
                primaryColumnIndex
668
        );
669
    }
670

671
    /**
672
     * Create a sheet in given workspace.
673
     * <p>
674
     * It mirrors to the following Smartsheet REST API method: POST /workspace/{workspaceId}/sheets
675
     *
676
     * @param workspaceId the workspace id
677
     * @param sheet       the sheet to create, limited to the following required attributes: * name (string) * columns
678
     *                    (array of Column objects, limited to the following attributes) - title - primary - type - symbol - options
679
     * @return the created sheet
680
     * @throws IllegalArgumentException    : if any argument is null
681
     * @throws InvalidRequestException     : if there is any problem with the REST API request
682
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
683
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
684
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
685
     * @throws SmartsheetException         : if there is any other error occurred during the operation
686
     */
687
    public Sheet createSheetInWorkspace(long workspaceId, Sheet sheet) throws SmartsheetException {
688
        return this.createResource(WORKSPACES + "/" + workspaceId + "/" + SHEETS, Sheet.class, sheet);
1✔
689
    }
690

691
    /**
692
     * Create a sheet (from existing sheet or template) in given workspace.
693
     * <p>
694
     * It mirrors to the following Smartsheet REST API method: POST /workspace/{workspaceId}/sheets
695
     *
696
     * @param workspaceId the workspace id
697
     * @param sheet       the sheet to create, limited to the following required attributes:
698
     *                    <ul>
699
     *                        <li>name (string)</li>
700
     *                        <li>fromId (number): ID of the Sheet or Template from which to create the sheet.</li>
701
     *                        <li>includes : used to specify the optional objects to include;
702
     *                        currently DATA, DISCUSSIONS and ATTACHMENTS are supported.</li>
703
     *                    </ul>
704
     * @param includes    the includes
705
     * @return the created sheet
706
     * @throws IllegalArgumentException    : if any argument is null
707
     * @throws InvalidRequestException     : if there is any problem with the REST API request
708
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
709
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
710
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
711
     * @throws SmartsheetException         : if there is any other error occurred during the operation
712
     */
713
    public Sheet createSheetInWorkspaceFromTemplate(long workspaceId, Sheet sheet, EnumSet<SheetTemplateInclusion> includes)
714
            throws SmartsheetException {
715
        Map<String, Object> parameters = new HashMap<>();
1✔
716
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
717
        String path = QueryUtil.generateUrl(WORKSPACES + "/" + workspaceId + "/" + SHEETS, parameters);
1✔
718

719
        return this.createResource(path, Sheet.class, sheet);
1✔
720
    }
721

722
    /**
723
     * Imports a sheet in given workspace.
724
     * <p>
725
     * It mirrors to the following Smartsheet REST API method: POST /workspaces/{workspaceId}/sheets/import
726
     *
727
     * @param workspaceId        the workspace id
728
     * @param file               path to the CSV file
729
     * @param sheetName          destination sheet name
730
     * @param headerRowIndex     index (0 based) of row to be used for column names
731
     * @param primaryColumnIndex index (0 based) of primary column
732
     * @return the created sheet
733
     * @throws IllegalArgumentException    if any argument is null or empty string
734
     * @throws InvalidRequestException     if there is any problem with the REST API request
735
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
736
     * @throws ResourceNotFoundException   if the resource cannot be found
737
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
738
     * @throws SmartsheetException         if there is any other error during the operation
739
     */
740
    public Sheet importCsvInWorkspace(
741
            long workspaceId,
742
            String file,
743
            String sheetName,
744
            Integer headerRowIndex,
745
            Integer primaryColumnIndex
746
    ) throws SmartsheetException {
747
        return importFile(
×
748
                WORKSPACES + "/" + workspaceId + "/" + SHEETS + "/" + IMPORT,
749
                file,
750
                TEXT_CSV,
751
                sheetName,
752
                headerRowIndex,
753
                primaryColumnIndex
754
        );
755
    }
756

757
    /**
758
     * Imports a sheet in given workspace.
759
     * <p>
760
     * It mirrors to the following Smartsheet REST API method: POST /workspaces/{workspaceId}/sheets/import
761
     *
762
     * @param workspaceId        the workspace id
763
     * @param file               path to the XLSX file
764
     * @param sheetName          destination sheet name
765
     * @param headerRowIndex     index (0 based) of row to be used for column names
766
     * @param primaryColumnIndex index (0 based) of primary column
767
     * @return the created sheet
768
     * @throws IllegalArgumentException    if any argument is null or empty string
769
     * @throws InvalidRequestException     if there is any problem with the REST API request
770
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
771
     * @throws ResourceNotFoundException   if the resource cannot be found
772
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
773
     * @throws SmartsheetException         if there is any other error during the operation
774
     */
775
    public Sheet importXlsxInWorkspace(
776
            long workspaceId,
777
            String file,
778
            String sheetName,
779
            Integer headerRowIndex,
780
            Integer primaryColumnIndex
781
    ) throws SmartsheetException {
782
        return importFile(
×
783
                WORKSPACES + "/" + workspaceId + "/" + SHEETS + "/" + IMPORT,
784
                file,
785
                XLSX_CONTENT_TYPE,
786
                sheetName,
787
                headerRowIndex,
788
                primaryColumnIndex
789
        );
790
    }
791

792
    /**
793
     * Delete a sheet.
794
     * <p>
795
     * It mirrors to the following Smartsheet REST API method: DELETE /sheet{id}
796
     *
797
     * @param id the ID of the sheet
798
     * @throws InvalidRequestException     : if there is any problem with the REST API request
799
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
800
     * @throws ResourceNotFoundException   : if the resource can not be found
801
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
802
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
803
     * @throws SmartsheetException         : if there is any other error occurred during the operation
804
     */
805
    public void deleteSheet(long id) throws SmartsheetException {
806
        this.deleteResource(SHEETS + "/" + id, Sheet.class);
1✔
807
    }
1✔
808

809
    /**
810
     * Update a sheet.
811
     * <p>
812
     * It mirrors to the following Smartsheet REST API method: PUT /sheet/{id}
813
     *
814
     * @param sheet the sheet to update
815
     * @return the updated sheet
816
     * @throws IllegalArgumentException    if any argument is null or empty string
817
     * @throws InvalidRequestException     if there is any problem with the REST API request
818
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
819
     * @throws ResourceNotFoundException   if the resource cannot be found
820
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
821
     * @throws SmartsheetException         if there is any other error during the operation
822
     */
823
    public Sheet updateSheet(Sheet sheet) throws SmartsheetException {
824
        return this.updateResource(SHEETS + "/" + sheet.getId(), Sheet.class, sheet);
1✔
825
    }
826

827
    /**
828
     * Get a sheet version.
829
     * <p>
830
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}/version
831
     *
832
     * @param id the id
833
     * @return the sheet version (note that if there is no such resource, this method will throw
834
     * ResourceNotFoundException rather than returning null).
835
     * @throws InvalidRequestException     : if there is any problem with the REST API request
836
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
837
     * @throws ResourceNotFoundException   : if the resource can not be found
838
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
839
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
840
     * @throws SmartsheetException         : if there is any other error occurred during the operation
841
     */
842
    public int getSheetVersion(long id) throws SmartsheetException {
843
        return this.getResource(SHEETS + "/" + id + "/version", Sheet.class).getVersion();
1✔
844
    }
845

846
    /**
847
     * Send a sheet as a PDF attachment via email to the designated recipients.
848
     * <p>
849
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/emails
850
     *
851
     * @param id    the id
852
     * @param email the email
853
     * @throws IllegalArgumentException    : if any argument is null
854
     * @throws InvalidRequestException     : if there is any problem with the REST API request
855
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
856
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
857
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
858
     * @throws SmartsheetException         : if there is any other error occurred during the operation
859
     */
860
    public void sendSheet(long id, SheetEmail email) throws SmartsheetException {
861
        this.createResource(SHEETS + "/" + id + "/emails", SheetEmail.class, email);
1✔
862
    }
1✔
863

864
    /**
865
     * Get a sheet as an Excel file.
866
     * <p>
867
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/vnd.ms-excel" Accept HTTP header
868
     *
869
     * @param id           the id of the sheet
870
     * @param outputStream the output stream to which the Excel file will be written.
871
     * @throws IllegalArgumentException    if any argument is null or empty string
872
     * @throws InvalidRequestException     if there is any problem with the REST API request
873
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
874
     * @throws ResourceNotFoundException   if the resource cannot be found
875
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
876
     * @throws SmartsheetException         if there is any other error during the operation
877
     */
878
    public void getSheetAsCSV(long id, OutputStream outputStream) throws SmartsheetException {
879
        getSheetAsFile(id, null, outputStream, TEXT_CSV);
×
880
    }
×
881

882
    /**
883
     * Get the status of the Publish settings of the sheet, including the URLs of any enabled publishings.
884
     * <p>
885
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{sheetId}/publish
886
     *
887
     * @param id the id
888
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
889
     * * rather than returning null).
890
     * @throws InvalidRequestException     : if there is any problem with the REST API request
891
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
892
     * @throws ResourceNotFoundException   : if the resource can not be found
893
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
894
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
895
     * @throws SmartsheetException         : if there is any other error occurred during the operation
896
     */
897
    public SheetPublish getPublishStatus(long id) throws SmartsheetException {
898
        return this.getResource(SHEETS + "/" + id + "/publish", SheetPublish.class);
1✔
899
    }
900

901
    /**
902
     * Sets the publish status of a sheet and returns the new status, including the URLs of any enabled publishings.
903
     * <p>
904
     * It mirrors to the following Smartsheet REST API method: PUT /sheet/{sheetId}/publish
905
     *
906
     * @param id      the id
907
     * @param publish the SheetPublish object limited to the following attributes *
908
     *                readOnlyLiteEnabled * readOnlyFullEnabled * readWriteEnabled * icalEnabled
909
     * @return the updated SheetPublish (note that if there is no such resource, this method will throw
910
     * ResourceNotFoundException rather than returning null).
911
     * @throws IllegalArgumentException    : if any argument is null
912
     * @throws InvalidRequestException     : if there is any problem with the REST API request
913
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
914
     * @throws ResourceNotFoundException   : if the resource can not be found
915
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
916
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
917
     * @throws SmartsheetException         : if there is any other error occurred during the operation
918
     */
919
    public SheetPublish updatePublishStatus(long id, SheetPublish publish) throws SmartsheetException {
920
        return this.updateResource(SHEETS + "/" + id + "/publish", SheetPublish.class, publish);
1✔
921
    }
922

923
    /**
924
     * Creates a copy of the specified sheet.
925
     * <p>
926
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/copy
927
     *
928
     * @param sheetId              the sheet id
929
     * @param containerDestination describes the destination container
930
     * @param includes             optional parameters to include
931
     * @return the sheet
932
     * @throws IllegalArgumentException    : if folder is null
933
     * @throws InvalidRequestException     : if there is any problem with the REST API request
934
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
935
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
936
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
937
     * @throws SmartsheetException         : if there is any other error occurred during the operation
938
     */
939
    public Sheet copySheet(
940
            long sheetId,
941
            ContainerDestination containerDestination,
942
            EnumSet<SheetCopyInclusion> includes
943
    ) throws SmartsheetException {
944
        return copySheet(sheetId, containerDestination, includes, null);
1✔
945
    }
946

947
    /**
948
     * Creates a copy of the specified sheet.
949
     * <p>
950
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/copy
951
     *
952
     * @param sheetId              the sheet id
953
     * @param containerDestination describes the destination container
954
     * @param includes             optional parameters to include
955
     * @param excludes             optional parameters to exclude
956
     * @return the sheet
957
     * @throws IllegalArgumentException    : if folder is null
958
     * @throws InvalidRequestException     : if there is any problem with the REST API request
959
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
960
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
961
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
962
     * @throws SmartsheetException         : if there is any other error occurred during the operation
963
     */
964
    public Sheet copySheet(
965
            long sheetId,
966
            ContainerDestination containerDestination,
967
            EnumSet<SheetCopyInclusion> includes,
968
            EnumSet<CopyExclusion> excludes
969
    ) throws SmartsheetException {
970

971
        String path = SHEETS + "/" + sheetId + "/copy";
1✔
972
        Map<String, Object> parameters = new HashMap<>();
1✔
973

974
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
975
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
1✔
976

977
        path += QueryUtil.generateUrl(null, parameters);
1✔
978

979
        return this.createResource(path, Sheet.class, containerDestination);
1✔
980
    }
981

982
    /**
983
     * Moves the specified Sheet to another location.
984
     * <p>
985
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/move
986
     *
987
     * @param sheetId              the folder id
988
     * @param containerDestination describes the destination container
989
     * @return the sheet
990
     * @throws IllegalArgumentException    : if folder is null
991
     * @throws InvalidRequestException     : if there is any problem with the REST API request
992
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
993
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
994
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
995
     * @throws SmartsheetException         : if there is any other error occurred during the operation
996
     */
997
    public Sheet moveSheet(long sheetId, ContainerDestination containerDestination) throws SmartsheetException {
998

999
        String path = SHEETS + "/" + sheetId + "/move";
1✔
1000
        return this.createResource(path, Sheet.class, containerDestination);
1✔
1001
    }
1002

1003
    /**
1004
     * Creates an Update Request for the specified Row(s) within the Sheet.
1005
     * <p>
1006
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/updaterequests
1007
     *
1008
     * @param sheetId the sheet id
1009
     * @param email   the email
1010
     * @return the update request object
1011
     * @throws IllegalArgumentException    : if any argument is null
1012
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1013
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1014
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1015
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1016
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1017
     */
1018
    public UpdateRequest createUpdateRequest(long sheetId, MultiRowEmail email) throws SmartsheetException {
1019
        return this.createResource(SHEETS + "/" + sheetId + "/updaterequests", UpdateRequest.class, email);
1✔
1020
    }
1021

1022
    /**
1023
     * Sort a sheet according to the sort criteria.
1024
     * <p>
1025
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/sort
1026
     *
1027
     * @param sheetId       the sheet id
1028
     * @param sortSpecifier the sort criteria
1029
     * @return the update request object
1030
     * @throws IllegalArgumentException    : if any argument is null
1031
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1032
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1033
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1034
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1035
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1036
     */
1037
    public Sheet sortSheet(long sheetId, SortSpecifier sortSpecifier) throws SmartsheetException {
1038
        return this.sortSheet(sheetId, sortSpecifier, null);
1✔
1039
    }
1040

1041
    /**
1042
     * Sort a sheet according to the sort criteria.
1043
     * <p>
1044
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/sort
1045
     *
1046
     * @param sheetId       the sheet id
1047
     * @param sortSpecifier the sort criteria
1048
     * @param level         compatibility level
1049
     * @return the update request object
1050
     * @throws SmartsheetException         the smartsheet exception
1051
     * @throws IllegalArgumentException    : if any argument is null
1052
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1053
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1054
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1055
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1056
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1057
     */
1058
    public Sheet sortSheet(long sheetId, SortSpecifier sortSpecifier, Integer level) throws SmartsheetException {
1059
        Util.throwIfNull(sortSpecifier);
1✔
1060

1061
        String path = SHEETS + "/" + sheetId + "/sort";
1✔
1062
        if (level != null) {
1✔
1063
            path += "?level=" + level;
×
1064
        }
1065

1066
        HttpRequest request = createHttpRequest(smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
1✔
1067

1068
        ByteArrayOutputStream objectBytesStream = new ByteArrayOutputStream();
1✔
1069
        this.smartsheet.getJsonSerializer().serialize(sortSpecifier, objectBytesStream);
1✔
1070

1071
        HttpEntity entity = new HttpEntity();
1✔
1072
        entity.setContentType("application/json");
1✔
1073
        entity.setContent(new ByteArrayInputStream(objectBytesStream.toByteArray()));
1✔
1074
        entity.setContentLength(objectBytesStream.size());
1✔
1075
        request.setEntity(entity);
1✔
1076

1077
        Sheet obj = null;
1✔
1078
        try {
1079
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
1✔
1080
            switch (response.getStatusCode()) {
1✔
1081
                case 200: {
1082
                    InputStream inputStream = response.getEntity().getContent();
1✔
1083
                    try {
1084
                        obj = this.smartsheet.getJsonSerializer().deserialize(Sheet.class, inputStream);
1✔
1085
                    } catch (IOException e) {
×
1086
                        throw new SmartsheetException(e);
×
1087
                    }
1✔
1088
                    break;
1089
                }
1090
                default:
1091
                    handleError(response);
×
1092
            }
1093
        } finally {
1094
            smartsheet.getHttpClient().releaseConnection();
1✔
1095
        }
1096
        return obj;
1✔
1097
    }
1098

1099
    /**
1100
     * Return the ShareResources object that provides access to Share resources associated with Sheet resources.
1101
     *
1102
     * @return the ShareResources object
1103
     */
1104
    public ShareResources shareResources() {
1105
        return this.shares;
1✔
1106
    }
1107

1108
    /**
1109
     * Return the SheetRowResources object that provides access to Row resources associated with Sheet resources.
1110
     *
1111
     * @return the sheet row resources
1112
     */
1113
    public SheetRowResources rowResources() {
1114
        return this.rows;
1✔
1115
    }
1116

1117
    /**
1118
     * Return the SheetColumnResources object that provides access to Column resources associated with Sheet resources.
1119
     *
1120
     * @return the sheet column resources
1121
     */
1122
    public SheetColumnResources columnResources() {
1123
        return this.columns;
1✔
1124
    }
1125

1126
    /**
1127
     * Return the AssociatedAttachmentResources object that provides access to attachment resources associated with
1128
     * Sheet resources.
1129
     *
1130
     * @return the associated attachment resources
1131
     */
1132
    public SheetAttachmentResources attachmentResources() {
1133
        return this.attachments;
×
1134
    }
1135

1136
    /**
1137
     * Return the AssociatedDiscussionResources object that provides access to discussion resources associated with
1138
     * Sheet resources.
1139
     *
1140
     * @return the associated discussion resources
1141
     */
1142
    public SheetDiscussionResources discussionResources() {
1143
        return this.discussions;
1✔
1144
    }
1145

1146
    /**
1147
     * Return the SheetCommentResources object that provides access to discussion resources associated with
1148
     * Sheet resources.
1149
     *
1150
     * @return the associated comment resources
1151
     */
1152
    public SheetCommentResources commentResources() {
1153
        return this.comments;
×
1154
    }
1155

1156
    /**
1157
     * Return the SheetUpdateRequestResources object that provides access to update request resources
1158
     * associated with Sheet resources.
1159
     *
1160
     * @return the associated update request resources
1161
     */
1162
    public SheetUpdateRequestResources updateRequestResources() {
1163
        return this.updateRequests;
×
1164
    }
1165

1166
    /**
1167
     * Return the SheetFilterResources object that provides access to sheet filter resources
1168
     * associated with Sheet resources.
1169
     *
1170
     * @return the associated sheet filter resources
1171
     */
1172
    public SheetFilterResources filterResources() {
1173
        return this.filters;
×
1174
    }
1175

1176
    /**
1177
     * Return the SheetAutomationRuleResources object that provides access to automation rule resources
1178
     * associated with the Sheet resources.
1179
     *
1180
     * @return the associated automation rule resources
1181
     */
1182
    public SheetAutomationRuleResources automationRuleResources() {
1183
        return automationRules;
×
1184
    }
1185

1186
    /**
1187
     * Return the SheetCrossSheetReferenceResources object that provides access to the cross sheet reference resources
1188
     * associated with the Sheet resources.
1189
     *
1190
     * @return the cross sheet reference resources
1191
     */
1192
    public SheetCrossSheetReferenceResources crossSheetReferenceResources() {
1193
        return crossSheetReferences;
×
1194
    }
1195

1196
    /**
1197
     * Return the SheetSummaryResources object that provides access to the sheet summary resources
1198
     * associated with the Sheet resources.
1199
     *
1200
     * @return the sheet summary resources
1201
     */
1202
    public SheetSummaryResources summaryResources() {
1203
        return sheetSummary;
×
1204
    }
1205

1206
    /**
1207
     * Internal function used by all the import routines
1208
     *
1209
     * @param path               endpoint for import
1210
     * @param file               full path to file
1211
     * @param contentType        content type of the file being imported (either CSV or XLSX)
1212
     * @param sheetName          sheetName from caller (can be null)
1213
     * @param headerRowIndex     headerRowIndex from caller (can be null)
1214
     * @param primaryColumnIndex primaryColumnIndex from caller (can be null)
1215
     * @return the new imported sheet
1216
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1217
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1218
     * @throws ResourceNotFoundException   : if the resource can not be found
1219
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1220
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1221
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1222
     */
1223
    private Sheet importFile(String path, String file, String contentType, String sheetName, Integer headerRowIndex,
1224
                             Integer primaryColumnIndex) throws SmartsheetException {
1225
        Util.throwIfNull(path, file, contentType);
×
1226
        Util.throwIfEmpty(path, file, contentType);
×
1227

1228
        File f = new File(file);
×
1229
        Map<String, Object> parameters = new HashMap<>();
×
1230
        if (sheetName == null) {
×
1231
            sheetName = f.getName();
×
1232
        }
1233
        parameters.put("sheetName", sheetName);
×
1234
        parameters.put("headerRowIndex", headerRowIndex);
×
1235
        parameters.put("primaryColumnIndex", primaryColumnIndex);
×
1236
        path = QueryUtil.generateUrl(path, parameters);
×
1237
        HttpRequest request = createHttpRequest(this.smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
×
1238
        request.getHeaders().put("Content-Disposition", "attachment");
×
1239
        request.getHeaders().put("Content-Type", contentType);
×
1240

1241
        InputStream is = null;
×
1242
        try {
1243
            is = new FileInputStream(f);
×
1244
        } catch (FileNotFoundException e) {
×
1245
            throw new SmartsheetException(e);
×
1246
        }
×
1247

1248
        HttpEntity entity = new HttpEntity();
×
1249
        entity.setContentType(contentType);
×
1250
        entity.setContent(is);
×
1251
        entity.setContentLength(f.length());
×
1252
        request.setEntity(entity);
×
1253

1254
        Sheet obj = null;
×
1255
        try {
1256
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
1257
            switch (response.getStatusCode()) {
×
1258
                case 200:
1259
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(Sheet.class,
×
1260
                            response.getEntity().getContent()).getResult();
×
1261
                    break;
×
1262
                default:
1263
                    handleError(response);
×
1264
            }
1265
        } finally {
1266
            smartsheet.getHttpClient().releaseConnection();
×
1267
        }
1268

1269
        return obj;
×
1270
    }
1271

1272
    /**
1273
     * Get a sheet as a file.
1274
     * <p>
1275
     *
1276
     * @param id           the id
1277
     * @param paperSize    the paper size
1278
     * @param outputStream the OutputStream to which the Excel file will be written
1279
     * @param contentType  the content type
1280
     * @throws InvalidRequestException     : if there is any problem with the REST API request
1281
     * @throws AuthorizationException      : if there is any problem with the REST API authorization(access token)
1282
     * @throws ResourceNotFoundException   : if the resource can not be found
1283
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1284
     * @throws SmartsheetRestException     : if there is any other REST API related error occurred during the operation
1285
     * @throws SmartsheetException         : if there is any other error occurred during the operation
1286
     */
1287
    private void getSheetAsFile(long id, PaperSize paperSize, OutputStream outputStream, String contentType)
1288
            throws SmartsheetException {
1289
        Util.throwIfNull(outputStream, contentType);
1✔
1290

1291
        String path = SHEETS + "/" + id;
1✔
1292
        if (paperSize != null) {
1✔
1293
            path += "?paperSize=" + paperSize;
1✔
1294
        }
1295

1296
        HttpRequest request;
1297
        request = createHttpRequest(this.getSmartsheet().getBaseURI().resolve(path), HttpMethod.GET);
1✔
1298
        request.getHeaders().put("Accept", contentType);
1✔
1299

1300
        com.smartsheet.api.internal.http.HttpResponse response = getSmartsheet().getHttpClient().request(request);
1✔
1301

1302
        switch (response.getStatusCode()) {
1✔
1303
            case 200:
1304
                try {
1305
                    copyStream(response.getEntity().getContent(), outputStream);
1✔
1306
                } catch (IOException e) {
×
1307
                    throw new SmartsheetException(e);
×
1308
                }
1✔
1309
                break;
1310
            default:
1311
                handleError(response);
×
1312
        }
1313

1314
        getSmartsheet().getHttpClient().releaseConnection();
1✔
1315
    }
1✔
1316

1317
    /**
1318
     * Copy stream.
1319
     *
1320
     * @param input  the input
1321
     * @param output the output
1322
     * @throws IOException Signals that an I/O exception has occurred.
1323
     * @deprecated replace with StreamUtil.copyContentIntoOutputStream()
1324
     */
1325
    @Deprecated(since = "2.0.0", forRemoval = true)
1326
    private static void copyStream(InputStream input, OutputStream output) throws IOException {
1327
        byte[] buffer = new byte[BUFFER_SIZE];
1✔
1328
        int len;
1329
        while ((len = input.read(buffer)) != -1) {
1✔
1330
            output.write(buffer, 0, len);
1✔
1331
        }
1332
    }
1✔
1333

1334
}
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