Search in sources :

Example 11 with DateTime

use of com.google.api.client.util.DateTime 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)

Example 12 with DateTime

use of com.google.api.client.util.DateTime in project data-transfer-project by google.

the class GoogleBloggerImporter method insertActivity.

private void insertActivity(IdempotentImportExecutor idempotentExecutor, SocialActivityActor actor, SocialActivityModel activity, String blogId, TokensAndUrlAuthData authData) throws Exception {
    String content = activity.getContent() == null ? "" : activity.getContent();
    Collection<SocialActivityAttachment> linkAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.LINK).collect(Collectors.toList());
    Collection<SocialActivityAttachment> imageAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.IMAGE).collect(Collectors.toList());
    // don't know how they were laid out in the originating service.
    for (SocialActivityAttachment attachment : linkAttachments) {
        content = "<a href=\"" + attachment.getUrl() + "\">" + attachment.getName() + "</a>\n</hr>\n" + content;
    }
    if (!imageAttachments.isEmpty()) {
        // Store any attached images in Drive in a new folder.
        Drive driveInterface = getOrCreateDriveService(authData);
        String folderId = idempotentExecutor.executeOrThrowException("MainAlbum", "Photo Album", () -> createAlbumFolder(driveInterface));
        for (SocialActivityAttachment image : imageAttachments) {
            try {
                String newImgSrc = idempotentExecutor.executeAndSwallowIOExceptions(image.toString(), "Image", () -> uploadImage(image, driveInterface, folderId));
                content += "\n<hr/><img src=\"" + newImgSrc + "\">";
            } catch (RuntimeException e) {
                throw new IOException("Couldn't import: " + imageAttachments, e);
            }
        }
    }
    String title = "";
    if (activity.getTitle() != null && !Strings.isNullOrEmpty(activity.getTitle())) {
        title = activity.getTitle();
    }
    Post post = new Post().setTitle("Imported post: " + title).setContent(content);
    if (actor != null) {
        Post.Author author = new Post.Author();
        if (!Strings.isNullOrEmpty(actor.getName())) {
            author.setDisplayName(actor.getName());
        }
        if (!Strings.isNullOrEmpty(actor.getUrl())) {
            author.setUrl(actor.getUrl());
        }
        post.setAuthor(author);
    }
    if (activity.getPublished() != null) {
        post.setPublished(new DateTime(activity.getPublished().toEpochMilli()));
    }
    idempotentExecutor.executeAndSwallowIOExceptions(title, title, () -> getOrCreateBloggerService(authData).posts().insert(blogId, post).setIsDraft(true).execute().getId());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Blogger(com.google.api.services.blogger.Blogger) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) Drive(com.google.api.services.drive.Drive) SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) Permission(com.google.api.services.drive.model.Permission) GoogleStaticObjects(org.datatransferproject.datatransfer.google.common.GoogleStaticObjects) Strings(com.google.common.base.Strings) DateTime(com.google.api.client.util.DateTime) ImmutableList(com.google.common.collect.ImmutableList) Post(com.google.api.services.blogger.model.Post) ResultType(org.datatransferproject.spi.transfer.provider.ImportResult.ResultType) BlogList(com.google.api.services.blogger.model.BlogList) File(com.google.api.services.drive.model.File) SocialActivityActor(org.datatransferproject.types.common.models.social.SocialActivityActor) Credential(com.google.api.client.auth.oauth2.Credential) Importer(org.datatransferproject.spi.transfer.provider.Importer) GoogleCredentialFactory(org.datatransferproject.datatransfer.google.common.GoogleCredentialFactory) ImageStreamProvider(org.datatransferproject.transfer.ImageStreamProvider) IdempotentImportExecutor(org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor) Collection(java.util.Collection) SocialActivityType(org.datatransferproject.types.common.models.social.SocialActivityType) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) TokensAndUrlAuthData(org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData) InputStreamContent(com.google.api.client.http.InputStreamContent) LocalDate(java.time.LocalDate) SocialActivityAttachmentType(org.datatransferproject.types.common.models.social.SocialActivityAttachmentType) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) InputStream(java.io.InputStream) Post(com.google.api.services.blogger.model.Post) Drive(com.google.api.services.drive.Drive) SocialActivityAttachment(org.datatransferproject.types.common.models.social.SocialActivityAttachment) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime)

Example 13 with DateTime

use of com.google.api.client.util.DateTime in project syndesis-qe by syndesisio.

the class GoogleCalendarSteps method getDateOrDateTime.

/**
 * Method that returns EventDateTime instance based on the row defined in table for the step.
 *
 * @param prefix either "start" or "end"
 * @param row the row with data (expecting presence of either prefix+"_date" or prefix+"_time"),
 * if none provided, time is defined as now()+24h for "start" and now()+25h for "end" times
 * @return EventDateTime instance with either prefix+"_date" or prefix+"_time" fields set
 */
private EventDateTime getDateOrDateTime(String prefix, Map<String, String> row) {
    EventDateTime edt = new EventDateTime();
    String dateValueIdentifier = prefix + "_date";
    String timeValueIdentifier = prefix + "_time";
    String dateValue = row.get(dateValueIdentifier);
    String timeValue = row.get(timeValueIdentifier);
    if (dateValue != null && !dateValue.isEmpty()) {
        // if date value are provided set it
        if (timeValue != null && !timeValue.isEmpty()) {
            edt.setDateTime(DateTime.parseRfc3339(dateValue + "T" + timeValue));
        } else {
            edt.setDate(DateTime.parseRfc3339(dateValue));
        }
    } else {
        // if date value not provided set time in future
        // offset of 24 or 25 hours to future: start time now()+24, end time now()+25
        long millisToFuture = (("start".equalsIgnoreCase(prefix) ? 0 : 1) + 24) * 60 * 60 * 1000;
        edt.setDateTime(new DateTime(System.currentTimeMillis() + millisToFuture));
    }
    return edt;
}
Also used : EventDateTime(com.google.api.services.calendar.model.EventDateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) DateTime(com.google.api.client.util.DateTime)

Example 14 with DateTime

use of com.google.api.client.util.DateTime in project muikku by otavanopisto.

the class GoogleCalendarClient method listEvents.

public List<CalendarEvent> listEvents(java.time.OffsetDateTime minTime, java.time.OffsetDateTime maxTime, String... calendarId) throws CalendarServiceException {
    ArrayList<CalendarEvent> result = new ArrayList<>();
    for (String calId : calendarId) {
        try {
            for (Event event : getClient().events().list(calId).setTimeMin(minTime != null ? new DateTime(minTime.toInstant().toEpochMilli()) : null).setTimeMax(maxTime != null ? new DateTime(maxTime.toInstant().toEpochMilli()) : null).execute().getItems()) {
                result.add(toMuikkuEvent(calId, event));
                logger.log(Level.INFO, event.toPrettyString());
            }
        } catch (GeneralSecurityException | IOException ex) {
            throw new CalendarServiceException(ex);
        }
    }
    return result;
}
Also used : CalendarServiceException(fi.otavanopisto.muikku.calendar.CalendarServiceException) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) DefaultCalendarEvent(fi.otavanopisto.muikku.calendar.DefaultCalendarEvent) CalendarEvent(fi.otavanopisto.muikku.calendar.CalendarEvent) Event(com.google.api.services.calendar.model.Event) DefaultCalendarEvent(fi.otavanopisto.muikku.calendar.DefaultCalendarEvent) CalendarEvent(fi.otavanopisto.muikku.calendar.CalendarEvent) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime)

Example 15 with DateTime

use of com.google.api.client.util.DateTime in project muikku by otavanopisto.

the class Convert method toEventDateTime.

static EventDateTime toEventDateTime(boolean dateOnly, CalendarEventTemporalField datetime) {
    EventDateTime result = new EventDateTime();
    result.setTimeZone(datetime.getTimeZone().getID());
    if (dateOnly) {
        long timestamp = datetime.getDateTime().getTime();
        long offset = datetime.getTimeZone().getOffset(timestamp);
        result.setDate(new DateTime(true, timestamp + offset, (int) msToMinutes(offset)));
    } else {
        result.setDateTime(toDateTime(datetime));
    }
    return result;
}
Also used : EventDateTime(com.google.api.services.calendar.model.EventDateTime) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime)

Aggregations

DateTime (com.google.api.client.util.DateTime)41 Event (com.google.api.services.calendar.model.Event)13 EventDateTime (com.google.api.services.calendar.model.EventDateTime)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)9 Calendar (com.google.api.services.calendar.Calendar)8 Events (com.google.api.services.calendar.model.Events)7 File (com.google.api.services.drive.model.File)7 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Test (org.junit.Test)6 Map (java.util.Map)5 ParseException (java.text.ParseException)4 GuildSettings (com.cloudcraftgaming.discal.api.object.GuildSettings)3 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)3 Recurrence (com.cloudcraftgaming.discal.api.object.event.Recurrence)3 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)3 FileList (com.google.api.services.drive.model.FileList)3 StorageObject (com.google.api.services.storage.model.StorageObject)3