/*
 * $Header: /home/cvs/jakarta-struts/contrib/artimus/WEB-INF/src/java/org/apache/artimus/http/Render.java.text,v 1.2 2004/03/14 07:15:04 sraeburn Exp $
 * $Revision: 1.2 $
 * $Date: 2004/03/14 07:15:04 $
 *
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.apache.artimus.http;


import java.io.IOException;
import java.io.PrintWriter;

import java.sql.SQLException;

import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.digester.rss.Channel;
import org.apache.commons.digester.rss.Item;

import org.apache.scaffold.sql.AccessBean;
import org.apache.artimus.http.ArticleForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;


/**
 * Convert result bean to channel and render as response.
 * <p>
 * @author Ted Husted
 * @version $Revision: 1.2 $ $Date: 2004/03/14 07:15:04 $
 */
public final class Render extends Action {

    // --------------------------------------------------------- Instances Variables
    // --------------------------------------------------------- Public Methods


    public ActionForward perform(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
        throws IOException, ServletException {

        ActionErrors errors = new ActionErrors();
        Channel channel = new Channel();
        ArticleHelper helper = (ArticleHelper)
            request.getAttribute(AccessBean.HELPER_KEY);
        if (helper==null) {
            errors.add(ActionErrors.GLOBAL_ERROR,
                new ActionError("access.missing.parameter"));
        }

        if (errors.empty()) {
            try {
                channel.setTitle("Articles");
                channel.setDescription("The Articles application is your passport to currency.");
                channel.setLink("http://localhost/articles/");
                Iterator rows = helper.getRows();
                while (rows.hasNext()) {
                    ArticleForm article = (ArticleForm) rows.next();
                    Item item = new Item();
                    item.setTitle(article.getTitle());
                    item.setLink(
                        "http://localhost/articles/do/article/View?key=" + article.getArticle());
                    channel.addItem(item);
                }
            }
            catch (Exception e) {
                errors.add(ActionErrors.GLOBAL_ERROR,
                    new ActionError("rss.access.error"));
                servlet.log(e.toString());
            }
        }

        // -- Handle Errors ---
        if (!errors.empty()) {
            saveErrors(request, errors);
            if (mapping.getInput()!=null)
                return (new ActionForward(mapping.getInput()));
            // If no input page, use error forwarding
            return (mapping.findForward("error"));
        }

        // -- Process complete ---
        response.setContentType("text/plain");
        channel.render(response.getWriter());
        return(null);

    } // ---- End perform ----

} // ---- End Render ----
