Package org.olat.commons.calendar.ui

Source Code of org.olat.commons.calendar.ui.KalendarEntryForm

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.commons.calendar.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.olat.commons.calendar.CalendarManager;
import org.olat.commons.calendar.CalendarUtils;
import org.olat.commons.calendar.model.KalendarEvent;
import org.olat.commons.calendar.ui.components.KalendarRenderWrapper;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.CheckBoxElement;
import org.olat.core.gui.formelements.DateElement;
import org.olat.core.gui.formelements.SingleSelectionElement;
import org.olat.core.gui.formelements.SpacerElement;
import org.olat.core.gui.formelements.StaticSingleSelectionElement;
import org.olat.core.gui.formelements.StaticTextElement;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.formelements.VisibilityDependsOnSelectionRule;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.util.Util;

public class KalendarEntryForm extends Form {

  public static final String PACKAGE = Util.getPackageName(CalendarManager.class);

  public static final String SUBMIT_MULTI = "multi";
  public static final String SUBMIT_SINGLE = "single";

  public static final String RECURRENCE_NONE = "NONE";

  private KalendarEvent event;
  private KalendarRenderWrapper choosenWrapper;
  private StaticTextElement calendarName;
  private SingleSelectionElement chooseCalendar;
  private TextAreaElement subject, location;
  private CheckBoxElement allDayEvent;
  private DateElement begin, end;
  private SingleSelectionElement classification;
  private boolean readOnly;
  private SingleSelectionElement chooseRecurrence;
  private DateElement recurrenceEnd;
  private SpacerElement spacer;

  /**
   * Display an event for modification or to add a new event.
   *
   * @param name
   * @param event
   * @param choosenWrapper
   * @param availableCalendars  At least one calendar must be editable if this is a new event.
   * @param isNew    If it is a new event, display a list of calendars to choose from.
   * @param locale
   */
  public KalendarEntryForm(String name, KalendarEvent event, KalendarRenderWrapper choosenWrapper,
      Collection availableCalendars, boolean isNew, Locale locale) {
    super(name, new PackageTranslator(PACKAGE, locale));
    this.event = event;
    this.choosenWrapper = choosenWrapper;
    this.readOnly = choosenWrapper.getAccess() == KalendarRenderWrapper.ACCESS_READ_ONLY;
   
    List writeableCalendars = new ArrayList();
    for (Iterator iter = availableCalendars.iterator(); iter.hasNext();) {
      KalendarRenderWrapper calendarRenderWrapper = (KalendarRenderWrapper) iter.next();
      if (calendarRenderWrapper.getAccess() == KalendarRenderWrapper.ACCESS_READ_WRITE) {
        writeableCalendars.add(calendarRenderWrapper);
      }
    }
    if (isNew) {
      // add drop down to choose calendar
      String calendarKeys[] = new String[writeableCalendars.size()];
      String calendarValues[] = new String[writeableCalendars.size()];
      for (int i = 0; i < writeableCalendars.size(); i++) {
        KalendarRenderWrapper cw = (KalendarRenderWrapper)writeableCalendars.get(i);
        calendarKeys[i] = cw.getKalendar().getCalendarID();
        calendarValues[i] = cw.getKalendarConfig().getDisplayName();
      }
      chooseCalendar = new StaticSingleSelectionElement("cal.form.chooseCalendar", calendarKeys, calendarValues);
      // preselect calendar if available
      chooseCalendar.select(choosenWrapper.getKalendar().getCalendarID(), true);
      addFormElement("cal.form.chooseCalendar", chooseCalendar);
    } else {
      // display calendar name
      calendarName = new StaticTextElement("cal.form.calendarname", choosenWrapper.getKalendarConfig().getDisplayName());
      addFormElement("cal.form.calendarname", calendarName);
    }
   
    // subject (hide if in readonly mode and only free/busy flag)
    subject = new TextAreaElement("cal.form.subject", 3, 40);
    subject.setMandatory(true);
    if (readOnly && event.getClassification() == KalendarEvent.CLASS_X_FREEBUSY) {
      subject.setValue(getTranslator().translate("cal.form.subject.hidden"));
    } else {
      subject.setValue(event.getSubject());
    }
    addFormElement("cal.form.subject", subject);
   
    // location (hide if in readonly mode and only free/busy flag)
    location = new TextAreaElement("cal.form.location", 3, 40);
    location.setMandatory(false);
    if (readOnly && event.getClassification() == KalendarEvent.CLASS_X_FREEBUSY) {
      location.setValue(getTranslator().translate("cal.form.location.hidden"));
    } else {
      location.setValue(event.getLocation());
    }
    addFormElement("cal.form.location", location);
   
    // start
    begin = new DateElement("cal.form.begin", event.getBegin(), "dd.MM.yyyy HH:mm");
    begin.setMandatory(true);
    begin.setDateChooserTimeEnabled(true);
    begin.setDateChooserDateFormat("%d.%m.%Y %H:%M");
    begin.setExample(begin.getExample());
    addFormElement("cal.form.begin", begin);
   
    // end
    end = new DateElement("cal.form.end", event.getEnd(), "dd.MM.yyyy HH:mm");
    end.setMandatory(true);
    end.setDateChooserTimeEnabled(true);
    end.setDateChooserDateFormat("%d.%m.%Y %H:%M");
    begin.setExample(end.getExample());
    addFormElement("cal.form.end", end);
   
    allDayEvent = new CheckBoxElement("cal.form.allday", event.isAllDayEvent());
    addFormElement("cal.form.allday", allDayEvent);

    spacer = new SpacerElement(true, false);
    addFormElement("spacer1", spacer);
    // recurrence
    String currentRecur = CalendarUtils.getRecurrence(event.getRecurrenceRule());
    VisibilityDependsOnSelectionRule rule;
    String[] keysRecurrence = new String[] {
        RECURRENCE_NONE,
        KalendarEvent.DAILY,
        KalendarEvent.WORKDAILY,
        KalendarEvent.WEEKLY,
        KalendarEvent.BIWEEKLY,
        KalendarEvent.MONTHLY,
        KalendarEvent.YEARLY
    };
    String[] valuesRecurrence = new String[] {
        translate("cal.form.recurrence.none"),
        translate("cal.form.recurrence.daily"),
        translate("cal.form.recurrence.workdaily"),
        translate("cal.form.recurrence.weekly"),
        translate("cal.form.recurrence.biweekly"),
        translate("cal.form.recurrence.monthly"),
        translate("cal.form.recurrence.yearly")
    };
    chooseRecurrence = new StaticSingleSelectionElement("cal.form.recurrence", keysRecurrence, valuesRecurrence);
    if(currentRecur != null && !currentRecur.equals("")) {
      chooseRecurrence.select(currentRecur, true);
    } else {
      chooseRecurrence.select(RECURRENCE_NONE, true);
    }
    addFormElement("cal.form.recurrence", chooseRecurrence);
    // recurrence end date
    recurrenceEnd = new DateElement("cal.form.recurrence.end", null, "dd.MM.yyyy");
    recurrenceEnd.setDateChooserDateFormat("%d.%m.%Y");
    recurrenceEnd.setExample(translate("cal.form.recurrence.end.example"));
    Date recurEnd = CalendarUtils.getRecurrenceEndDate(event.getRecurrenceRule());
    if(recurEnd != null) recurrenceEnd.setDate(recurEnd);
    addFormElement("cal.form.recurrence.end", recurrenceEnd);
    rule = new VisibilityDependsOnSelectionRule(chooseRecurrence, recurrenceEnd, RECURRENCE_NONE, false, "", true);
    addVisibilityDependsOnSelectionRule(rule);
    addFormElement("spacer2", spacer);

    // classification
    String[] keys = new String[] {"0", "1", "2"};
    String[] values = new String[] {
        getTranslator().translate("cal.form.class.private"),
        getTranslator().translate("cal.form.class.freebusy"),
        getTranslator().translate("cal.form.class.public")
    };
   
    classification = new StaticSingleSelectionElement("cal.form.class", keys, values);
    switch (event.getClassification()) {
      case KalendarEvent.CLASS_PRIVATE: classification.select("0", true); break;
      case KalendarEvent.CLASS_X_FREEBUSY: classification.select("1", true); break;
      case KalendarEvent.CLASS_PUBLIC: classification.select("2", true); break;
      default: classification.select("0", true);
    }
    addFormElement("cal.form.class", classification);
   
    if (readOnly) {
      setDisplayOnly(true);
    } else {
      // submit/cancel buttons
      addSubmitKey("cal.form.submitSingle", SUBMIT_SINGLE);
      // if we can possibly write to more than just the selected calendar, offer to create copies
      if (writeableCalendars.size() > 1) addSubmitKey("cal.form.submitMulti", SUBMIT_MULTI);
      setCancelKey("cal.form.cancel");
    }
  }
 
  public void setEntry(KalendarEvent kalendarEvent) {
    // subject
    if (readOnly && kalendarEvent.getClassification() == KalendarEvent.CLASS_X_FREEBUSY) {
      subject.setValue(getTranslator().translate("cal.form.subject.hidden"));
    } else {
      subject.setValue(kalendarEvent.getSubject());
    }
    // location
    if (readOnly && kalendarEvent.getClassification() == KalendarEvent.CLASS_X_FREEBUSY) {
      location.setValue(getTranslator().translate("cal.form.location.hidden"));
    } else {
      location.setValue(kalendarEvent.getLocation());
    }
    begin.setDate(kalendarEvent.getBegin());
    end.setDate(kalendarEvent.getEnd());
    allDayEvent.setChecked(kalendarEvent.isAllDayEvent());
    switch (kalendarEvent.getClassification()) {
      case KalendarEvent.CLASS_PRIVATE: classification.select("0", true); break;
      case KalendarEvent.CLASS_X_FREEBUSY: classification.select("1", true); break;
      case KalendarEvent.CLASS_PUBLIC: classification.select("2", true); break;
      default: classification.select("0", true);
    }
    String recurrence = CalendarUtils.getRecurrence(kalendarEvent.getRecurrenceRule());
    if(recurrence != null && !recurrence.equals("") && !recurrence.equals(RECURRENCE_NONE)) {
      chooseRecurrence.select(recurrence, true);
      Date recurEnd = CalendarUtils.getRecurrenceEndDate(kalendarEvent.getRecurrenceRule());
      if(recurEnd != null) {
        recurrenceEnd.setDate(recurEnd);
      }
    } else {
      chooseRecurrence.select(RECURRENCE_NONE, true);
    }
  }
 
  public boolean validate() {
    boolean valid = true;
    valid = valid && subject.notEmpty("cal.form.error.mandatory");
    valid = valid && begin.notEmpty("cal.form.error.mandatory");
    valid = valid && end.notEmpty("cal.form.error.mandatory");
    if (!valid) return false;
   
    if (begin.getDate() == null) {
      begin.setErrorKey("cal.form.error.date");
      return false;
    }
    if (end.getDate() == null) {
      end.setErrorKey("cal.form.error.date");
      return false;
    }
    if (end.getDate().before(begin.getDate())) {
      end.setErrorKey("cal.form.error.endbeforebegin");
      return false;
    }
    if (!recurrenceEnd.isEmpty() && recurrenceEnd.getDate() == null) {
      recurrenceEnd.setErrorKey("cal.form.error.date");
      return false;
    }
    if (!recurrenceEnd.isEmpty() && recurrenceEnd.getDate().before(begin.getDate())) {
      recurrenceEnd.setErrorKey("cal.form.error.endbeforebegin");
      return false;
    }

    return true;
  }

  /**
   * Get event with updated values.
   * @return
   */
  public KalendarEvent getUpdatedKalendarEvent() {
    // subject
    event.setSubject(subject.getValue());

    // location
    event.setLocation(location.getValue());

    // date / time
    event.setBegin(begin.getDate());
    event.setEnd(end.getDate());
    event.setLastModified(new Date().getTime());
    if(event.getCreated() == 0) {
      event.setCreated(new Date().getTime());
    }
   
    // allday event?
    event.setAllDayEvent(allDayEvent.isChecked());
   
    // classification
    switch (classification.getSelected()) {
      case 0: event.setClassification(KalendarEvent.CLASS_PRIVATE); break;
      case 1: event.setClassification(KalendarEvent.CLASS_X_FREEBUSY); break;
      case 2: event.setClassification(KalendarEvent.CLASS_PUBLIC); break;
      default: throw new OLATRuntimeException("getSelected() in KalendarEntryForm.classification returned weitrd value", null);
    }

    // recurrence
    if (!chooseRecurrence.getSelectedKey().equals(RECURRENCE_NONE)) {
      String rrule = CalendarUtils.getRecurrenceRule(chooseRecurrence.getSelectedKey(), recurrenceEnd.getDate());
      event.setRecurrenceRule(rrule);
    }

    return event;
  }
 
  public String getChoosenKalendarID() {
    if (chooseCalendar == null) {
      return choosenWrapper.getKalendar().getCalendarID();
    }
    return chooseCalendar.getSelectedKey();
  }
}
TOP

Related Classes of org.olat.commons.calendar.ui.KalendarEntryForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.