package com.yourcompany.struts.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.biz.IGoodsBiz;
import com.biz.ITimeBiz;
import com.vo.Goods;
import com.vo.UserInfo;
import com.yourcompany.struts.form.GoodsForm;
public class GoodsAction extends DispatchAction {
private IGoodsBiz gdBz;
private Goods gdBn;
private ITimeBiz timeBz;
private UserInfo userinfoBn;
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
GoodsForm goodsForm = (GoodsForm) form;
try {
org.apache.commons.beanutils.BeanUtils.copyProperties(gdBn,
goodsForm);
} catch (Exception e) {
e.printStackTrace();
}
int year = Integer.parseInt(request.getParameter("year"));
int month = Integer.parseInt(request.getParameter("month"));
int day = Integer.parseInt(request.getParameter("day"));
if (year < timeBz.getYear() && month < timeBz.getMonth()
&& day < timeBz.getDay()) {
year = timeBz.getYear();
month = timeBz.getMonth();
day = timeBz.getDay();
} else if (month < timeBz.getMonth() && day < timeBz.getDay()) {
month = timeBz.getMonth();
day = timeBz.getDay();
} else if (day < timeBz.getDay()) {
day = timeBz.getDay();
}
Calendar cal = Calendar.getInstance();
cal.set(year, month + 1, day);
HttpSession session = request.getSession();
userinfoBn = (UserInfo) session.getAttribute("userinfo");
gdBn.setGbtime(timeBz.getDate());
gdBn.setGetime(cal.getTime());
gdBn.setUserInfo(userinfoBn);
gdBn.setGstatus("1");
if (gdBz.add(gdBn)) {
request.setAttribute("goods", true);
if(!upload_img(goodsForm)){
request.setAttribute("upload_img", true);
}
} else {
request.setAttribute("goods", false);
}
request.setAttribute("gname", gdBn.getGname());
request.setAttribute("view", "isResult.jsp");
return mapping.findForward("goods");
}
public boolean upload_img(GoodsForm goodsForm) {
String path = this.getServlet().getServletContext().getRealPath("/userImg");
FormFile file = goodsForm.getGimg();
InputStream input;
try {
input = file.getInputStream();
OutputStream out = new FileOutputStream(path + "/"
+ file.getFileName());
byte[] bt = new byte[1024];
while (input.read(bt, 0, bt.length) != -1) {
out.write(bt, 0, bt.length);
}
input.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public ActionForward view(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("time", timeBz.getTime());
request.setAttribute("view", "goods.jsp");
return mapping.findForward("goods");
}
public ActionForward goods_Detail(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int gid = Integer.parseInt(request.getParameter("gid"));
goods_Detail(request, gid);
return mapping.findForward("goods");
}
public void goods_Detail(HttpServletRequest request, int gid) {
gdBn = gdBz.goods_Detail(gid);
request.setAttribute("btime", gdBn.getGbtime().toString().substring(0,
10));
if (gdBn.getGetime() != null) {
String etime = gdBn.getGetime().toString();
request.setAttribute("etime", etime.substring(0, 10));
request.setAttribute("eyear", etime.substring(0, 4));
request.setAttribute("emonth", etime.substring(5, 7));
request.setAttribute("eday", etime.substring(8, 10));
}
request.setAttribute("detail", gdBn);
request.setAttribute("view", "goods_detail.jsp");
}
public ActionForward get_U_Goods(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
userinfoBn = (UserInfo) session.getAttribute("userinfo");
List gdLt = gdBz.getUserGoods(userinfoBn);
if (gdLt.iterator().hasNext()) {
request.setAttribute("gdLt", true);
request.setAttribute("U_goodsList", gdLt);
} else {
request.setAttribute("gdLt", false);
}
request.setAttribute("view", "userGoods.jsp");
return mapping.findForward("goods");
}
public ActionForward delete_U_Goods(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int gid = Integer.parseInt(request.getParameter("gid"));
gdBn.setGid(gid);
gdBz.delete_Goods(gdBn);
HttpSession session = request.getSession();
userinfoBn = (UserInfo) session.getAttribute("userinfo");
request.setAttribute("U_goodsList", gdBz.getUserGoods(userinfoBn));
request.setAttribute("view", "userGoods.jsp");
return mapping.findForward("goods");
}
public ActionForward update_U_Goods(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int gid = Integer.parseInt(request.getParameter("gid"));
gdBn.setGid(gid);
gdBn.setGname(request.getParameter("gname"));
gdBn.setGprice(Double.parseDouble(request.getParameter("gprice")));
int year = Integer.parseInt(request.getParameter("year"));
int month = Integer.parseInt(request.getParameter("month"));
int day = Integer.parseInt(request.getParameter("day"));
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
gdBn.setGetime(cal.getTime());
gdBn.setGdesc(request.getParameter("gdesc"));
gdBz.update_Goods(gdBn);
goods_Detail(request, gid);
return mapping.findForward("goods");
}
public ActionForward else_Goods_Detail(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
int gid = Integer.parseInt(request.getParameter("gid"));
gdBn = gdBz.goods_Detail(gid);
request.setAttribute("btime", gdBn.getGbtime().toString().substring(0,
10));
if (gdBn.getGetime() != null) {
request.setAttribute("etime", gdBn.getGetime().toString()
.substring(0, 10));
} else {
request.setAttribute("etime", "无限时");
}
request.setAttribute("detail", gdBn);
request.setAttribute("view", "else_goods_detail.jsp");
return mapping.findForward("goods");
}
public ActionForward allGoods(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("allList", gdBz.getAllGoods());
request.setAttribute("view", "all_goods.jsp");
return mapping.findForward("goods");
}
public void setGdBz(IGoodsBiz gdBz) {
this.gdBz = gdBz;
}
public void setGdBn(Goods gdBn) {
this.gdBn = gdBn;
}
public void setTimeBz(ITimeBiz timeBz) {
this.timeBz = timeBz;
}
public void setUserinfoBn(UserInfo userinfoBn) {
this.userinfoBn = userinfoBn;
}
}