Skip to content

Commit 7a6b77d

Browse files
new changes
1 parent 42846e3 commit 7a6b77d

File tree

4 files changed

+71
-20
lines changed

4 files changed

+71
-20
lines changed

src/main/java/io/cdap/plugin/servicenow/apiclient/ServiceNowTableAPIClientImpl.java

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.cdap.plugin.servicenow.model.SchemaAPISchemaResponse;
3939
import io.cdap.plugin.servicenow.restapi.RestAPIClient;
4040
import io.cdap.plugin.servicenow.restapi.RestAPIResponse;
41+
import io.cdap.plugin.servicenow.source.ServiceNowSourceConfig;
4142
import io.cdap.plugin.servicenow.util.SchemaBuilder;
4243
import io.cdap.plugin.servicenow.util.SchemaType;
4344
import io.cdap.plugin.servicenow.util.ServiceNowColumn;
@@ -81,6 +82,7 @@ public class ServiceNowTableAPIClientImpl extends RestAPIClient {
8182
private static final String GLIDE_DATE_TIME_DATATYPE = "glide_date_time";
8283
private static final Gson GSON = new Gson();
8384
private final ServiceNowConnectorConfig conf;
85+
private ServiceNowSourceConfig sourceConfig;
8486
public final SchemaType schemaType;
8587
public static JsonArray serviceNowJsonResultArray;
8688

@@ -89,11 +91,22 @@ public ServiceNowTableAPIClientImpl(ServiceNowConnectorConfig conf, Boolean useC
8991
this.schemaType = getSchemaTypeBasedOnUseConnection(useConnection);
9092
}
9193

94+
public ServiceNowTableAPIClientImpl(ServiceNowSourceConfig sourceConfig, Boolean useConnection) {
95+
this(sourceConfig, useConnection, null);
96+
}
97+
98+
public ServiceNowTableAPIClientImpl(ServiceNowSourceConfig sourceConfig, Boolean useConnection,
99+
ServiceNowSourceConfig serviceNowSourceConfig) {
100+
this.conf = sourceConfig.getConnection();
101+
this.sourceConfig = serviceNowSourceConfig;
102+
this.schemaType = getSchemaTypeBasedOnUseConnection(useConnection);
103+
}
104+
92105
public String getAccessToken() throws ServiceNowAPIException {
93106
try {
94107
return generateAccessToken(String.format(OAUTH_URL_TEMPLATE, conf.getRestApiEndpoint()),
95-
conf.getClientId(),
96-
conf.getClientSecret(), conf.getUser(), conf.getPassword());
108+
conf.getClientId(),
109+
conf.getClientSecret(), conf.getUser(), conf.getPassword());
97110
} catch (OAuthProblemException | OAuthSystemException e) {
98111
throw new ServiceNowAPIException("An error occurred while authenticating.", e, null, false);
99112
}
@@ -132,13 +145,13 @@ public String getAccessTokenRetryableMode() throws ExecutionException, RetryExce
132145
* @return The list of Map; each Map representing a table row
133146
*/
134147
public List<Map<String, String>> fetchTableRecords(
135-
String tableName,
136-
SourceValueType valueType,
137-
String startDate,
138-
String endDate,
139-
int offset,
140-
int limit)
141-
throws ServiceNowAPIException {
148+
String tableName,
149+
SourceValueType valueType,
150+
String startDate,
151+
String endDate,
152+
int offset,
153+
int limit)
154+
throws ServiceNowAPIException {
142155
ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder(
143156
this.conf.getRestApiEndpoint(), tableName, false, schemaType)
144157
.setExcludeReferenceLink(true)
@@ -149,16 +162,23 @@ public List<Map<String, String>> fetchTableRecords(
149162
requestBuilder.setOffset(offset);
150163
}
151164

152-
applyDateRangeToRequest(requestBuilder, startDate, endDate);
165+
applyQueryToRequest(requestBuilder, startDate, endDate);
153166

154167
String accessToken = getAccessToken();
155168
requestBuilder.setAuthHeader(accessToken);
156169
RestAPIResponse apiResponse = executeGetWithRetries(requestBuilder.build());
157170
return parseResponseToResultListOfMap(apiResponse.getResponseBody());
158171
}
159172

160-
private void applyDateRangeToRequest(ServiceNowTableAPIRequestBuilder requestBuilder, String startDate,
161-
String endDate) {
173+
private void applyQueryToRequest(ServiceNowTableAPIRequestBuilder requestBuilder, String startDate,
174+
String endDate) {
175+
// Check if a query is provided in the source configuration
176+
if (sourceConfig != null && !Strings.isNullOrEmpty(sourceConfig.getQuery())) {
177+
requestBuilder.setQuery(sourceConfig.getQuery());
178+
return;
179+
}
180+
181+
// Fallback to the existing date range logic if no query is provided
162182
String dateRange = generateDateRangeQuery(startDate, endDate);
163183
if (!Strings.isNullOrEmpty(dateRange)) {
164184
requestBuilder.setQuery(dateRange);
@@ -250,8 +270,8 @@ public List<Map<String, String>> fetchTableRecordsRetryableMode(String tableName
250270
retryer.call(fetchRecords);
251271
} catch (RetryException | ExecutionException e) {
252272
throw new ServiceNowAPIException(
253-
String.format("Data Recovery failed for batch %s to %s.", offset, (offset + limit)),
254-
e, null, false);
273+
String.format("Data Recovery failed for batch %s to %s.", offset, (offset + limit)),
274+
e, null, false);
255275
}
256276

257277
return results;
@@ -290,7 +310,7 @@ public MetadataAPISchemaResponse parseSchemaResponse(String responseBody) {
290310
* @throws ServiceNowAPIException
291311
*/
292312
public Schema fetchTableSchema(String tableName, SourceValueType valueType)
293-
throws ServiceNowAPIException {
313+
throws ServiceNowAPIException {
294314
return fetchTableSchema(tableName, getAccessToken(), valueType, schemaType);
295315
}
296316

@@ -314,7 +334,7 @@ private SchemaType getSchemaTypeBasedOnUseConnection(Boolean useConnection) {
314334
*/
315335
public Schema fetchTableSchema(String tableName, String accessToken, SourceValueType valueType,
316336
SchemaType schemaType)
317-
throws ServiceNowAPIException {
337+
throws ServiceNowAPIException {
318338
ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder(
319339
this.conf.getRestApiEndpoint(), tableName, true, schemaType)
320340
.setExcludeReferenceLink(true);
@@ -356,7 +376,7 @@ private Schema prepareSchemaWithSchemaAPI(RestAPIResponse restAPIResponse, List<
356376

357377
if (schemaAPISchemaResponse.getResult() == null || schemaAPISchemaResponse.getResult().isEmpty()) {
358378
throw new ServiceNowAPIException(
359-
"Schema Response does not contain any result", null, null, false);
379+
"Schema Response does not contain any result", null, null, false);
360380
}
361381

362382
for (SchemaAPISchemaField field : schemaAPISchemaResponse.getResult()) {
@@ -421,7 +441,7 @@ private Schema prepareSchemaWithMetadataAPI(RestAPIResponse restAPIResponse, Lis
421441
* @throws ServiceNowAPIException
422442
*/
423443
public int getTableRecordCount(String tableName)
424-
throws ServiceNowAPIException {
444+
throws ServiceNowAPIException {
425445
return getTableRecordCount(tableName, getAccessToken());
426446
}
427447

@@ -516,7 +536,7 @@ private String getSystemId(RestAPIResponse restAPIResponse) {
516536
* @param query The query
517537
*/
518538
public Map<String, String> getRecordFromServiceNowTable(String tableName, String query)
519-
throws ServiceNowAPIException {
539+
throws ServiceNowAPIException {
520540

521541
ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder(
522542
this.conf.getRestApiEndpoint(), tableName, false, schemaType)

src/main/java/io/cdap/plugin/servicenow/source/ServiceNowRecordReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private void fetchData() throws ServiceNowAPIException {
116116
protected void initialize(InputSplit split) {
117117
this.split = (ServiceNowInputSplit) split;
118118
this.pos = 0;
119-
restApi = new ServiceNowTableAPIClientImpl(pluginConf.getConnection(), pluginConf.getUseConnection());
119+
restApi = new ServiceNowTableAPIClientImpl(pluginConf, pluginConf.getUseConnection(), pluginConf);
120120
tableName = ((ServiceNowInputSplit) split).getTableName();
121121
tableNameField = pluginConf.getTableNameField();
122122
}

src/main/java/io/cdap/plugin/servicenow/source/ServiceNowSourceConfig.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public class ServiceNowSourceConfig extends ServiceNowBaseSourceConfig {
5757
"will be ignored if the Mode is set to `Reporting`. The ServiceNow table can be browsed using browse button.")
5858
private String tableName;
5959

60+
@Name(ServiceNowConstants.PROPERTY_QUERY)
61+
@Macro
62+
@Nullable
63+
@Description("The query to fetch the data from table. The query is created from the WHERE clause of the " +
64+
"SQL query. For example, WHERE column > '2022-01-01' AND column < '2022-02-01'.")
65+
private String query;
66+
6067
/**
6168
* Constructor for ServiceNowSourceConfig object.
6269
*
@@ -88,6 +95,20 @@ public ServiceNowSourceConfig(String referenceName, String queryMode, @Nullable
8895
this.tableName = tableName;
8996
}
9097

98+
public ServiceNowSourceConfig(String referenceName, String queryMode, @Nullable String applicationName,
99+
@Nullable String tableNameField, @Nullable String tableName, String clientId,
100+
String clientSecret, String restApiEndpoint, String user, String password,
101+
String valueType, @Nullable String startDate, @Nullable String endDate,
102+
Integer pageSize, @Nullable String query) {
103+
super(referenceName, clientId, clientSecret, restApiEndpoint, user, password, tableNameField, valueType, startDate,
104+
endDate, pageSize);
105+
this.referenceName = referenceName;
106+
this.queryMode = queryMode;
107+
this.applicationName = applicationName;
108+
this.tableName = tableName;
109+
this.query = query;
110+
}
111+
91112
/**
92113
* Returns the query mode chosen.
93114
*
@@ -153,6 +174,11 @@ public SourceApplication getApplicationName() {
153174
public String getTableName() {
154175
return tableName;
155176
}
177+
178+
@Nullable
179+
public String getQuery() {
180+
return query;
181+
}
156182

157183
/**
158184
* Validates {@link ServiceNowSourceConfig} instance.

src/main/java/io/cdap/plugin/servicenow/util/ServiceNowConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,9 @@ public interface ServiceNowConstants {
286286
* Suffix for ServiceNow fields that store elapsed time in seconds (e.g., calendar_stc, business_stc).
287287
*/
288288
String STC_FIELD_SUFFIX = "_stc";
289+
290+
/**
291+
* Configuration property name used to specify the query.
292+
*/
293+
String PROPERTY_QUERY = "query";
289294
}

0 commit comments

Comments
 (0)