/*
 * 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.Setting;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Lari Natri
 */
public class SettingDAODerby extends BaseDAODerby implements SettingDAO {

    private String SQL_UPDATESETTING =
            "UPDATE SETTINGS " +
            "SET SETVALUE=?, " +
            "    SETDESCR=?, " +
            "    SETACTIVE=?, " +
            "    SETUSEREDITABLE=?, " +
            "    SETFLAGS=? " +
            "WHERE SETTINGS_ID = ?"; // NOI18N

    private String SQL_GETSETTINGBYPK =
            "SELECT * FROM SETTINGS WHERE SETTINGS_ID = ?"; // NOI18N


    private final DAOFactory daoFactory;


    public SettingDAODerby(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

    @Override
    public Setting get(String settingName) throws DAOException {
        
        Connection conn = getNewConnection();
        Setting setting = null;
        int pk = findPrimaryKey(conn, new Setting(settingName,"",""));
        if (pk<0) { // not found
            return null;
        } else {
            PreparedStatement getStmt = prepareStatement(conn, SQL_GETSETTINGBYPK);
            try {
                getStmt.setInt(1, pk);
                ResultSet rs = getStmt.executeQuery();
                if (rs.next()) {
                    String name = rs.getString("SETNAME"); // NOI18N
                    String value = rs.getString("SETVALUE"); // NOI18N
                    String descr = rs.getString("SETDESCR"); // NOI18N
                    int active = rs.getInt("SETACTIVE"); // NOI18N
                    int userEd = rs.getInt("SETUSEREDITABLE"); // NOI18N
                    int flags = rs.getInt("SETFLAGS"); // NOI18N
                    setting = new Setting(name,value,descr,active,userEd,flags);
                    setting.setPrimaryKey(pk);
                }
            } catch (Exception ex) {
                throw new DAOException(ex);
            }
            return setting;

        }
    }

    @Override
    public void addOrUpdate(Setting setting) throws DAOException {

        Connection conn = getNewConnection();
        PreparedStatement updStmt = null;

        int pk = findPrimaryKey(conn, setting);

        try {
            if (pk>0) { // a setting with the same name exists

                updStmt = prepareStatement(conn, SQL_UPDATESETTING);
                updStmt.setString(1, setting.getValue());
                updStmt.setString(2, setting.getDescription());
                updStmt.setInt(3, setting.getActive());
                updStmt.setInt(4, setting.getUserEditable());
                updStmt.setInt(5, setting.getFlags());
                updStmt.setInt(6, pk);

                updStmt.executeUpdate();

            } else { // setting doesn't exist, insert it
                pk = insertIfNotExists(setting);
            }
        } catch (Exception ex) {
            throw new DAOException(ex);
        } finally {
            close(updStmt);
            close(conn);
        }

        setting.setPrimaryKey(pk);
    }
}
