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

📄 ringaction.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.SerializeData;
import com.eline.wap.common.util.StringUtils;
import com.eline.wap.resource.client.RingHelper;
import com.eline.wap.resource.exceptions.ResourceException;
import com.eline.wap.resource.model.Ring;

public class RingAction extends Action{
	
	private static final int ACTION_CREATE	= 0;
	private static final int ACTION_UPDATE	= 1;
	private static final int ACTION_DELETE	= 2;

	/**
	 * @param 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("RingAction.execute()");

		boolean isOK = false;
		int webAction = StringUtils.getInt(request.getParameter("webAction"), -1);
		System.out.println(webAction);
		if (form instanceof RingForm) {
			RingForm actionForm = (RingForm) 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(RingForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
		try {
			Ring item = new Ring();
			item.setParentId(form.getParentId());
			item.setName(form.getName());
			item.setSearchable(form.isSearchable());
			item.setActive(form.isActive());
			item.setDescription(form.getDescription());
			item.setAuthor(form.getAuthor());
			item.setLyric(form.getLyric());

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

			// 设置搜索关键字
			if (form.isSearchable())
				item.setSearchKey(form.getName() + ";" + form.getAuthor() + ";");
			else
				item.setSearchKey("UNDEFINED");

			// 写入数据库
			SerializeData data = item.deserialize();
			AppLogger.debug("SerializeData.getKeys()=" + data.getKeys());
			AppLogger.debug("SerializeData.getValues()=" + data.getValues());

			RingHelper rh = new RingHelper();
			rh.createRing(item);

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

	/**
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private boolean doUpdate(RingForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		RingHelper helper = new RingHelper();
		try {
			System.out.println("form.getIndexId()=" + form.getIndexId());
			Ring item = helper.getRing(form.getIndexId());

			item.setName(form.getName());
			item.setSearchable(form.isSearchable());
			item.setActive(form.isActive());
			item.setDescription(form.getDescription());
			item.setAuthor(form.getAuthor());
			item.setLyric(form.getLyric());

			if (form.isSearchable())
				item.setSearchKey(form.getName() + ";" + form.getAuthor() + ";");
			else
				item.setSearchKey("UNDEFINED");
						
			// 设置时间
			Date currentTime = new Date();
			item.setLastUpdate(currentTime);
			
			// 写入数据库
			
			helper.updateRing(item);

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

	/**
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private boolean doDelete(RingForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String[] strIdList = form.getDeletes().split(";");
		RingHelper rh = new RingHelper();
		boolean returnValue = true;

		// parse id(s) and delete them.
		for (int i = 0; i < strIdList.length; i ++) {
			try {
				if (strIdList[i] == null || strIdList[i].length() < 1)
					continue;

				int itemId = Integer.parseInt(strIdList[i]);
				rh.deleteItem(itemId);
			} catch (NumberFormatException e) {
				AppLogger.error("NumberFormatException: RingAction.doDelete() - parse id error, contiune.");
				continue;
			} catch (ResourceException e) {
				AppLogger.error("ResourceException: RingAction.doDelete() - " + e.getMessage());
				returnValue = false;
			}
		}
		return returnValue;
	}
}

⌨️ 快捷键说明

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