コード例 #1
0
  @Override
  protected Number executeCountQuery(
      Table table, List<FilterItem> whereItems, boolean functionApproximationAllowed) {
    final String query;

    try {
      final StringBuilder sb = new StringBuilder();
      sb.append("SELECT COUNT() FROM ");
      sb.append(table.getName());

      boolean firstWhere = true;
      for (FilterItem filterItem : whereItems) {
        if (firstWhere) {
          sb.append(" WHERE ");
          firstWhere = false;
        } else {
          sb.append(" AND ");
        }
        rewriteFilterItem(sb, filterItem);
      }
      query = sb.toString();
    } catch (UnsupportedOperationException e) {
      logger.debug("Failed to rewrite count query, falling back to client side counting", e);

      // unable to rewrite to SOQL, counting will be done client side.
      return null;
    }

    final QueryResult queryResult = executeSoqlQuery(query);

    assert queryResult.isDone();

    return queryResult.getSize();
  }
コード例 #2
0
  public void testDateUsingDefaultTimeZone() throws Exception {
    runProcess(getTestConfig(OperationInfo.insert, false), 1);
    QueryResult qr =
        getBinding().query("select CustomDateTime__c from Account where AccountNumber__c='ACCT_0'");
    assertEquals(qr.getSize(), 1);

    Date expectedDate = parseDateWithTimezone("2010-10-14T12:00:00.000GMT");
    assertEquals(
        expectedDate,
        parseDateFromPartnerApi((String) qr.getRecords()[0].getField("CustomDateTime__c")));
  }
コード例 #3
0
  @Test
  public void testQuery() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    final SObject sObject1 = Mockito.mock(SObject.class);
    final SObject sObject2 = Mockito.mock(SObject.class);
    final SObject sObject3 = Mockito.mock(SObject.class);
    when(sObject1.getId()).thenReturn("1");
    when(sObject2.getId()).thenReturn("2");
    when(sObject3.getId()).thenReturn("3");
    QueryResult queryResult = Mockito.mock(QueryResult.class);
    when(queryResult.getRecords())
        .thenAnswer(
            new Answer<Object>() {
              int counter = 0;

              @Override
              public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                SObject[] result = null;
                switch (counter) {
                  case 0:
                    result = new SObject[] {sObject1};
                    break;
                  case 1:
                    result = new SObject[] {sObject2, sObject3};
                    break;
                  case 2:
                    result = new SObject[] {};
                    break;
                }
                counter++;
                return result;
              }
            });
    when(queryResult.isDone()).thenReturn(false).thenReturn(true);
    when(queryResult.getQueryLocator()).thenReturn("001").thenReturn(null);

    PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class);
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    connector.setBulkConnection(bulkConnection);
    connector.setConnection(partnerConnection);

    when(partnerConnection.query(eq(MOCK_QUERY))).thenReturn(queryResult);
    when(partnerConnection.queryMore("001")).thenReturn(queryResult);

    PagingDelegate<Map<String, Object>> delegate =
        connector.query(MOCK_QUERY, new PagingConfiguration(1));
    List<Map<String, Object>> result = delegate.getPage();
    assertEquals(1, result.size());

    result = delegate.getPage();
    assertEquals(2, result.size());

    assertNull(delegate.getPage());
  }
コード例 #4
0
  @Test
  public void testSearch() throws Exception {
    String input =
        "exec native('search;SELECT Account.Id, Account.Type, Account.Name FROM Account')";

    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);

    QueryResult qr = Mockito.mock(QueryResult.class);
    Mockito.stub(qr.isDone()).toReturn(true);

    ArrayList<SObject> results = new ArrayList<SObject>();
    ArrayList<Object> values = new ArrayList<Object>();

    SObject s = Mockito.mock(SObject.class);
    // Mockito.stub(s.getId()).toReturn("theID");
    Mockito.stub(s.getType()).toReturn("Account");
    results.add(s);

    Element e1 = Mockito.mock(Element.class);
    Mockito.stub(e1.getTextContent()).toReturn("The ID");
    values.add(e1);

    Element e2 = Mockito.mock(Element.class);
    Mockito.stub(e2.getTextContent()).toReturn("The Type");
    values.add(e2);

    Element e3 = Mockito.mock(Element.class);
    Mockito.stub(e3.getTextContent()).toReturn("The Name");
    values.add(e3);

    Mockito.stub(s.getAny()).toReturn(values);
    Mockito.stub(qr.getRecords()).toReturn(results);
    Mockito.stub(
            connection.query(
                "SELECT Account.Id, Account.Type, Account.Name FROM Account", 0, false))
        .toReturn(qr);

    DirectQueryExecution execution =
        (DirectQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
    execution.execute();

    Mockito.verify(connection, Mockito.times(1))
        .query("SELECT Account.Id, Account.Type, Account.Name FROM Account", 0, false);

    assertArrayEquals(
        new Object[] {"The ID", "The Type", "The Name"}, (Object[]) execution.next().get(0));
  }
コード例 #5
0
  @Test
  public void testQuerySingle() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    QueryResult queryResult = Mockito.mock(QueryResult.class);
    when(queryResult.getRecords()).thenReturn(new SObject[] {});
    PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class);
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    connector.setBulkConnection(bulkConnection);
    connector.setConnection(partnerConnection);

    when(partnerConnection.query(eq(MOCK_QUERY))).thenReturn(queryResult);

    connector.querySingle(MOCK_QUERY);
  }
コード例 #6
0
 @Override
 protected void executeFunction(final PartnerConnection connection) throws Exception {
   if (logger.isDebugEnabled()) {
     logger.debug("query = " + query);
   }
   queryResult = connection.query(query);
   sObjects = Arrays.asList(queryResult.getRecords());
 }
コード例 #7
0
  @Test
  public void testPublishTopic() throws Exception {
    SalesforceConnector connector = new SalesforceConnector();
    PartnerConnection partnerConnection = Mockito.mock(PartnerConnection.class);
    connector.setConnection(partnerConnection);
    BulkConnection bulkConnection = Mockito.mock(BulkConnection.class);
    connector.setBulkConnection(bulkConnection);
    SaveResult saveResult = Mockito.mock(SaveResult.class);
    when(saveResult.isSuccess()).thenReturn(true);
    when(partnerConnection.create(Mockito.argThat(new SObjectArrayMatcher())))
        .thenReturn(new SaveResult[] {saveResult});
    QueryResult queryResult = Mockito.mock(QueryResult.class);
    when(partnerConnection.query(eq("SELECT Id FROM PushTopic WHERE Name = 'TopicName'")))
        .thenReturn(queryResult);
    when(queryResult.getSize()).thenReturn(0);

    connector.publishTopic("TopicName", "SELECT * FROM Account", "Description");

    verify(partnerConnection, atLeastOnce()).create(Mockito.argThat(new SObjectArrayMatcher()));
  }
コード例 #8
0
ファイル: Lookup.java プロジェクト: pohualin/EM_server
 public SalesForce findAccountById(String id, boolean reUpConnection) {
   if (salesForceConnection.get() == null) {
     LOGGER.error(
         "No Connection to SalesForce present, unable to process search request for '{}'", id);
     return null;
   }
   try {
     QueryResult queryResult = salesForceConnection.get().query(String.format(ID_QUERY, id));
     if (queryResult.getSize() > 0) {
       return convert(queryResult.getRecords()[0]);
     }
   } catch (ConnectionException e) {
     if (reUpConnection) {
       salesForceConnection.reUp();
       findAccountById(id, false);
     } else {
       LOGGER.error("SalesForce error", e);
     }
   }
   return null;
 }
コード例 #9
0
  public void querySample() {
    try {
      // Set query batch size
      partnerConnection.setQueryOptions(250);

      // SOQL query to use
      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
      // Make the query call and get the query results
      QueryResult qr = partnerConnection.query(soqlQuery);

      boolean done = false;
      int loopCount = 0;
      // Loop through the batches of returned results
      while (!done) {
        System.out.println("Records in results set " + loopCount++ + " - ");
        SObject[] records = qr.getRecords();
        // Process the query results
        for (int i = 0; i < records.length; i++) {
          SObject contact = records[i];
          Object firstName = contact.getField("FirstName");
          Object lastName = contact.getField("LastName");
          if (firstName == null) {
            System.out.println("Contact " + (i + 1) + ": " + lastName);
          } else {
            System.out.println("Contact " + (i + 1) + ": " + firstName + " " + lastName);
          }
        }
        if (qr.isDone()) {
          done = true;
        } else {
          qr = partnerConnection.queryMore(qr.getQueryLocator());
        }
      }
    } catch (ConnectionException ce) {
      ce.printStackTrace();
    }
    System.out.println("\nQuery execution completed.");
  }
コード例 #10
0
 @SuppressWarnings("unchecked")
 private String buildJsonQueryResult(QueryResult queryResult) throws KettleException {
   JSONArray list = new JSONArray();
   for (SObject sobject : queryResult.getRecords()) {
     list.add(buildJSONSObject(sobject));
   }
   StringWriter sw = new StringWriter();
   try {
     list.writeJSONString(sw);
   } catch (IOException e) {
     throw new KettleException(e);
   }
   return sw.toString();
 }
コード例 #11
0
ファイル: Lookup.java プロジェクト: pohualin/EM_server
  private IdNameLookupResultContainer find(
      String searchString, int pageSizeRequested, boolean reUpConnection, String... types) {

    if (salesForceConnection.get() == null) {
      LOGGER.error(
          "No Connection to SalesForce present, unable to process search request for '{}'",
          searchString);
      return new IdNameLookupResultContainer(true, new ArrayList<IdNameLookupResult>());
    }

    String strippedSearchString = StringUtils.stripToNull(searchString);

    // make sure a filter and types are present
    if (StringUtils.isBlank(strippedSearchString) || ArrayUtils.isEmpty(types)) {
      return new IdNameLookupResultContainer(true, new ArrayList<IdNameLookupResult>());
    }

    int pageSize;
    if (pageSizeRequested <= 0) {
      pageSize = DEFAULT_PAGE_SIZE;
    } else {
      pageSize = pageSizeRequested;
    }

    int totalNumber = 0;
    List<IdNameLookupResult> lookupResults = new ArrayList<>();
    try {
      for (String type : types) {
        QueryResult queryResult =
            salesForceConnection
                .get()
                .query(
                    String.format(
                        ID_NAME_SOQL_QUERY,
                        type,
                        "'%",
                        escape(strippedSearchString),
                        "%'",
                        pageSize + 1));

        if (queryResult != null) {
          totalNumber += queryResult.getSize();
          if (queryResult.getSize() > 0) {
            // found a match
            int count = 0;
            for (SObject searchRecord : queryResult.getRecords()) {
              count++;
              lookupResults.add(
                  new IdNameLookupResult(
                      (String) searchRecord.getSObjectField("Id"),
                      (String) searchRecord.getSObjectField("Name")));
              if (count >= pageSize) {
                break;
              }
            }
          }
        }
        if (totalNumber >= pageSize) {
          // no need to fetch the next object, already full
          break;
        }
      }
    } catch (ConnectionException e) {
      if (reUpConnection) {
        salesForceConnection.reUp();
        find(searchString, pageSizeRequested, false, types);
      } else {
        LOGGER.error("SalesForce error", e);
      }
    }
    return new IdNameLookupResultContainer(totalNumber <= pageSize, lookupResults);
  }