/*
 * 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.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 ProtDbInfoDAODerby extends BaseDAODerby implements ProtDbInfoDAO {

    private final DAOFactory daoFactory;
    
    private static final String SQL_GETPROTDBINFOBYID =
            "SELECT ACCPREFIX, DBNAME, DBDESCR" +
            " FROM " + ProtDbInfo.TABLELABEL +
            " WHERE PROTDBINFO_ID = ?";
    
    public ProtDbInfoDAODerby(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }
    
    @Override
    public void add(ProtDbInfo protDbInfo) throws DAOException {
        // this tuple has no foreign keys, so make it empty
        if (protDbInfo.getForeignKeys()==null) {
            protDbInfo.setAllForeignKeys(new HashMap<String,Object>(0,1));
        }
        if (protDbInfo.getPrimaryKey()>-1) { // has been added before
            return;
        }
        if (protDbInfo.getPrimaryKey()==Tuple.PK_ERRORINSERTING) {
            return;
        }
        int pk = insertIfNotExists(protDbInfo);

        protDbInfo.setPrimaryKey(pk);
    }
    
    @Override
    public ProtDbInfo getByPK(int id) throws DAOException {
        ProtDbInfo tmpProtDbInfo = new ProtDbInfo("","","");
        tmpProtDbInfo.setPrimaryKey(id);
        ProtDbInfo result = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            conn = getNewConnection();
            ArrayList<ProtDbInfo> tmpColl = new ArrayList<ProtDbInfo>(1);
            tmpColl.add(tmpProtDbInfo);
            rs = getIntersectByPK(conn, SQL_GETPROTDBINFOBYID, tmpColl, 1);
            if (rs.next()) {
                String accPrefix = rs.getString("ACCPREFIX");
                String dbName = rs.getString("DBNAME");
                String dbDescr = rs.getString("DBDESCR");
                
                result = new ProtDbInfo(accPrefix, dbName, dbDescr);
                result.setPrimaryKey(id);
            }
        } catch (DAOException ex) {
            throw ex; 
        } catch (SQLException ex) {
            throw new DAOException(ex);
        } finally {
            close(rs);
            close(conn);
        }
        
        return result;
    }    
    
    
}
