Search in sources :

Example 26 with DateTime

use of com.google.api.client.util.DateTime in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method listOwnerFilesAfterDate.

public List<File> listOwnerFilesAfterDate(String stringDateLastChange) {
    SimpleDateFormat formatFullDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dateLastChange = null;
    try {
        dateLastChange = formatFullDate.parse(stringDateLastChange);
    } catch (ParseException e1) {
        log.error("Error parse date in list", e1);
    }
    FileList result;
    List<File> files = null;
    try {
        result = service.files().list().setMaxResults(500).execute();
        files = result.getItems();
    } catch (IOException e) {
        log.error("Error list files", e);
    }
    if (files == null || files.size() == 0) {
        log.error("No files found");
    } else {
        log.info("Files:\n");
        for (File file : files) {
            DateTime dateTime = file.getModifiedDate();
            Date dateModifyFile = new Date(dateTime.getValue());
            if (dateModifyFile.after(dateLastChange))
                log.info("Title " + file.getTitle() + " id " + file.getId() + " DateTime " + formatFullDate.format(dateModifyFile));
        }
    }
    return files;
}
Also used : FileList(com.google.api.services.drive.model.FileList) ParseException(java.text.ParseException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) File(com.google.api.services.drive.model.File) Date(java.util.Date) DateTime(com.google.api.client.util.DateTime)

Example 27 with DateTime

use of com.google.api.client.util.DateTime in project DisCal-Discord-Bot by NovaFox161.

the class AnnouncementTask method getEvents.

private List<Event> getEvents(GuildSettings gs, CalendarData cd, Calendar service, Announcement a) {
    if (!allEvents.containsKey(gs.getGuildID())) {
        Logger.getLogger().announcement("getting events for guild...", gs.getGuildID() + "", a.getAnnouncementId() + "", "N/a");
        try {
            Events events = service.events().list(cd.getCalendarAddress()).setMaxResults(15).setTimeMin(new DateTime(System.currentTimeMillis())).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
            List<Event> items = events.getItems();
            allEvents.put(gs.getGuildID(), items);
        } catch (IOException e) {
            Logger.getLogger().exception(null, "Failed to get events list! 00x2304 | Guild: " + gs.getGuildID() + " | Announcement: " + a.getAnnouncementId(), e, this.getClass(), true);
            // allEvents.put(gs.getGuildID(), new VirtualFlow.ArrayLinkedList<>());
            return new ArrayList<>();
        }
    }
    return allEvents.get(gs.getGuildID());
}
Also used : Events(com.google.api.services.calendar.model.Events) Event(com.google.api.services.calendar.model.Event) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime)

Example 28 with DateTime

use of com.google.api.client.util.DateTime in project DisCal-Discord-Bot by NovaFox161.

the class EventCommand method moduleEndDate.

private void moduleEndDate(String[] args, MessageReceivedEvent event, GuildSettings settings) {
    long guildId = event.getGuild().getLongID();
    if (args.length == 2) {
        if (EventCreator.getCreator().hasPreEvent(guildId)) {
            String dateRaw = args[1].trim();
            if (dateRaw.length() > 10) {
                try {
                    // Do a lot of date shuffling to get to proper formats and shit like that.
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
                    TimeZone tz = TimeZone.getTimeZone(EventCreator.getCreator().getPreEvent(guildId).getTimeZone());
                    sdf.setTimeZone(tz);
                    Date dateObj = sdf.parse(dateRaw);
                    DateTime dateTime = new DateTime(dateObj);
                    EventDateTime eventDateTime = new EventDateTime();
                    eventDateTime.setDateTime(dateTime);
                    // Wait! Lets check now if its in the future and not the past!
                    if (!TimeUtils.inPast(dateRaw, tz) && !TimeUtils.endBeforeStart(dateRaw, tz, EventCreator.getCreator().getPreEvent(guildId))) {
                        // Date shuffling done, now actually apply all that damn stuff here.
                        EventCreator.getCreator().getPreEvent(guildId).setEndDateTime(eventDateTime);
                        // Apply viewable date/times...
                        SimpleDateFormat sdfV = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
                        Date dateObjV = sdfV.parse(dateRaw);
                        DateTime dateTimeV = new DateTime(dateObjV);
                        EventDateTime eventDateTimeV = new EventDateTime();
                        eventDateTimeV.setDateTime(dateTimeV);
                        EventCreator.getCreator().getPreEvent(guildId).setViewableEndDate(eventDateTimeV);
                        if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                            Message.deleteMessage(event);
                            Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                            EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.End.Success.New", settings), event));
                        } else {
                            String msg = MessageManager.getMessage("Creator.Event.End.Success", settings);
                            msg = msg.replaceAll("%date%", EventMessageFormatter.getHumanReadableDate(eventDateTimeV, settings, true)).replaceAll("%time%", EventMessageFormatter.getHumanReadableTime(eventDateTimeV, settings, true));
                            Message.sendMessage(msg, event);
                        }
                    } else {
                        // Oops! Time is in the past or before the starting time...
                        if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                            Message.deleteMessage(event);
                            Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                            EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.End.Failure.Illegal", settings), event));
                        } else {
                            Message.sendMessage(MessageManager.getMessage("Creator.Event.End.Failure.Illegal", settings), event);
                        }
                    }
                } catch (ParseException e) {
                    if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                        Message.deleteMessage(event);
                        Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                        EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Time.Invalid", settings), event));
                    } else {
                        Message.sendMessage(MessageManager.getMessage("Creator.Event.Time.Invalid", settings), event);
                    }
                }
            } else {
                if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
                    Message.deleteMessage(event);
                    Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
                    EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.Time.InvalidFormat", settings), event));
                } else {
                    Message.sendMessage(MessageManager.getMessage("Creator.Event.Time.InvalidFormat", settings), event);
                }
            }
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Event.NotInit", settings), event);
        }
    } else {
        if (EventCreator.getCreator().hasCreatorMessage(guildId)) {
            Message.deleteMessage(event);
            Message.deleteMessage(EventCreator.getCreator().getCreatorMessage(guildId));
            EventCreator.getCreator().setCreatorMessage(Message.sendMessage(EventMessageFormatter.getPreEventEmbed(EventCreator.getCreator().getPreEvent(guildId), settings), MessageManager.getMessage("Creator.Event.End.Specify", settings), event));
        } else {
            Message.sendMessage(MessageManager.getMessage("Creator.Event.End.Specify", settings), event);
        }
    }
}
Also used : TimeZone(java.util.TimeZone) EventDateTime(com.google.api.services.calendar.model.EventDateTime) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime)

Example 29 with DateTime

use of com.google.api.client.util.DateTime in project DisCal-Discord-Bot by NovaFox161.

the class EventEndpoint method updateEvent.

public static String updateEvent(Request request, Response response) {
    JSONObject body = new JSONObject(request.body());
    String eventId = body.getString("id");
    GuildSettings settings;
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        long guildId = body.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // Okay, time to update the event
    try {
        Calendar service;
        if (settings.useExternalCalendar()) {
            service = CalendarAuth.getCalendarService(settings);
        } else {
            service = CalendarAuth.getCalendarService();
        }
        CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute();
        Event event = new Event();
        event.setId(eventId);
        event.setVisibility("public");
        event.setSummary(body.getString("summary"));
        event.setDescription(body.getString("description"));
        EventDateTime start = new EventDateTime();
        start.setDateTime(new DateTime(body.getLong("epochStart")));
        event.setStart(start.setTimeZone(cal.getTimeZone()));
        EventDateTime end = new EventDateTime();
        end.setDateTime(new DateTime(body.getLong("epochEnd")));
        event.setEnd(end.setTimeZone(cal.getTimeZone()));
        if (!body.getString("color").equalsIgnoreCase("NONE")) {
            event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString());
        }
        if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) {
            event.setLocation(body.getString("location"));
        }
        JSONObject recur = body.getJSONObject("recurrence");
        if (recur.getBoolean("recur")) {
            // Handle recur
            Recurrence recurrence = new Recurrence();
            recurrence.setFrequency(EventFrequency.fromValue(recur.getString("frequency")));
            recurrence.setCount(recur.getInt("count"));
            recurrence.setInterval(recur.getInt("interval"));
            String[] rr = new String[] { recurrence.toRRule() };
            event.setRecurrence(Arrays.asList(rr));
        }
        EventData ed = new EventData(settings.getGuildID());
        if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) {
            ed.setImageLink(body.getString("image"));
            ed.setEventId(eventId);
            ed.setEventEnd(event.getEnd().getDateTime().getValue());
        }
        if (ed.shouldBeSaved()) {
            DatabaseManager.getManager().updateEventData(ed);
        }
        service.events().update(calendarData.getCalendarId(), eventId, event).execute();
        response.status(200);
        response.body(ResponseUtils.getJsonResponseMessage("Successfully updated event!"));
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Failed to update event!", e, EventEndpoint.class, true);
        e.printStackTrace();
        response.status(500);
        response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!"));
    }
    return response.body();
}
Also used : Recurrence(com.cloudcraftgaming.discal.api.object.event.Recurrence) EventDateTime(com.google.api.services.calendar.model.EventDateTime) Calendar(com.google.api.services.calendar.Calendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) EventData(com.cloudcraftgaming.discal.api.object.event.EventData) JSONObject(org.json.JSONObject) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

Example 30 with DateTime

use of com.google.api.client.util.DateTime in project DisCal-Discord-Bot by NovaFox161.

the class EventEndpoint method createEvent.

public static String createEvent(Request request, Response response) {
    JSONObject body = new JSONObject(request.body());
    GuildSettings settings;
    if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
        settings = g.getSettings();
    } else {
        long guildId = body.getLong("guild_id");
        settings = DatabaseManager.getManager().getSettings(guildId);
    }
    // Okay, time to update the event
    try {
        Calendar service;
        if (settings.useExternalCalendar()) {
            service = CalendarAuth.getCalendarService(settings);
        } else {
            service = CalendarAuth.getCalendarService();
        }
        CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
        com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute();
        Event event = new Event();
        event.setId(KeyGenerator.generateEventId());
        event.setVisibility("public");
        event.setSummary(body.getString("summary"));
        event.setDescription(body.getString("description"));
        EventDateTime start = new EventDateTime();
        start.setDateTime(new DateTime(body.getLong("epochStart")));
        event.setStart(start.setTimeZone(cal.getTimeZone()));
        EventDateTime end = new EventDateTime();
        end.setDateTime(new DateTime(body.getLong("epochEnd")));
        event.setEnd(end.setTimeZone(cal.getTimeZone()));
        if (!body.getString("color").equalsIgnoreCase("NONE")) {
            event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString());
        }
        if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) {
            event.setLocation(body.getString("location"));
        }
        JSONObject recur = body.getJSONObject("recurrence");
        if (recur.getBoolean("recur")) {
            // Handle recur
            Recurrence recurrence = new Recurrence();
            recurrence.setFrequency(EventFrequency.fromValue(recur.getString("frequency")));
            recurrence.setCount(recur.getInt("count"));
            recurrence.setInterval(recur.getInt("interval"));
            String[] rr = new String[] { recurrence.toRRule() };
            event.setRecurrence(Arrays.asList(rr));
        }
        EventData ed = new EventData(settings.getGuildID());
        if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) {
            ed.setImageLink(body.getString("image"));
            ed.setEventEnd(event.getEnd().getDateTime().getValue());
        }
        if (ed.shouldBeSaved()) {
            DatabaseManager.getManager().updateEventData(ed);
        }
        Event confirmed = service.events().insert(calendarData.getCalendarId(), event).execute();
        response.status(200);
        JSONObject respondBody = new JSONObject();
        respondBody.put("Message", "Successfully create event!");
        respondBody.put("id", confirmed.getId());
        response.body(respondBody.toString());
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Failed to update event!", e, EventEndpoint.class, true);
        e.printStackTrace();
        response.status(500);
        response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!"));
    }
    return response.body();
}
Also used : Recurrence(com.cloudcraftgaming.discal.api.object.event.Recurrence) EventDateTime(com.google.api.services.calendar.model.EventDateTime) Calendar(com.google.api.services.calendar.Calendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) EventData(com.cloudcraftgaming.discal.api.object.event.EventData) JSONObject(org.json.JSONObject) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) Event(com.google.api.services.calendar.model.Event) Map(java.util.Map) GuildSettings(com.cloudcraftgaming.discal.api.object.GuildSettings)

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