// package
package org.mskcc.cgds.dao;

// imports
import org.mskcc.cgds.model.User;
import org.mskcc.cgds.model.UserAuthorities;

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

/**
 * DAO into authorities table.
 *
 * @author Benjamin Gross
 */
public class DaoUserAuthorities {

	public static int addUserAuthorities(UserAuthorities userAuthorities) throws DaoException {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int toReturn = 0;
		try {
			con = JdbcUtil.getDbConnection();
			String email = userAuthorities.getEmail();
			for (String authority : userAuthorities.getAuthorities()) {
				pstmt = con.prepareStatement("INSERT INTO authorities (`EMAIL`, `AUTHORITY`) VALUES (?,?)");
				pstmt.setString(1, email);
				pstmt.setString(2, authority);
				toReturn += pstmt.executeUpdate();
			}
		} catch (SQLException e) {
			throw new DaoException(e);
		} finally {
			JdbcUtil.closeAll(con, pstmt, rs);
		}

		// outta here
		return toReturn;
	}

	public static UserAuthorities getUserAuthorities(User user) throws DaoException {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		UserAuthorities toReturn;
		try {
			con = JdbcUtil.getDbConnection();
			pstmt = con.prepareStatement("SELECT * FROM authorities where EMAIL=?");
			pstmt.setString(1, user.getEmail());
			rs = pstmt.executeQuery();
			ArrayList<String> authorities = new ArrayList<String>();
			while (rs.next()) {
				authorities.add(rs.getString("AUTHORITY"));
			}
			return new UserAuthorities(user.getEmail(), authorities);
		} catch (SQLException e) {
			throw new DaoException(e);
		} finally {
			JdbcUtil.closeAll(con, pstmt, rs);
		}
	}

	public static void deleteAllRecords() throws DaoException {
	   Connection con = null;
	   PreparedStatement pstmt = null;
	   ResultSet rs = null;
      try {
		  con = JdbcUtil.getDbConnection();
		  pstmt = con.prepareStatement("TRUNCATE TABLE access_rights");
		  pstmt.executeUpdate();
      } catch (SQLException e) {
		  throw new DaoException(e);
      } finally {
		  JdbcUtil.closeAll(con, pstmt, rs);
      }
   }
}

@@ -1783,24 +1783,513 @@
 s);%0A%09%09%7D%0A%09%7D%0A%0A
+%09public static void removeUserAuthorities(User user) throws DaoException %7B%0A%09%09Connection con = null;%0A%09%09PreparedStatement pstmt = null;%0A%09%09ResultSet rs = null;%0A%09%09UserAuthorities toReturn;%0A%09%09try %7B%0A%09%09%09con = JdbcUtil.getDbConnection();%0A%09%09%09pstmt = con.prepareStatement(%22DELETE FROM authorities where EMAIL=?%22);%0A%09%09%09pstmt.setString(1, user.getEmail());%0A%09%09%09pstmt.executeUpdate();%0A%09%09%7D catch (SQLException e) %7B%0A%09%09%09throw new DaoException(e);%0A%09%09%7D finally %7B%0A%09%09%09JdbcUtil.closeAll(con, pstmt, rs);%0A%09%09%7D%0A%09%7D%0A%0A
 %09public stat
@@ -2337,16 +2337,16 @@
 ption %7B%0A
-
 %09   Conn
@@ -2529,19 +2529,17 @@
 LE a
-ccess_right
+uthoritie
 s%22);

Update to extract authorities.