⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 categoryaction.java

📁 一个免费wap站
💻 JAVA
字号:
package com.eline.wap.resource.struts;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eline.wap.common.util.AppLogger;
import com.eline.wap.common.util.StringUtils;
import com.eline.wap.resource.client.CategoryHelper;
import com.eline.wap.resource.model.Category;

/**
 * 
 * @author Lucifer
 *
 */
public class CategoryAction extends Action {

	private static final int ACTION_CREATE	= 0;
	private static final int ACTION_UPDATE	= 1;
	private static final int ACTION_DELETE	= 2;

	/**
	 * 
	 * @parem mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("hello, CategoryAction.execute()");

		boolean isOK = false;
		int webAction = StringUtils.getInt(request.getParameter("webAction"), -1);

		if (form instanceof CategoryForm) {
			CategoryForm actionForm = (CategoryForm) form;
			
			if (webAction == ACTION_CREATE) {
				isOK = doCreate(actionForm, request, response);
			} else if (webAction == ACTION_UPDATE) {
				isOK = doUpdate(actionForm, request, response);
			} else if (webAction == ACTION_DELETE) {
				isOK = doDelete(actionForm, request, response);
			}
		}
		String strMsg = isOK ? "操作成功" : "操作失败";
		request.setAttribute("message", strMsg);

		return mapping.findForward(isOK ? "success" : "failure");
	}

	/**
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private boolean doCreate(CategoryForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try {
			Category item = new Category();

			item.setParentId(form.getParentId());
			item.setType(form.getType());
			item.setName(form.getName());
			item.setSearchable(form.isSearchable());
			item.setCategoryAttribute(form.isTopLevel() ? Category.ATTRIBUTE_ITEM : Category.ATTRIBUTE_DIR);
			item.setActive(form.isActive());
			item.setDescription(form.getDescription());

			// 设置时间
			Date currentTime = new Date();
			item.setDateCreated(currentTime);
			item.setLastUpdate(currentTime);

			// 设置搜索关键字
			if (form.isSearchable()) {
				item.setSearchKey(form.getSearchKey());
				if (item.getSearchKey().length() > 0)
					 item.setSearchKey(item.getSearchKey() + ";");
				if (item.getSearchKey().indexOf(form.getName()) < 0)
					item.setSearchKey(form.getName() + ";" + item.getSearchKey());
			}
			else
				item.setSearchKey("UNDEFINED");
			
			// 写入数据库
			CategoryHelper ch = new CategoryHelper();
			ch.createCategory(item);
			
		} catch (Exception e) {
			AppLogger.error("CategoryAction.doCreate() Exception: " + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private boolean doUpdate(CategoryForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			CategoryHelper ch = new CategoryHelper();

			Category item = ch.getCategory(form.getIndexId());

			item.setIndexId(form.getIndexId());
			//item.setParentId(form.getParentId());
			//item.setType(form.getType());
			item.setName(form.getName());
			item.setSearchable(form.isSearchable());
			// item.setCategoryAttribute(form.isTopLevel() ? Category.ATTRIBUTE_ITEM : Category.ATTRIBUTE_DIR);
			item.setActive(form.isActive());
			item.setDescription(form.getDescription());
			
			// 设置时间
			Date currentTime = new Date();
			item.setDateCreated(currentTime);
			item.setLastUpdate(currentTime);

			// 设置搜索关键字
			if (form.isSearchable()) {
				item.setSearchKey(form.getSearchKey());
				if (item.getSearchKey().length() > 0)
					 item.setSearchKey(item.getSearchKey() + ";");
				if (item.getSearchKey().indexOf(form.getName()) < 0)
					item.setSearchKey(form.getName() + ";" + item.getSearchKey());
			}
			else
				item.setSearchKey("UNDEFINED");
			
			// 写入数据库
			ch.updateCategory(item);

		} catch (Exception e) {
			e.printStackTrace();
			AppLogger.error("CategoryAction.doUpdate() Exception: " + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private boolean doDelete(CategoryForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int categoryId = form.getIndexId();
		
		try {
			CategoryHelper ch = new CategoryHelper();
			ch.deleteCategory(categoryId);
		} catch (Exception e) {
			AppLogger.error("CategoryAction.doDelete() Exception: " + e.getMessage());
			return false;
		}
		return true;
	}
}

⌨️ 快捷键说明

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