private static WeekDay convert(String value) {
   for (WeekDay weekDay : WeekDay.values()) {
     if (weekDay.getValue().equals(value)) {
       return weekDay;
     }
   }
   return null;
 }
示例#2
0
文件: Menu.java 项目: pbmartins/P3
public class Menu implements Serializable {
  private static final long serialVersionUID = 75264722956L;
  private String name;
  private String local;
  private Map<WeekDay, List<Plate>> menu;
  private static final WeekDay[] days = WeekDay.values();

  public Menu(String name, String local) {
    if (name.length() == 0 || local.length() == 0)
      throw new IllegalArgumentException("Nome do menu e/ou local inválidos.");

    this.name = name;
    this.local = local;
    this.menu = new TreeMap<WeekDay, List<Plate>>();
    for (WeekDay day : days) this.menu.put(day, new ArrayList<Plate>());
  }

  public String name() {
    return this.name;
  }

  public String local() {
    return this.local;
  }

  public Map<WeekDay, List<Plate>> menu() {
    return this.menu;
  }

  public List<Plate> dayMenu(WeekDay day) {
    if (day == null) throw new IllegalArgumentException("Dia inválido");
    return this.menu.get(day);
  }

  public boolean addPlate(Plate p, WeekDay day) {
    if (plateExistsInDay(p, day))
      throw new AssertionError("O prato que tentou inserir já existe na ementa do dia inserido.");
    return menu.get(day).add(p);
  }

  public boolean removePlate(Plate p, WeekDay day) {
    if (menu.isEmpty()) throw new AssertionError("A ementa está vazia.");
    return menu.get(day).remove(p);
  }

  public Plate searchPlate(String name) {
    Plate to_return = null;
    for (WeekDay day : days) {
      for (Plate p : menu.get(day)) {
        if (p.name().equals(name)) to_return = p;
      }
    }
    return to_return;
  }

  @Override
  public String toString() {
    String to_return = new String();

    for (WeekDay day : days) {
      for (Plate p : menu.get(day)) to_return += p.toString() + ", dia " + day + "\n";
    }

    return to_return;
  }

  @Override
  public boolean equals(Object m) {
    if (m == null) return false;
    if (m.getClass() != this.getClass()) return false;
    return this.name.equals(((Menu) m).name())
        && this.local.equals(((Menu) m).local())
        && this.menu.equals(((Menu) m).menu());
  }

  private boolean plateExistsInDay(Plate p, WeekDay day) {
    if (p == null) throw new IllegalArgumentException("Prato inválido.");
    if (day == null) throw new IllegalArgumentException("Dia inválido.");
    return menu.get(day).contains(p);
  }
}
  /**
   * F&uuml;gt die Wiederholungen des &uuml;bergebenen Appointment-Objekts dem &uuml;bergebenen
   * Event-Objekt hinzu.
   *
   * @param appointment Ein Rapla Appointment.
   */
  private void addRepeatings(Appointment appointment, PropertyList properties) {
    Repeating repeating = appointment.getRepeating();

    if (repeating == null) {
      return;
    }

    // This returns the strings DAYLY, WEEKLY, MONTHLY, YEARLY
    String type = repeating.getType().toString().toUpperCase();

    Recur recur;

    // here is evaluated, if a COUNT is set in Rapla, or an enddate is
    // specified
    if (repeating.getNumber() == -1) {
      recur = new Recur(type, -1);
    } else if (repeating.isFixedNumber()) {
      recur = new Recur(type, repeating.getNumber());
    } else {
      net.fortuna.ical4j.model.Date endDate = new net.fortuna.ical4j.model.Date(repeating.getEnd());
      // TODO do we need to translate the enddate in utc?
      recur = new Recur(type, endDate);
    }

    if (repeating.isDaily()) {
      // DAYLY -> settings : intervall
      recur.setInterval(repeating.getInterval());

    } else if (repeating.isWeekly()) {
      // WEEKLY -> settings : every nTh Weekday
      recur.setInterval(repeating.getInterval());

      calendar.setTime(appointment.getStart());
      recur.getDayList().add(WeekDay.getWeekDay(calendar));
    } else if (repeating.isMonthly()) {
      // MONTHLY -> settings : every nTh Weekday
      recur.setInterval(repeating.getInterval());
      calendar.setTime(appointment.getStart());
      int weekofmonth =
          Math.round(calendar.get(java.util.Calendar.DAY_OF_MONTH) / DateTools.DAYS_PER_WEEK) + 1;
      recur.getDayList().add(new WeekDay(WeekDay.getWeekDay(calendar), weekofmonth));
    } else if (repeating.isYearly()) {
      // YEARLY -> settings : every nTh day mTh Monthname
      calendar.setTime(appointment.getStart());
      calendar.get(java.util.Calendar.DAY_OF_YEAR);
    } else {
      getLogger().warn("Invalid data in recurrency rule!");
    }

    properties.add(new RRule(recur));

    // bugfix - if rapla has no exceptions, an empty EXDATE: element is
    // produced. This may bother some iCal tools
    if (repeating.getExceptions().length == 0) {
      return;
    }

    // Add exception dates
    // DateList dl = new DateList(Value.DATE);

    ExDate exDate = new ExDate();
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    net.fortuna.ical4j.model.TimeZone tz = registry.getTimeZone(timeZone.getID());

    // rku: use seperate EXDATE for each exception
    for (Iterator<Date> itExceptions = Arrays.asList(repeating.getExceptions()).iterator();
        itExceptions.hasNext(); ) {
      // DateList dl = new DateList(Value.DATE);
      Date date = itExceptions.next();
      // dl.add(new net.fortuna.ical4j.model.Date( date));
      java.util.Calendar cal = raplaLocale.createCalendar();
      cal.setTime(date);
      int year = cal.get(java.util.Calendar.YEAR);
      int day_of_year = cal.get(java.util.Calendar.DAY_OF_YEAR);
      cal.setTime(appointment.getStart());
      cal.set(java.util.Calendar.YEAR, year);
      cal.set(java.util.Calendar.DAY_OF_YEAR, day_of_year);
      int offset =
          (int) (tz.getOffset(DateTools.cutDate(date).getTime()) / DateTools.MILLISECONDS_PER_HOUR);
      cal.add(java.util.Calendar.HOUR, -offset);
      Date dateToSave = cal.getTime();
      net.fortuna.ical4j.model.DateTime dateTime = new net.fortuna.ical4j.model.DateTime();
      dateTime.setTime(dateToSave.getTime());
      exDate.getDates().add(dateTime);
    }
    exDate.setTimeZone(tz);

    properties.add(exDate);
    // properties.add(new ExDate(dl));
  }