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

smartsheet / smartsheet-java-sdk / #41

24 Aug 2023 04:59PM UTC coverage: 50.458% (+0.01%) from 50.444%
#41

push

github-actions

web-flow
Fix Checkstyle Violations in "Impl" Classes (#53)

241 of 241 new or added lines in 32 files covered. (100.0%)

3417 of 6772 relevant lines covered (50.46%)

0.5 hits per line

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

66.67
/src/main/java/com/smartsheet/api/internal/SheetResourcesImpl.java
1
package com.smartsheet.api.internal;
2

3
/*
4
 * #[license]
5
 * Smartsheet SDK for Java
6
 * %%
7
 * Copyright (C) 2023 Smartsheet
8
 * %%
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *      http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 * %[license]
21
 */
22

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

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

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

86
    /** The Constant BUFFER_SIZE. */
87
    private static final int BUFFER_SIZE = 4098;
88

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

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

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

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

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

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

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

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

170
    /**
171
     * Constructor.
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
     * @param includes the source inclusion
195
     * @param pagination the object containing the pagination parameters
196
     * @return A list of all sheets (note that an empty list will be returned if there are none).
197
     * @throws IllegalArgumentException if any argument is null or empty string
198
     * @throws InvalidRequestException if there is any problem with the REST API request
199
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
200
     * @throws ResourceNotFoundException if the resource cannot be found
201
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
202
     * @throws SmartsheetException if there is any other error during the operation
203
     */
204
    public PagedResult<Sheet> listSheets(EnumSet<SourceInclusion> includes, PaginationParameters pagination) throws SmartsheetException {
205
        return this.listSheets(includes, pagination, null);
×
206
    }
207

208
    /**
209
     * List Sheets
210
     */
211
    public PagedResult<Sheet> listSheets(
212
            EnumSet<SourceInclusion> includes,
213
            PaginationParameters pagination,
214
            Date modifiedSince
215
    ) throws SmartsheetException {
216
        String path = SHEETS;
1✔
217

218
        Map<String, Object> parameters = new HashMap<>();
1✔
219
        if (pagination != null) {
1✔
220
            parameters = pagination.toHashMap();
1✔
221
        }
222
        if (modifiedSince != null) {
1✔
223
            String isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(modifiedSince);
×
224
            parameters.put("modifiedSince", isoDate);
×
225
        }
226
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
227

228
        path += QueryUtil.generateUrl(null, parameters);
1✔
229
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
230
    }
231

232
    /**
233
     * List all sheets in the organization.
234
     * <p>
235
     * It mirrors to the following Smartsheet REST API method: GET /users/sheets
236
     * @param parameters the object containing the pagination parameters
237
     * @return all sheets (note that empty list will be returned if there is none)
238
     * @throws InvalidRequestException : if there is any problem with the REST API request
239
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
240
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
241
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
242
     * @throws SmartsheetException : if there is any other error occurred during the operation
243
     */
244
    @Deprecated
245
    public PagedResult<Sheet> listOrganizationSheets(PaginationParameters parameters) throws SmartsheetException {
246
        String path = "users/sheets";
1✔
247

248
        if (parameters != null) {
1✔
249
            path += parameters.toQueryString();
1✔
250
        }
251
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
252
    }
253

254
    /**
255
     * Get a sheet.
256
     * <p>
257
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
258
     * @param id the id
259
     * @param includes used to specify the optional objects to include, currently DISCUSSIONS and ATTACHMENTS are supported.
260
     * @param columnIds the column ids
261
     * @param excludes the exclude parameters
262
     * @param page the page number
263
     * @param pageSize the page size
264
     * @param rowIds the row ids
265
     * @param rowNumbers the row numbers
266
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
267
     *     rather than returning null).
268
     * @throws InvalidRequestException : if there is any problem with the REST API request
269
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
270
     * @throws ResourceNotFoundException : if the resource can not be found
271
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
272
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
273
     * @throws SmartsheetException : if there is any other error occurred during the operation
274
     */
275
    public Sheet getSheet(
276
            long id,
277
            EnumSet<SheetInclusion> includes,
278
            EnumSet<ObjectExclusion> excludes,
279
            Set<Long> rowIds,
280
            Set<Integer> rowNumbers,
281
            Set<Long> columnIds,
282
            Integer pageSize,
283
            Integer page
284
    ) throws SmartsheetException {
285
        return this.getSheet(id, includes, excludes, rowIds, rowNumbers, columnIds, pageSize, page, null, null);
1✔
286
    }
287

288
    /**
289
     * Get a sheet.
290
     * <p>
291
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
292
     * @param id the id
293
     * @param includes used to specify the optional objects to include, currently DISCUSSIONS and ATTACHMENTS are supported.
294
     * @param columnIds the column ids
295
     * @param excludes the exclude parameters
296
     * @param page the page number
297
     * @param pageSize the page size
298
     * @param rowIds the row ids
299
     * @param rowNumbers the row numbers
300
     * @param ifVersionAfter only fetch Sheet if more recent version available
301
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
302
     *     rather than returning 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 ResourceNotFoundException : if the resource can not be found
306
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
307
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
308
     * @throws SmartsheetException : if there is any other error occurred during the operation
309
     */
310
    public Sheet getSheet(
311
            long id,
312
            EnumSet<SheetInclusion> includes,
313
            EnumSet<ObjectExclusion> excludes,
314
            Set<Long> rowIds,
315
            Set<Integer> rowNumbers,
316
            Set<Long> columnIds,
317
            Integer pageSize,
318
            Integer page,
319
            Integer ifVersionAfter
320
    ) throws SmartsheetException {
321
        return this.getSheet(id, includes, excludes, rowIds, rowNumbers, columnIds, pageSize, page, ifVersionAfter, null);
×
322
    }
323

324
    /**
325
     * Get a sheet.
326
     * <p>
327
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}
328
     * @param id the id of the sheet
329
     * @param includes used to specify the optional objects to include.
330
     * @param columnIds the column ids
331
     * @param excludes the exclude parameters
332
     * @param page the page number
333
     * @param pageSize the page size
334
     * @param rowIds the row ids
335
     * @param rowNumbers the row numbers
336
     * @param ifVersionAfter only fetch Sheet if more recent version available
337
     * @param level compatibility level
338
     * @return the sheet resource (note that if there is no such resource, this method will throw
339
     *     ResourceNotFoundException rather than returning null).
340
     * @throws IllegalArgumentException if any argument is null or empty string
341
     * @throws InvalidRequestException if there is any problem with the REST API request
342
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
343
     * @throws ResourceNotFoundException if the resource cannot be found
344
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
345
     * @throws SmartsheetException if there is any other error during the operation
346
     */
347
    public Sheet getSheet(
348
            long id,
349
            EnumSet<SheetInclusion> includes,
350
            EnumSet<ObjectExclusion> excludes,
351
            Set<Long> rowIds,
352
            Set<Integer> rowNumbers,
353
            Set<Long> columnIds,
354
            Integer pageSize,
355
            Integer page,
356
            Integer ifVersionAfter,
357
            Integer level
358
    ) throws SmartsheetException {
359

360
        String path = SHEETS + "/" + id;
1✔
361

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

365
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
366
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
1✔
367
        parameters.put("rowIds", QueryUtil.generateCommaSeparatedList(rowIds));
1✔
368
        parameters.put("rowNumbers", QueryUtil.generateCommaSeparatedList(rowNumbers));
1✔
369
        parameters.put("columnIds", QueryUtil.generateCommaSeparatedList(columnIds));
1✔
370
        parameters.put("pageSize", pageSize);
1✔
371
        parameters.put("page", page);
1✔
372
        parameters.put("ifVersionAfter", ifVersionAfter);
1✔
373
        parameters.put("level", level);
1✔
374

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

378
        return this.getResource(path, Sheet.class);
1✔
379
    }
380

381
    /**
382
     * Get a sheet as an Excel file.
383
     * <p>
384
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/vnd.ms-excel" Accept
385
     * HTTP header
386
     * @param id the id
387
     * @param outputStream the OutputStream to which the Excel file will be written
388
     * @throws IllegalArgumentException : if outputStream is null
389
     * @throws InvalidRequestException : if there is any problem with the REST API request
390
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
391
     * @throws ResourceNotFoundException : if the resource can not be found
392
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
393
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
394
     * @throws SmartsheetException : if there is any other error occurred during the operation
395
     */
396
    public void getSheetAsExcel(long id, OutputStream outputStream) throws SmartsheetException {
397
        getSheetAsFile(id, null, outputStream, "application/vnd.ms-excel");
1✔
398
    }
1✔
399

400
    /**
401
     * Get a sheet as a PDF file.
402
     * <p>
403
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/pdf" Accept HTTP
404
     * header
405
     * @param id the id
406
     * @param outputStream the output stream to which the PDF file will be written.
407
     * @param paperSize the optional paper size
408
     * @throws IllegalArgumentException : if outputStream is null
409
     * @throws InvalidRequestException : if there is any problem with the REST API request
410
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
411
     * @throws ResourceNotFoundException : if the resource can not be found
412
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
413
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
414
     * @throws SmartsheetException : if there is any other error occurred during the operation
415
     */
416
    public void getSheetAsPDF(long id, OutputStream outputStream, PaperSize paperSize) throws SmartsheetException {
417
        getSheetAsFile(id, paperSize, outputStream, "application/pdf");
1✔
418
    }
1✔
419

420
    /**
421
     * Create a sheet in default "Sheets" collection.
422
     * <p>
423
     * It mirrors to the following Smartsheet REST API method: POST /sheets
424
     * @param sheet the sheet to create, limited to the following required attributes: * name (string) *
425
     *     columns (array of Column objects, limited to the following attributes) - title - primary - type - symbol -
426
     *     options
427
     * @return the created sheet
428
     * @throws IllegalArgumentException : if any argument is null
429
     * @throws InvalidRequestException : if there is any problem with the REST API request
430
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
431
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
432
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
433
     * @throws SmartsheetException : if there is any other error occurred during the operation
434
     */
435
    public Sheet createSheet(Sheet sheet) throws SmartsheetException {
436
        return this.createResource(SHEETS, Sheet.class, sheet);
1✔
437
    }
438

439
    /**
440
     * Create a sheet (from existing sheet or template) in default "Sheets" collection.
441
     * <p>
442
     * It mirrors to the following Smartsheet REST API method: POST /sheets
443
     * @param sheet the sheet to create, limited to the following required attributes: * name (string) * fromId
444
     *     (number): ID of the Sheet or Template from which to create the sheet.
445
     * @param includes used to specify the optional objects to include, currently DATA, DISCUSSIONS and ATTACHMENTS are supported.
446
     * @return the sheet
447
     * @throws IllegalArgumentException : if any argument is null
448
     * @throws InvalidRequestException : if there is any problem with the REST API request
449
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
450
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
451
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
452
     * @throws SmartsheetException : if there is any other error occurred during the operation
453
     */
454
    public Sheet createSheetFromTemplate(Sheet sheet, EnumSet<SheetTemplateInclusion> includes) throws SmartsheetException {
455
        Map<String, Object> parameters = new HashMap<>();
1✔
456
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
457
        String path = QueryUtil.generateUrl(SHEETS, parameters);
1✔
458

459
        return this.createResource(path, Sheet.class, sheet);
1✔
460
    }
461

462
    /**
463
     * Imports a sheet.
464
     * <p>
465
     * It mirrors to the following Smartsheet REST API method: POST /sheets/import
466
     * @param file path to the CSV file
467
     * @param sheetName destination sheet name
468
     * @param headerRowIndex index (0 based) of row to be used for column names
469
     * @param primaryColumnIndex index (0 based) of primary column
470
     * @return the created sheet
471
     * @throws IllegalArgumentException if any argument is null or empty string
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 ResourceNotFoundException if the resource cannot be found
475
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
476
     * @throws SmartsheetException if there is any other error during the operation
477
     */
478
    public Sheet importCsv(String file, String sheetName, Integer headerRowIndex, Integer primaryColumnIndex) throws SmartsheetException {
479
        return importFile("sheets/import", file, TEXT_CSV, sheetName, headerRowIndex, primaryColumnIndex);
×
480
    }
481

482
    /**
483
     * Imports a sheet.
484
     * <p>
485
     * It mirrors to the following Smartsheet REST API method: POST /sheets/import
486
     * @param file path to the XLSX file
487
     * @param sheetName destination sheet name
488
     * @param headerRowIndex index (0 based) of row to be used for column names
489
     * @param primaryColumnIndex index (0 based) of primary column
490
     * @return the created sheet
491
     * @throws IllegalArgumentException if any argument is null or empty string
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 ResourceNotFoundException if the resource cannot be found
495
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
496
     * @throws SmartsheetException if there is any other error during the operation
497
     */
498
    public Sheet importXlsx(String file, String sheetName, Integer headerRowIndex, Integer primaryColumnIndex) throws SmartsheetException {
499
        return importFile("sheets/import", file, XLSX_CONTENT_TYPE,
×
500
                sheetName, headerRowIndex, primaryColumnIndex);
501
    }
502

503
    /**
504
     * Create a sheet in given folder.
505
     * <p>
506
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets
507
     * @param folderId the folder id
508
     * @param sheet the sheet to create, limited to the following required
509
     *     attributes: * name (string) * columns (array of Column objects, limited to the following attributes) - title -
510
     *     primary - type - symbol - options
511
     * @return the created sheet
512
     * @throws IllegalArgumentException : if any argument is null
513
     * @throws InvalidRequestException : if there is any problem with the REST API request
514
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
515
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
516
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
517
     * @throws SmartsheetException : if there is any other error occurred during the operation
518
     */
519
    public Sheet createSheetInFolder(long folderId, Sheet sheet) throws SmartsheetException {
520

521
        return this.createResource(FOLDERS + "/" + folderId + "/" + SHEETS, Sheet.class, sheet);
1✔
522
    }
523

524
    /**
525
     * Create a sheet in given folder.
526
     * <p>
527
     * It mirrors to the following Smartsheet REST API method: POST /folder/{folderId}/sheets
528
     * @param folderId the folder id
529
     * @param sheet the sheet
530
     * @param includes the includes
531
     * @return the sheet to create, limited to the following required
532
     *     attributes: * name (string) * columns (array of Column objects, limited to the following attributes) - title -
533
     *     primary - type - symbol - options
534
     * @throws IllegalArgumentException : if any argument is null
535
     * @throws InvalidRequestException : if there is any problem with the REST API request
536
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
537
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
538
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
539
     * @throws SmartsheetException : if there is any other error occurred during the operation
540
     */
541
    public Sheet createSheetInFolderFromTemplate(
542
            long folderId,
543
            Sheet sheet,
544
            EnumSet<SheetTemplateInclusion> includes
545
    ) throws SmartsheetException {
546
        Map<String, Object> parameters = new HashMap<>();
1✔
547
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
548
        String path = QueryUtil.generateUrl(FOLDERS + "/" + folderId + "/" + SHEETS, parameters);
1✔
549

550
        return this.createResource(path, Sheet.class, sheet);
1✔
551
    }
552

553
    /**
554
     * Imports a sheet in given folder.
555
     * <p>
556
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets/import
557
     * @param folderId the folder id
558
     * @param file path to the CSV file
559
     * @param sheetName destination sheet name
560
     * @param headerRowIndex index (0 based) of row to be used for column names
561
     * @param primaryColumnIndex index (0 based) of primary column
562
     * @return the created sheet
563
     * @throws IllegalArgumentException if any argument is null or empty string
564
     * @throws InvalidRequestException if there is any problem with the REST API request
565
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
566
     * @throws ResourceNotFoundException if the resource cannot be found
567
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
568
     * @throws SmartsheetException if there is any other error during the operation
569
     */
570
    public Sheet importCsvInFolder(
571
            long folderId,
572
            String file,
573
            String sheetName,
574
            Integer headerRowIndex,
575
            Integer primaryColumnIndex
576
    ) throws SmartsheetException {
577
        return importFile(
×
578
                FOLDERS + "/" + folderId + "/" + SHEETS + "/" + IMPORT,
579
                file,
580
                TEXT_CSV,
581
                sheetName,
582
                headerRowIndex,
583
                primaryColumnIndex
584
        );
585
    }
586

587
    /**
588
     * Imports a sheet in given folder.
589
     * <p>
590
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/sheets/import
591
     * @param folderId the folder id
592
     * @param file path to the XLSX file
593
     * @param sheetName destination sheet name
594
     * @param headerRowIndex index (0 based) of row to be used for column names
595
     * @param primaryColumnIndex index (0 based) of primary column
596
     * @return the created sheet
597
     * @throws IllegalArgumentException if any argument is null or empty string
598
     * @throws InvalidRequestException if there is any problem with the REST API request
599
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
600
     * @throws ResourceNotFoundException if the resource cannot be found
601
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
602
     * @throws SmartsheetException if there is any other error during the operation
603
     */
604
    public Sheet importXlsxInFolder(
605
            long folderId,
606
            String file,
607
            String sheetName,
608
            Integer headerRowIndex,
609
            Integer primaryColumnIndex
610
    ) throws SmartsheetException {
611
        return importFile(
×
612
                FOLDERS + "/" + folderId + "/" + SHEETS + "/" + IMPORT,
613
                file,
614
                XLSX_CONTENT_TYPE,
615
                sheetName,
616
                headerRowIndex,
617
                primaryColumnIndex
618
        );
619
    }
620

621
    /**
622
     * Create a sheet in given workspace.
623
     * <p>
624
     * It mirrors to the following Smartsheet REST API method: POST /workspace/{workspaceId}/sheets
625
     * @param workspaceId the workspace id
626
     * @param sheet the sheet to create, limited to the following required attributes: * name (string) * columns
627
     *     (array of Column objects, limited to the following attributes) - title - primary - type - symbol - options
628
     * @return the created sheet
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 ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
633
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
634
     * @throws SmartsheetException : if there is any other error occurred during the operation
635
     */
636
    public Sheet createSheetInWorkspace(long workspaceId, Sheet sheet) throws SmartsheetException {
637
        return this.createResource(WORKSPACES + "/" + workspaceId + "/" + SHEETS, Sheet.class, sheet);
1✔
638
    }
639

640
    /**
641
     * Create a sheet (from existing sheet or template) in given workspace.
642
     * <p>
643
     * It mirrors to the following Smartsheet REST API method: POST /workspace/{workspaceId}/sheets
644
     * @param workspaceId the workspace id
645
     * @param sheet the sheet to create, limited to the following required
646
     *      attributes: * name (string) * fromId (number): ID of the Sheet or Template from which to create the sheet. -
647
     *     includes : used to specify the optional objects to include, currently DATA, DISCUSSIONS and ATTACHMENTS are supported.
648
     * @param includes the includes
649
     * @return the created sheet
650
     * @throws IllegalArgumentException : if any argument is null
651
     * @throws InvalidRequestException : if there is any problem with the REST API request
652
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
653
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
654
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
655
     * @throws SmartsheetException : if there is any other error occurred during the operation
656
     */
657
    public Sheet createSheetInWorkspaceFromTemplate(long workspaceId, Sheet sheet, EnumSet<SheetTemplateInclusion> includes)
658
            throws SmartsheetException {
659
        Map<String, Object> parameters = new HashMap<>();
1✔
660
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
661
        String path = QueryUtil.generateUrl(WORKSPACES + "/" + workspaceId + "/" + SHEETS, parameters);
1✔
662

663
        return this.createResource(path, Sheet.class, sheet);
1✔
664
    }
665

666
    /**
667
     * Imports a sheet in given workspace.
668
     * <p>
669
     * It mirrors to the following Smartsheet REST API method: POST /workspaces/{workspaceId}/sheets/import
670
     * @param workspaceId the workspace id
671
     * @param file path to the CSV file
672
     * @param sheetName destination sheet name
673
     * @param headerRowIndex index (0 based) of row to be used for column names
674
     * @param primaryColumnIndex index (0 based) of primary column
675
     * @return the created sheet
676
     * @throws IllegalArgumentException if any argument is null or empty string
677
     * @throws InvalidRequestException if there is any problem with the REST API request
678
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
679
     * @throws ResourceNotFoundException if the resource cannot be found
680
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
681
     * @throws SmartsheetException if there is any other error during the operation
682
     */
683
    public Sheet importCsvInWorkspace(
684
            long workspaceId,
685
            String file,
686
            String sheetName,
687
            Integer headerRowIndex,
688
            Integer primaryColumnIndex
689
    ) throws SmartsheetException {
690
        return importFile(
×
691
                WORKSPACES + "/" + workspaceId + "/" + SHEETS + "/" + IMPORT,
692
                file,
693
                TEXT_CSV,
694
                sheetName,
695
                headerRowIndex,
696
                primaryColumnIndex
697
        );
698
    }
699

700
    /**
701
     * Imports a sheet in given workspace.
702
     * <p>
703
     * It mirrors to the following Smartsheet REST API method: POST /workspaces/{workspaceId}/sheets/import
704
     * @param workspaceId the workspace id
705
     * @param file path to the XLSX file
706
     * @param sheetName destination sheet name
707
     * @param headerRowIndex index (0 based) of row to be used for column names
708
     * @param primaryColumnIndex index (0 based) of primary column
709
     * @return the created sheet
710
     * @throws IllegalArgumentException if any argument is null or empty string
711
     * @throws InvalidRequestException if there is any problem with the REST API request
712
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
713
     * @throws ResourceNotFoundException if the resource cannot be found
714
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
715
     * @throws SmartsheetException if there is any other error during the operation
716
     */
717
    public Sheet importXlsxInWorkspace(
718
            long workspaceId,
719
            String file,
720
            String sheetName,
721
            Integer headerRowIndex,
722
            Integer primaryColumnIndex
723
    ) throws SmartsheetException {
724
        return importFile(
×
725
                WORKSPACES + "/" + workspaceId + "/" + SHEETS + "/" + IMPORT,
726
                file,
727
                XLSX_CONTENT_TYPE,
728
                sheetName,
729
                headerRowIndex,
730
                primaryColumnIndex
731
        );
732
    }
733

734
    /**
735
     * Delete a sheet.
736
     * <p>
737
     * It mirrors to the following Smartsheet REST API method: DELETE /sheet{id}
738
     * @param id the ID of the sheet
739
     * @throws InvalidRequestException : if there is any problem with the REST API request
740
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
741
     * @throws ResourceNotFoundException : if the resource can not be found
742
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
743
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
744
     * @throws SmartsheetException : if there is any other error occurred during the operation
745
     */
746
    public void deleteSheet(long id) throws SmartsheetException {
747
        this.deleteResource(SHEETS + "/" + id, Sheet.class);
1✔
748
    }
1✔
749

750
    /**
751
     * Update a sheet.
752
     * <p>
753
     * It mirrors to the following Smartsheet REST API method: PUT /sheet/{id}
754
     * @param sheet the sheet to update
755
     * @return the updated sheet
756
     * @throws IllegalArgumentException if any argument is null or empty string
757
     * @throws InvalidRequestException if there is any problem with the REST API request
758
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
759
     * @throws ResourceNotFoundException if the resource cannot be found
760
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
761
     * @throws SmartsheetException if there is any other error during the operation
762
     */
763
    public Sheet updateSheet(Sheet sheet) throws SmartsheetException {
764
        return this.updateResource(SHEETS + "/" + sheet.getId(), Sheet.class, sheet);
1✔
765
    }
766

767
    /**
768
     * Get a sheet version.
769
     * <p>
770
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id}/version
771
     * @param id the id
772
     * @return the sheet version (note that if there is no such resource, this method will throw
773
     *      ResourceNotFoundException rather than returning null).
774
     * @throws InvalidRequestException : if there is any problem with the REST API request
775
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
776
     * @throws ResourceNotFoundException : if the resource can not be found
777
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
778
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
779
     * @throws SmartsheetException : if there is any other error occurred during the operation
780
     */
781
    public int getSheetVersion(long id) throws SmartsheetException {
782
        return this.getResource(SHEETS + "/" + id + "/version", Sheet.class).getVersion();
1✔
783
    }
784

785
    /**
786
     * Send a sheet as a PDF attachment via email to the designated recipients.
787
     * <p>
788
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/emails
789
     * @param id the id
790
     * @param email the email
791
     * @throws IllegalArgumentException : if any argument is null
792
     * @throws InvalidRequestException : if there is any problem with the REST API request
793
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
794
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
795
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
796
     * @throws SmartsheetException : if there is any other error occurred during the operation
797
     */
798
    public void sendSheet(long id, SheetEmail email) throws SmartsheetException {
799
        this.createResource(SHEETS + "/" + id + "/emails", SheetEmail.class, email);
1✔
800
    }
1✔
801

802
    /**
803
     * Get a sheet as an Excel file.
804
     * <p>
805
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{id} with "application/vnd.ms-excel" Accept HTTP header
806
     * @param id the id of the sheet
807
     * @param outputStream the output stream to which the Excel file will be written.
808
     * @throws IllegalArgumentException if any argument is null or empty string
809
     * @throws InvalidRequestException if there is any problem with the REST API request
810
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
811
     * @throws ResourceNotFoundException if the resource cannot be found
812
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
813
     * @throws SmartsheetException if there is any other error during the operation
814
     */
815
    public void getSheetAsCSV(long id, OutputStream outputStream) throws SmartsheetException {
816
        getSheetAsFile(id, null, outputStream, TEXT_CSV);
×
817
    }
×
818

819
    /**
820
     * Get the status of the Publish settings of the sheet, including the URLs of any enabled publishings.
821
     * <p>
822
     * It mirrors to the following Smartsheet REST API method: GET /sheet/{sheetId}/publish
823
     * @param id the id
824
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
825
     *      * rather than returning null).
826
     * @throws InvalidRequestException : if there is any problem with the REST API request
827
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
828
     * @throws ResourceNotFoundException : if the resource can not be found
829
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
830
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
831
     * @throws SmartsheetException : if there is any other error occurred during the operation
832
     */
833
    public SheetPublish getPublishStatus(long id) throws SmartsheetException {
834
        return this.getResource(SHEETS + "/" + id + "/publish", SheetPublish.class);
1✔
835
    }
836

837
    /**
838
     * Sets the publish status of a sheet and returns the new status, including the URLs of any enabled publishings.
839
     * <p>
840
     * It mirrors to the following Smartsheet REST API method: PUT /sheet/{sheetId}/publish
841
     * @param id the id
842
     * @param publish the SheetPublish object limited to the following attributes *
843
     *     readOnlyLiteEnabled * readOnlyFullEnabled * readWriteEnabled * icalEnabled
844
     * @return the updated SheetPublish (note that if there is no such resource, this method will throw
845
     *     ResourceNotFoundException rather than returning null).
846
     * @throws IllegalArgumentException : if any argument is null
847
     * @throws InvalidRequestException : if there is any problem with the REST API request
848
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
849
     * @throws ResourceNotFoundException : if the resource can not be found
850
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
851
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
852
     * @throws SmartsheetException : if there is any other error occurred during the operation
853
     */
854
    public SheetPublish updatePublishStatus(long id, SheetPublish publish) throws SmartsheetException {
855
        return this.updateResource(SHEETS + "/" + id + "/publish", SheetPublish.class, publish);
1✔
856
    }
857

858
    /**
859
     * Creates a copy of the specified sheet.
860
     * <p>
861
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/copy
862
     * @param sheetId the sheet id
863
     * @param containerDestination describes the destination container
864
     * @param includes optional parameters to include
865
     * @return the sheet
866
     * @throws IllegalArgumentException : if folder is null
867
     * @throws InvalidRequestException : if there is any problem with the REST API request
868
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
869
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
870
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
871
     * @throws SmartsheetException : if there is any other error occurred during the operation
872
     */
873
    public Sheet copySheet(
874
            long sheetId,
875
            ContainerDestination containerDestination,
876
            EnumSet<SheetCopyInclusion> includes
877
    ) throws SmartsheetException {
878
        return copySheet(sheetId, containerDestination, includes, null);
1✔
879
    }
880

881
    /**
882
     * Creates a copy of the specified sheet.
883
     * <p>
884
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/copy
885
     * @param sheetId the sheet id
886
     * @param containerDestination describes the destination container
887
     * @param includes optional parameters to include
888
     * @param excludes optional parameters to exclude
889
     * @return the sheet
890
     * @throws IllegalArgumentException : if folder is null
891
     * @throws InvalidRequestException : if there is any problem with the REST API request
892
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
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 Sheet copySheet(
898
            long sheetId,
899
            ContainerDestination containerDestination,
900
            EnumSet<SheetCopyInclusion> includes,
901
            EnumSet<CopyExclusion> excludes
902
    ) throws SmartsheetException {
903

904
        String path = SHEETS + "/" + sheetId + "/copy";
1✔
905
        Map<String, Object> parameters = new HashMap<>();
1✔
906

907
        parameters.put(INCLUDE, QueryUtil.generateCommaSeparatedList(includes));
1✔
908
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
1✔
909

910
        path += QueryUtil.generateUrl(null, parameters);
1✔
911

912
        return this.createResource(path, Sheet.class, containerDestination);
1✔
913
    }
914

915
    /**
916
     * Moves the specified Sheet to another location.
917
     * <p>
918
     * It mirrors to the following Smartsheet REST API method: POST /folders/{folderId}/move
919
     * @param sheetId the folder id
920
     * @param containerDestination describes the destination container
921
     * @return the sheet
922
     * @throws IllegalArgumentException : if folder is null
923
     * @throws InvalidRequestException : if there is any problem with the REST API request
924
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
925
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
926
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
927
     * @throws SmartsheetException : if there is any other error occurred during the operation
928
     */
929
    public Sheet moveSheet(long sheetId, ContainerDestination containerDestination) throws SmartsheetException {
930

931
        String path = SHEETS + "/" + sheetId + "/move";
1✔
932
        return this.createResource(path, Sheet.class, containerDestination);
1✔
933
    }
934

935
    /**
936
     * Creates an Update Request for the specified Row(s) within the Sheet.
937
     * <p>
938
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/updaterequests
939
     * @param sheetId the sheet id
940
     * @param email the email
941
     * @return the update request object
942
     * @throws IllegalArgumentException : if any argument is null
943
     * @throws InvalidRequestException : if there is any problem with the REST API request
944
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
945
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
946
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
947
     * @throws SmartsheetException : if there is any other error occurred during the operation
948
     */
949
    public UpdateRequest createUpdateRequest(long sheetId, MultiRowEmail email) throws SmartsheetException {
950
        return this.createResource(SHEETS + "/" + sheetId + "/updaterequests", UpdateRequest.class, email);
1✔
951
    }
952

953
    /**
954
     * Sort a sheet according to the sort criteria.
955
     * <p>
956
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/sort
957
     * @param sheetId the sheet id
958
     * @param sortSpecifier the sort criteria
959
     * @return the update request object
960
     * @throws IllegalArgumentException : if any argument is null
961
     * @throws InvalidRequestException : if there is any problem with the REST API request
962
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
963
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
964
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
965
     * @throws SmartsheetException : if there is any other error occurred during the operation
966
     */
967
    public Sheet sortSheet(long sheetId, SortSpecifier sortSpecifier) throws SmartsheetException {
968
        return this.sortSheet(sheetId, sortSpecifier, null);
1✔
969
    }
970

971
    /**
972
     * Sort a sheet according to the sort criteria.
973
     * <p>
974
     * It mirrors to the following Smartsheet REST API method: POST /sheet/{sheetId}/sort
975
     * @param sheetId the sheet id
976
     * @param sortSpecifier the sort criteria
977
     * @param level compatibility level
978
     * @return the update request object
979
     * @throws SmartsheetException the smartsheet exception
980
     * @throws IllegalArgumentException : if any argument is null
981
     * @throws InvalidRequestException : if there is any problem with the REST API request
982
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
983
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
984
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
985
     * @throws SmartsheetException : if there is any other error occurred during the operation
986
     */
987
    public Sheet sortSheet(long sheetId, SortSpecifier sortSpecifier, Integer level) throws SmartsheetException {
988
        Util.throwIfNull(sortSpecifier);
1✔
989

990
        String path = SHEETS + "/" + sheetId + "/sort";
1✔
991
        if (level != null) {
1✔
992
            path += "?level=" + level;
×
993
        }
994

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

997
        ByteArrayOutputStream objectBytesStream = new ByteArrayOutputStream();
1✔
998
        this.smartsheet.getJsonSerializer().serialize(sortSpecifier, objectBytesStream);
1✔
999

1000
        HttpEntity entity = new HttpEntity();
1✔
1001
        entity.setContentType("application/json");
1✔
1002
        entity.setContent(new ByteArrayInputStream(objectBytesStream.toByteArray()));
1✔
1003
        entity.setContentLength(objectBytesStream.size());
1✔
1004
        request.setEntity(entity);
1✔
1005

1006
        Sheet obj = null;
1✔
1007
        try {
1008
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
1✔
1009
            switch (response.getStatusCode()) {
1✔
1010
                case 200: {
1011
                    InputStream inputStream = response.getEntity().getContent();
1✔
1012
                    try {
1013
                        obj = this.smartsheet.getJsonSerializer().deserialize(Sheet.class, inputStream);
1✔
1014
                    } catch (IOException e) {
×
1015
                        throw new SmartsheetException(e);
×
1016
                    }
1✔
1017
                    break;
1018
                }
1019
                default:
1020
                    handleError(response);
×
1021
            }
1022
        } finally {
1023
            smartsheet.getHttpClient().releaseConnection();
1✔
1024
        }
1025
        return obj;
1✔
1026
    }
1027

1028
    /**
1029
     * Return the ShareResources object that provides access to Share resources associated with Sheet resources.
1030
     *
1031
     * @return the ShareResources object
1032
     */
1033
    public ShareResources shareResources() {
1034
        return this.shares;
1✔
1035
    }
1036

1037
    /**
1038
     * Return the SheetRowResources object that provides access to Row resources associated with Sheet resources.
1039
     *
1040
     * @return the sheet row resources
1041
     */
1042
    public SheetRowResources rowResources() {
1043
        return this.rows;
1✔
1044
    }
1045

1046
    /**
1047
     * Return the SheetColumnResources object that provides access to Column resources associated with Sheet resources.
1048
     *
1049
     * @return the sheet column resources
1050
     */
1051
    public SheetColumnResources columnResources() {
1052
        return this.columns;
1✔
1053
    }
1054

1055
    /**
1056
     * Return the AssociatedAttachmentResources object that provides access to attachment resources associated with
1057
     * Sheet resources.
1058
     *
1059
     * @return the associated attachment resources
1060
     */
1061
    public SheetAttachmentResources attachmentResources() {
1062
        return this.attachments;
×
1063
    }
1064

1065
    /**
1066
     * Return the AssociatedDiscussionResources object that provides access to discussion resources associated with
1067
     * Sheet resources.
1068
     *
1069
     * @return the associated discussion resources
1070
     */
1071
    public SheetDiscussionResources discussionResources() {
1072
        return this.discussions;
1✔
1073
    }
1074

1075
    /**
1076
     * Return the SheetCommentResources object that provides access to discussion resources associated with
1077
     * Sheet resources.
1078
     *
1079
     * @return the associated comment resources
1080
     */
1081
    public SheetCommentResources commentResources() {
1082
        return this.comments;
×
1083
    }
1084

1085
    /**
1086
     * Return the SheetUpdateRequestResources object that provides access to update request resources
1087
     * associated with Sheet resources.
1088
     *
1089
     * @return the associated update request resources
1090
     */
1091
    public SheetUpdateRequestResources updateRequestResources() {
1092
        return this.updateRequests;
×
1093
    }
1094

1095
    /**
1096
     * Return the SheetFilterResources object that provides access to sheet filter resources
1097
     * associated with Sheet resources.
1098
     *
1099
     * @return the associated sheet filter resources
1100
     */
1101
    public SheetFilterResources filterResources() {
1102
        return this.filters;
×
1103
    }
1104

1105
    /**
1106
     * Return the SheetAutomationRuleResources object that provides access to automation rule resources
1107
     * associated with the Sheet resources.
1108
     *
1109
     * @return the associated automation rule resources
1110
     */
1111
    public SheetAutomationRuleResources automationRuleResources() {
1112
        return automationRules;
×
1113
    }
1114

1115
    /**
1116
     * Return the SheetCrossSheetReferenceResources object that provides access to the cross sheet reference resources
1117
     * associated with the Sheet resources.
1118
     *
1119
     * @return the cross sheet reference resources
1120
     */
1121
    public SheetCrossSheetReferenceResources crossSheetReferenceResources() {
1122
        return crossSheetReferences;
×
1123
    }
1124

1125
    /**
1126
     * Return the SheetSummaryResources object that provides access to the sheet summary resources
1127
     * associated with the Sheet resources.
1128
     *
1129
     * @return the sheet summary resources
1130
     */
1131
    public SheetSummaryResources summaryResources() {
1132
        return sheetSummary;
×
1133
    }
1134

1135
    /**
1136
     * Internal function used by all the import routines
1137
     * @param path endpoint for import
1138
     * @param file full path to file
1139
     * @param contentType content type of the file being imported (either CSV or XLSX)
1140
     * @param sheetName sheetName from caller (can be null)
1141
     * @param headerRowIndex headerRowIndex from caller (can be null)
1142
     * @param primaryColumnIndex primaryColumnIndex from caller (can be null)
1143
     * @return the new imported sheet
1144
     * @throws InvalidRequestException : if there is any problem with the REST API request
1145
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
1146
     * @throws ResourceNotFoundException : if the resource can not be found
1147
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1148
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
1149
     * @throws SmartsheetException : if there is any other error occurred during the operation
1150
     */
1151
    private Sheet importFile(String path, String file, String contentType, String sheetName, Integer headerRowIndex,
1152
                             Integer primaryColumnIndex) throws SmartsheetException {
1153
        Util.throwIfNull(path, file, contentType);
×
1154
        Util.throwIfEmpty(path, file, contentType);
×
1155

1156
        File f = new File(file);
×
1157
        Map<String, Object> parameters = new HashMap<>();
×
1158
        if (sheetName == null) {
×
1159
            sheetName = f.getName();
×
1160
        }
1161
        parameters.put("sheetName", sheetName);
×
1162
        parameters.put("headerRowIndex", headerRowIndex);
×
1163
        parameters.put("primaryColumnIndex", primaryColumnIndex);
×
1164
        path = QueryUtil.generateUrl(path, parameters);
×
1165
        HttpRequest request = createHttpRequest(this.smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
×
1166
        request.getHeaders().put("Content-Disposition", "attachment");
×
1167
        request.getHeaders().put("Content-Type", contentType);
×
1168

1169
        InputStream is = null;
×
1170
        try {
1171
            is = new FileInputStream(f);
×
1172
        } catch (FileNotFoundException e) {
×
1173
            throw new SmartsheetException(e);
×
1174
        }
×
1175

1176
        HttpEntity entity = new HttpEntity();
×
1177
        entity.setContentType(contentType);
×
1178
        entity.setContent(is);
×
1179
        entity.setContentLength(f.length());
×
1180
        request.setEntity(entity);
×
1181

1182
        Sheet obj = null;
×
1183
        try {
1184
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
1185
            switch (response.getStatusCode()) {
×
1186
                case 200:
1187
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(Sheet.class,
×
1188
                            response.getEntity().getContent()).getResult();
×
1189
                    break;
×
1190
                default:
1191
                    handleError(response);
×
1192
            }
1193
        } finally {
1194
            smartsheet.getHttpClient().releaseConnection();
×
1195
        }
1196

1197
        return obj;
×
1198
    }
1199

1200
    /**
1201
     * Get a sheet as a file.
1202
     * <p>
1203
     * @param id the id
1204
     * @param paperSize the paper size
1205
     * @param outputStream the OutputStream to which the Excel file will be written
1206
     * @param contentType the content type
1207
     * @throws InvalidRequestException : if there is any problem with the REST API request
1208
     * @throws AuthorizationException : if there is any problem with the REST API authorization(access token)
1209
     * @throws ResourceNotFoundException : if the resource can not be found
1210
     * @throws ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
1211
     * @throws SmartsheetRestException : if there is any other REST API related error occurred during the operation
1212
     * @throws SmartsheetException : if there is any other error occurred during the operation
1213
     */
1214
    private void getSheetAsFile(long id, PaperSize paperSize, OutputStream outputStream, String contentType)
1215
            throws SmartsheetException {
1216
        Util.throwIfNull(outputStream, contentType);
1✔
1217

1218
        String path = SHEETS + "/" + id;
1✔
1219
        if (paperSize != null) {
1✔
1220
            path += "?paperSize=" + paperSize;
1✔
1221
        }
1222

1223
        HttpRequest request;
1224
        request = createHttpRequest(this.getSmartsheet().getBaseURI().resolve(path), HttpMethod.GET);
1✔
1225
        request.getHeaders().put("Accept", contentType);
1✔
1226

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

1229
        switch (response.getStatusCode()) {
1✔
1230
            case 200:
1231
                try {
1232
                    copyStream(response.getEntity().getContent(), outputStream);
1✔
1233
                } catch (IOException e) {
×
1234
                    throw new SmartsheetException(e);
×
1235
                }
1✔
1236
                break;
1237
            default:
1238
                handleError(response);
×
1239
        }
1240

1241
        getSmartsheet().getHttpClient().releaseConnection();
1✔
1242
    }
1✔
1243

1244
    /**
1245
     * Copy stream.
1246
     *
1247
     * @param input the input
1248
     * @param output the output
1249
     * @throws IOException Signals that an I/O exception has occurred.
1250
     */
1251
    private static void copyStream(InputStream input, OutputStream output) throws IOException {
1252
        byte[] buffer = new byte[BUFFER_SIZE];
1✔
1253
        int len;
1254
        while ((len = input.read(buffer)) != -1) {
1✔
1255
            output.write(buffer, 0, len);
1✔
1256
        }
1257
    }
1✔
1258

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

© 2026 Coveralls, Inc