Ejemplo n.º 1
0
  public void createCalEvent(Date startDate, Date endDate, String summary, String desc)
      throws IOException {

    com.google.api.services.calendar.Calendar service = getCalendarService();
    Event createEvent = new Event().setSummary(summary).setDescription(desc);

    DateTime startTime = new DateTime(startDate);
    DateTime endTime = new DateTime(endDate);

    EventDateTime start = new EventDateTime().setDateTime(startTime).setTimeZone("Asia/Kolkata");

    createEvent.setStart(start);

    EventDateTime end = new EventDateTime().setDateTime(endTime).setTimeZone("Asia/Kolkata");

    createEvent.setEnd(end);

    EventAttendee[] attendees =
        new EventAttendee[] {
          new EventAttendee().setEmail("*****@*****.**"),
          new EventAttendee().setEmail("*****@*****.**"),
          new EventAttendee().setEmail("*****@*****.**")
        };

    createEvent.setAttendees(Arrays.asList(attendees));

    EventReminder[] reminderOverrides =
        new EventReminder[] {new EventReminder().setMethod("popup").setMinutes(10)};

    Event.Reminders reminders =
        new Event.Reminders().setUseDefault(false).setOverrides(Arrays.asList(reminderOverrides));

    createEvent.setReminders(reminders);

    String calendarId = "primary";
    createEvent = service.events().insert(calendarId, createEvent).execute();
    System.out.printf("Event created: %s\n", createEvent.getHtmlLink());
  }
  @Override
  protected void channelRead0(
      ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest)
      throws Exception {
    String jsonReturn = "";

    String messageContent = getHttpMessageContent(fullHttpRequest);

    if (isPost(fullHttpRequest) && messageContent != null) {
      BookingInquiryObject bookingInquiry =
          new Gson().fromJson(messageContent, BookingInquiryObject.class);

      if (bookingInquiry != null
          && bookingInquiry.getBookingInquiry() != null
          && bookingInquiry.getBookingInquiry().getFirmanavn() != null
          && bookingInquiry.getBookingInquiry().getEvents().size() > 0) {

        bookingInquiry
            .getBookingInquiry()
            .setId(
                bookingInquiry.getBookingInquiry().getFirmanavn().replace("/", "_")
                    + "_"
                    + System.currentTimeMillis());
        bookingInquiry
            .getBookingInquiry()
            .setSubject(
                "Ny Reservasjonsforespørsel fra: "
                    + bookingInquiry.getBookingInquiry().getFirmanavn());

        StringBuilder message = new StringBuilder();

        if (bookingInquiry.getBookingInquiry().getCommunityBeskrivelse() != null) {
          message
              .append(bookingInquiry.getBookingInquiry().getCommunityBeskrivelse())
              .append("\n;;;\n");
        }

        message
            .append("firmanavn: " + bookingInquiry.getBookingInquiry().getFirmanavn())
            .append("\n");
        message.append("epost: " + bookingInquiry.getBookingInquiry().getEpost()).append("\n");
        message.append("tlf: " + bookingInquiry.getBookingInquiry().getTlf()).append("\n");
        message
            .append("beskrivelse: " + bookingInquiry.getBookingInquiry().getBeskrivelse())
            .append("\n");
        message
            .append("bevertning: " + bookingInquiry.getBookingInquiry().getOenskerBevertning())
            .append("\n");
        message.append("events: \n");

        StringBuilder emailMessage = new StringBuilder();
        emailMessage
            .append("Ny booking forespoersel fra: ")
            .append(bookingInquiry.getBookingInquiry().getFirmanavn())
            .append(" \r\n ");

        for (String event : bookingInquiry.getBookingInquiry().getEvents()) {
          String[] eventParts = event.split("-|_|;");
          if (eventParts.length == 5) {
            Event googleEvent =
                createEvent(
                    eventParts[0],
                    eventParts[1],
                    eventParts[2],
                    eventParts[3],
                    eventParts[4],
                    bookingInquiry.getBookingInquiry().getFirmanavn(),
                    bookingInquiry.getBookingInquiry().getEpost(),
                    message.toString());
            emailMessage.append(googleEvent.getHtmlLink()).append(" \r\n");
          }
          message.append(event).append("\n");
        }
        bookingInquiry.getBookingInquiry().setMessage(emailMessage.toString());

        // Email to Customer
        String filename =
            System.getProperty("no.haagensoftware.contentice.webappDir")
                + "/"
                + getDomain().getWebappName()
                + "/uploads/booking_kvittering.html";
        File template = new File(filename);
        String templateHtml = null;

        if (template != null && template.isFile() && template.exists()) {
          try {
            templateHtml = FileUtil.getFilecontents(template);
            BookingInquiry emailToCustomer = new BookingInquiry(bookingInquiry.getBookingInquiry());
            emailToCustomer.setId(emailToCustomer.getId() + "_customer");
            emailToCustomer.setSubject("Takk for din bookingforespørsel på Teknologihuset!");
            emailToCustomer.setMessage(templateHtml);

            getStorage()
                .setSubCategory(
                    getDomain().getWebappName(),
                    "emailsNotSent",
                    emailToCustomer.getId(),
                    BookingInquiryAssembler.convertBookingInquiryToSubCategory(emailToCustomer));
          } catch (IOException e) {
            e.printStackTrace();
            templateHtml = null;
          }
        }

        // Email to vert!
        getStorage()
            .setSubCategory(
                getDomain().getWebappName(),
                "emailsNotSent",
                bookingInquiry.getBookingInquiry().getId(),
                BookingInquiryAssembler.convertBookingInquiryToSubCategory(
                    bookingInquiry.getBookingInquiry()));

        EpostExecutor.getInstance(getDomain().getWebappName()).sendRemainingEmails(getStorage());

        jsonReturn = "{\"bookingInquiry\": {}}";

        /*Event event = new Event();
        event.setSummary("New Event");
        Date startDate = new Date();
        Date endDate = new Date(startDate.getTime() + 3600000);
        DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
        event.setStart(new EventDateTime().setDateTime(start));
        DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
        event.setEnd(new EventDateTime().setDateTime(end));
        return event;*/
      }
    }

    writeContentsToBuffer(channelHandlerContext, jsonReturn, "application/json");
  }