/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.data.db_derby;

import compid.data.*;
import compid.data.DatasetInfoDAO;
import compid.data.tuples.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Lari Natri
 */
public class DatasetInfoDAODerby extends BaseDAODerby implements DatasetInfoDAO {
    
    
    private static final String SQL_GETDATASETBYID =
            "SELECT DSNAME, DSDESCR, PEPDB, SOURCESTR, ALGORITHM, PARAMS, ADDTIME " +
            "FROM DATASETINFO " +
            "WHERE DATASETINFO_ID = ?";

    private final DAOFactory daoFactory;

    public DatasetInfoDAODerby(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }
    
    @Override
    public DatasetInfo getByPK(int id) throws DAOException {
        DatasetInfo tmpDataSetInfo = new DatasetInfo("","","","",DatasetInfo.AlgType.UNKNOWN, "", new java.util.Date());
        tmpDataSetInfo.setPrimaryKey(id);
        DatasetInfo result = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = getNewConnection();
            ArrayList<DatasetInfo> tmpColl = new ArrayList<DatasetInfo>(1);
            tmpColl.add(tmpDataSetInfo);
            rs = getIntersectByPK(conn, SQL_GETDATASETBYID, tmpColl, 1);
            if (rs.next()) {
                String dsname = rs.getString("DSNAME");
                String dsdescr = rs.getString("DSDESCR");
                String pepdb = rs.getString("PEPDB");
                String sourcestr = rs.getString("SOURCESTR");
                String algorithm = rs.getString("ALGORITHM");
                String params = rs.getString("PARAMS");
                DatasetInfo.AlgType algType = DatasetInfo.AlgType.getAlgTypeFromString(algorithm);
                String dataCounts = rs.getString("DATACOUNTS");
                java.util.Date date = rs.getDate("ADDTIME"); // = new java.util.Date(milliseconds);
                result = new DatasetInfo(dsname,dsdescr,pepdb,sourcestr,algType,params,date);
                result.setPrimaryKey(id);
                result.setDataCountsStr(dataCounts);
            }
        } catch (DAOException ex) {
            throw ex; 
        } catch (SQLException ex) {
            throw new DAOException(ex);
        } finally {
            close(rs);
            close(conn);
        }
        
        return result;
    }

    @Override
    public void add(DatasetInfo dsInfo) throws DAOException {
        // this tuple has no foreign keys, so make it empty
        if (dsInfo.getForeignKeys()==null)
            dsInfo.setAllForeignKeys(new HashMap<String,Object>(0,1));
        if (dsInfo.getPrimaryKey()>-1) { // has been added before
            return;
        }
        if (dsInfo.getPrimaryKey()==Tuple.PK_ERRORINSERTING) {
            return;
        }
        int pk = insertIfNotExists(dsInfo);

        dsInfo.setPrimaryKey(pk);
    }



    
}
