Lehel44 commented on code in PR #6794:
URL: https://github.com/apache/nifi/pull/6794#discussion_r1113585762
##########
nifi-nar-bundles/nifi-salesforce-bundle/nifi-salesforce-processors/src/main/java/org/apache/nifi/processors/salesforce/QuerySalesforceObject.java:
##########
@@ -387,11 +461,63 @@ public void onTrigger(final ProcessContext context, final
ProcessSession session
} while (nextRecordsUrl.get() != null);
}
- private InputStream getResultInputStream(AtomicReference<String>
nextRecordsUrl, String querySObject) {
- if (nextRecordsUrl.get() == null) {
+ private void processCustomQuery(ProcessContext context, ProcessSession
session, FlowFile flowFile, AtomicReference<String> nextRecordsUrl,
AtomicReference<String> totalSize) {
+ String customQuery =
context.getProperty(CUSTOM_SOQL_QUERY).evaluateAttributeExpressions(flowFile).getValue();
+ do {
+ FlowFile outgoingFlowFile;
+ if (flowFile != null) {
+ outgoingFlowFile = session.create(flowFile);
+ } else {
+ outgoingFlowFile = session.create();
+ }
+ Map<String, String> attributes = new HashMap<>();
+ try (InputStream response =
getResultInputStream(nextRecordsUrl.get(), customQuery)) {
+ outgoingFlowFile = session.write(outgoingFlowFile,
parseHttpResponse(response, nextRecordsUrl, totalSize));
+ int recordCount = nextRecordsUrl.get() != null ? 2000 :
Integer.parseInt(totalSize.get()) % 2000;
+ attributes.put(CoreAttributes.MIME_TYPE.key(),
"application/json");
+ attributes.put(TOTAL_RECORD_COUNT,
String.valueOf(recordCount));
+ session.adjustCounter("Salesforce records processed",
recordCount, false);
+ session.putAllAttributes(outgoingFlowFile, attributes);
+ session.transfer(outgoingFlowFile, REL_SUCCESS);
+ } catch (IOException e) {
+ session.remove(outgoingFlowFile);
+ throw new ProcessException("Couldn't get Salesforce records",
e);
+ }
+ } while (nextRecordsUrl.get() != null);
+ if (flowFile != null) {
+ session.remove(flowFile);
+ }
+ }
+
+ private OutputStreamCallback parseHttpResponse(final InputStream in,
AtomicReference<String> nextRecordsUrl, AtomicReference<String> totalSize) {
+ nextRecordsUrl.set(null);
+ return out -> {
+ try (JsonParser jsonParser = JSON_FACTORY.createParser(in);
+ JsonGenerator jsonGenerator =
JSON_FACTORY.createGenerator(out, JsonEncoding.UTF8)) {
+ while (jsonParser.nextToken() != null) {
+ if (jsonParser.getCurrentToken() == JsonToken.FIELD_NAME
&& jsonParser.getCurrentName()
+ .equals(TOTAL_SIZE)) {
+ jsonParser.nextToken();
+ totalSize.set(jsonParser.getValueAsString());
+ } else if (jsonParser.getCurrentToken() ==
JsonToken.FIELD_NAME && jsonParser.getCurrentName()
+ .equals(NEXT_RECORDS_URL)) {
+ jsonParser.nextToken();
+ nextRecordsUrl.set(jsonParser.getValueAsString());
+ } else if (jsonParser.getCurrentToken() ==
JsonToken.FIELD_NAME && jsonParser.getCurrentName()
+ .equals(RECORDS)) {
+ jsonParser.nextToken();
+ jsonGenerator.copyCurrentStructure(jsonParser);
+ }
Review Comment:
This is a good idea. I'd leave the fieldName parameter out for now as it's
value is always JsonToken.FIELD_NAME. Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]