package cn.jbit.easybuy.biz.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.dao.CommentDao;
import cn.jbit.easybuy.dao.ProductCategoryDao;
import cn.jbit.easybuy.dao.ProductDao;
import cn.jbit.easybuy.dao.impl.CommentDaoImpl;
import cn.jbit.easybuy.dao.impl.ProductCategoryDaoImpl;
import cn.jbit.easybuy.dao.impl.ProductDaoImpl;
import cn.jbit.easybuy.entity.Pager;
import cn.jbit.easybuy.entity.Product;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.util.DataSourceUtil;
import cn.jbit.easybuy.util.Validator;
public class ProductServiceImpl implements ProductService {
public void delete(String id) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
productDao.delete(Long.parseLong(id));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public void deleteCategory(String id) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
productCategoryDao.delete(Long.parseLong(id));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public Product findById(String id) {
Connection connection = null;
Product product = null;
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
product = productDao.findById(Long.parseLong(id));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return product;
}
public ProductCategory findCategoryById(String id) {
Connection connection = null;
ProductCategory productCategory = null;
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
productCategory = productCategoryDao.findById(Long.parseLong(id));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return productCategory;
}
public List<ProductCategory> getProductCategories(String parentId) {
Connection connection = null;
Long id = null;
if (!Validator.isEmpty(parentId))
id = Long.parseLong(parentId);
List<ProductCategory> rtn = new ArrayList<ProductCategory>();
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
rtn = productCategoryDao.getProductCategories(id);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return rtn;
}
public List<Product> getProductsByCategory(String categoryId, Pager pager) {
Connection connection = null;
List<Product> rtn = new ArrayList<Product>();
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
if (categoryId == null)
rtn = productDao.getProductsByCategoryLevelTwo(null, pager);
else
rtn = productDao.getProductsByCategoryLevelTwo(Long
.parseLong(categoryId), pager);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return rtn;
}
public void save(Product product) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
productDao.save(product);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public void saveCategory(ProductCategory productCategory) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
productCategoryDao.save(productCategory);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public void update(Product product) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
productDao.update(product);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public void updateCategory(ProductCategory productCategory, String type) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
ProductDao productDao = new ProductDaoImpl(connection);
String sql = null;
if (type.equals("ModifyParent")) {
sql = " UPDATE easybuy_product SET epc_id="
+ productCategory.getParentId()
+ " where epc_child_id=" + productCategory.getId();
} else {
sql = " UPDATE easybuy_product SET epc_child_id = null,epc_id="
+ productCategory.getId() + " where epc_child_id="
+ productCategory.getId();
}
connection.setAutoCommit(false);
try {
productCategoryDao.update(productCategory);
productDao.modifyCategoryOfProductBySql(sql);
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
connection.rollback();
connection.setAutoCommit(true);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public List<ProductCategory> getRootCategories() {
Connection connection = null;
List<ProductCategory> rtn = new ArrayList<ProductCategory>();
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
rtn = productCategoryDao.getRootCategories();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return rtn;
}
public void updateCategoryName(ProductCategory productCategory) {
Connection connection = null;
try {
connection = DataSourceUtil.openConnection();
ProductCategoryDao productCategoryDao = new ProductCategoryDaoImpl(
connection);
productCategoryDao.update(productCategory);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
}
public long getProductRowCount(String categoryId) {
Connection connection = null;
long rtn = 0;
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
Long id = null;
if (categoryId != null)
id = Long.parseLong(categoryId);
rtn = productDao.getProductRowCountByCategoryLevelTwo(id);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return rtn;
}
public Long getNewProductId() {
Connection connection = null;
long rtn = 0;
try {
connection = DataSourceUtil.openConnection();
ProductDao productDao = new ProductDaoImpl(connection);
rtn = productDao.getNewProductId();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DataSourceUtil.closeConnection(connection);
}
return rtn;
}
}
- 1
- 2
- 3
前往页