}
void retrieveCallLogSinceDate(UpdateInfo updateInfo, BigInteger historyId) throws Exception {
long then = System.currentTimeMillis();
String query = "(incremental call log retrieval)";
ObjectType callLogObjectType = ObjectType.getObjectType(connector(),
"call_log");
String callLogFolderName = getSettingsOrPortLegacySettings(updateInfo.apiKey).callLogFolderName;
try {
Gmail gmail = getGmailService(updateInfo.apiKey);
String email = getEmailAddress(updateInfo.apiKey);
BigInteger originalHistoryId = historyId;
Label callLogLabel = null;
for (Label label : gmail.users().labels().list(email).execute().getLabels()){
if (label.getName().equals(callLogFolderName)){
callLogLabel = label;
}
}
if (callLogLabel == null)
throw new FolderNotFoundException();
//if we get to this point then we were able to access the folder and should delete our error notification
Notification errorNotification = notificationsService.getNamedNotification(updateInfo.getGuestId(), connector().getName() + ".callLogFolderError");
if (errorNotification != null && !errorNotification.deleted){
notificationsService.deleteNotification(updateInfo.getGuestId(),errorNotification.getId());
}
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
if (historyId != null){
ListHistoryResponse historyResponse = null;
BigInteger queryHistoryId = historyId;
do{
historyResponse = invokeListHistory(gmail,email,queryHistoryId,callLogLabel.getId(),historyResponse == null ? null : historyResponse.getNextPageToken());
if (historyResponse == null){
//if historyResponse is null that means we got a 404 error which means historyId is no longer valid
historyId = null;
break;
}
List<History> histories = historyResponse.getHistory();
for (History history : histories){
if (history.getMessages() == null)
continue;
for (Message messageStub : history.getMessages()){
Message message = invokeGetMessage(gmail, email, messageStub.getId());
if (message == null)
continue;
if (historyId.compareTo(message.getHistoryId()) < 0)
historyId = message.getHistoryId();
byte[] emailBytes = Base64.decodeBase64(message.getRaw());
MimeMessage mimeMessage = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
if (flushEntry(updateInfo, email, mimeMessage, CallLogEntryFacet.class) == null){
throw new Exception("Could not persist Call log");
}
}
}
} while (historyResponse.getNextPageToken() != null);
}
if (historyId == null){
ListMessagesResponse listResponse = null;
do{
listResponse = invokeList(gmail,email,callLogLabel.getId(),listResponse == null ? null : listResponse.getNextPageToken());
if (listResponse.getMessages() == null){
continue;
}
for (Message messageStub : listResponse.getMessages()){
Message message = invokeGetMessage(gmail, email, messageStub.getId());
if (message == null)
continue;
if (historyId == null || historyId.compareTo(message.getHistoryId()) < 0)
historyId = message.getHistoryId();
byte[] emailBytes = Base64.decodeBase64(message.getRaw());
MimeMessage mimeMessage = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
if (flushEntry(updateInfo, email, mimeMessage, CallLogEntryFacet.class) == null){
throw new Exception("Could not persist Call log");
}
}
} while (listResponse.getNextPageToken() != null);
}
if (historyId != null && !historyId.equals(originalHistoryId)){
updateHistoryId(updateInfo,callLogObjectType,historyId);
}
countSuccessfulApiCall(updateInfo.apiKey, callLogObjectType.value(), then, query);
}
catch (MessagingException ex){
notificationsService.addNamedNotification(updateInfo.getGuestId(), Notification.Type.ERROR, connector().getName() + ".callLogFolderError",
"The call log folder configured for SMS Backup, \"" + callLogFolderName + "\", does not exist. Either change it in your connector settings or check if SMS Backup is set to use this folder.");
throw new UpdateFailedException("Couldn't open Call Log folder.",true, null);
}
catch (Exception ex) {
ex.printStackTrace();
reportFailedApiCall(updateInfo.apiKey,
callLogObjectType.value(), then, query, Utils.stackTrace(ex),
ex.getMessage());
throw ex;
}
}