Package dao.implementation.mysql.compteUser

Source Code of dao.implementation.mysql.compteUser.MySqlAttributionRoleUserDao

package dao.implementation.mysql.compteUser;

import modele.compteUser.AttributionRoleUser;
import modele.compteUser.CompteUser;
import modele.compteUser.RoleUser;
import dao.ICompteUserDao;
import dao.IDao;
import dao.IRoleUserDao;
import dao.implementation.mysql.AccessMySql;
import gui.MainWindow;
import gui.util.TaxiGuiUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

/**
* class MySqlAttributionRoleUserDao
* impl�mente les m�thodes de dao :
*   insert(final AttributionRoleUser attributionRoleUser),
*  update(final AttributionRoleUser attributionRoleUser),
*  find(final int id),
*  findAll(),
*  delete(final AttributionRoleUser attributionRoleUser)
* @author Kasandra
*
*/
public class MySqlAttributionRoleUserDao implements IDao<AttributionRoleUser> {

    private static final String INSERT_ATTRIBUTION_ROLE = "insert into TAttributionRoleCompteUser (idCompte, idRole, dateAttributionRole) values (?,?,?)";

    private static final String UPDATE_ATTRIBUTION_ROLE = "update TAttributionRoleCompteUser  set  dateAttributionRole = ? where idCompte = ? and idRole = ?";

    private static final String DELETE_ATTRIBUTION_ROLE = "delete from TAttributionRoleCompteUser where idCompte = ? and idRole = ? ";

    private static final String GET_ALL_ATTRIBUTION_ROLE = "select idCompte, idRole, dateAttributionRole from TAttributionRoleCompteUser";

    private static final String GET_ATTRIBUTION_ROLE_BY_ID = GET_ALL_ATTRIBUTION_ROLE
            + " where idCompte = ? and idRole = ?";

    public void delete(final AttributionRoleUser attributionRoleUser) {
        AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {

            public void runWithconnection(Connection connection) {
                try {
                    PreparedStatement smt = null;

                    try {
                        smt = connection.prepareStatement(DELETE_ATTRIBUTION_ROLE);

                        smt.setInt(1, attributionRoleUser.getCompteUser().getId());
                        smt.setInt(2, attributionRoleUser.getRoleUser().getId());

                        smt.executeUpdate();
                    } finally {
                        if (smt != null) {
                            smt.close();
                        }
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                 
                }
            }
        });
    }

    public AttributionRoleUser find(final int id) {
        final AttributionRoleUser[] result = new AttributionRoleUser[1];
        AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {

            public void runWithconnection(Connection connection) {
                try {
                    PreparedStatement smt = null;
                    ResultSet res = null;
                    try {
                        smt = connection.prepareStatement(GET_ATTRIBUTION_ROLE_BY_ID);
                        smt.setInt(1, id);
                        smt.setInt(2, id);
                        res = smt.executeQuery();

                        if (res.next()) {

                            CompteUser compteUser = new CompteUser(res.getInt(1));
                            compteUser.setLogin(res.getString(2));
                            result[0].setCompteUser(compteUser);

                            RoleUser roleUser = new RoleUser(res.getInt(3));
                            roleUser.setLibelle(res.getString(4));
                            result[0].setRoleUser(roleUser);
                        }
                    } finally {
                        try {
                            if (res != null) {
                                res.close();
                            }
                        } finally {
                            if (smt != null) {
                                smt.close();
                            }
                        }
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return result[0];
    }

    public ArrayList<AttributionRoleUser> findAll() {
        final ArrayList<AttributionRoleUser> listeAttributionRoleUser = new ArrayList<AttributionRoleUser>();
        final IDao<CompteUser> compteUserDao= getCompteUserDao();
        final IDao<RoleUser> roleUserDao= getRoleUserDao();

        AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {

            public void runWithconnection(Connection connection) {
                try {
                    PreparedStatement smt = null;
                    ResultSet res = null;
                    try {
                        smt = connection.prepareStatement(GET_ALL_ATTRIBUTION_ROLE);
                        res = smt.executeQuery();
                        while (res.next()) {
                            int idCompte = res.getInt(1);
                            int idRole = res.getInt(2);
                            Date dateAttribution = new Date(res.getDate(3).getTime());

                            RoleUser roleUser = roleUserDao.find(idRole);
                            CompteUser compteUser = compteUserDao.find(idCompte);

                            AttributionRoleUser attributionRoleUser = new AttributionRoleUser(
                                    roleUser, compteUser);
                            attributionRoleUser.setDateAttributionRoleUser(dateAttribution);

                            listeAttributionRoleUser.add(attributionRoleUser);
                        }
                    } finally {
                        try {
                            if (res != null) {
                                res.close();
                            }
                        } finally {
                            if (smt != null) {
                                smt.close();
                            }
                        }
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                 
                }
            }
        });
        return listeAttributionRoleUser;
    }

    public AttributionRoleUser insert(final AttributionRoleUser attributionRoleUser) {
        AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {

            public void runWithconnection(Connection connection) {
                try {
                    PreparedStatement psmt = null;
                    try {

                        psmt = connection.prepareStatement(INSERT_ATTRIBUTION_ROLE);

                        psmt.setInt(1, attributionRoleUser.getCompteUser().getId());
                        psmt.setInt(2, attributionRoleUser.getRoleUser().getId());
                        psmt.setDate(3, new java.sql.Date(attributionRoleUser.getDateAttributionRoleUser().getTime()));

                        psmt.executeUpdate();
                    } finally {
                        if (psmt != null) {
                            psmt.close();
                        }
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        return attributionRoleUser;
    }

    public AttributionRoleUser update(final AttributionRoleUser attributionRoleUser) {
        AccessMySql.withConnection(new AccessMySql.RunnableWithConnection() {

            public void runWithconnection(Connection connection) {
                try {
                    PreparedStatement smt = null;
                    try {
                        smt = connection.prepareStatement(UPDATE_ATTRIBUTION_ROLE);
                        smt.setDate(1, new java.sql.Date(attributionRoleUser.getDateAttributionRoleUser().getTime()));
                       
                        smt.setInt(2, attributionRoleUser.getCompteUser().getId());
                        smt.setInt(3, attributionRoleUser.getRoleUser().getId());

                        smt.executeUpdate();
                    } finally {
                        if (smt != null) {
                            smt.close();
                        }
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                 
                }
            }
        });
        return attributionRoleUser;
    }

    private IRoleUserDao getRoleUserDao() {
        return new MySqlRoleUserDao();
    }

    private ICompteUserDao getCompteUserDao() {
        return new MySqlCompteUserDao();
    }


}
TOP

Related Classes of dao.implementation.mysql.compteUser.MySqlAttributionRoleUserDao

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.