Search in sources :

Example 1 with CalendarListEntry

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;
}
Also used : CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Calendar(com.google.api.services.calendar.Calendar) IOException(java.io.IOException)

Example 2 with CalendarListEntry

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);
}
Also used : PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) ArrayList(java.util.ArrayList) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) IOException(java.io.IOException) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) IdOnlyContainerResource(org.dataportabilityproject.spi.transfer.types.IdOnlyContainerResource) CalendarList(com.google.api.services.calendar.model.CalendarList) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 3 with CalendarListEntry

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));
}
Also used : ArrayList(java.util.ArrayList) Resource(org.dataportabilityproject.dataModels.Resource) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) CalendarModel(org.dataportabilityproject.dataModels.calendar.CalendarModel) IdOnlyResource(org.dataportabilityproject.shared.IdOnlyResource) CalendarModelWrapper(org.dataportabilityproject.dataModels.calendar.CalendarModelWrapper) CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) CalendarList(com.google.api.services.calendar.model.CalendarList) PaginationInformation(org.dataportabilityproject.dataModels.PaginationInformation) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken)

Example 4 with CalendarListEntry

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;
}
Also used : CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Calendar(com.google.api.services.calendar.Calendar)

Example 5 with CalendarListEntry

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;
}
Also used : CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) DateConverter(net.sf.kerner.utils.time.DateConverter) Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) DateTime(com.google.api.client.util.DateTime)

Aggregations

CalendarListEntry (com.google.api.services.calendar.model.CalendarListEntry)16 Calendar (com.google.api.services.calendar.Calendar)6 CalendarList (com.google.api.services.calendar.model.CalendarList)6 IOException (java.io.IOException)6 Event (com.google.api.services.calendar.model.Event)5 ArrayList (java.util.ArrayList)4 Events (com.google.api.services.calendar.model.Events)3 Credential (com.google.api.client.auth.oauth2.Credential)2 DateTime (com.google.api.client.util.DateTime)2 AESEncryption (com.cloudcraftgaming.discal.api.crypto.AESEncryption)1 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)1 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)1 AuthPollResponseError (com.cloudcraftgaming.discal.api.object.json.google.AuthPollResponseError)1 AuthPollResponseGrant (com.cloudcraftgaming.discal.api.object.json.google.AuthPollResponseGrant)1 Booking (com.github.drbookings.core.datamodel.api.Booking)1 BookingBean (com.github.drbookings.core.datamodel.impl.BookingBean)1 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)1 Calendar (com.google.api.services.calendar.model.Calendar)1 EventDateTime (com.google.api.services.calendar.model.EventDateTime)1 Gson (com.google.gson.Gson)1