camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zregv...@apache.org
Subject [camel] branch master updated: CAMEL-10452: Add an option to simulate SELECT *
Date Mon, 04 Jun 2018 10:08:34 GMT
This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new f6be5a7  CAMEL-10452: Add an option to simulate SELECT *
f6be5a7 is described below

commit f6be5a7a186e9eb5165d5b244fd921894ed63107
Author: Zoran Regvart <zregvart@apache.org>
AuthorDate: Mon Jun 4 11:44:02 2018 +0200

    CAMEL-10452: Add an option to simulate SELECT *
    
    This adds a simple `QueryHelper` utility class to generate SOQL queries
    from `AbstractDescribedSObjectBase` instances, optionally filtering the
    fields added to the query.
---
 .../src/main/docs/salesforce-component.adoc        | 10 +++
 .../salesforce/api/utils/QueryHelper.java          | 90 ++++++++++++++++++++++
 .../salesforce/api/utils/QueryHelperTest.java      | 58 ++++++++++++++
 3 files changed, 158 insertions(+)

diff --git a/components/camel-salesforce/camel-salesforce-component/src/main/docs/salesforce-component.adoc
b/components/camel-salesforce/camel-salesforce-component/src/main/docs/salesforce-component.adoc
index f1656e5..8806cd7 100644
--- a/components/camel-salesforce/camel-salesforce-component/src/main/docs/salesforce-component.adoc
+++ b/components/camel-salesforce/camel-salesforce-component/src/main/docs/salesforce-component.adoc
@@ -532,6 +532,16 @@ final Map<String, ?> accountUpdateBody = accountUpdateResult.getBody();
 final SObjectCompositeResult contactCreationResult = results.stream().filter(r -> "JunctionRecord".equals(r.getReferenceId())).findFirst().get()
 ----
 
+=== Generating SOQL query strings
+
+`org.apache.camel.component.salesforce.api.utils.QueryHelper` contains helper
+methods to generate SOQL queries. For instance to fetch all custom fields from
+_Account_ SObject you can simply generate the SOQL SELECT by invoking:
+
+[source,java]
+----
+String allCustomFieldsQuery = QueryHelper.queryToFetchFilteredFieldsOf(new Account(), SObjectField::isCustom);
+----
 
 === Camel Salesforce Maven Plugin
 
diff --git a/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/utils/QueryHelper.java
b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/utils/QueryHelper.java
new file mode 100644
index 0000000..e4aad41
--- /dev/null
+++ b/components/camel-salesforce/camel-salesforce-component/src/main/java/org/apache/camel/component/salesforce/api/utils/QueryHelper.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.salesforce.api.utils;
+
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import org.apache.camel.component.salesforce.api.dto.AbstractDescribedSObjectBase;
+import org.apache.camel.component.salesforce.api.dto.SObjectDescription;
+import org.apache.camel.component.salesforce.api.dto.SObjectField;
+
+public final class QueryHelper {
+
+    private static final String[] NONE = new String[0];
+
+    private QueryHelper() {
+        // utility class
+    }
+
+    public static String[] fieldNamesOf(final AbstractDescribedSObjectBase object) {
+        if (object == null) {
+            return NONE;
+        }
+
+        final SObjectDescription description = object.description();
+        final List<SObjectField> fields = description.getFields();
+
+        return fields.stream().map(SObjectField::getName).toArray(String[]::new);
+    }
+
+    public static String[] filteredFieldNamesOf(final AbstractDescribedSObjectBase object,
+        final Predicate<SObjectField> filter) {
+        if (object == null) {
+            return NONE;
+        }
+
+        if (filter == null) {
+            return fieldNamesOf(object);
+        }
+
+        final SObjectDescription description = object.description();
+        final List<SObjectField> fields = description.getFields();
+
+        return fields.stream().filter(filter).map(SObjectField::getName).toArray(String[]::new);
+    }
+
+    public static String queryToFetchAllFieldsOf(final AbstractDescribedSObjectBase object)
{
+        if (object == null) {
+            return null;
+        }
+
+        final SObjectDescription description = object.description();
+        final List<SObjectField> fields = description.getFields();
+
+        return fields.stream().map(SObjectField::getName)
+            .collect(Collectors.joining(", ", "SELECT ", " FROM " + description.getName()));
+    }
+
+    public static String queryToFetchFilteredFieldsOf(final AbstractDescribedSObjectBase
object,
+        final Predicate<SObjectField> filter) {
+        if (object == null) {
+            return null;
+        }
+
+        if (filter == null) {
+            return queryToFetchAllFieldsOf(object);
+        }
+
+        final SObjectDescription description = object.description();
+        final List<SObjectField> fields = description.getFields();
+
+        return fields.stream().filter(filter).map(SObjectField::getName)
+            .collect(Collectors.joining(", ", "SELECT ", " FROM " + description.getName()));
+    }
+}
diff --git a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/api/utils/QueryHelperTest.java
b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/api/utils/QueryHelperTest.java
new file mode 100644
index 0000000..d2db62e
--- /dev/null
+++ b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/api/utils/QueryHelperTest.java
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.salesforce.api.utils;
+
+import org.apache.camel.component.salesforce.api.dto.SObjectField;
+import org.apache.camel.component.salesforce.dto.generated.Account;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class QueryHelperTest {
+
+    @Test
+    public void shouldFilterAndGatherAllFieldNames() {
+        assertThat(QueryHelper.filteredFieldNamesOf(new Account(), SObjectField::isCustom))
+            .contains("CustomerPriority__c", "SLA__c", "Active__c");
+    }
+
+    @Test
+    public void shouldGatherAllFieldNames() {
+        assertThat(QueryHelper.fieldNamesOf(new Account())).contains("Id", "Name", "ShippingCity");
+    }
+
+    @Test
+    public void shouldGenerateQueryForAllFields() {
+        assertThat(QueryHelper.queryToFetchAllFieldsOf(new Account())).isEqualTo(
+            "SELECT Id, IsDeleted, MasterRecordId, Name, Type, ParentId, BillingStreet, BillingCity,
BillingState, "
+                + "BillingPostalCode, BillingCountry, BillingLatitude, BillingLongitude,
BillingAddress, ShippingStreet, "
+                + "ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry, ShippingLatitude,
ShippingLongitude, "
+                + "ShippingAddress, Phone, Fax, AccountNumber, Website, PhotoUrl, Sic, Industry,
AnnualRevenue, NumberOfEmployees, "
+                + "Ownership, TickerSymbol, Description, Rating, Site, OwnerId, CreatedDate,
CreatedById, LastModifiedDate, "
+                + "LastModifiedById, SystemModstamp, LastActivityDate, LastViewedDate, LastReferencedDate,
Jigsaw, JigsawCompanyId, "
+                + "CleanStatus, AccountSource, DunsNumber, Tradestyle, NaicsCode, NaicsDesc,
YearStarted, SicDesc, DandbCompanyId, "
+                + "CustomerPriority__c, SLA__c, Active__c, NumberofLocations__c, UpsellOpportunity__c,
SLASerialNumber__c, "
+                + "SLAExpirationDate__c, Shipping_Location__Latitude__s, Shipping_Location__Longitude__s,
Shipping_Location__c FROM Account");
+    }
+
+    @Test
+    public void shouldGenerateQueryForFilteredFields() {
+        assertThat(QueryHelper.queryToFetchFilteredFieldsOf(new Account(), SObjectField::isCustom)).isEqualTo(
+            "SELECT CustomerPriority__c, SLA__c, Active__c, NumberofLocations__c, UpsellOpportunity__c,
SLASerialNumber__c, "
+                + "SLAExpirationDate__c, Shipping_Location__Latitude__s, Shipping_Location__Longitude__s,
Shipping_Location__c FROM Account");
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
zregvart@apache.org.

Mime
View raw message