@Test
  public void testGetUpdatedObjects() throws Exception {
    SalesforceConnector connector = Mockito.spy(new SalesforceConnector());
    connector.setConnection(connection);
    when(connection.getConfig()).thenReturn(createConnectorConfig("userX"));
    connector.setObjectStoreHelper(objectStoreHelper);
    Calendar lastUpdateTime = createCalendar(4, 15);
    when(objectStoreHelper.getTimestamp("Account")).thenReturn(lastUpdateTime);
    setServerTime(connection, 5, 15);

    when(connection.getUpdated(anyString(), any(Calendar.class), any(Calendar.class)))
        .thenReturn(getUpdatedResult);
    Calendar latestDateCovered = createCalendar(5, 10);
    when(getUpdatedResult.getLatestDateCovered()).thenReturn(latestDateCovered);
    when(getUpdatedResult.getIds()).thenReturn(new String[] {"1", "3"});

    List<Map<String, Object>> updatedObjects = new ArrayList<Map<String, Object>>();
    doReturn(updatedObjects)
        .when(connector)
        .retrieve("Account", Arrays.asList("1", "3"), Arrays.asList("Id", "Name"));

    assertSame(
        updatedObjects, connector.getUpdatedObjects("Account", 60, Arrays.asList("Id", "Name")));

    verify(connection)
        .getUpdated(eq("Account"), startTimeCaptor.capture(), endTimeCaptor.capture());
    assertStartTime(4, 15);
    assertEndTime(5, 15);
    verify(objectStoreHelper).updateTimestamp(getUpdatedResult, "Account");
  }
Exemplo n.º 2
0
  public void query(boolean specifyQuery) throws KettleException {

    if (getBinding() == null) {
      throw new KettleException(
          BaseMessages.getString(PKG, "SalesforceInput.Exception.CanNotGetBiding"));
    }

    try {
      if (!specifyQuery) {
        // check if we can query this Object
        DescribeSObjectResult describeSObjectResult = getBinding().describeSObject(getModule());
        if (describeSObjectResult == null) {
          throw new KettleException(
              BaseMessages.getString(PKG, "SalesforceInput.ErrorGettingObject"));
        }
        if (!describeSObjectResult.isQueryable()) {
          throw new KettleException(
              BaseMessages.getString(PKG, "SalesforceInputDialog.ObjectNotQueryable", module));
        }
        if (this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_UPDATED
            || this.recordsFilter == SalesforceConnectionUtils.RECORDS_FILTER_DELETED) {
          // The object must be replicateable
          if (!describeSObjectResult.isReplicateable()) {
            throw new KettleException(
                BaseMessages.getString(
                    PKG, "SalesforceInput.Error.ObjectNotReplicateable", getModule()));
          }
        }
      }

      if (getSQL() != null && log.isDetailed()) {
        log.logDetailed(
            BaseMessages.getString(PKG, "SalesforceInput.Log.SQLString") + " : " + getSQL());
      }

      switch (this.recordsFilter) {
        case SalesforceConnectionUtils.RECORDS_FILTER_UPDATED:
          // Updated records ...
          GetUpdatedResult updatedRecords =
              getBinding().getUpdated(getModule(), this.startDate, this.endDate);

          if (updatedRecords.getIds() != null) {
            int nr = updatedRecords.getIds().length;
            if (nr > 0) {
              String[] ids = updatedRecords.getIds();
              // We can pass a maximum of 2000 object IDs
              if (nr > SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS) {
                this.sObjects = new SObject[nr];
                List<String> list = new ArrayList<String>();
                int desPos = 0;
                for (int i = 0; i < nr; i++) {
                  list.add(updatedRecords.getIds(i));

                  if (i % SalesforceConnectionUtils.MAX_UPDATED_OBJECTS_IDS == 0 || i == nr - 1) {
                    SObject[] s =
                        getBinding()
                            .retrieve(
                                this.fieldsList,
                                getModule(),
                                list.toArray(new String[list.size()]));
                    System.arraycopy(s, 0, this.sObjects, desPos, s.length);
                    desPos += s.length;
                    s = null;
                    list = new ArrayList<String>();
                  }
                }
              } else {
                this.sObjects = getBinding().retrieve(this.fieldsList, getModule(), ids);
              }
              if (this.sObjects != null) {
                this.queryResultSize = this.sObjects.length;
              }
            }
          }
          break;
        case SalesforceConnectionUtils.RECORDS_FILTER_DELETED:
          // Deleted records ...
          GetDeletedResult deletedRecordsResult =
              getBinding().getDeleted(getModule(), this.startDate, this.endDate);

          DeletedRecord[] deletedRecords = deletedRecordsResult.getDeletedRecords();

          if (log.isDebug()) {
            log.logDebug(
                toString(),
                BaseMessages.getString(
                    PKG,
                    "SalesforceConnection.DeletedRecordsFound",
                    String.valueOf(deletedRecords == null ? 0 : deletedRecords.length)));
          }

          if (deletedRecords != null && deletedRecords.length > 0) {
            getDeletedList = new HashMap<String, Date>();

            for (DeletedRecord dr : deletedRecords) {
              getDeletedList.put(dr.getId(), dr.getDeletedDate().getTime());
            }
            this.qr = getBinding().queryAll(getSQL());
            this.sObjects = getQueryResult().getRecords();
            if (this.sObjects != null) {
              this.queryResultSize = this.sObjects.length;
            }
          }
          break;
        default:
          // return query result
          this.qr = isQueryAll() ? getBinding().queryAll(getSQL()) : getBinding().query(getSQL());
          this.sObjects = getQueryResult().getRecords();
          this.queryResultSize = getQueryResult().getSize();
          break;
      }
      if (this.sObjects != null) {
        this.recordsCount = this.sObjects.length;
      }
    } catch (Exception e) {
      log.logError(Const.getStackTracker(e));
      throw new KettleException(
          BaseMessages.getString(PKG, "SalesforceConnection.Exception.Query"), e);
    }
  }