?? storehouseaction.java
字號:
package com.action;
import org.apache.struts.action.*;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.sql.SQLException;
import com.bean.ClientTable;
import com.bean.LogTable;
import com.dao.*;
import com.tool.pagetool.*;
import com.tool.QueryStrCreator;
import com.tool.LogTableWriter;
import com.bean.Storehouse;
public class StorehouseAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
Storehouse storehouse = (Storehouse) form;
String flag = request.getParameter("flag");
ActionErrors actionErrors = new ActionErrors();
String target = "";
//添加倉庫
if (flag.equals("forA")) {
try {
String whereSQL="where sh_name='"+storehouse.getShName()+"' and sh_addr='"+storehouse.getShAddr()+"' and sh_kind='"+storehouse.getShKind()+"'";
boolean tmp = StorehouseDAO.selectIsHasStorehouse(whereSQL);
if(tmp){
ActionError actionError = new ActionError(
"errors.generally.failed","倉庫名重復");
actionErrors.add("addClientSuccese", actionError);
saveErrors(request, actionErrors);
target = "addStorehouse";
return mapping.findForward(target);
}
StorehouseDAO.startTransation();
int lastId = StorehouseDAO.getLastId();
storehouse.setShId(lastId + 1);
storehouse.setDelFlag(0);
StorehouseDAO.insertStorehouse(storehouse);
StorehouseDAO.commit();
ActionError actionError = new ActionError(
"errors.addSuccese");
actionErrors.add("addClientSuccese", actionError);
saveErrors(request, actionErrors);
target = "addStorehouse";
//寫入日志
String operatorType = "增";
String sql = "insert into STOREHOUSE "+
"(sh_id,sh_name,sh_addr,conn_peop,tel,"+
"fax,ku_rong,sh_kind,sh_rate,count_cost_kind,"+
"count_cost_unit,del_flag) values "+
"("+storehouse.getShId()+","+storehouse.getShName()+","+storehouse.getShAddr()+","+
storehouse.getConnPeop()+","+storehouse.getTel()+","+storehouse.getFax()+","+
storehouse.getKuRong()+","+storehouse.getShKind()+","+storehouse.getShRate()+","
+storehouse.getCountCostKind()+","+storehouse.getCountCostUnit()+","+storehouse.getDelFlag()+")";
LogTableWriter.write(request, operatorType, sql);
} catch (SQLException e) {
e.printStackTrace();
ActionError actionError = new ActionError("errors.addFailed");
actionErrors.add("addClientFeiled", actionError);
saveErrors(request, actionErrors);
target = "addStorehouse";
} finally {
StorehouseDAO.rollback();
}
}
//列出倉庫修改或刪除列表
if (!flag.equals("forA")) {
if (flag.equals("forM")) {
target = "listForMStorehouse"; //重定向到用戶修改例表
initPage(request); //初始化分頁數據并放于request中
} else if (flag.equals("forD")) {
target = "deleteStorehouse"; //重定向到用戶刪除列表
initPage(request); //初始化分頁數據并放于request中
} else if (flag.equals("forDiscount")) {
target = "";
initPage(request); //初始化分頁數據并放于request中
}
}
//跳轉修改倉庫
if (flag.equals("modify")) {
if ((storehouse.getShName() == null || storehouse.getShName().equals(""))&&
storehouse.getShId() != 0) { //跳轉到信息修改頁面
try {
Storehouse storehouse2 = StorehouseDAO.selectStorehouseByshId(
storehouse.getShId());
request.setAttribute("forMStorehouse", storehouse2);
} catch (Exception e) {
e.printStackTrace();
} finally {
CompUserDAO.rollback();
}
target = "modifStorehouse";
}
if (storehouse.getShName() != null &
storehouse.getShId() != 0 &&!storehouse.getShName().equals("")) { //執行修改
String whereSQL="where sh_name='"+storehouse.getShName()+"' and sh_addr='"+storehouse.getShAddr()+"' and sh_kind='"+storehouse.getShKind()+"' and sh_id<>"+storehouse.getShId();
boolean tmp = StorehouseDAO.selectIsHasStorehouse(whereSQL);
if(tmp){
ActionError actionError = new ActionError(
"errors.generally.failed","倉庫名重復");
actionErrors.add("addClientSuccese", actionError);
saveErrors(request, actionErrors);
request.setAttribute("forMStorehouse", storehouse);
target = "modifStorehouse";
return mapping.findForward(target);
}
try {
StorehouseDAO.updateStorehouse(storehouse);
request.setAttribute("forMStorehouse", storehouse);
ActionError actionError = new ActionError(
"errors.modifyUserInfoSuccese");
actionErrors.add("modifyCUserSuccese", actionError);
saveErrors(request, actionErrors);
//寫入日志
String operatorType = "改";
String sql = "update STOREHOUSE set "+
"SH_ID="+storehouse.getShId()+",SH_NAME="+storehouse.getShName()+","+
"SH_ADDR="+storehouse.getShAddr()+",CONN_PEOP="+storehouse.getConnPeop()+","+
"TEL"+storehouse.getTel()+",FAX="+storehouse.getFax()+",KU_RONG="+storehouse.getKuRong()+","+
"SH_KIND="+storehouse.getShKind()+",SH_RATE="+storehouse.getShRate()+","+
"COUNT_COST_KIND="+storehouse.getCountCostKind()+","+
"COUNT_COST_UNIT="+storehouse.getCountCostUnit()+","+
"DEL_FLAG="+storehouse.getDelFlag()+" "+
"where SH_ID = "+storehouse.getShId();
LogTableWriter.write(request, operatorType, sql);
} catch (Exception e) {
e.printStackTrace();
ActionError actionError = new ActionError(
"errors.modifyUserInfoFailed");
actionErrors.add("modifyCUserFeiled", actionError);
saveErrors(request, actionErrors);
} finally {
CompUserDAO.rollback();
}
target = "modifStorehouse";
}
}
//刪除倉庫,只作標記不物理刪除
if (flag.equals("delete")) {
String shIdStr = request.getParameter("shId");
int shId = -1;
target = "deleteStorehouse";
// System.out.println("-------------||"+shIdStr);
if (shIdStr != null) {
try {
StorehouseDAO.startTransation();
shId = Integer.parseInt(shIdStr);
Storehouse storehouse2 = StorehouseDAO.selectStorehouseByshId(
shId);
boolean tmp = false;
tmp=StorehouseDAO.selectStorehouseIsHasRefer(storehouse2);
if (tmp) {
ActionError actionError = new ActionError(
"errors.generally.failed","倉庫已被引用");
actionErrors.add("deleteCUserSuccese", actionError);
saveErrors(request, actionErrors);
StorehouseDAO.commit();
initPage(request); //初始化分頁數據并放于request中
return mapping.findForward(target);
}
storehouse2.setDelFlag(1);
StorehouseDAO.updateStorehouseSI(storehouse2);
ActionError actionError = new ActionError(
"errors.deleteSuccese");
actionErrors.add("deleteCUserSuccese", actionError);
saveErrors(request, actionErrors);
StorehouseDAO.commit();
initPage(request); //初始化分頁數據并放于request中
//寫入日志
String operatorType = "刪";
String sql = "update STOREHOUSE set DEL_FLAG="+storehouse2.getDelFlag()+" where SH_ID = "+storehouse2.getShId();
LogTableWriter.write(request, operatorType, sql);
} catch (Exception e) {
e.printStackTrace();
ActionError actionError = new ActionError(
"errors.deleteFeiled");
actionErrors.add("deleteCUserFeiled", actionError);
saveErrors(request, actionErrors);
} finally {
StorehouseDAO.rollback();
}
}
}
return mapping.findForward(target);
}
//初始化并獲得分頁
public Page initPage(HttpServletRequest request) {
String currentPageStr = request.getParameter("currentPage");
String queryStr = QueryStrCreator.create("storehouse", request);
int currentPage = 1; //當前頁碼
int size = 20; //每頁的記錄數
try { //防止從地址欄直接輸入非法頁碼
if (currentPageStr != null) currentPage = new Integer(
currentPageStr).intValue();
} catch (Exception e) {
e.printStackTrace();
}
//處理分頁
//會話中是否已有分頁生成工具
PageTool pageTool = null;
HttpSession session = request.getSession();
pageTool = (PageTool) session.getAttribute("storehousePageTool"); //有,則直接拿來用
if (pageTool == null) { //沒有,則新建,并放進會話中,以備下次用
pageTool = PageToolBuilder.builder(new StorehouseDAO());
session.setAttribute("storehousePageTool", pageTool);
}
//由分頁工具生成分頁
Page page = null;
try {
String flag = request.getParameter("flag");
if (flag.equals("query")) //查詢時的分頁
page = pageTool.createPage(currentPage, size, queryStr);
else //其它非查詢的分頁
page = pageTool.createPage(currentPage, size);
} catch (SQLException ex) {
ex.printStackTrace();
}
//將分頁放進請求中,分發到下一個頁面顯示
request.setAttribute("allStorehousePage", page);
return page;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -