Package org.olat.core.util.mail

Source Code of org.olat.core.util.mail.MailTemplateForm

/**
* 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) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.core.util.mail;

import java.util.Locale;

import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.formelements.CheckBoxElement;
import org.olat.core.gui.formelements.StaticSingleSelectionElement;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.formelements.TextElement;
import org.olat.core.gui.formelements.VisibilityDependsOnSelectionRule;
import org.olat.core.gui.translator.PackageTranslator;
import org.olat.core.util.Util;

/**
* Description:<br>
* The MailTemplateForm allows the user to enter a mailtext based on the MailTemplate
* <p>
* Initial Date: 21.11.2006 <br>
*
* @author Florian Gnaegi, frentix GmbH<br>
* http://www.frentix.com
*/
public class MailTemplateForm extends Form {

  private StaticSingleSelectionElement sendMailSwitchElem;
  private TextElement subjectElem;
  private TextAreaElement bodyElem;
  private CheckBoxElement ccSender;
  private final static String NLS_CONTACT_SEND_CP_FROM = "contact.cp.from";
 
  /**
   * Constructor for the mail notification form
   * @param locale
   * @param template Default values taken from this template
   * @param useCancel
   * @param listeningController Controller that listens to form events
   */
  public MailTemplateForm(Locale locale, MailTemplate template, boolean useCancel, Controller listeningController) {
    super("MailTemplateForm", new PackageTranslator(Util.getPackageName(MailTemplateForm.class), locale));
   
    addListener(listeningController);

    String[] skipKeys = new String[] { Boolean.TRUE.toString(), Boolean.FALSE.toString() };
    String[] skipValues = new String[] { translate("yes"), translate("no") };
    sendMailSwitchElem = new StaticSingleSelectionElement("mailtemplateform.sendMailSwitchElem", skipKeys, skipValues);
    sendMailSwitchElem.select(Boolean.TRUE.toString(), true);
    addFormElement("sendMailSwitchElem", sendMailSwitchElem);
    ccSender = new CheckBoxElement(NLS_CONTACT_SEND_CP_FROM, true);
    addFormElement("tcpfrom", ccSender);
    String value = template.getSubjectTemplate();
    subjectElem = new TextElement("mailtemplateform.subject", value, true, 60, 128);
    addFormElement("subjectElem", subjectElem);

    bodyElem = new TextAreaElement("mailtemplateform.body", 15, 60, template.getBodyTemplate());
    bodyElem.setMandatory(true);
    addFormElement("bodyElem", bodyElem);

    // rules
    VisibilityDependsOnSelectionRule rule;
    rule = new VisibilityDependsOnSelectionRule(sendMailSwitchElem, ccSender, Boolean.TRUE.toString(), true, template.getCpfrom().toString(), true);
    addVisibilityDependsOnSelectionRule(rule);
    rule = new VisibilityDependsOnSelectionRule(sendMailSwitchElem, subjectElem, Boolean.TRUE.toString(), true, template.getSubjectTemplate(), true);
    addVisibilityDependsOnSelectionRule(rule);
    rule = new VisibilityDependsOnSelectionRule(sendMailSwitchElem, bodyElem, Boolean.TRUE.toString(), true, template.getBodyTemplate(), true);
    addVisibilityDependsOnSelectionRule(rule);

    if (useCancel) {
      setCancelButton();
    }
    addSubmitKey("finish", "finish");
  }

  /**
   * @see org.olat.core.gui.components.form.Form#validate()
   */
  @Override
  public boolean validate() {
    if (sendMailSwitchEnabled()) {
      if (subjectElem.isEmpty("mailtemplateform.error.emptyfield")) return false;
      if (bodyElem.isEmpty("mailtemplateform.error.emptyfield")) return false;
      if (subjectElem.getValue().indexOf("#") != -1) {
        subjectElem.setErrorKey("mailtemplateform.error.velocity");
        return false;
      }
      if (bodyElem.getValue().indexOf("#") != -1) {
        bodyElem.setErrorKey("mailtemplateform.error.velocity");
        return false;
      }
    }
    return true;
  }
 
  /**
   * @return true: mail switch is enabled; false: otherwhise
   */
  public boolean sendMailSwitchEnabled() {
    return (sendMailSwitchElem.getSelectedKey().equals(Boolean.TRUE.toString()));
  }
 
  /**
   * Update the given templates with the values entered in the form
   * @param template
   */
  public void updateTemplateFromForm(MailTemplate template) {
    template.setSubjectTemplate(subjectElem.getValue());
    template.setBodyTemplate(bodyElem.getValue());
    template.setCpfrom(ccSender.isChecked());
  }
 
}
TOP

Related Classes of org.olat.core.util.mail.MailTemplateForm

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.