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