Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashSet; import java.util.Vector; import model.Directory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import util.DbConstants; /** * This class is used by spring to populate a <code>Directory</code> bean. * This class implements DirectoryListUsersQuery * This class is used by DirectoryAuthorDao to list all users for a directory * with user access scope * **/ /** * @param conn the connection * @param directoryId the directory id * @return HashSet the set that has the list of users for a directory * @throws BaseDaoException * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ class DirectoryListUsersQuery extends BasicDirectQuery { protected final Log logger = LogFactory.getLog(getClass()); /** * This method lists all users for a directory * @param conn the connection * @param directoryId the directory id * @return HashSet the set that has the list of users for this directory. * @throws BaseDaoException * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ /* Uses tables - directory, dirallow, hdlogin */ public HashSet run(Connection conn, String directoryid) throws BaseDaoException { String sqlQuery = "select distinct CONCAT(hd.fname,' ',hd.lname) AS membername, " + "hd.login, hd.loginid, d1.directoryid, d1.dirname from directory d1, " + "dirallow d2, hdlogin hd where d2.loginid=hd.loginid " + "and d1.directoryid=d2.directoryid and d1.directoryid=" + directoryid + ""; try { PreparedStatement stmt = conn.prepareStatement(sqlQuery); ResultSet rs = stmt.executeQuery(); Vector columnNames = null; Directory directory = null; HashSet dirSet = new HashSet(); if (rs != null) { columnNames = dbutils.getColumnNames(rs); } else { return null; } while (rs.next()) { directory = (Directory) eop.newObject(DbConstants.DIRECTORY); for (int j = 0; j < columnNames.size(); j++) { directory.setValue((String) columnNames.elementAt(j), (String) rs.getString((String) columnNames.elementAt(j))); } dirSet.add(directory); } return dirSet; } catch (Exception e) { throw new BaseDaoException("Error occured DirectoryListUsersQuery run query " + sqlQuery, e); } } }