/*
 * 
 * Copyright
 *  2009 axYus - www.axyus.com
 *  2009 Christophe 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.cp.xemelios.data.impl.mysql.serialization;

import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Christophe Marchand <christophe.marchand@axyus.com>
 */
public class ExportJobImpl extends ExportJob {

    public ExportJobImpl(ResultSet rs, boolean full) throws SQLException {
        super();
        int i=1;
        if(full) {
            setIdExport(rs.getLong(i++));
            setFileName(rs.getString(i++));
            setExportName(rs.getString(i++));
            setDocumentId(rs.getString(i++));
            setEtatId(rs.getString(i++));
            setElementId(rs.getString(i++));
            setExportConfigId(rs.getLong(i++));
            setSize(rs.getInt(i++));
            setPriority(rs.getInt(i++));
            setStatus(rs.getInt(i++));
            setSubmitDate(rs.getDate(i++)); if(rs.wasNull()) setSubmitDate(null);

            try {
                String s = rs.getString(i);
                if(!rs.wasNull()) setRunDate(rs.getDate(i));
            } catch(Throwable t) {
                // ont ignore, ca pete quand c'est nul
            }
            i++;

            try {
                String s= rs.getString(i);
                if(!rs.wasNull()) setDownloadDate(rs.getDate(i));
            } catch(Throwable t) {
                // on ignore, ca pete quand c'est nul
            }
            i++;
            setErrorMessage(rs.getString(i++));
            setGeneratedFileName(rs.getString(i++));
            i++;
            try {
                InputStream is = rs.getBinaryStream(i++);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int read = 0;
                byte[] buffer = new byte[1024];
                while((read=is.read(buffer))>0) {
                    baos.write(buffer,0,read);
                }
                setDrs(baos.toByteArray());
            } catch(IOException ioEx) {}
        } else {
            setIdExport(rs.getLong(i++));
            setFileName(rs.getString(i++));
            setExportName(rs.getString(i++));
            setDocumentId(rs.getString(i++));
            setEtatId(rs.getString(i++));
            setElementId(rs.getString(i++));
            setExportConfigId(rs.getLong(i++));
            setSize(rs.getInt(i++));
            setPriority(rs.getInt(i++));
            setStatus(rs.getInt(i++));
            setSubmitDate(rs.getDate(i++)); if(rs.wasNull()) setSubmitDate(null);

            try {
                String s = rs.getString(i);
                if(!rs.wasNull()) setRunDate(rs.getDate(i));
            } catch(Throwable t) {
                // ont ignore, ca pete quand c'est nul
            }
            i++;

            try {
                String s= rs.getString(i);
                if(!rs.wasNull()) setDownloadDate(rs.getDate(i));
            } catch(Throwable t) {
                // on ignore, ca pete quand c'est nul
            }
            i++;

            setErrorMessage(rs.getString(i++));
            setGeneratedFileName(rs.getString(i++));
        }
    }

}
