예제 #1
0
  /**
   * main method to interact with {@link AvailableApplicationTool}.
   *
   * @param args
   * @throws SchedulingException
   * @throws NotAVisitorException
   * @throws CalendarAccountNotFoundException
   */
  public static void main(String[] args)
      throws CalendarAccountNotFoundException, NotAVisitorException, SchedulingException {
    // scan the arguments
    if (args.length == 0) {
      System.err.println(
          "Usage: AppointmentTool create [-owner username] [-visitor username] [-start YYYYmmdd-hhmm] [-duration minutes]");
      System.exit(1);
    }

    if (CREATE.equals(args[0])) {
      String visitorUsername = null;
      String ownerUsername = null;
      Date startTime = null;
      int duration = 30;

      for (int i = 1; i < args.length; i++) {
        if (OWNER_ARG.equalsIgnoreCase(args[i])) {
          ownerUsername = args[++i];
        } else if (VISITOR_ARG.equalsIgnoreCase(args[i])) {
          visitorUsername = args[++i];
        } else if (START_ARG.equalsIgnoreCase(args[i])) {
          String start = args[++i];
          SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
          try {
            startTime = df.parse(start);
          } catch (ParseException e) {
            System.err.println("Invalid format for start parameter, must match: " + DATE_FORMAT);
            System.exit(1);
          }
        } else if (DURATION_ARG.equalsIgnoreCase(args[i])) {
          String dur = args[++i];
          duration = Integer.parseInt(dur);
        }
      }

      Validate.notEmpty(ownerUsername, "owner argument cannot be empty");
      Validate.notEmpty(visitorUsername, "visitor argument cannot be empty");
      Validate.notNull(startTime, "start argument cannot be empty");

      ApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG);

      AppointmentTool tool =
          new AppointmentTool(
              (SchedulingAssistantService) applicationContext.getBean("schedulingAssistantService"),
              (ICalendarAccountDao) applicationContext.getBean("calendarAccountDao"),
              (OwnerDao) applicationContext.getBean("ownerDao"),
              (VisitorDao) applicationContext.getBean("visitorDao"),
              (AvailableScheduleDao) applicationContext.getBean("availableScheduleDao"));

      Date endDate = DateUtils.addMinutes(startTime, duration);
      VEvent event =
          tool.createAvailableAppointment(visitorUsername, ownerUsername, startTime, endDate);
      System.out.println("Event successfully created: ");
      System.out.println(event.toString());
    } else {
      System.err.println("Unrecognized command: " + args[0]);
      System.exit(1);
    }
  }