/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao_impl;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.AbstractDocument.Content;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import dao.PriceDAO;
import dao.ProductDAO;
import pojo.Bill;
import pojo.MyInteger;
import pojo.Price;
import pojo.Product;
import pojo.User;
import util.HibernateUtil;
import web.IndexController;
/**
*
* @author phuc
*/
public class ProductDAOImpl implements ProductDAO {
private static final Logger logger = Logger.getLogger(ProductDAOImpl.class);
//get All product from cdatabase
public List<Product> getListProduct() {
logger.debug("getListProduct start");
List<Product> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Product";
Query query = session.createQuery(hql);
ds = query.list();
logger.debug("getListProduct success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getListProduct error : " + ex);
} finally {
session.close();
}
logger.debug("getListProduct end");
return ds;
}
//get list product with id and number product in page
public List<Product> getListProduct(String idDanhMuc,
int soLuongProductTrenTrang, int trang) {
logger.debug("getListProduct start");
List<Product> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "";
Query query = null;
if (idDanhMuc.equals("")) {
hql = "FROM Product";
query = session.createQuery(hql);
} else {
hql = "FROM Product p WHERE p.type=:idDanhMuc";
query = session.createQuery(hql);
query.setString("idDanhMuc", idDanhMuc);
}
if (soLuongProductTrenTrang != -1) {
query.setFirstResult((trang - 1) * soLuongProductTrenTrang);
query.setMaxResults(soLuongProductTrenTrang);
}
ds = query.list();
logger.debug("getListProduct success");
} catch (HibernateException ex) {
logger.error("getListProduct error : " + ex);
System.err.println(ex);
} finally {
session.close();
}
logger.debug("getListProduct end");
return ds;
}
//get infor of a product
public Product getInfoProduct(String idProduct) {
logger.debug("getinfoProduct start");
Product sp = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
sp = (Product) session.get(Product.class,
Integer.parseInt(idProduct));
logger.debug("getinfoProduct success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getinfoProduct error : " + ex);
} finally {
session.close();
}
logger.debug("get infoProduct end");
return sp;
}
//add a product to database
public boolean addProduct(Product sp) {
logger.debug("addproduct start");
Session session = HibernateUtil.getSessionFactory().openSession();
ProductDAO spDAO = new ProductDAOImpl();
if (spDAO.getInfoProduct(sp.getIdproduct().toString()) != null) {
return false;
}
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(sp);
transaction.commit();
logger.debug("addProduct success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("addProduct error : " + ex);
} finally {
session.close();
}
logger.debug("addProduct end");
return true;
}
//del a product from database
public boolean delProduct(int idProduct) {
logger.debug("delProduct start");
Session session = HibernateUtil.getSessionFactory().openSession();
Product sp = (Product) session.get(Product.class, idProduct);
session.close();
if (sp == null) {
return false;
}
Transaction transaction = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
Query q = session
.createQuery("delete from Product p where p.idproduct='1'");
// q.setString("idList", idList);
q.executeUpdate();
transaction.commit();
logger.debug("delProduct success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("delProduct error : " + ex);
} finally {
session.close();
}
logger.debug("delProduct end");
return true;
}
//find list product by conditions
public List<Product> findListProduct(String id, boolean isSearch,
List<String[]> conditions, int soLuongProductTrenTrang, int trang,
MyInteger soTrang) {
logger.debug("findListProduct start");
List<Product> Products = null;
PriceDAO priceDAO = new PriceDAOImpl();
List<Price> listPrice = priceDAO.getListPrice();
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "FROM Product sp ";
Query query = null;
boolean bWhere = false;
if (!id.equals("")) {
if (isSearch == false) {
hql += " where sp.idproduct = '" + id + "' ";
} else {
hql += " where sp.name like '%" + id + "%' ";
}
bWhere = true;
}
for (int i = 0; i < conditions.size(); i++) {
String content = "";
if (!conditions.get(i)[0].equals("")) {
if (bWhere == false) {
content += " where (";
bWhere = true;
} else {
content += " and ( ";
}
if (i == 0 || i == 2 || i == 4) {
for (int j = 0; j < conditions.get(i).length; j++) {
content += " sp.replace ='" + conditions.get(i)[j]
+ "' ";
if (j < conditions.get(i).length - 1)
content += " or ";
}
} else if (i == 1) {
for (int j = 0; j < conditions.get(i).length; j++) {
content += " ( sp.price >= '"
+ listPrice
.get(Integer.parseInt(conditions
.get(i)[j]) - 1).getMin()
+ "' and sp.price < '"
+ listPrice
.get(Integer.parseInt(conditions
.get(i)[j]) - 1).getMax()
+ "' ) ";
if (j < conditions.get(i).length - 1)
content += " or ";
}
} else if (i == 3) {
for (int j = 0; j < conditions.get(i).length; j++) {
content += " sp.rating >= '" + conditions.get(i)[j]
+ "'";
if (j < conditions.get(i).length - 1)
content += " or ";
}
}
content += " ) ";
switch (i) {
case 0:
content = content.replace("replace", "provider");
break;
case 2:
content = content.replace("replace", "condition");
break;
case 4:
content = content.replace("replace", "color");
break;
}
hql += content;
}
}
query = session.createQuery(hql);
int size = query.list().size();
int nPage = 1;
if (soLuongProductTrenTrang != -1) {
nPage = size / soLuongProductTrenTrang;
if (nPage == 0 || (size % soLuongProductTrenTrang != 0))
nPage++;
query.setFirstResult((trang - 1) * soLuongProductTrenTrang);
query.setMaxResults(soLuongProductTrenTrang);
}
soTrang.setValue(nPage);
Products = query.list();
logger.debug("findListProduct success");
} catch (HibernateException ex) {
logger.debug("findListProduct error : " + ex);
System.err.println(ex);
} finally {
session.close();
}
return Products;
}
//get list best seller product
public List<Product> getBestSellerListProduct(int soluong) {
logger.debug("getBestSeller start");
List<Product> Products = new ArrayList<Product>();
Session session = HibernateUtil.getSessionFactory().openSession();
try {
ProductDAO spDAO = new ProductDAOImpl();
List<Product> sps = spDAO.getListProduct();
String hql = "";
Query query = null;
int[] listFrequence = new int[sps.size()];
for (int i = 0; i < sps.size(); i++) {
hql = " SELECT ls.count FROM Billdetail ls WHERE ls.product=:idProduct";
query = session.createQuery(hql);
query.setString("idProduct", sps.get(i).getIdproduct()
.toString());
List<Integer> l = query.list();
int n = 0;
for (int j = 0; j < l.size(); j++) {
n += l.get(j);
}
listFrequence[i] = n;
}
int[] idx = new int[sps.size()];
for (int i = 0; i < idx.length; i++) {
idx[i] = i;
}
for (int i = 0; i < listFrequence.length - 1; i++) {
for (int j = i + 1; j < listFrequence.length; j++) {
if (listFrequence[i] > listFrequence[j]) {
int temp1 = listFrequence[i];
listFrequence[i] = listFrequence[j];
listFrequence[j] = temp1;
int temp2 = idx[i];
idx[i] = idx[j];
idx[j] = temp2;
}
}
}
for (int i = idx.length - 1; i >= idx.length - soluong; i--) {
Products.add(sps.get(idx[i]));
}
logger.debug("getBestSeller success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getBestSeller error : " + ex);
} finally {
session.close();
}
logger.debug("getBestSeller end");
return Products;
}
//get list product newest
public List<Product> getNewListProduct(int soluong) {
List<Product> listProduct = null;
logger.debug("getNewProduct start");
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "FROM Product ORDER BY RAND()";
Query query = session.createQuery(hql);
query.setMaxResults(soluong);
listProduct = query.list().subList(0, soluong);
logger.debug("getNewProduct success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getNewProduct error : "+ ex);
} finally {
session.close();
}
logger.debug("get new product start");
return listProduct;
}
//change status active of a product
public void changeActive(int id, String active) {
logger.debug("changeActive start");
Session session = HibernateUtil.getSessionFactory().openSession();
Product product = (Product) session.get(Product.class, id);
session.close();
if (product == null) {
return;
}
if(product.getActivated() == 1) {
active = "0";
}
else
active = "1";
java.sql.Connection con = null;
java.sql.Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/appliances";
String user = "root";
String password = "";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("UPDATE product SET Activated='" +active +"' where IDProduct = " + id+"");
logger.debug("changeActive success");
} catch (SQLException ex) {
System.out.println(ex.toString());
logger.error("changeActive error");
}
finally {
}
logger.debug("changeActive end");
}
}