protected long changes(List<Change> changes) throws IOException { com.google.api.services.drive.Drive.Changes.List request = drive.changes().list(); request.setMaxResults(100); long lastChange = -1; Object lastChangeData = stub.storage().get(StorageService.REMOTE_CHANGE); if (lastChangeData != null) { if (lastChangeData instanceof Double) { lastChange = ((Double) lastChangeData).longValue(); request.setStartChangeId(lastChange + 1); } else if (lastChangeData instanceof Long) { lastChange = (Long) lastChangeData; request.setStartChangeId(lastChange + 1); } else { throw new IllegalArgumentException(lastChangeData.getClass().getName()); } } while (true) { ChangeList response = execute(request); if (changes != null) { changes.addAll(response.getItems()); } if (StringUtils.isEmpty(response.getNextPageToken())) { List<Change> items = response.getItems(); if (!items.isEmpty()) lastChange = items.get(items.size() - 1).getId(); break; } else { request.setPageToken(response.getNextPageToken()); } } return lastChange; }
/** * Retrieve a collection of files that have changed since the provided {@code changeId}. * * @param changeId Change ID to retrieve changed files from. * @return Map of changed files key'ed by their file ID. */ private Map<String, File> getChangedFiles(long changeId) { Map<String, File> result = new HashMap<String, File>(); try { Changes.List request = mService.changes().list().setStartChangeId(changeId); do { ChangeList changes = request.execute(); long largestChangeId = changes.getLargestChangeId().longValue(); for (Change change : changes.getItems()) { if (change.getDeleted()) { result.put(change.getFileId(), null); } else if (TEXT_PLAIN.equals(change.getFile().getMimeType())) { result.put(change.getFileId(), change.getFile()); } } if (largestChangeId > mLargestChangeId) { mLargestChangeId = largestChangeId; } request.setPageToken(changes.getNextPageToken()); } while (request.getPageToken() != null && request.getPageToken().length() > 0); } catch (IOException e) { e.printStackTrace(); } Log.d(TAG, "Got changed Drive files: " + result.size() + " - " + mLargestChangeId); return result; }
/** * connect. * * @param messageContext ESB messageContext. * @throws ConnectException if connection fails. */ public void connect(MessageContext messageContext) throws ConnectException { ChangeList changeList; OMElement changeListResult; HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put( GoogleDriveUtils.StringConstants.INCLUDE_DELETED, (String) getParameter(messageContext, GoogleDriveUtils.StringConstants.INCLUDE_DELETED)); parameters.put( GoogleDriveUtils.StringConstants.INCLUDE_SUBSCRIBED, (String) getParameter(messageContext, GoogleDriveUtils.StringConstants.INCLUDE_SUBSCRIBED)); parameters.put( GoogleDriveUtils.StringConstants.MAX_RESULTS, (String) getParameter(messageContext, GoogleDriveUtils.StringConstants.MAX_RESULTS)); parameters.put( GoogleDriveUtils.StringConstants.PAGE_TOKEN, (String) getParameter(messageContext, GoogleDriveUtils.StringConstants.PAGE_TOKEN)); parameters.put( GoogleDriveUtils.StringConstants.START_CHANGE_ID, (String) getParameter(messageContext, GoogleDriveUtils.StringConstants.START_CHANGE_ID)); HashMap<String, String> hashMapForResultEnvelope = new HashMap<String, String>(); try { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); Drive service = GoogleDriveUtils.getDriveService(messageContext, httpTransport, jsonFactory); changeList = getChangeList(service, parameters); if (changeList != null) { hashMapForResultEnvelope.put( GoogleDriveUtils.StringConstants.CHANGE_LIST, changeList.toPrettyString()); changeListResult = GoogleDriveUtils.buildResultEnvelope( GoogleDriveUtils.StringConstants.URN_GOOGLEDRIVE_LISTCHANGESFORUSER, GoogleDriveUtils.StringConstants.LIST_CHANGES_FOR_USER_RESULT, true, hashMapForResultEnvelope); messageContext.getEnvelope().getBody().addChild(changeListResult); } } catch (Exception e) { hashMapForResultEnvelope.put(GoogleDriveUtils.StringConstants.ERROR, e.getMessage()); changeListResult = GoogleDriveUtils.buildResultEnvelope( GoogleDriveUtils.StringConstants.URN_GOOGLEDRIVE_LISTCHANGESFORUSER, GoogleDriveUtils.StringConstants.LIST_CHANGES_FOR_USER_RESULT, false, hashMapForResultEnvelope); messageContext.getEnvelope().getBody().addChild(changeListResult); log.error("Error: " + GoogleDriveUtils.getStackTraceAsString(e)); } }