use of com.google.api.services.calendar.model.CalendarListEntry in project openhab1-addons by openhab.
the class GCalGoogleOAuth method getCalendarId.
/**
* Return calendarID based on calendar name.
* if calendar name is "primary" not check primary - just return primary
*
* @param calendar object
*/
public static CalendarListEntry getCalendarId(String calendar_name) {
CalendarListEntry calendarID = null;
if (calendarList == null) {
Calendar client = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredential(false)).setApplicationName("openHAB").build();
try {
calendarList = client.calendarList().list().execute();
} catch (com.google.api.client.auth.oauth2.TokenResponseException ae) {
logger.error("authentication failed: {}", ae.getMessage());
return null;
} catch (IOException e1) {
logger.error("authentication I/O exception: {}", e1.getMessage());
return null;
}
}
if (calendarList != null && calendarList.getItems() != null) {
for (CalendarListEntry entry : calendarList.getItems()) {
if ((entry.getSummary().equals(calendar_name)) || (calendar_name.equals("primary")) && entry.isPrimary()) {
calendarID = entry;
logger.debug("Got calendar {} CalendarID: {}", calendar_name, calendarID.getId());
}
}
}
if (calendarID == null) {
logger.warn("Calendar {} not found", calendar_name);
}
return calendarID;
}
use of com.google.api.services.calendar.model.CalendarListEntry in project data-transfer-project by google.
the class GoogleCalendarExporter method exportCalendars.
private ExportResult<CalendarContainerResource> exportCalendars(AuthData authData, Optional<PaginationData> pageData) {
Calendar.CalendarList.List listRequest;
CalendarList listResult;
// Get calendar information
try {
listRequest = getOrCreateCalendarInterface(authData).calendarList().list();
if (pageData.isPresent()) {
StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
Preconditions.checkState(paginationToken.getToken().startsWith(CALENDAR_TOKEN_PREFIX), "Token is not applicable");
listRequest.setPageToken(((StringPaginationToken) pageData.get()).getToken().substring(CALENDAR_TOKEN_PREFIX.length()));
}
listResult = listRequest.execute();
} catch (IOException e) {
return new ExportResult<CalendarContainerResource>(ResultType.ERROR, e.getMessage());
}
// Set up continuation data
PaginationData nextPageData = null;
if (listResult.getNextPageToken() != null) {
nextPageData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + listResult.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
// Process calendar list
List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
for (CalendarListEntry calendarData : listResult.getItems()) {
CalendarModel model = convertToCalendarModel(calendarData);
continuationData.addContainerResource(new IdOnlyContainerResource(calendarData.getId()));
calendarModels.add(model);
}
CalendarContainerResource calendarContainerResource = new CalendarContainerResource(calendarModels, null);
// Get result type
ExportResult.ResultType resultType = ResultType.CONTINUE;
if (calendarModels.isEmpty()) {
resultType = ResultType.END;
}
return new ExportResult<CalendarContainerResource>(resultType, calendarContainerResource, continuationData);
}
use of com.google.api.services.calendar.model.CalendarListEntry in project data-transfer-project by google.
the class GoogleCalendarService method exportCalendars.
private CalendarModelWrapper exportCalendars(Optional<PaginationInformation> pageInfo) throws IOException {
Calendar.CalendarList.List listRequest = calendarClient.calendarList().list();
if (pageInfo.isPresent()) {
listRequest.setPageToken(((StringPaginationToken) pageInfo.get()).getId());
}
CalendarList listResult = listRequest.execute();
List<CalendarModel> calendarModels = new ArrayList<>(listResult.getItems().size());
List<Resource> resources = new ArrayList<>(listResult.getItems().size());
for (CalendarListEntry calendarData : listResult.getItems()) {
CalendarModel model = GoogleCalendarToModelConverter.convertToCalendarModel(calendarData);
resources.add(new IdOnlyResource(calendarData.getId()));
calendarModels.add(model);
}
PaginationInformation newPageInfo = null;
if (listResult.getNextPageToken() != null) {
newPageInfo = new StringPaginationToken(listResult.getNextPageToken());
}
return new CalendarModelWrapper(calendarModels, null, new ContinuationInformation(resources, newPageInfo));
}
use of com.google.api.services.calendar.model.CalendarListEntry in project DisCal-Discord-Bot by NovaFox161.
the class AddCalendarCommand method issueCommand.
/**
* Issues the command this Object is responsible for.
*
* @param args The command arguments.
* @param event The event received.
* @return <code>true</code> if successful, else <code>false</code>.
*/
@Override
public Boolean issueCommand(String[] args, MessageReceivedEvent event, GuildSettings settings) {
if (settings.isDevGuild() || settings.isPatronGuild()) {
if (PermissionChecker.hasManageServerRole(event)) {
if (args.length == 0) {
if (DatabaseManager.getManager().getMainCalendar(settings.getGuildID()).getCalendarAddress().equalsIgnoreCase("primary")) {
Message.sendMessage(MessageManager.getMessage("AddCalendar.Start", settings), event);
Authorization.getAuth().requestCode(event, settings);
} else {
Message.sendMessage(MessageManager.getMessage("Creator.Calendar.HasCalendar", settings), event);
}
} else if (args.length == 1) {
// Check if arg is calendar ID that is supported, if so, complete the setup.
if (!DatabaseManager.getManager().getMainCalendar(settings.getGuildID()).getCalendarAddress().equalsIgnoreCase("primary")) {
Message.sendMessage(MessageManager.getMessage("Creator.Calendar.HasCalendar", settings), event);
} else if (settings.getEncryptedAccessToken().equalsIgnoreCase("N/a") && settings.getEncryptedRefreshToken().equalsIgnoreCase("N/a")) {
Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.NotAuth", settings), event);
} else {
try {
Calendar service = CalendarAuth.getCalendarService(settings);
List<CalendarListEntry> items = service.calendarList().list().setMinAccessRole("writer").execute().getItems();
boolean valid = false;
for (CalendarListEntry i : items) {
if (!i.isDeleted() && i.getId().equals(args[0])) {
// valid
valid = true;
break;
}
}
if (valid) {
// Update and save.
CalendarData data = new CalendarData(event.getGuild().getLongID(), 1);
data.setCalendarId(args[0]);
data.setCalendarAddress(args[0]);
data.setExternal(true);
DatabaseManager.getManager().updateCalendar(data);
// Update guild settings
settings.setUseExternalCalendar(true);
DatabaseManager.getManager().updateSettings(settings);
Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.Success", settings), event);
} else {
// Invalid
Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.Failure.Invalid", settings), event);
}
} catch (Exception e) {
Message.sendMessage(MessageManager.getMessage("AddCalendar.Select.Failure.Unknown", settings), event);
Logger.getLogger().exception(event.getAuthor(), "Failed to connect external calendar!", e, this.getClass(), true);
}
}
} else {
Message.sendMessage(MessageManager.getMessage("AddCalendar.Specify", settings), event);
}
} else {
Message.sendMessage(MessageManager.getMessage("Notification.Perm.MANAGE_SERVER", settings), event);
}
} else {
Message.sendMessage(MessageManager.getMessage("Notification.Patron", settings), event);
}
return false;
}
use of com.google.api.services.calendar.model.CalendarListEntry in project drbookings by DrBookings.
the class GoogleCalendarSync method clear.
public GoogleCalendarSync clear(final LocalDate date) throws IOException {
final CalendarListEntry flats = getCalendar();
// Iterate over the events in the specified calendar
String pageToken = null;
int cnt = 0;
do {
final Events events;
if (date != null) {
events = client.events().list(flats.getId()).setTimeMin(new DateTime(new DateConverter().convert(date))).setPageToken(pageToken).execute();
} else {
events = client.events().list(flats.getId()).setPageToken(pageToken).execute();
}
final List<Event> items = events.getItems();
for (final Event event : items) {
if (event == null) {
if (logger.isWarnEnabled()) {
logger.warn("Skipping null event");
}
continue;
}
clearEvent(flats.getId(), event);
cnt++;
}
pageToken = events.getNextPageToken();
} while (pageToken != null);
if (logger.isDebugEnabled()) {
logger.debug("Processed " + cnt + " events");
}
return this;
}
Aggregations