• 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

20.37
/src/main/java/com/smartsheet/api/internal/RowColumnResourcesImpl.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.RowColumnResources;
24
import com.smartsheet.api.SmartsheetException;
25
import com.smartsheet.api.internal.http.HttpEntity;
26
import com.smartsheet.api.internal.http.HttpMethod;
27
import com.smartsheet.api.internal.http.HttpRequest;
28
import com.smartsheet.api.internal.http.HttpResponse;
29
import com.smartsheet.api.internal.util.QueryUtil;
30
import com.smartsheet.api.internal.util.Util;
31
import com.smartsheet.api.models.CellHistory;
32
import com.smartsheet.api.models.PagedResult;
33
import com.smartsheet.api.models.PaginationParameters;
34
import com.smartsheet.api.models.enums.CellHistoryInclusion;
35

36
import java.io.File;
37
import java.io.FileInputStream;
38
import java.io.FileNotFoundException;
39
import java.io.InputStream;
40
import java.net.URLEncoder;
41
import java.nio.charset.StandardCharsets;
42
import java.util.EnumSet;
43
import java.util.HashMap;
44
import java.util.Map;
45

46
/**
47
 * This is the implementation of the RowColumnResources.
48
 * <p>
49
 * Thread Safety: This class is thread safe because it is immutable and its base class is thread safe.
50
 */
51
public class RowColumnResourcesImpl extends AbstractResources implements RowColumnResources {
52
    private static final String SHEETS_PATH = "sheets/";
53
    private static final String ROWS_PATH = "/rows/";
54
    private static final String COLUMNS_PATH = "/columns/";
55
    private static final String CELL_IMAGES_PATH = "/cellimages";
56

57
    /**
58
     * Constructor.
59
     * <p>
60
     * Parameters: - smartsheet : the SmartsheetImpl
61
     * <p>
62
     * Exceptions: - IllegalArgumentException : if any argument is null
63
     *
64
     * @param smartsheet the smartsheet
65
     */
66
    public RowColumnResourcesImpl(SmartsheetImpl smartsheet) {
67
        super(smartsheet);
1✔
68
    }
1✔
69

70
    /**
71
     * Get the cell modification history.
72
     * <p>
73
     * It mirrors to the following Smartsheet REST API method: GET /sheets/{sheetId}/rows/{rowId}/columns/{columnId}/history
74
     * <p>
75
     * Exceptions:
76
     *   InvalidRequestException : if there is any problem with the REST API request
77
     *   AuthorizationException : if there is any problem with the REST API authorization(access token)
78
     *   ResourceNotFoundException : if the resource can not be found
79
     *   ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
80
     *   SmartsheetRestException : if there is any other REST API related error occurred during the operation
81
     *   SmartsheetException : if there is any other error occurred during the operation
82
     *
83
     * @param rowId the row id
84
     * @param columnId the column id
85
     * @param sheetId the sheet Id
86
     * @param parameters the pagination parameters
87
     * @return the modification history (note that if there is no such resource, this method will throw
88
     *     ResourceNotFoundException rather than returning null).
89
     * @throws SmartsheetException the smartsheet exception
90
     */
91
    public PagedResult<CellHistory> getCellHistory(
92
            long sheetId,
93
            long rowId,
94
            long columnId,
95
            PaginationParameters parameters
96
    ) throws SmartsheetException {
97
        return this.getCellHistory(sheetId, rowId, columnId, parameters, null, null);
1✔
98
    }
99

100
    /**
101
     * Get the cell modification history.
102
     * <p>
103
     * It mirrors to the following Smartsheet REST API method: GET /sheets/{sheetId}/rows/{rowId}/columns/{columnId}/history
104
     * <p>
105
     * Exceptions:
106
     *   InvalidRequestException : if there is any problem with the REST API request
107
     *   AuthorizationException : if there is any problem with the REST API authorization(access token)
108
     *   ResourceNotFoundException : if the resource can not be found
109
     *   ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
110
     *   SmartsheetRestException : if there is any other REST API related error occurred during the operation
111
     *   SmartsheetException : if there is any other error occurred during the operation
112
     *
113
     * @param rowId the row id
114
     * @param columnId the column id
115
     * @param sheetId the sheet Id
116
     * @param pagination the pagination parameters
117
     * @param includes cell history inclusion
118
     * @param level compatibility level
119
     * @return the modification history (note that if there is no such resource, this method will throw
120
     *      ResourceNotFoundException rather than returning null).
121
     * @throws SmartsheetException the smartsheet exception
122
     */
123
    public PagedResult<CellHistory> getCellHistory(long sheetId, long rowId, long columnId, PaginationParameters pagination,
124
                                                   EnumSet<CellHistoryInclusion> includes, Integer level) throws SmartsheetException {
125
        String path = SHEETS_PATH + sheetId + ROWS_PATH + rowId + COLUMNS_PATH + columnId + "/history";
1✔
126

127
        Map<String, Object> parameters = new HashMap<>();
1✔
128
        if (pagination != null) {
1✔
129
            parameters = pagination.toHashMap();
1✔
130
        }
131
        if (level != null) {
1✔
132
            parameters.put("level", level);
×
133
        }
134
        parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
1✔
135

136
        path += QueryUtil.generateUrl(null, parameters);
1✔
137

138
        return this.listResourcesWithWrapper(path, CellHistory.class);
1✔
139
    }
140

141
    /**
142
     * Add an image to a cell.
143
     * <p>
144
     * It mirrors the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/{rowId}/columns/{columnId}/cellimages
145
     * <p>
146
     * Exceptions:
147
     *   InvalidRequestException : if there is any problem with the REST API request
148
     *   AuthorizationException : if there is any problem with the REST API authorization(access token)
149
     *   ResourceNotFoundException : if the resource can not be found
150
     *   ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
151
     *   SmartsheetRestException : if there is any other REST API related error occurred during the operation
152
     *   SmartsheetException : if there is any other error occurred during the operation
153
     *
154
     * @param sheetId the sheet Id
155
     * @param rowId the row id
156
     * @param columnId the column id
157
     * @param file the file path
158
     * @param contentType MIME type of the image
159
     * @throws SmartsheetException the smartsheet exception
160
     * @throws FileNotFoundException image file not found
161
     */
162
    public void addImageToCell(
163
            long sheetId,
164
            long rowId,
165
            long columnId,
166
            String file,
167
            String contentType
168
    ) throws FileNotFoundException, SmartsheetException {
169
        Util.throwIfNull(file);
×
170
        File f = new File(file);
×
171
        String path = SHEETS_PATH + sheetId + ROWS_PATH + rowId + COLUMNS_PATH + columnId + CELL_IMAGES_PATH;
×
172
        addImage(path, new FileInputStream(f), contentType, f.length(), false, null, file);
×
173
    }
×
174

175
    /**
176
     * Add an image to a cell.
177
     * <p>
178
     * It mirrors the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/{rowId}/columns/{columnId}/cellimages
179
     * <p>
180
     * Exceptions:
181
     *   InvalidRequestException : if there is any problem with the REST API request
182
     *   AuthorizationException : if there is any problem with the REST API authorization(access token)
183
     *   ResourceNotFoundException : if the resource can not be found
184
     *   ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
185
     *   SmartsheetRestException : if there is any other REST API related error occurred during the operation
186
     *   SmartsheetException : if there is any other error occurred during the operation
187
     *
188
     * @param sheetId the sheet Id
189
     * @param rowId the row id
190
     * @param columnId the column id
191
     * @param file the file path
192
     * @param contentType MIME type of the image
193
     * @param overrideValidation override column type validation if true
194
     * @param altText alternate description for the image
195
     * @throws SmartsheetException the smartsheet exception
196
     * @throws FileNotFoundException image file not found
197
     */
198
    public void addImageToCell(
199
            long sheetId,
200
            long rowId,
201
            long columnId,
202
            String file,
203
            String contentType,
204
            boolean overrideValidation,
205
            String altText
206
    ) throws FileNotFoundException, SmartsheetException {
207
        Util.throwIfNull(file);
×
208
        File f = new File(file);
×
209
        String path = SHEETS_PATH + sheetId + ROWS_PATH + rowId + COLUMNS_PATH + columnId + CELL_IMAGES_PATH;
×
210
        addImage(path, new FileInputStream(f), contentType, f.length(), overrideValidation, altText, file);
×
211
    }
×
212

213
    /**
214
     * Add an image to a cell.
215
     * <p>
216
     * It mirrors the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/{rowId}/columns/{columnId}/cellimages
217
     * <p>
218
     * Exceptions:
219
     *   InvalidRequestException : if there is any problem with the REST API request
220
     *   AuthorizationException : if there is any problem with the REST API authorization(access token)
221
     *   ResourceNotFoundException : if the resource can not be found
222
     *   ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
223
     *   SmartsheetRestException : if there is any other REST API related error occurred during the operation
224
     *   SmartsheetException : if there is any other error occurred during the operation
225
     *
226
     * @param sheetId the sheet Id
227
     * @param rowId the row id
228
     * @param columnId the column id
229
     * @param file the File object
230
     * @param contentType MIME type
231
     * @param overrideValidation override column type validation if true
232
     * @param altText alternate description for the image
233
     * @throws SmartsheetException the smartsheet exception
234
     * @throws FileNotFoundException image file not found
235
     */
236
    public void addImageToCell(long sheetId, long rowId, long columnId, File file, String contentType,
237
                               boolean overrideValidation, String altText) throws FileNotFoundException, SmartsheetException {
238
        Util.throwIfNull(file);
×
239
        String path = SHEETS_PATH + sheetId + ROWS_PATH + rowId + COLUMNS_PATH + columnId + CELL_IMAGES_PATH;
×
240
        addImage(path, new FileInputStream(file), contentType, file.length(), overrideValidation, altText, file.getName());
×
241
    }
×
242

243
    /**
244
     * Add an image to a cell.
245
     * <p>
246
     * It mirrors the following Smartsheet REST API method: POST /sheets/{sheetId}/rows/{rowId}/columns/{columnId}/cellimages
247
     * <p>
248
     * Exceptions:
249
     *   InvalidRequestException : if there is any problem with the REST API request
250
     *   AuthorizationException : if there is any problem with the REST API authorization(access token)
251
     *   ResourceNotFoundException : if the resource can not be found
252
     *   ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
253
     *   SmartsheetRestException : if there is any other REST API related error occurred during the operation
254
     *   SmartsheetException : if there is any other error occurred during the operation
255
     *
256
     * @param sheetId the sheet Id
257
     * @param rowId the row id
258
     * @param columnId the column id
259
     * @param inputStream the input stream of the contents
260
     * @param contentType MIME type
261
     * @param contentLength length of the input stream
262
     * @param overrideValidation override column type validation if true
263
     * @param altText alternate description for the image
264
     * @throws SmartsheetException the smartsheet exception
265
     */
266
    public void addImageToCell(long sheetId, long rowId, long columnId, InputStream inputStream, String contentType,
267
                               long contentLength, boolean overrideValidation, String altText) throws SmartsheetException {
268
        Util.throwIfNull(inputStream);
×
269
        String path = SHEETS_PATH + sheetId + ROWS_PATH + rowId + COLUMNS_PATH + columnId + CELL_IMAGES_PATH;
×
270
        addImage(path, inputStream, contentType, contentLength, overrideValidation, altText, altText);
×
271
    }
×
272

273
    private void addImage(String path, InputStream inputStream, String contentType, long contentLength,
274
                          boolean overrideValidation, String altText, String imageName) throws SmartsheetException {
275
        if (imageName == null) {
×
276
            inputStream.toString();
×
277
        }
278
        if (contentType == null) {
×
279
            contentType = "application/octet-stream";
×
280
        }
281

282
        Map<String, Object> parameters = new HashMap<>();
×
283
        if (altText != null) {
×
284
            parameters.put("altText", altText);
×
285
        }
286
        if (overrideValidation) {
×
287
            parameters.put("overrideValidation", "true");
×
288
        }
289
        path += QueryUtil.generateUrl(null, parameters);
×
290

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

295
        HttpEntity entity = new HttpEntity();
×
296
        entity.setContentType(contentType);
×
297
        entity.setContent(inputStream);
×
298
        entity.setContentLength(contentLength);
×
299
        request.setEntity(entity);
×
300

301
        HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
302
        switch (response.getStatusCode()) {
×
303
            case 200:
304
                break;
×
305
            default:
306
                handleError(response);
×
307
        }
308
        smartsheet.getHttpClient().releaseConnection();
×
309
    }
×
310
}
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