001: /*
002: * JDBCMySQLUserDAO.java
003: *
004: * Created on 28 de marzo de 2005, 13:46
005: */
006:
007: package org.manentia.kasai.role;
008:
009: import java.sql.Connection;
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013: import java.sql.Statement;
014:
015: import org.apache.commons.lang.StringEscapeUtils;
016: import org.apache.commons.lang.StringUtils;
017: import org.manentia.kasai.Constants;
018: import org.manentia.kasai.Operative;
019: import org.manentia.kasai.Role;
020: import org.manentia.kasai.exceptions.AlreadyExistsException;
021: import org.manentia.kasai.exceptions.DataAccessException;
022: import org.manentia.kasai.exceptions.DoesntExistsException;
023: import org.manentia.kasai.exceptions.InvalidAttributesException;
024: import org.manentia.kasai.operative.OperativeHandler;
025:
026: import com.manentia.commons.log.Log;
027: import com.manentia.commons.persistence.DBUtil;
028:
029: /**
030: *
031: * @author rzuasti
032: */
033: public class JDBCPGSQLRoleDAO extends JDBCANSISQLRoleDAO {
034:
035: /** Creates a new instance of JDBCMySQLUserDAO */
036: public JDBCPGSQLRoleDAO() {
037: }
038:
039: public int create(String name, String description,
040: String[] operatives) throws InvalidAttributesException,
041: AlreadyExistsException, DoesntExistsException,
042: DataAccessException {
043:
044: Log.write("Creating role '" + name + "'", Log.DEBUG, "create",
045: JDBCSQLServerRoleDAO.class);
046:
047: Connection con = null;
048: String sql;
049: Role r = null;
050: ResultSet rs = null;
051: int idRole = -1;
052: try {
053: r = new Role();
054: r.setDescription(description);
055: r.setName(name);
056: r.validate();
057:
058: if (this .list(name, true).size() > 0) {
059: Log.write("Role name already exist", Log.WARN,
060: "create", JDBCSQLServerRoleDAO.class);
061:
062: throw new AlreadyExistsException(this .getClass()
063: .getName()
064: + ".roleAlreadyExist");
065: }
066: sql = "INSERT INTO kasai_roles (name, description) VALUES (?, ?)";
067: con = DBUtil.getConnection(Constants.DATABASE_SOURCE,
068: Constants.CONFIG_PROPERTY_FILE);
069: con.setAutoCommit(false);
070: PreparedStatement stm = con.prepareStatement(sql);
071: stm.setString(1, name);
072: stm.setString(2, description);
073: stm.executeUpdate();
074: stm.close();
075:
076: Log.write("Role '" + name
077: + "' created, about to insert operatives",
078: Log.DEBUG, "create", JDBCSQLServerRoleDAO.class);
079:
080: stm = con
081: .prepareStatement("SELECT CURRVAL('kasai_roles_id_seq')");
082: rs = stm.executeQuery();
083:
084: if (rs.next()) {
085: idRole = rs.getInt(1);
086:
087: Log.write("Role '" + name + "' created with id="
088: + idRole, Log.DEBUG, "create",
089: JDBCSQLServerRoleDAO.class);
090: } else {
091: Log
092: .write("Cannot retrieve generated role ID",
093: Log.ERROR, "create",
094: JDBCSQLServerRoleDAO.class);
095:
096: throw new DataAccessException(
097: "Cannot retrieve generated role ID");
098: }
099:
100: String idOperative = null;
101: if (operatives != null) {
102: for (int i = 0; i < operatives.length; i++) {
103: idOperative = operatives[i];
104:
105: if (StringUtils.isEmpty(idOperative)
106: || (OperativeHandler.getInstance().list(
107: idOperative).size() == 0)) {
108: Log.write("Operative doesn't exist", Log.WARN,
109: "create", JDBCSQLServerRoleDAO.class);
110:
111: throw new DoesntExistsException(Operative.class
112: .getName()
113: + ".operativeDoesntExist");
114: }
115:
116: sql = "INSERT INTO kasai_roles_operatives (id_role, id_operative) VALUES ("
117: + idRole
118: + ",'"
119: + org.apache.commons.lang.StringEscapeUtils
120: .escapeSql(idOperative) + "')";
121: con.createStatement().executeUpdate(sql);
122: }
123: }
124: con.commit();
125: } catch (DataAccessException sqlE) {
126: try {
127: con.rollback();
128: } catch (SQLException e) {
129: }
130: throw sqlE;
131: } catch (DoesntExistsException deE) {
132: try {
133: con.rollback();
134: } catch (SQLException e) {
135: }
136: throw deE;
137: } catch (SQLException sqle) {
138: Log.write("SQL Error", sqle, Log.ERROR, "create",
139: JDBCSQLServerRoleDAO.class);
140:
141: throw new DataAccessException(sqle);
142: } finally {
143: try {
144:
145: con.setAutoCommit(true);
146: con.close();
147: } catch (Exception e) {
148: }
149: }
150:
151: return idRole;
152: }
153: }
|