Package beans.core.db

Source Code of beans.core.db.CostControl

package beans.core.db;

import beans.enumeration.Stat;
import beans.serializable.ChangeStatut;
import beans.serializable.Cost;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author mastersnes
*/
public class CostControl {

    public static final String OBJET_COST = "OBJET_COUT";
    public static final String SPELL_COST = "SPELL_COUT";
    public static final String SKILL_COST = "SKILL_COUT";
    public static final String SKILL_NAME = "nameSkill";
    public static final String SPELL_NAME = "nameSpell";
    public static final String OBJET_NAME = "nameObjet";
   
    private final String COSTID = "coutId";
    private final String IMPACT = "impactCout";
    private final String STAT = "stat";
    private final String TABLE_NAME = "COUT";

    public List<Cost> getCost(final String key, final String junction) {
        final List<Cost> cost = new ArrayList<Cost>();
        List<Integer> ids = null;
        CostJunctionControl costJunctionControl = null;

        if (ObjetControl.TABLE_NAME.equals(junction)) {
            costJunctionControl = new CostJunctionControl(OBJET_COST, OBJET_NAME);
        } else if (SpellControl.TABLE_NAME.equals(junction)) {
            costJunctionControl = new CostJunctionControl(SPELL_COST, SPELL_NAME);
        } else if (SkillControl.TABLE_NAME.equals(junction)) {
            costJunctionControl = new CostJunctionControl(SKILL_COST, SKILL_NAME);
        }
        if (costJunctionControl != null) {
            ids = costJunctionControl.getCost(key);
            for (final Integer id : ids) {
                cost.add(getCostById(id));
            }
        }
        return cost;
    }

    private Cost getCostById(final int id) {
        ResultSet set = null;
        PreparedStatement statement = null;
        try {
            statement = Database.getStatement("SELECT * FROM " + TABLE_NAME + " where " + COSTID + " = ?");
            statement.setInt(1, id);

            set = statement.executeQuery();

            return getFromRowSet(set);
        } catch (final SQLException ex) {
            Logger.getLogger(ChangeStatutControl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            closeProperly(statement);
        }
    }

    private void closeProperly(final Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (final SQLException ex) {
                Logger.getLogger(ChangeStatutControl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private Cost getFromRowSet(final ResultSet set) {
        try {
            final Stat stat = Stat.valueOf(set.getString(STAT));
            final int impact = set.getInt(IMPACT);
            return new Cost(stat, impact);
        } catch (final SQLException ex) {
            Logger.getLogger(ChangeStatutControl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void saveCost(final String key, final Cost cost, final String junction) {
        PreparedStatement statement = null;
        ResultSet set = null;
        try {
            statement = Database.getStatement("insert into " + TABLE_NAME + " values (?, ?, ?)");
            statement.setInt(2, cost.getImpact());
            statement.setString(3, cost.getStat().name());
            int nextId = getNextId();
            statement.setInt(1, nextId);
            statement.executeUpdate();
            saveJunction(key, nextId, junction);

        } catch (final SQLException ex) {
            Logger.getLogger(ObjetControl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeProperly(statement);
        }
    }

    private void saveJunction(final String key, final int id, final String junction) {
        ChangeStatutJunctionControl changeStatutJunctionControl = null;
        if (ObjetControl.TABLE_NAME.equals(junction)) {
            changeStatutJunctionControl = new ChangeStatutJunctionControl(OBJET_COST, OBJET_NAME);
        } else if (SpellControl.TABLE_NAME.equals(junction)) {
            changeStatutJunctionControl = new ChangeStatutJunctionControl(SPELL_COST, SPELL_NAME);
        } else if (SkillControl.TABLE_NAME.equals(junction)) {
            changeStatutJunctionControl = new ChangeStatutJunctionControl(SKILL_COST, SKILL_NAME);
        }

        if (changeStatutJunctionControl != null) {
            changeStatutJunctionControl.saveJunction(key, id);
        }

    }

    private int getNextId() {
        PreparedStatement statement = null;
        ResultSet set = null;
        try {
            statement = Database.getStatement("select count(*) from " + TABLE_NAME);
            set = statement.executeQuery();

            return set.getInt(1);
        } catch (final SQLException ex) {
            Logger.getLogger(ObjetControl.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        } finally {
            closeProperly(statement);
        }
    }
}
TOP

Related Classes of beans.core.db.CostControl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.