📄 mssqlcategorydao.java
字号:
package com.eline.wap.resource.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.eline.wap.common.jdbc.DBCommonUtils;
import com.eline.wap.common.jdbc.DBSqlManager;
import com.eline.wap.common.model.Page;
import com.eline.wap.common.util.AppLogger;
import com.eline.wap.common.util.SerializeData;
import com.eline.wap.common.util.StringUtils;
import com.eline.wap.resource.exceptions.ResourceDAOSysException;
import com.eline.wap.resource.model.Category;
import com.eline.wap.resource.model.CategoryCondition;
/**
*
* @author Lucifer
*
*/
public class MSSqlCategoryDAO extends DBSqlManager implements CategoryDAO {
static private final int TYPE_ADD = 0;
static private final int TYPE_UPDATE = 1;
public Page getCategories(int parentId, int type, int start, int count) throws ResourceDAOSysException {
// TODO Auto-generated method stub
return null;
}
public Category getCategory(int categoryId) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
Category item = null;
String sql = "SELECT IndexID, Type, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues FROM TAB_CATEGORY "
+ "WHERE IndexId = ?";
System.out.println("getCategory().sql=" + sql);
System.out.println("getCategory().categoryId=" + categoryId);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, categoryId);
rst = stmt.executeQuery();
if (rst.next())
item = populateCategoryFormResultSet(rst);
} catch (Exception e) {
AppLogger.debug("MSSqlCategoryDAO.getCategory().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeResultSet(rst);
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
return item;
}
protected static String[] SEARCH_CATEGORIES_STATEMENT_FRAGMENTS = {
"SELECT IndexID, Type, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues FROM TAB_CATEGORY WHERE Deleted <> 1 ",
"AND ParentID = ? ",
"AND Type = ? ",
"AND SearchKeywords = ? ",
"AND IsSearchable = 1 ",
"AND IsActive = 1 ",
"AND DateCreated >= ? ",
"AND DateCreated < ? "
};
public Page searchCategories(CategoryCondition condition, int start, int count) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
Page page = new Page();
String sql = SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[0];
if (condition != null) {
if (condition.getParentId() != -1)
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[1];
if (condition.getType() != -1)
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[2];
// 注意: SearchKeywords == null的情况下就不需要判断是否ForceToSearch了
if (condition.getSearchKeywords() != null) {
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[3];
if (!condition.isForceToSearch())
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[4];
}
if (condition.isActiveOnly())
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[5];
if (condition.getDateCreated() != null)
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[6];
if (condition.getDateCreated2() != null)
sql += SEARCH_CATEGORIES_STATEMENT_FRAGMENTS[7];
}
sql += "ORDER BY SortOrder";
AppLogger.debug("MSSqlCategoryDAO.searchCategories().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
// Set condition parameters
if (condition != null) {
int index = 1;
if (condition.getParentId() != -1)
stmt.setInt(index++, condition.getParentId());
if (condition.getType() != -1)
stmt.setInt(index++, condition.getType());
if (condition.getSearchKeywords() != null)
stmt.setString(index++, "%" + condition.getSearchKeywords() + "%");
if (condition.getDateCreated() != null)
stmt.setString(index++, StringUtils.toString(condition.getDateCreated()));
if (condition.getDateCreated2() != null)
stmt.setString(index++, StringUtils.toString(condition.getDateCreated2()));
}
rst = stmt.executeQuery();
int totalRecords = 0;
for (totalRecords = 0; rst.next(); totalRecords ++) {
if ((totalRecords >= start) && (totalRecords < (start + count))) {
Category item = populateCategoryFormResultSet(rst);
page.getItems().add(item);
}
}
page.setTotalRecords(totalRecords);
} catch (Exception e) {
AppLogger.debug("MSSqlCategoryDAO.searchCategories().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeResultSet(rst);
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
return page;
}
/**
* 专用于生成xml tree
* 必须严格按照parentId,sortOrder asc排序方式获取数据
* @param type
* @return
* @throws ResourceDAOSysException
*/
public List getCategoriesForXmlTree(int type) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
List list = new ArrayList();
String sql = "SELECT ParentID, IndexID, Type, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues "
+ "FROM TAB_CATEGORY WHERE Type = ? AND Deleted <> 1 AND IsActive = 1 ORDER BY ParentID, SortOrder";
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, type);
rst = stmt.executeQuery();
while (rst.next()) {
Category item = populateCategoryFormResultSet(rst);
list.add(item);
}
} catch (Exception e) {
AppLogger.debug("MSSqlCategoryDAO.getCategoriesForXmlTree().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeResultSet(rst);
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
return list;
}
public void createCategory(Category item) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "INSERT INTO TAB_CATEGORY "
+ "(ParentID, Type, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?)";
AppLogger.debug("MSSqlCategoryDAO.createCategory().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
fillCategoryParameters(stmt, item, TYPE_ADD);
int rowCount = stmt.executeUpdate();
if (rowCount != 1)
throw new ResourceDAOSysException("effected row count must be 1");
} catch (Exception e) {
AppLogger.debug("MSSqlCategoryDAO.createCategory().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
}
public void deleteCategory(int categoryId) throws ResourceDAOSysException {
// TODO Auto-generated method stub
}
public void updateCategory(Category item) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "UPDATE TAB_CATEGORY SET SortOrder = ?, LastUpdate = ?, IsActive = ?, IsSearchable = ?, SearchKeywords = ?, PropertyNames = ?, PropertyValues = ? WHERE IndexID = ?";
AppLogger.debug("MSSqlCategoryDAO.createCategory().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
fillCategoryParameters(stmt, item, TYPE_UPDATE);
int rowCount = stmt.executeUpdate();
if (rowCount != 1)
throw new ResourceDAOSysException("effected row count must be 1");
} catch (Exception e) {
AppLogger.debug("MSSqlCategoryDAO.updateCategory().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
}
protected Category populateCategoryFormResultSet(ResultSet rst) throws SQLException {
Category item = new Category();
item.setType(rst.getInt("Type"));
// Fill section properties.
DBCommonUtils.populateSectionFormResultSet(rst, item);
return item;
}
protected void fillCategoryParameters(PreparedStatement stmt, Category item, int type) throws SQLException {
int index = 1;
try {
if (type == TYPE_ADD) {
stmt.setInt(index++, item.getParentId());
stmt.setInt(index++, item.getType());
stmt.setInt(index++, item.getSortOrder());
stmt.setString(index++, StringUtils.toString(item.getDateCreated()));
stmt.setString(index++, StringUtils.toString(item.getLastUpdate()));
stmt.setBoolean(index++, item.isActive());
stmt.setBoolean(index++, item.isSearchable());
stmt.setString(index++, item.getSearchKey());
// Serialize datas
SerializeData data = item.deserialize();
stmt.setString(index++, data.getKeys());
stmt.setString(index++, data.getValues());
} else if (type == TYPE_UPDATE) {
stmt.setInt(index++, item.getSortOrder());
stmt.setString(index++, StringUtils.toString(item.getLastUpdate()));
stmt.setBoolean(index++, item.isActive());
stmt.setBoolean(index++, item.isSearchable());
stmt.setString(index++, item.getSearchKey());
// Serialize datas
SerializeData data = item.deserialize();
stmt.setString(index++, data.getKeys());
stmt.setString(index++, data.getValues());
// indexId
stmt.setInt(index++, item.getIndexId());
}
} catch (SQLException e) {
throw e;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -