Search in sources :

Example 1 with AclRule

use of com.google.api.services.calendar.model.AclRule in project DisCal-Discord-Bot by NovaFox161.

the class DashboardHandler method handleCalendarCreate.

public static String handleCalendarCreate(Request request, Response response) {
    try {
        String name = request.queryParams("cal-name");
        String desc = request.queryParams("cal-desc");
        String tz = request.queryParams("cal-tz");
        Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
        WebGuild g = (WebGuild) m.get("selected");
        if (g.isDiscalRole()) {
            Calendar calendar = new Calendar();
            calendar.setSummary(name);
            calendar.setDescription(desc);
            calendar.setTimeZone(tz.replace("___", "/"));
            try {
                com.google.api.services.calendar.Calendar service;
                if (g.getSettings().useExternalCalendar()) {
                    service = CalendarAuth.getCalendarService(g.getSettings());
                } else {
                    service = CalendarAuth.getCalendarService();
                }
                Calendar confirmed = service.calendars().insert(calendar).execute();
                AclRule rule = new AclRule();
                AclRule.Scope scope = new AclRule.Scope();
                scope.setType("default");
                rule.setScope(scope).setRole("reader");
                service.acl().insert(confirmed.getId(), rule).execute();
                CalendarData calendarData = new CalendarData(Long.valueOf(g.getId()), 1);
                calendarData.setCalendarId(confirmed.getId());
                calendarData.setCalendarAddress(confirmed.getId());
                DatabaseManager.getManager().updateCalendar(calendarData);
                // Refresh to display correct info...
                g.setCalendar(new WebCalendar().fromCalendar(calendarData, g.getSettings()));
            } catch (Exception ex) {
                Logger.getLogger().exception(null, "[WEB] Failed to confirm calendar.", ex, DashboardHandler.class, true);
            }
        }
        // Finally redirect back to the dashboard
        response.redirect("/dashboard/guild/calendar", 301);
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Calendar create failed!", e, DashboardHandler.class, true);
        halt(500, "Internal Server Exception");
    }
    return response.body();
}
Also used : Calendar(com.google.api.services.calendar.model.Calendar) WebCalendar(com.cloudcraftgaming.discal.api.object.web.WebCalendar) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) WebCalendar(com.cloudcraftgaming.discal.api.object.web.WebCalendar) JSONException(org.json.JSONException) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) AclRule(com.google.api.services.calendar.model.AclRule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AclRule

use of com.google.api.services.calendar.model.AclRule in project DisCal-Discord-Bot by NovaFox161.

the class CalendarCreator method confirmCalendar.

/**
 * Confirms the calendar and creates it within Google Calendar.
 *
 * @param e The event received upon confirmation.
 * @return A CalendarCreatorResponse Object with detailed info about the confirmation.
 */
public CalendarCreatorResponse confirmCalendar(MessageReceivedEvent e, GuildSettings settings) {
    if (hasPreCalendar(e.getMessage().getGuild().getLongID())) {
        long guildId = e.getMessage().getGuild().getLongID();
        PreCalendar preCalendar = getPreCalendar(guildId);
        if (preCalendar.hasRequiredValues()) {
            if (!preCalendar.isEditing()) {
                Calendar calendar = new Calendar();
                calendar.setSummary(preCalendar.getSummary());
                calendar.setDescription(preCalendar.getDescription());
                calendar.setTimeZone(preCalendar.getTimezone());
                try {
                    com.google.api.services.calendar.Calendar service;
                    if (settings.useExternalCalendar()) {
                        service = CalendarAuth.getCalendarService(settings);
                    } else {
                        service = CalendarAuth.getCalendarService();
                    }
                    Calendar confirmed = service.calendars().insert(calendar).execute();
                    AclRule rule = new AclRule();
                    AclRule.Scope scope = new AclRule.Scope();
                    scope.setType("default");
                    rule.setScope(scope).setRole("reader");
                    service.acl().insert(confirmed.getId(), rule).execute();
                    CalendarData calendarData = new CalendarData(guildId, 1);
                    calendarData.setCalendarId(confirmed.getId());
                    calendarData.setCalendarAddress(confirmed.getId());
                    DatabaseManager.getManager().updateCalendar(calendarData);
                    terminate(e);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(true, confirmed);
                    response.setEdited(false);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                } catch (Exception ex) {
                    Logger.getLogger().exception(e.getMessage().getAuthor(), "Failed to confirm calendar.", ex, this.getClass(), true);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(false);
                    response.setEdited(false);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                }
            } else {
                // Editing calendar...
                Calendar calendar = new Calendar();
                calendar.setSummary(preCalendar.getSummary());
                calendar.setDescription(preCalendar.getDescription());
                calendar.setTimeZone(preCalendar.getTimezone());
                try {
                    com.google.api.services.calendar.Calendar service;
                    if (settings.useExternalCalendar()) {
                        service = CalendarAuth.getCalendarService(settings);
                    } else {
                        service = CalendarAuth.getCalendarService();
                    }
                    Calendar confirmed = service.calendars().update(preCalendar.getCalendarId(), calendar).execute();
                    AclRule rule = new AclRule();
                    AclRule.Scope scope = new AclRule.Scope();
                    scope.setType("default");
                    rule.setScope(scope).setRole("reader");
                    service.acl().insert(confirmed.getId(), rule).execute();
                    terminate(e);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(true, confirmed);
                    response.setEdited(true);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                } catch (Exception ex) {
                    Logger.getLogger().exception(e.getMessage().getAuthor(), "Failed to update calendar.", ex, this.getClass(), true);
                    CalendarCreatorResponse response = new CalendarCreatorResponse(false);
                    response.setEdited(true);
                    response.setCreatorMessage(preCalendar.getCreatorMessage());
                    return response;
                }
            }
        }
    }
    return new CalendarCreatorResponse(false);
}
Also used : PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) PreCalendar(com.cloudcraftgaming.discal.api.object.calendar.PreCalendar) Calendar(com.google.api.services.calendar.model.Calendar) CalendarCreatorResponse(com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse) CalendarData(com.cloudcraftgaming.discal.api.object.calendar.CalendarData) AclRule(com.google.api.services.calendar.model.AclRule)

Example 3 with AclRule

use of com.google.api.services.calendar.model.AclRule in project muikku by otavanopisto.

the class GoogleCalendarClient method insertCalendarUserAclRule.

public AclRule insertCalendarUserAclRule(String calendarId, String email, String role) throws CalendarServiceException {
    Scope scope = new Scope();
    scope.setType("user");
    scope.setValue(email);
    AclRule aclRule = new AclRule();
    aclRule.setRole(role);
    aclRule.setScope(scope);
    return insertCalendarAclRule(calendarId, aclRule);
}
Also used : Scope(com.google.api.services.calendar.model.AclRule.Scope) AclRule(com.google.api.services.calendar.model.AclRule)

Aggregations

AclRule (com.google.api.services.calendar.model.AclRule)3 CalendarData (com.cloudcraftgaming.discal.api.object.calendar.CalendarData)2 Calendar (com.google.api.services.calendar.model.Calendar)2 CalendarCreatorResponse (com.cloudcraftgaming.discal.api.object.calendar.CalendarCreatorResponse)1 PreCalendar (com.cloudcraftgaming.discal.api.object.calendar.PreCalendar)1 WebCalendar (com.cloudcraftgaming.discal.api.object.web.WebCalendar)1 WebGuild (com.cloudcraftgaming.discal.api.object.web.WebGuild)1 Scope (com.google.api.services.calendar.model.AclRule.Scope)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JSONException (org.json.JSONException)1