package cz.hsrs.rest.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cz.hsrs.db.pool.SQLExecutor;
import cz.hsrs.rest.beans.RightBean;
import cz.hsrs.rest.beans.UserBean;

public class UserRestUtil {

	/**
	 * 
	 * @param userName
	 * @return
	 * @throws SQLException
	 */
	public static UserBean getUser(String userName) throws SQLException {
		String query = "SELECT * FROM system_users WHERE user_name = '"+userName+"';";
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		UserBean user = null;
		if(res != null) {
			while(res.next()) {
				user = new UserBean(
						res.getInt("user_id"),
						res.getString("user_name"),
						res.getString("user_real_name"),
						res.getInt("group_id"),
						res.getInt("rights_id"));
			}
		}
		return user;
	}
	
	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static List<RightBean> getAllRights() throws SQLException{
		String query = "SELECT * FROM public.rights ORDER BY rights_id;";
		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
		List<RightBean> rightsList = new ArrayList<RightBean>();
		if(res != null) {
			while(res.next()) {
				rightsList.add(new RightBean(res.getInt("rights_id"), res.getString("user_role"), res.getString("note_cz")));
			}
		}
		return rightsList;
	}
}
