3838import io .cdap .plugin .servicenow .model .SchemaAPISchemaResponse ;
3939import io .cdap .plugin .servicenow .restapi .RestAPIClient ;
4040import io .cdap .plugin .servicenow .restapi .RestAPIResponse ;
41+ import io .cdap .plugin .servicenow .source .ServiceNowSourceConfig ;
4142import io .cdap .plugin .servicenow .util .SchemaBuilder ;
4243import io .cdap .plugin .servicenow .util .SchemaType ;
4344import 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 )
0 commit comments