/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import com.ORM.*;
import com.base.*;
import com.service.*;
import com.struts.form.MerForm;
/**
* MyEclipse Struts
* Creation date: 09-13-2007
*
* XDoclet definition:
* @struts.action path="/Admin/adminMer" name="merForm" input="/Admin/adminAddMer.jsp" parameter="method" scope="request" validate="true"
*/
public class AdminMerAction extends BaseAction {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward addMer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MerForm merForm = (MerForm) form;
Merchandise mer = new Merchandise();
MerService service = new MerServiceImpl();
ActionMessages msgs = new ActionMessages();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
mer.setMerName(merForm.getMerName().trim());
mer.setMerModel(merForm.getMerModel().trim());
mer.setPrice(merForm.getPrice());
mer.setSpecial(merForm.getSpecial());
mer.setSprice(merForm.getSprice());
mer.setMerDesc(merForm.getMerDesc().trim());
mer.setManufacturer(merForm.getManufacturer().trim());
/** 存放文件的文件夹 */
String dir = request.getRealPath("/Picture");
/** 文件名 */
String filename = null;
/** 上传的文件 */
FormFile file = merForm.getPicture();
/** 输入流对象(用于读取上传的文件数据) */
InputStream in = null;
/** 输出流对象(用于将读取到的文件数据输出到服务端的另一个文件中) */
OutputStream out = null;
try{
mer.setLeaveFactoryDate(df.parse(merForm.getLeaveFactoryDate()));
mer.setCategory(service.loadCategory(new Integer(merForm.getCategory())));
filename = file.getFileName();
if (file==null||filename.length()<1){
logger.info("您没有上传文件!");
}else{
in = file.getInputStream();
out = new FileOutputStream(dir+"/"+filename);
int readed = 0;
byte[] buffer= new byte[1024];
while ((readed=in.read(buffer,0,1024))!=-1){
out.write(buffer,0,readed);
}
logger.info("文件"+filename+"已经上传到了服务器上的"+dir+"下");
mer.setPicture("/Picture/"+filename);
}
boolean status = service.addMer(mer);
if (status){
msgs.add("addMerStatus",new ActionMessage(Constants.ADDMER_SUC_KEY));
}else{
msgs.add("addMerStatus",new ActionMessage(Constants.ADDMER_FAIL_KEY));
}
saveErrors(request, msgs);
}catch(Exception ex){
logger.info("在执行AdminMerAction类中的addMer方法时出错:\n");
ex.printStackTrace();
}finally{
try{
if (in!=null)in.close();
if (out!=null)out.close();
}catch(Exception ex){
logger.info("在执行AdminMerAction类中的addMer方法时出错:\n");
ex.printStackTrace();
}
}
return mapping.findForward("addMer");
}
public ActionForward browseMer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List list = null;
MerService service = new MerServiceImpl();
Admin admin = (Admin)request.getSession().getAttribute("Admin");
if (admin==null||admin.getAdminType().intValue()!=1){
return mapping.findForward("sorry");
}
String hql ="from Merchandise as a order by a.id";
try{
list = service.browseMer(hql);
request.setAttribute("merList", list);
}catch(Exception ex){
logger.info("在执行AdminMerAction类中的browseMer方法时出错:\n");
ex.printStackTrace();
}
return mapping.findForward("browse");
}
public ActionForward delMer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionMessages msgs = new ActionMessages();
MerService service = new MerServiceImpl();
String p = request.getParameter("id");
Integer id = null;
if(p!=null){
id = new Integer(p);
}else{
id = new Integer(0);
}
try{
boolean status = service.delMer(id);
if (status){
msgs.add("delMerStatus",new ActionMessage(Constants.DELMER_SUC_KEY));
}else{
msgs.add("delMerStatus",new ActionMessage(Constants.DELMER_FAIL_KEY));
}
saveErrors(request, msgs);
}catch(Exception ex){
logger.info("在执行AdminMerAction类中的delMer方法时出错:\n");
ex.printStackTrace();
}
return mapping.findForward("delete");
}
public ActionForward loadMer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MerService service = new MerServiceImpl();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Merchandise mer = null;
String p = request.getParameter("id");
Integer id = null;
if(p!=null){
id = new Integer(p);
}else{
id = new Integer(0);
}
try{
mer = service.loadMer(id);
if(mer!=null){
form = new MerForm();
((MerForm)form).setMerName(mer.getMerName().trim());
((MerForm)form).setMerModel(mer.getMerModel().trim());
((MerForm)form).setMerDesc(mer.getMerDesc().trim());
((MerForm)form).setPrice(mer.getPrice());
((MerForm)form).setSpecial(mer.getSpecial());
((MerForm)form).setManufacturer(mer.getManufacturer().trim());
((MerForm)form).setLeaveFactoryDate(df.format(mer.getLeaveFactoryDate()));
((MerForm)form).setSprice(mer.getSprice());
request.setAttribute("merForm", form);
request.setAttribute("id", mer.getId());
request.setAttribute("cateId", mer.getCategory().getId());
}
}catch(Exception ex){
logger.info("在执行AdminMerAction类中的loadMer方法时出错:\n");
ex.printStackTrace();
}
return mapping.findForward("modify");
}
public ActionForward modiMer(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionMessages msgs = new ActionMessages();
MerForm merForm = (MerForm) form;
Merchandise mer = null;
MerService service = new MerServiceImpl();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String p = request.getParameter("id");
Integer id = null;
if(p!=null){
id = new Integer(p);
}else{
id = new Integer(0);
}
/** 存放文件的文件夹 */
String dir = request.getRealPath("/Picture");
/** 文件名 */
String filename = null;
/** 上传的文件 */
FormFile file = merForm.getPicture();
/** 输入流对象(用于读取上传的文件数据) */
InputStream in = null;
/** 输出流对象(用于将读取到的文件数据输出到服务端的另一个文件中) */
OutputStream out = null;
try{
mer = service.loadMer(id);
if (mer!=null){
mer.setId(id);
mer.setMerName(merForm.getMerName().trim());
mer.setMerModel(merForm.getMerModel().trim());
mer.setPrice(merForm.getPrice());
mer.setSpecial(merForm.getSpecial());
mer.setSprice(merForm.getSprice());
mer.setMerDesc(merForm.getMerDesc().trim());
mer.setManufacturer(merForm.getManufacturer().trim());
mer.setLeaveFactoryDate(df.parse(merForm.getLeaveFactoryDate()));
mer.setCategory(service.loadCategory(new Integer(merForm.getCategory())));
filename = file.getFileName();
if (file==null||filename.length()<1){
logger.info("您没有上传文件!");
}else{
in = file.getInputStream();
out = new FileOutputStream(dir+"/"+filename);
int readed = 0;
byte[] buffer= new byte[1024];
while ((readed=in.read(buffer,0,1024))!=-1){
out.write(buffer,0,readed);
}
logger.info("文件"+filename+"已经上传到了服务器上的"+dir+"下");