private SalesForceSearchResponse findAccounts( String searchString, int pageSizeRequested, boolean reUpConnection) { if (salesForceConnection.get() == null) { LOGGER.error( "No Connection to SalesForce present, unable to process search request for '{}'", searchString); return new SalesForceSearchResponse(true, new ArrayList<SalesForce>()); } String strippedSearchString = StringUtils.stripToNull(searchString); // make sure a filter is there if (StringUtils.isBlank(strippedSearchString)) { return new SalesForceSearchResponse(true, new ArrayList<SalesForce>()); } int pageSize; if (pageSizeRequested <= 0) { pageSize = DEFAULT_PAGE_SIZE; } else { pageSize = pageSizeRequested; } List<SalesForce> accounts = new ArrayList<>(); int totalNumber; // perform a search for the term SearchResult searchResult = null; try { searchResult = salesForceConnection .get() .search(String.format(FIND_QUERY, escape(strippedSearchString), pageSize + 1)); } catch (ConnectionException e) { if (reUpConnection) { salesForceConnection.reUp(); findAccounts(searchString, pageSizeRequested, false); } else { LOGGER.error("SalesForce error", e); } } totalNumber = searchResult != null ? searchResult.getSearchRecords().length : 0; if (totalNumber > 0) { // found a match int count = 0; for (SearchRecord o : searchResult.getSearchRecords()) { accounts.add(convert(o.getRecord())); count++; if (count >= pageSize) { break; } } } return new SalesForceSearchResponse(totalNumber <= pageSize, accounts); }
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; }
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); }