📄 companyaction.java
字号:
//---------------------------------------------------------
// Application: Company Applcation
// Author : Cao guangxin
// File : CompanyAction.java
//
// Copyright 2006 RelationInfo Software
// Writed at Wed Apr 12 08:58:55 CST 2006
// writed by Eclipse SDK
// Visit http://www.37signals.cn
//---------------------------------------------------------
package net.cn37signals.company.controller;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import net.cn37signals.company.dao.CompanyDAO;
import net.cn37signals.company.model.Company;
import net.cn37signals.company.util.CacheManager;
import net.cn37signals.company.util.Pager;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class CompanyAction extends Action {
private org.apache.commons.logging.Log __log = LogFactory.getFactory()
.getInstance(this.getClass());
private static String DATE_FORMAT = "dd/MM/yyyy"; // date format for
// Indonesian locale
private static int PAGE_LENGTH = 20;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForward myforward = null;
String myaction = mapping.getParameter();
if (isCancelled(request)) {
if (__log.isInfoEnabled()) {
__log.info(" [Company] " + mapping.getAttribute()
+ " - action was cancelled");
}
return mapping.findForward("cancel");
}
if (__log.isInfoEnabled()) {
__log.info(" [Company] action: " + myaction);
}
if ("".equalsIgnoreCase(myaction)) {
myforward = mapping.findForward("failure");
} else if ("VIEW".equalsIgnoreCase(myaction)) {
myforward = performView(mapping, form, request, response);
} else if ("EDIT".equalsIgnoreCase(myaction)) {
myforward = performEdit(mapping, form, request, response);
} else if ("ADD".equalsIgnoreCase(myaction)) {
myforward = performAdd(mapping, form, request, response);
} else if ("SAVE".equalsIgnoreCase(myaction)) {
myforward = performSave(mapping, form, request, response);
} else if ("REMOVE".equalsIgnoreCase(myaction)) {
myforward = performRemove(mapping, form, request, response);
} else if ("TRASH".equalsIgnoreCase(myaction)) {
myforward = performTrash(mapping, form, request, response);
} else if ("LIST".equalsIgnoreCase(myaction)) {
myforward = performList(mapping, form, request, response);
} else {
myforward = mapping.findForward("failure");
}
return myforward;
}
private ActionForward performList(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
try {
DataSource ds = getDataSource(request);
CompanyDAO companyDAO = new CompanyDAO(ds);
int offset;
int length = PAGE_LENGTH;
String pageOffset = request.getParameter("pager.offset");
if (pageOffset == null || pageOffset.equals("")) {
offset = 0;
} else {
offset = Integer.parseInt(pageOffset);
}
List companys = companyDAO.list(offset, length);
String[] objKeys = { "Company", "list" };
String objKey = CacheManager.createKey(objKeys);
Integer size = (Integer) SizeCacheManager.getCache(objKey);
if (size == null) {
size = new Integer(companyDAO.getSize("company", ""));
SizeCacheManager.putCache(size, objKey, 1);
}
String url = request.getContextPath() + "/" + mapping.getPath()
+ ".do";
String pagerHeader = Pager.generate(offset, size.intValue(),
length, url);
request.setAttribute("pagerHeader", pagerHeader);
request.setAttribute("COMPANYS", companys);
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("failure");
}
return mapping.findForward("success");
}
private ActionForward performView(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
CompanyForm form = (CompanyForm) actionForm;
try {
DataSource ds = getDataSource(request);
;
CompanyDAO companyDAO = new CompanyDAO(ds);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
long id = Long.parseLong(request.getParameter("id"));
Company company = companyDAO.retrieve(id);
if (company == null) {
ActionErrors aes = new ActionErrors();
aes.add(aes.GLOBAL_ERROR, new ActionError(
"error.object.notfound", "Company"));
saveErrors(request, aes);
if (__log.isErrorEnabled()) {
__log.error(" [Company] Object not found");
}
} else {
org.apache.commons.beanutils.BeanUtils.populate(form,
org.apache.commons.beanutils.BeanUtils
.describe(company));
form.setFoundDate(sdf.format(company.getFoundDate()));
}
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("failure");
}
return mapping.findForward("success");
}
private ActionForward performSave(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
CompanyForm form = (CompanyForm) actionForm;
try {
DataSource ds = getDataSource(request);
CompanyDAO companyDAO = new CompanyDAO(ds);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Company company = new Company();
org.apache.commons.beanutils.BeanUtils.populate(company,
org.apache.commons.beanutils.BeanUtils.describe(form));
company.setFoundDate(sdf.parse(form.getFoundDate()));
int strutsAction = form.getStrutsAction();
if (strutsAction == CompanyForm.ADD) {
long id = company.getId();
if (companyDAO.retrieve(id) == null) {
companyDAO.insert(company);
} else {
sqlDuplicateError(request, "Company");
return mapping.findForward("failure");
}
} else if (strutsAction == CompanyForm.EDIT) {
companyDAO.update(company);
}
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("failure");
}
return mapping.findForward("success");
}
private ActionForward performEdit(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
CompanyForm form = (CompanyForm) actionForm;
form.setStrutsAction(CompanyForm.EDIT);
try {
DataSource ds = getDataSource(request);
;
CompanyDAO companyDAO = new CompanyDAO(ds);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
long id = Long.parseLong(request.getParameter("id"));
Company company = companyDAO.retrieve(id);
org.apache.commons.beanutils.BeanUtils.populate(form,
org.apache.commons.beanutils.BeanUtils.describe(company));
form.setFoundDate(sdf.format(company.getFoundDate()));
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("failure");
}
return mapping.findForward("success");
}
private ActionForward performAdd(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
CompanyForm form = (CompanyForm) actionForm;
form.setStrutsAction(CompanyForm.ADD);
return mapping.findForward("success");
}
private ActionForward performRemove(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
return performView(mapping, actionForm, request, response);
}
private ActionForward performTrash(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
CompanyForm form = (CompanyForm) actionForm;
try {
DataSource ds = getDataSource(request);
CompanyDAO companyDAO = new CompanyDAO(ds);
long id = Long.parseLong(request.getParameter("id"));
companyDAO.delete(id);
} catch (Exception e) {
generalError(request, e);
return mapping.findForward("failure");
}
return mapping.findForward("success");
}
private void sqlDuplicateError(HttpServletRequest request, String objName) {
ActionErrors aes = new ActionErrors();
aes.add(aes.GLOBAL_ERROR, new ActionError("errors.database.duplicate",
objName));
saveErrors(request, aes);
if (__log.isErrorEnabled()) {
__log.error(" [Company] Duplicate key Error - " + objName);
}
}
private void generalError(HttpServletRequest request, Exception e) {
ActionErrors aes = new ActionErrors();
aes.add(aes.GLOBAL_ERROR, new ActionError("error.general", e
.getMessage()));
saveErrors(request, aes);
e.printStackTrace();
if (__log.isErrorEnabled()) {
__log.error(" [Company] Error - " + e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -