/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.rubis.admin.dao.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import org.xulfaces.rubis.admin.dao.ItemDAO;
import org.xulfaces.rubis.admin.dao.UserDAO;
import org.xulfaces.rubis.admin.dao.UserNotFoundException;
import org.xulfaces.rubis.model.Category;
import org.xulfaces.rubis.model.Item;
import org.xulfaces.rubis.model.User;
public class JDBCItemDAO extends JDBCAbstractDAO implements ItemDAO {
public JDBCItemDAO() throws Exception {
super();
}
public Collection<Item> getItemsForSeller(User user) {
Collection<Item> items = new ArrayList<Item>();
StringBuffer sqlBuffer = new StringBuffer();
sqlBuffer.append("SELECT * FROM items WHERE SELLER = ");
sqlBuffer.append(user.getId());
ResultSet resultSet = null;
try {
resultSet = excecuteScrollableQuery(sqlBuffer.toString());
while(resultSet.next()){
Item item = new Item();
item.setSeller(user);
items.add(item);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
closeConnection(resultSet);
}
return items;
}
public Collection<Item> getItemsForCategory(Category category) {
UserDAO userDAO = null;
try {
userDAO = new JDBCUserDAO();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Collection<Item> items = new ArrayList<Item>();
StringBuffer sqlBuffer = new StringBuffer();
sqlBuffer.append("SELECT * FROM items WHERE CATEGORY=");
sqlBuffer.append(category.getId());
ResultSet resultSet = null;
try {
resultSet = excecuteScrollableQuery(sqlBuffer.toString());
while (resultSet.next()) {
Item item = new Item();
item.setId(new Integer(resultSet.getInt("ID")));
item.setName(resultSet.getString("NAME"));
try {
item.setSeller(userDAO.loadUser(new Integer(resultSet.getInt("SELLER"))));
} catch (UserNotFoundException e) {
e.printStackTrace();
}
item.setCategory(category);
item.setBuyNow(resultSet.getFloat("BUY_NOW"));
item.setDescription(resultSet.getString("DESCRIPTION"));
item.setEndDate(resultSet.getDate("END_DATE"));
item.setStartDate(resultSet.getDate("START_DATE"));
items.add(item);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
closeConnection(resultSet);
}
return items;
}
public void deleteItem(Integer id) {
StringBuffer sqlBuffer = new StringBuffer();
try {
sqlBuffer.append("DELETE FROM items WHERE ID=");
sqlBuffer.append(id);
excecuteUpdateQuery(sqlBuffer.toString());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}