/*
 * 
 * Created on Aug 2, 2005
 * 
 * György Süveges
 * Etixpert Kft.
 * Copyright(c) 2005
 * 
 */
package com.etixpert.evolution.gui.servlets.pageactions;

import javax.servlet.http.*;
import java.io.*;

import org.apache.commons.fileupload.*;
import java.util.*;
import javax.servlet.jsp.jstl.fmt.*;
import com.etixpert.evolution.*;

/**
 * @author Suveges Gyorgy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class newsletter_attach extends MultipartPageActionHandler {

    /**
     * 
     */
    public newsletter_attach() {
        super();
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.etixpert.evolution.gui.servlets.pageactions.PageActionHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void handle(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
       
        HttpSession session = request.getSession();
       
        Map jspMap = (Map) session.getAttribute("newsletter.jsp");
        List l = (List) jspMap.get("files");
        if (l==null) l=new ArrayList();
        
        for (int i=0; i<fileItems.size(); i++) {
            FileItem item = (FileItem) fileItems.get(i);
            if (!item.isFormField() && item.getSize()>0) {
                String name = Tools.getFileName(item.getName());
                logger.info("Adding file "+name+"("+item.getSize()+") as attachment ");
                l.add(item);
            }  else  if (!item.isFormField()) {
               
                logger.error("Invalid file path oder file with 0 size");
                session.setAttribute("newsletter_error",LocaleSupport.getLocalizedMessage(pageContext,"INVALID_PATH_OR_0_SIZE"));
            }
                
                
        }
        jspMap.put("files", l);
        
            
        
    }

}
