/** * Project : LIS<br> * File name : PatientTypeUpdateAction.java<br> * Description : * @author TienDH * @date Aug 20, 2007 */ package us.mn.state.health.lims.patienttype.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.Globals; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessages; import us.mn.state.health.lims.common.action.BaseAction; import us.mn.state.health.lims.common.action.BaseActionForm; import us.mn.state.health.lims.common.exception.LIMSDuplicateRecordException; import us.mn.state.health.lims.common.exception.LIMSRuntimeException; import us.mn.state.health.lims.common.util.StringUtil; import us.mn.state.health.lims.common.util.resources.ResourceLocator; import us.mn.state.health.lims.common.util.validator.ActionError; import us.mn.state.health.lims.hibernate.HibernateUtil; import us.mn.state.health.lims.login.valueholder.UserSessionData; import us.mn.state.health.lims.patienttype.dao.PatientTypeDAO; import us.mn.state.health.lims.patienttype.daoimpl.PatientTypeDAOImpl; import us.mn.state.health.lims.patienttype.valueholder.PatientType; public class PatientTypeUpdateAction extends BaseAction { private boolean isNew = false; protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String forward = FWD_SUCCESS; request.setAttribute(ALLOW_EDITS_KEY, TRUE); request.setAttribute(PREVIOUS_DISABLED, FALSE); request.setAttribute(NEXT_DISABLED, FALSE); String id = request.getParameter(ID); isNew = (StringUtil.isNullorNill(id) || "0".equals(id)); BaseActionForm dynaForm = (BaseActionForm) form; // server-side validation (validation.xml) ActionMessages errors = dynaForm.validate(mapping, request); if (errors != null && errors.size() > 0) { saveErrors(request, errors); // since we forward to jsp - not Action we don't need to repopulate // the lists here return mapping.findForward(FWD_FAIL); } String start = (String) request.getParameter("startingRecNo"); String direction = (String) request.getParameter("direction"); PatientType patientType = new PatientType(); patientType.setSysUserId(getSysUserId(request)); PropertyUtils.copyProperties(patientType, dynaForm); org.hibernate.Transaction tx = HibernateUtil.getSession().beginTransaction(); try { PatientTypeDAO patientTypeDAO = new PatientTypeDAOImpl(); if (isNew) { patientTypeDAO.insertData(patientType); } else { patientTypeDAO.updateData(patientType); } tx.commit(); } catch (LIMSRuntimeException lre) { tx.rollback(); errors = new ActionMessages(); java.util.Locale locale = (java.util.Locale) request.getSession() .getAttribute("org.apache.struts.action.LOCALE"); ActionError error = null; if (lre.getException() instanceof org.hibernate.StaleObjectStateException) { // how can I get popup instead of struts error at the top of // page? // ActionMessages errors = dynaForm.validate(mapping, request); error = new ActionError("errors.OptimisticLockException", null, null); } else { if (lre.getException() instanceof LIMSDuplicateRecordException) { String messageKey = "patienttype.description"; String msg = ResourceLocator.getInstance() .getMessageResources().getMessage(locale, messageKey); error = new ActionError("errors.DuplicateRecord", msg, null); } else { error = new ActionError("errors.UpdateException", null, null); } } errors.add(ActionMessages.GLOBAL_MESSAGE, error); saveErrors(request, errors); request.setAttribute(Globals.ERROR_KEY, errors); // request.setAttribute(IActionConstants.ALLOW_EDITS_KEY, FALSE); // disable previous and next request.setAttribute(PREVIOUS_DISABLED, TRUE); request.setAttribute(NEXT_DISABLED, TRUE); forward = FWD_FAIL; } finally { HibernateUtil.closeSession(); } if (forward.equals(FWD_FAIL)) return mapping.findForward(forward); // initialize the form dynaForm.initialize(mapping); // repopulate the form from valueholder PropertyUtils.copyProperties(dynaForm, patientType); if (TRUE.equalsIgnoreCase(request.getParameter("close"))) { forward = FWD_CLOSE; } if (patientType.getId() != null && !patientType.getId().equals("0")) { request.setAttribute(ID, patientType.getId()); } if (isNew) forward = FWD_SUCCESS_INSERT; return getForward(mapping.findForward(forward), id, start, direction); } protected String getPageTitleKey() { return isNew ? "patienttype.add.title" : "patienttype.edit.title"; } protected String getPageSubtitleKey() { return isNew ? "patienttype.add.title" : "patienttype.edit.title"; } }