/**
   * Build the normal state content for this portlet.
   *
   * @param portlet The jsp-based portlet that is being built.
   * @param rundata The turbine rundata context for this request.
   */
  protected void buildNormalContext(Portlet portlet, RunData rundata) {
    PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
    Iterator i = entry.getParameterNames();
    Hashtable qa = new Hashtable();

    while (i.hasNext()) {
      String name = (String) i.next();
      Parameter param = entry.getParameter(name);
      if (param.isHidden() == false) {
        String title = param.getTitle();
        String value = portlet.getPortletConfig().getInitParameter(name);
        qa.put(title, value);
      }
    }

    rundata.getRequest().setAttribute("questions", qa);

    // After successful send, the user may or may not click the Continue button so
    // reset to default template here
    if (rundata.getRequest().getAttribute("email") == null) {
      // this.setTemplate(rundata, portlet, null);
      resetTemplate(rundata);
    }
  }
  /**
   * Sort the quotes.
   *
   * @param portlet The jsp-based portlet that is being built.
   * @param rundata The turbine rundata context for this request.
   */
  public void doEmail(RunData rundata, Portlet portlet) {
    StringBuffer emailBody = new StringBuffer();
    PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
    Iterator i = entry.getParameterNames();

    while (i.hasNext()) {
      String name = (String) i.next();
      Parameter param = entry.getParameter(name);
      if (param.isHidden() == false) {
        String title = param.getTitle();
        String value = portlet.getPortletConfig().getInitParameter(name);
        value = value == null || value.length() == 0 ? "NOT PROVIDED" : value;
        emailBody.append(title);
        emailBody.append(" ===> ");
        emailBody.append(value);
        emailBody.append("\n\n");
      }
    }

    String emailSmtp = JetspeedResources.getString(JetspeedResources.MAIL_SERVER_KEY);
    String emailFrom = JetspeedResources.getString("mail.support", "*****@*****.**");
    String emailTo =
        rundata.getParameters().getString("emailTo", "*****@*****.**");
    String emailAttachment = rundata.getRequest().getParameter("emailAttachment");
    try {
      String emailText = emailBody.toString();

      // Create the JavaMail session
      java.util.Properties properties = System.getProperties();
      properties.put("mail.smtp.host", emailSmtp);
      Session emailSession = Session.getInstance(properties, null);

      // Construct the message
      MimeMessage message = new MimeMessage(emailSession);

      // Set the from address
      Address fromAddress = new InternetAddress(emailFrom);
      message.setFrom(fromAddress);

      // Parse and set the recipient addresses
      Address[] toAddresses = InternetAddress.parse(emailTo);
      message.setRecipients(Message.RecipientType.TO, toAddresses);

      // Set the subject and text
      message.setSubject("Jetspeed Questionnaire from " + rundata.getUser().getEmail());
      message.setText(emailText);

      // Attach file with message
      if (emailAttachment != null) {
        File file = new File(emailAttachment);
        if (file.exists()) {
          // create and fill the first message part
          MimeBodyPart mbp1 = new MimeBodyPart();
          mbp1.setText(emailText);

          // create the second message part
          MimeBodyPart mbp2 = new MimeBodyPart();

          // attach the file to the message
          FileDataSource fds = new FileDataSource(emailAttachment);
          mbp2.setDataHandler(new DataHandler(fds));
          mbp2.setFileName(fds.getName());

          // create the Multipart and its parts to it
          Multipart mp = new MimeMultipart();
          mp.addBodyPart(mbp1);
          mp.addBodyPart(mbp2);

          // add the Multipart to the message
          message.setContent(mp);
        } else {
          message.setText(emailBody.toString());
        }
      }

      // send the message
      Transport.send(message);

      // Display confirmation
      rundata.getRequest().setAttribute("email", emailBody.toString());
      String confirmTemplate =
          portlet
              .getPortletConfig()
              .getInitParameter("confirm.template", "JetspeedQuestionnaireConfirmation.jsp");
      // this.setTemplate(rundata, portlet, confirmTemplate);
      setTemplate(rundata, confirmTemplate, true);

      rundata.setMessage("Email successfully sent");
    } catch (Exception e) {
      logger.error("Exception", e);
      rundata.setMessage("Error sending email: " + e);
    }

    // buildNormalContext(portlet, rundata);

  }