/**
   * 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);
    }
  }
Exemplo n.º 2
0
  /** Subclasses must override this method to provide default behavior for the portlet action */
  protected void buildNormalContext(Portlet portlet, Context context, RunData rundata) {
    String regName = portlet.getPortletConfig().getInitParameter("registry", Registry.PORTLET);

    Boolean refresh =
        (Boolean)
            PortletSessionState.getAttribute(
                rundata, PREFIX + regName + ":" + REFRESH, Boolean.FALSE);

    if (refresh.equals(Boolean.TRUE)) {
      PortletSessionState.clearAttribute(portlet, rundata, START);
      PortletSessionState.clearAttribute(portlet, rundata, RESULTS);
      PortletSessionState.clearAttribute(portlet, rundata, FILTERED_RESULTS);
      PortletSessionState.clearAttribute(rundata, PREFIX + regName + ":" + REFRESH);
    }

    int start = getStart(rundata, portlet);
    if (start < 0) start = 0;

    String pageSize = portlet.getPortletConfig().getInitParameter("page-size", "20");

    int size = Integer.parseInt(pageSize);

    int prev = start - size;

    if (prev < 0) {
      prev = 0;
    }

    String[] filterFields =
        (String[]) PortletSessionState.getAttribute(portlet, rundata, FILTER_FIELDS);
    String[] filterValues =
        (String[]) PortletSessionState.getAttribute(portlet, rundata, FILTER_VALUES);

    List regEntries = (List) PortletSessionState.getAttribute(portlet, rundata, RESULTS);
    List filteredEntries =
        (List) PortletSessionState.getAttribute(portlet, rundata, FILTERED_RESULTS);
    if (regEntries == null) {
      Iterator i = Registry.get(regName).listEntryNames();
      regEntries = new ArrayList();

      while (i.hasNext()) {
        String name = (String) i.next();

        RegistryEntry regEntry = Registry.getEntry(regName, name);

        if ((regEntry != null) && (!regEntry.isHidden())) {
          regEntries.add(regEntry);
        }
      }

      Collections.sort(
          regEntries,
          new Comparator() {
            public int compare(Object o1, Object o2) {
              String t1 = ((RegistryEntry) o1).getName().toLowerCase();
              String t2 = ((RegistryEntry) o2).getName().toLowerCase();

              return t1.compareTo(t2);
            }
          });

      PortletSessionState.setAttribute(portlet, rundata, RESULTS, regEntries);

      filteredEntries = filter(regEntries, filterFields, filterValues);
      PortletSessionState.setAttribute(portlet, rundata, FILTERED_RESULTS, filteredEntries);
    }

    if (filterFields != null
        && filterValues != null
        && filterFields.length == filterValues.length) {
      for (int i = 0; i < filterFields.length; i++) {
        String field = filterFields[i];
        String value = filterValues[i];

        context.put(field + "_filter_value", value);
      }
    }

    int end = start + size;
    if (end > filteredEntries.size()) {
      end = filteredEntries.size();
    }
    List pageEntries = filteredEntries.subList(start, end);

    context.put("registry", pageEntries);
    context.put("filtered_entries", filteredEntries);
    if (start > 0) {
      context.put("prev", String.valueOf(prev));
    }
    if (end < filteredEntries.size()) {
      context.put("next", String.valueOf(end));
    }
  }
  /**
   * 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);

  }