/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.imports;

import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.web.AttributeHelper;
import fr.gouv.finances.dgfip.xemelios.web.utils.StdDispatchAction;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author chm
 */
public class NewImportAction extends StdDispatchAction {
    public static final int FILE_SIZE_LIMIT_KB = 200;
    public static final long FILE_SIZE_LIMIT = FILE_SIZE_LIMIT_KB*1024;
    private static Logger logger = Logger.getLogger(NewImportAction.class);

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        UploadForm uf = (UploadForm)form;
        XemeliosUser user = AttributeHelper.getUser(request.getSession());
        FormFile ff = uf.getTheFile();
        java.io.File ioFile = java.io.File.createTempFile("imp-", ".tmp");
        FileOutputStream os = new FileOutputStream(ioFile);
        byte[] buffer = new byte[2048];
        InputStream is = ff.getInputStream();
        int read = 0;
        do {
            read = is.read(buffer);
            if(read>0)
                os.write(buffer, 0, read);
        } while(read>0);
        os.flush(); os.close();
        ImportHelper.newImport(user, ioFile, ff.getFileName(), uf.getFileType(), request.getRemoteAddr());
        StringBuffer sb = new StringBuffer();
        sb.append("<p style=\"font-size: 14px; font-weight: bold; vertical-align: baseline\"><img src=\"").append(request.getContextPath()).append("/img/face-smile_003.png\" style=\"border-style: none;\"/>").append("<span style=\"margin-left: 12px;\">Import correctement soumis.</span></p>");
        request.setAttribute("message",sb.toString());
        return mapping.findForward("success");
    }


//    @Override
//    protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        try {
//            Uploader su = new Uploader();
//            su.setTotalMaxFileSize(200*1024);
//            su.initialize(request,response);
//            su.upload();
//            Files files = su.getFiles();
//            File file = files.getFile(0);
//            if(file.getBuffer().length==0) {
//                request.setAttribute("error.message","buffer length is 0");
//                return mapping.findForward("simple.error");
//            } else {
//                java.io.File ioFile = java.io.File.createTempFile("imp-", ".tmp");
//                OutputStream os = new FileOutputStream(ioFile);
//                os.write(file.getBuffer(), 0, file.getBuffer().length);
//                os.flush(); os.close();
//                ImportHelper.newImport(user, ioFile, file.getFileName(), su.getRequest().getParameter("fileType"), request.getRemoteAddr());
//                StringBuffer sb = new StringBuffer();
//                sb.append("<p style=\"font-size: 14px; font-weight: bold; vertical-align: baseline\"><img src=\"").append(request.getContextPath()).append("/img/face-smile_003.png\" style=\"border-style: none;\"/>").append("<span style=\"margin-left: 12px;\">Import correctement soumis.</span></p>");
//                request.setAttribute("message",sb.toString());
//                return mapping.findForward("success");
//            }
//        } catch(SecurityException sEx) {
//            String message = sEx.getMessage();
//            if(message.indexOf("1105")>=0 || message.indexOf("1110")>=0) {
//                message = "Ce fichier excède la taille limite de fichier importables par Xemelios Web: "+FILE_SIZE_LIMIT_KB+"Kb";
//            } else {
//                // proposer une traduction ?
//            }
//            StringBuffer sb = new StringBuffer();
//            sb.append("<p style=\"font-size: 14px; font-weight: bold; vertical-align: baseline\"><img src=\"").append(request.getContextPath()).append("/img/process-stop\" style=\"border-style: none;\"/>").append("<span style=\"margin-left: 12px;\">").append(message).append(".</span></p>");
//            request.setAttribute("message",sb.toString());
//            return mapping.findForward("success");
//        }
//    }

    
}
