• 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

2.2
/src/main/java/com/smartsheet/api/internal/SheetSummaryResourcesImpl.java
1
package com.smartsheet.api.internal;
2

3
/*
4
 * #[license]
5
 * Smartsheet Java SDK
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.SheetSummaryResources;
28
import com.smartsheet.api.SmartsheetException;
29
import com.smartsheet.api.internal.http.HttpEntity;
30
import com.smartsheet.api.internal.http.HttpMethod;
31
import com.smartsheet.api.internal.http.HttpRequest;
32
import com.smartsheet.api.internal.http.HttpResponse;
33
import com.smartsheet.api.internal.util.QueryUtil;
34
import com.smartsheet.api.internal.util.Util;
35
import com.smartsheet.api.models.BulkItemResult;
36
import com.smartsheet.api.models.PagedResult;
37
import com.smartsheet.api.models.PaginationParameters;
38
import com.smartsheet.api.models.Result;
39
import com.smartsheet.api.models.SheetSummary;
40
import com.smartsheet.api.models.SummaryField;
41
import com.smartsheet.api.models.enums.SummaryFieldExclusion;
42
import com.smartsheet.api.models.enums.SummaryFieldInclusion;
43

44
import java.io.ByteArrayInputStream;
45
import java.io.ByteArrayOutputStream;
46
import java.io.File;
47
import java.io.FileInputStream;
48
import java.io.FileNotFoundException;
49
import java.io.InputStream;
50
import java.net.URLEncoder;
51
import java.nio.charset.StandardCharsets;
52
import java.util.EnumSet;
53
import java.util.HashMap;
54
import java.util.List;
55
import java.util.Map;
56
import java.util.Set;
57

58
public class SheetSummaryResourcesImpl extends AbstractResources implements SheetSummaryResources {
59
    private static final String SHEETS_PATH = "sheets/";
60

61
    private static final String SUMMARY = "summary";
62
    private static final String FIELDS = "fields";
63
    private static final String IMAGES = "images";
64
    private static final String RENAME_IF_CONFLICT = "renameIfConflict";
65

66
    /**
67
     * Constructor.
68
     * <p>
69
     * Exceptions: - IllegalArgumentException : if any argument is null
70
     *
71
     * @param smartsheet the smartsheet
72
     */
73
    public SheetSummaryResourcesImpl(SmartsheetImpl smartsheet) {
74
        super(smartsheet);
1✔
75
    }
1✔
76

77
    /**
78
     * Gets the sheet summary
79
     * <p>
80
     * It mirrors to the following Smartsheet REST API method: GET /sheets/{id}/summary
81
     *
82
     * @param sheetId the sheet id
83
     * @param includes optional objects to include
84
     * @param excludes optional objects to exclude
85
     * @return the sheet summary
86
     * @throws IllegalArgumentException if any argument is null or empty string
87
     * @throws com.smartsheet.api.InvalidRequestException if there is any problem with the REST API request
88
     * @throws com.smartsheet.api.AuthorizationException if there is any problem with  the REST API authorization (access token)
89
     * @throws com.smartsheet.api.ResourceNotFoundException if the resource cannot be found
90
     * @throws com.smartsheet.api.ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
91
     * @throws SmartsheetException if there is any other error during the operation
92
     **/
93
    @Override
94
    public SheetSummary getSheetSummary(
95
            long sheetId,
96
            EnumSet<SummaryFieldInclusion> includes,
97
            EnumSet<SummaryFieldExclusion> excludes
98
    ) throws SmartsheetException {
99
        String path = SHEETS_PATH + sheetId + "/summary";
×
100

101
        // Add the parameters to a map and build the query string at the end
102
        Map<String, Object> parameters = new HashMap<>();
×
103

104
        parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
×
105
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
×
106

107
        // Iterate through the map of parameters and generate the query string
108
        path += QueryUtil.generateUrl(null, parameters);
×
109

110
        return this.getResource(path, SheetSummary.class);
×
111
    }
112

113
    /**
114
     * Gets the sheet summary fields
115
     * <p>
116
     * It mirrors to the following Smartsheet REST API method: GET /sheets/{id}/summary/fields
117
     *
118
     * @param sheetId the sheet id
119
     * @param includes optional objects to include
120
     * @param excludes optional objects to exclude
121
     * @param pagination pagination parameters for the response
122
     * @return the list of sheet summary fields
123
     * @throws IllegalArgumentException if any argument is null or empty string
124
     * @throws InvalidRequestException if there is any problem with the REST API request
125
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
126
     * @throws ResourceNotFoundException if the resource cannot be found
127
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
128
     * @throws SmartsheetException if there is any other error during the operation
129
     */
130
    public PagedResult<SummaryField> getSheetSummaryFields(long sheetId, EnumSet<SummaryFieldInclusion> includes,
131
                                                           EnumSet<SummaryFieldExclusion> excludes,
132
                                                           PaginationParameters pagination) throws SmartsheetException {
133
        String path = SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS;
×
134

135
        Map<String, Object> parameters = new HashMap<>();
×
136
        if (pagination != null) {
×
137
            parameters = pagination.toHashMap();
×
138
        }
139
        parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
×
140
        parameters.put("exclude", QueryUtil.generateCommaSeparatedList(excludes));
×
141

142
        path += QueryUtil.generateUrl(null, parameters);
×
143
        return this.listResourcesWithWrapper(path, SummaryField.class);
×
144
    }
145

146
    /**
147
     * Insert fields into a sheet summary.
148
     * <p>
149
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/summary/fields
150
     *
151
     * @param sheetId the sheet id
152
     * @param fields a list of summary fields to add
153
     * @param renameIfConflict true if the call should rename conflicting field titles
154
     * @return the list of created fields
155
     * @throws IllegalArgumentException if any argument is null or empty string
156
     * @throws InvalidRequestException if there is any problem with the REST API request
157
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
158
     * @throws ResourceNotFoundException if the resource cannot be found
159
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
160
     * @throws SmartsheetException if there is any other error during the operation
161
     */
162
    public List<SummaryField> addSheetSummaryFields(
163
            long sheetId,
164
            List<SummaryField> fields,
165
            Boolean renameIfConflict
166
    ) throws SmartsheetException {
167
        String path = SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS;
×
168

169
        Map<String, Object> parameters = new HashMap<>();
×
170
        parameters.put(RENAME_IF_CONFLICT, renameIfConflict);
×
171

172
        path += QueryUtil.generateUrl(null, parameters);
×
173
        return this.postAndReceiveList(path, fields, SummaryField.class);
×
174
    }
175

176
    /**
177
     * Insert fields into a sheet summary.
178
     * <p>
179
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/summary/fields
180
     *
181
     * @param sheetId the sheet id
182
     * @param fields the list of summary fields to add
183
     * @param renameIfConflict true if the call should rename conflicting field titles
184
     * @return a bulk item result containing the created fields
185
     * @throws IllegalArgumentException if any argument is null or empty string
186
     * @throws InvalidRequestException if there is any problem with the REST API request
187
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
188
     * @throws ResourceNotFoundException if the resource cannot be found
189
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
190
     * @throws SmartsheetException if there is any other error during the operation
191
     */
192
    public BulkItemResult<SummaryField> addSheetSummaryFieldsWithPartialSuccess(long sheetId, List<SummaryField> fields,
193
                                                                                Boolean renameIfConflict) throws SmartsheetException {
194
        return this.doBulkOperation(sheetId, fields, renameIfConflict, HttpMethod.POST);
×
195
    }
196

197
    /**
198
     * Update fields in a sheet summary.
199
     * <p>
200
     * It mirrors to the following Smartsheet REST API method: PUT /sheets/{sheetId}/summary/fields
201
     *
202
     * @param sheetId the sheet id
203
     * @param fields a list of summary fields to update
204
     * @param renameIfConflict true if the call should rename conflicting field titles
205
     * @return the updated fields
206
     * @throws IllegalArgumentException if any argument is null or empty string
207
     * @throws InvalidRequestException if there is any problem with the REST API request
208
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
209
     * @throws ResourceNotFoundException if the resource cannot be found
210
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
211
     * @throws SmartsheetException if there is any other error during the operation
212
     */
213
    public List<SummaryField> updateSheetSummaryFields(
214
            long sheetId,
215
            List<SummaryField> fields,
216
            Boolean renameIfConflict
217
    ) throws SmartsheetException {
218
        String path = SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS;
×
219

220
        Map<String, Object> parameters = new HashMap<>();
×
221
        parameters.put(RENAME_IF_CONFLICT, renameIfConflict);
×
222

223
        path += QueryUtil.generateUrl(null, parameters);
×
224
        return this.putAndReceiveList(path, fields, SummaryField.class);
×
225
    }
226

227
    /**
228
     * Update fields in a sheet summary.
229
     * <p>
230
     * It mirrors to the following Smartsheet REST API method: PUT /sheets/{sheetId}/summary/fields
231
     *
232
     * @param sheetId the sheet id
233
     * @param fields a list of summary fields to update
234
     * @param renameIfConflict true if the call should rename conflicting field titles
235
     * @return a bulk item result containing the updated fields
236
     * @throws IllegalArgumentException if any argument is null or empty string
237
     * @throws InvalidRequestException if there is any problem with the REST API request
238
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
239
     * @throws ResourceNotFoundException if the resource cannot be found
240
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
241
     * @throws SmartsheetException if there is any other error during the operation
242
     */
243
    public BulkItemResult<SummaryField> updateSheetSummaryFieldsWithPartialSuccess(long sheetId, List<SummaryField> fields,
244
                                                                                   Boolean renameIfConflict) throws SmartsheetException {
245
        return this.doBulkOperation(sheetId, fields, renameIfConflict, HttpMethod.PUT);
×
246
    }
247

248
    /**
249
     * Delete fields in a sheet summary.
250
     * <p>
251
     * It mirrors to the following Smartsheet REST API method: DELETE /sheets/{sheetId}/summary/fields
252
     *
253
     * @param sheetId the sheet id
254
     * @param fieldIds List of field Ids
255
     * @param ignoreSummaryFieldsNotFound true if the call should ignore fields not found
256
     * @return List of field Ids deleted
257
     * @throws IllegalArgumentException if any argument is null or empty string
258
     * @throws InvalidRequestException if there is any problem with the REST API request
259
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
260
     * @throws ResourceNotFoundException if the resource cannot be found
261
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
262
     * @throws SmartsheetException if there is any other error during the operation
263
     */
264
    public List<Long> deleteSheetSummaryFields(
265
            long sheetId,
266
            Set<Long> fieldIds,
267
            Boolean ignoreSummaryFieldsNotFound
268
    ) throws SmartsheetException {
269
        Map<String, Object> parameters = new HashMap<>();
×
270
        String path = SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS;
×
271
        parameters.put("ids", QueryUtil.generateCommaSeparatedList(fieldIds));
×
272
        parameters.put("ignoreSummaryFieldsNotFound", ignoreSummaryFieldsNotFound);
×
273

274
        path += QueryUtil.generateUrl(null, parameters);
×
275

276
        return this.deleteListResources(path, Long.class);
×
277
    }
278

279
    /**
280
     * Adds an image to the sheet summary field.
281
     * <p>
282
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/summary/fields/{fieldId}/images
283
     *
284
     * @param sheetId the sheet id
285
     * @param fieldId the summary field id
286
     * @param file path to image file to upload
287
     * @param contentType content-type of the file being uploaded
288
     * @param altText alternate text for the image
289
     * @return Result
290
     * @throws IllegalArgumentException if any argument is null or empty string
291
     * @throws InvalidRequestException if there is any problem with the REST API request
292
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
293
     * @throws ResourceNotFoundException if the resource cannot be found
294
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
295
     * @throws SmartsheetException if there is any other error during the operation
296
     */
297
    public Result<SummaryField> addSheetSummaryFieldImage(
298
            long sheetId,
299
            long fieldId,
300
            String file,
301
            String contentType,
302
            String altText
303
    ) throws SmartsheetException, FileNotFoundException {
304
        Util.throwIfNull(file);
×
305
        File f = new File(file);
×
306
        return addSheetSummaryFieldImage(
×
307
                SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS + "/" + fieldId + "/" + IMAGES,
308
                new FileInputStream(f),
309
                contentType,
310
                f.length(),
×
311
                altText,
312
                file
313
        );
314
    }
315

316
    /**
317
     * Adds an image to the sheet summary field.
318
     * <p>
319
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/summary/fields/{fieldId}/images
320
     *
321
     * @param sheetId the sheet id
322
     * @param fieldId the summary field id
323
     * @param file File to upload
324
     * @param contentType content-type of the file being uploaded
325
     * @param altText alternate text for the image
326
     * @return Result
327
     * @throws IllegalArgumentException if any argument is null or empty string
328
     * @throws InvalidRequestException if there is any problem with the REST API request
329
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
330
     * @throws ResourceNotFoundException if the resource cannot be found
331
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
332
     * @throws SmartsheetException if there is any other error during the operation
333
     */
334
    public Result<SummaryField> addSheetSummaryFieldImage(
335
            long sheetId,
336
            long fieldId,
337
            File file,
338
            String contentType,
339
            String altText
340
    ) throws SmartsheetException, FileNotFoundException {
341
        Util.throwIfNull(file);
×
342
        return addSheetSummaryFieldImage(
×
343
                SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS + "/" + fieldId + "/" + IMAGES,
344
                new FileInputStream(file),
345
                contentType,
346
                file.length(),
×
347
                altText,
348
                file.getName()
×
349
        );
350
    }
351

352
    /**
353
     * Adds an image to the sheet summary field.
354
     * <p>
355
     * It mirrors to the following Smartsheet REST API method: POST /sheets/{sheetId}/summary/fields/{fieldId}/images
356
     *
357
     * @param sheetId the sheet id
358
     * @param fieldId the summary field id
359
     * @param inputStream File to upload
360
     * @param contentType content-type of the file being uploaded
361
     * @param contentLength content length
362
     * @param altText alternate text for the image
363
     * @return Result
364
     * @throws IllegalArgumentException if any argument is null or empty string
365
     * @throws InvalidRequestException if there is any problem with the REST API request
366
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
367
     * @throws ResourceNotFoundException if the resource cannot be found
368
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
369
     * @throws SmartsheetException if there is any other error during the operation
370
     */
371
    public Result<SummaryField> addSheetSummaryFieldImage(
372
            long sheetId,
373
            long fieldId,
374
            InputStream inputStream,
375
            String contentType,
376
            long contentLength,
377
            String altText
378
    ) throws SmartsheetException, FileNotFoundException {
379
        Util.throwIfNull(inputStream);
×
380
        return addSheetSummaryFieldImage(SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS + "/" + fieldId + "/" + IMAGES, inputStream,
×
381
                contentType, contentLength, altText, altText);
382
    }
383

384
    private Result<SummaryField> addSheetSummaryFieldImage(
385
            String path,
386
            InputStream inputStream,
387
            String contentType,
388
            long contentLength,
389
            String altText,
390
            String imageName
391
    ) throws SmartsheetException, FileNotFoundException {
392
        if (imageName == null) {
×
393
            inputStream.toString();
×
394
        }
395
        if (contentType == null) {
×
396
            contentType = "application/octet-stream";
×
397
        }
398

399
        Map<String, Object> parameters = new HashMap<>();
×
400
        if (altText != null) {
×
401
            parameters.put("altText", altText);
×
402
        }
403
        path += QueryUtil.generateUrl(null, parameters);
×
404

405
        HttpRequest request = createHttpRequest(this.smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
×
406
        String contentDispositionValue = "attachment; filename=\"" + URLEncoder.encode(imageName, StandardCharsets.UTF_8) + "\"";
×
407
        request.getHeaders().put("Content-Disposition", contentDispositionValue);
×
408

409
        HttpEntity entity = new HttpEntity();
×
410
        entity.setContentType(contentType);
×
411
        entity.setContent(inputStream);
×
412
        entity.setContentLength(contentLength);
×
413
        request.setEntity(entity);
×
414

415
        Result<SummaryField> obj = null;
×
416
        HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
417
        switch (response.getStatusCode()) {
×
418
            case 200:
419
                obj = this.smartsheet.getJsonSerializer().deserializeResult(SummaryField.class,
×
420
                        response.getEntity().getContent());
×
421
                break;
×
422
            default:
423
                handleError(response);
×
424
        }
425
        smartsheet.getHttpClient().releaseConnection();
×
426
        return obj;
×
427
    }
428

429
    private BulkItemResult<SummaryField> doBulkOperation(
430
            long sheetId,
431
            List<SummaryField> fields,
432
            Boolean renameIfConflict,
433
            HttpMethod method
434
    ) throws SmartsheetException {
435
        String path = SHEETS_PATH + sheetId + "/" + SUMMARY + "/" + FIELDS;
×
436
        Map<String, Object> parameters = new HashMap<>();
×
437
        parameters.put("allowPartialSuccess", "true");
×
438
        parameters.put(RENAME_IF_CONFLICT, renameIfConflict);
×
439

440
        path = QueryUtil.generateUrl(path, parameters);
×
441

442
        HttpRequest request;
443
        request = createHttpRequest(smartsheet.getBaseURI().resolve(path), method);
×
444

445
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
×
446

447
        this.smartsheet.getJsonSerializer().serialize(fields, baos);
×
448

449
        HttpEntity entity = new HttpEntity();
×
450
        entity.setContentType("application/json");
×
451
        entity.setContent(new ByteArrayInputStream(baos.toByteArray()));
×
452
        entity.setContentLength(baos.size());
×
453
        request.setEntity(entity);
×
454

455
        HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
456

457
        BulkItemResult<SummaryField> result = null;
×
458
        switch (response.getStatusCode()) {
×
459
            case 200:
460
                result = this.smartsheet.getJsonSerializer().deserializeBulkItemResult(SummaryField.class,
×
461
                        response.getEntity().getContent());
×
462
                break;
×
463
            default:
464
                handleError(response);
×
465
        }
466

467
        smartsheet.getHttpClient().releaseConnection();
×
468

469
        return result;
×
470
    }
471
}
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