assetlenddispatchaction.java

来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 311 行

JAVA
311
字号
package com.qrsx.appcam.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.qrsx.appcam.dao.AssetDAO;
import com.qrsx.appcam.dao.AssetLendDAO;
import com.qrsx.appcam.dao.ClientDAO;
import com.qrsx.appcam.form.AssetLendForm;
import com.qrsx.appcam.model.Asset;
import com.qrsx.appcam.model.AssetLend;
import com.qrsx.appcam.model.Client;

public class AssetLendDispatchAction extends BaseDispatchAction {
	/**
	 * 借出检索:检索所有资产用于借出
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward lendList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		AssetLend assetLend = new AssetLend();

		// 把form表单中的数据复制到asset对象中去
		BeanUtils.copyProperties(assetLend, form);

		// 把厂家信息存放在单独的集合中,并通过request返回到页面
		ClientDAO dao1 = new ClientDAO();
		List clients = dao1.findAll();
		request.setAttribute("clients", clients);

		Asset asset = new Asset();
		AssetDAO dao = new AssetDAO();
		// 执行检索操作,并且把检索结果返回list页面
		List<Asset> list = dao.list(asset);
		request.setAttribute("assets", list);

		return mapping.findForward("lendList");
	}

	/**
	 * 查看:进入查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward lendCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		String id = request.getParameter("id");

		if (id != null && id.trim().length() > 0) {

			AssetDAO dao = new AssetDAO();
			// 获取要查看的对象
			Asset asset = dao.findById(Integer.valueOf(id));
			// 如果该对象不为空,则把该对象组装到对象的form中
			if (asset != null) {
				BeanUtils.copyProperties(form, asset);
			}
		}
		return mapping.findForward("lendCheck");
	}

	/**
	 * 进行借出编辑
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward lendEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		String assetId = request.getParameter("id");
		if (assetId != null && assetId.trim().length() > 0) {

			AssetDAO dao = new AssetDAO();
			Asset asset = dao.findById(Integer.valueOf(assetId));
			// 如果该资产已经借出,则不能在借出
			if (asset.getState() != 2) {

				saveMessage(request, "assetlendForm.lendSave.lendFailure");
				return mapping.findForward("lendFailure");
			}
			String assetName = asset.getName();
			String assetCode = asset.getCode();
			AssetLendForm assetLendForm = new AssetLendForm();
			// 把资产的Id name code 设置到对应的AssetLendForm中,组装到表单中去
			assetLendForm.setAssetId(assetId);
			assetLendForm.setAssetName(assetName);
			assetLendForm.setCode(assetCode);
			BeanUtils.copyProperties(form, assetLendForm);
		}
		ClientDAO clientDao = new ClientDAO();
		List<Client> list = clientDao.findAll();
		request.setAttribute("clients", list);

		return mapping.findForward("lendEdit");
	}

	/**
	 * 保存:保存借出信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward lendSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		// 进行有效性验证,如果出现错误则再转发到编辑页面进行编辑
		ActionErrors errors = form.validate(mapping, request);

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			return lendEdit(mapping, form, request, response);
		}
		// 获取借出输入表单的信息
		AssetLendForm assetLendForm = (AssetLendForm) form;
		AssetLend assetLend = new AssetLend();
		// 把表单信息保存到对象中去
		BeanUtils.copyProperties(assetLend, assetLendForm);
		AssetDAO assetDao = new AssetDAO();

		Integer assetId = assetLend.getAssetId();// 获取当前借出资产的ID
		// 更新资产列表,改变其中的状态为借出使用状态
		assetDao.lendUpdate(assetId);
		// 把该资产加入到借出表中,形成借出明细
		AssetLendDAO assetLendDao = new AssetLendDAO();
		assetLendDao.create(assetLend);
		saveMessage(request, "assetlendForm.lendSave.lendsuccess");
		return mapping.findForward("lendSuccess");
	}

	/**
	 * 归还编辑:进入归还编辑页面,输入归还时间
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward givebackEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		// 获取借出ID
		String id = request.getParameter("id");
		// 如果Id不为空则查询该资产的情况
		if (id != null && id.trim().length() > 0) {
			AssetLendDAO dao = new AssetLendDAO();
			// 检索出来该Id对应的assetLend对象
			AssetLend assetLend = dao.findById(Integer.valueOf(id));

			// 如果该资产已经归还则在返回归还列表界面 2表示已经归还
			if (assetLend.getState() != 1) {
				saveMessage(request,
						"assetGivebackForm.givebackSave.giveBackFailure");
				return mapping.findForward("giveBackFailure");
			}

			AssetLendForm assetLendForm = new AssetLendForm();
			if (assetLend != null) {
				// 把assetLend复制到assetLendForm对应的表单form对象中
				BeanUtils.copyProperties(assetLendForm, assetLend);
				// 设置该借出资产的代码和名称
				assetLendForm.setAssetName(assetLend.getAsset().getName());
				assetLendForm.setCode(assetLend.getAsset().getCode());
				// 把最终的assetLendForm复制到form中,返回编辑界面
				BeanUtils.copyProperties(form, assetLendForm);
			}
		}

		return mapping.findForward("givebackEdit");
	}

	/**
	 * 归还保存:保存归还信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward givebackSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆

		// 进行有效性验证,如果出现错误则再转发到编辑页面进行编辑
		ActionErrors errors = form.validate(mapping, request);
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			return givebackEdit(mapping, form, request, response);
		}

		AssetLendForm assetLendForm = (AssetLendForm) form;
		if (assetLendForm.getGivebackDate() == null
				|| assetLendForm.getGivebackDate().trim().length() == 0) {
			
			saveMessage(request, "assetGivebackForm.givebackDate.required");
			return givebackEdit(mapping, form, request, response);
		}
		AssetLend assetLend = new AssetLend();
		// 将表单数据保存到对象中
		BeanUtils.copyProperties(assetLend, assetLendForm);		
		Integer assetId = assetLend.getAssetId();// 获取资产编号
		AssetDAO dao1 = new AssetDAO();
		dao1.givebackUpdate(assetId);// 更新资产状态
		AssetLendDAO dao2 = new AssetLendDAO();
		dao2.giveBackUpdate(assetLend);// 更新资产借出状态
		saveMessage(request, "assetGivebackForm.givebackSave.success");
		return mapping.findForward("givebackSuccess");
	}

	/**
	 * 资产归还页面:检索所有已经借出的资产信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward givebackList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		AssetLendForm assetLendForm = (AssetLendForm) form;
		AssetLend assetLend = new AssetLend();
		// 在表单中获取资产的代码和名称
		Asset asset = new Asset();
		asset.setCode(assetLendForm.getCode());
		asset.setName(assetLendForm.getAssetName());
		// 把表单信息转存到对象中
		BeanUtils.copyProperties(assetLend, assetLendForm);
		assetLend.setAsset(asset);// 把asset组装到assetLend对象中去,行成查询条件
		AssetLendDAO dao = new AssetLendDAO();
		// 进行数据检索
		List<AssetLend> list = dao.list(assetLend);
		// 把查询结果返回页面
		request.setAttribute("assetLends", list);

		return mapping.findForward("givebackList");
	}

	/**
	 * 删除操作
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BaseDispatchAction.isLogon(request, response);//验证用户是否已经登陆
		String id = request.getParameter("id");// 获取要删除借出明细的编号
		// 如果id存在,则执行删除操作
		if (id != null || id.trim().length() > 0) {

			AssetLendDAO dao = new AssetLendDAO();
			AssetLend assetLend = dao.findById(Integer.valueOf(id));
			// 如果该资产没有归还,则不能删除,2表示已经归还
			if (assetLend.getState() != 2) {
				saveMessage(request, "assetGivebackForm.givebackDelete.Failure");
				return mapping.findForward("giveBackFailure");
			}
			dao.delete(assetLend.getId());// 执行删除操作
		}
		saveMessage(request, "assetGivebackForm.deletekSave.success");
		return mapping.findForward("givebackSuccess");
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?