package com.eforce.baby.auth.action;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.eforce.baby.auth.vo.SessionUserVO;
import com.eforce.baby.common.dao.DAOException;
import com.eforce.baby.common.delegates.BusinessException;
import com.eforce.baby.common.factory.BusinessDelegateFactory;
import com.eforce.baby.auth.delegates.GroupBD;
import com.eforce.baby.auth.vo.GroupVO;
import com.eforce.baby.utils.IConstants;
import com.eforce.baby.utils.IErrorMessageKeys;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.apache.log4j.Logger;
/**
* Implementation of <strong>Action</strong> that validates a multi-page
* registration form.
*/
public final class GroupAction extends DispatchAction {
private Logger log = (Logger) Logger.getInstance(this.getClass().getName());
/**
* Called when a new Group is created
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
* @throws ServletException
*/
public ActionForward createGroup(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
log.debug("Creating group ...");
ActionForward actionFrwd = null;
GroupVO groupVO = new GroupVO();
try {
BeanUtils.copyProperties(groupVO, form);
} catch (IllegalAccessException e) {
log.error("ERROR in createGroup", e);
} catch (InvocationTargetException e) {
log.error("ERROR in createGroup", e);
}
log.debug("Asking for GroupBD");
try {
GroupBD bd =
(GroupBD) BusinessDelegateFactory.getInstance().getDelegate(
"com.eteam.ems.auth.delegates.GroupBD");
log.debug("BD got, calling create group");
HttpSession session = request.getSession();
SessionUserVO sessUser =
(SessionUserVO) session.getAttribute(
IConstants.SESSION_ATTR_USER_SESSION_INFO);
String dsName = sessUser.getDsName();
String dbType = sessUser.getDbType();
bd.createGroup(dsName, dbType, groupVO);
log.debug("Group Created Successfully !!!");
request.setAttribute(
"fwd",
"/admin/Group.do?target=viewGroup&id=" + groupVO.getId());
actionFrwd = mapping.findForward("pop_success");
} catch(DAOException e){
ActionMessage message;
log.debug("Message Key:= "+e.getMessageKey());
log.debug("Error Message := "+IErrorMessageKeys.KEY_ERROR_DUPLICATE_GROUP);
if( e.getMessageKey().equals(IErrorMessageKeys.KEY_ERROR_DUPLICATE_GROUP)){
message = new ActionMessage(IErrorMessageKeys.KEY_ERROR_DUPLICATE_GROUP);
}
else{
message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
}
//ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
} catch(BusinessException e){
ActionMessage message = new ActionMessage(e.getMessageKey());
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
} catch (Exception e1) {
// TODO Auto-generated catch block
// If some business error occured, will return to the input page
ActionMessage message =
new ActionMessage(IErrorMessageKeys.KEY_FATAL_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
return actionFrwd;
}
/**
* Called when the Group Report is viewed
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
* @throws ServletException
*/
public ActionForward viewGroup(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ActionForward actionFrwd = null;
DynaActionForm dynaForm = (DynaActionForm) form;
GroupBD bd =
(GroupBD) BusinessDelegateFactory.getInstance().getDelegate(
"com.eteam.ems.auth.delegates.GroupBD");
// get dsName and dbType from the session - begin
HttpSession session = request.getSession();
SessionUserVO sessUser =
(SessionUserVO) session.getAttribute(
IConstants.SESSION_ATTR_USER_SESSION_INFO);
String dsName = sessUser.getDsName();
String dbType = sessUser.getDbType();
GroupVO groupVO = new GroupVO();
/* Forward to the View Page */
try {
groupVO = bd.findGroup(dsName, dbType, request.getParameter(IConstants.REQ_PARAM_ID));
BeanUtils.copyProperties(form, groupVO);
} catch (IllegalAccessException ex) {
log.error("Error GroupAction:viewGroup()");
} catch (InvocationTargetException ex) {
log.error("Error GroupAction:viewGroup()");
} catch(DAOException e){
ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
} catch(BusinessException e){
ActionMessage message = new ActionMessage(e.getMessageKey());
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
dynaForm.set("reportType", IConstants.REPORT_NAME_GROUP);
actionFrwd = mapping.findForward(request.getParameter("target"));
return actionFrwd;
}
/**
* This method is called when a report is opened in an update mode
*
* @param ActionMapping
* @param ActionForm
* @param HttpServletRequest
* @param HttpServletResponse
*
* @return ActionForward
*
* @throws IOException
* @throws ServletException
*/
public ActionForward viewGroupForUpdate(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ActionForward actionFrwd = null;
DynaActionForm dynaForm = (DynaActionForm) form;
GroupBD groupbd =
(GroupBD) BusinessDelegateFactory.getInstance().getDelegate(
"com.eteam.ems.auth.delegates.GroupBD");
// get dsName and dbType from the session - begin
HttpSession session = request.getSession();
SessionUserVO sessUser =
(SessionUserVO) session.getAttribute(
IConstants.SESSION_ATTR_USER_SESSION_INFO);
String dsName = sessUser.getDsName();
String dbType = sessUser.getDbType();
// get dsName and dbType from the session - end
GroupVO groupVO = new GroupVO();
try {
groupVO =
(GroupVO) groupbd.findGroup(
dsName,
dbType,
request.getParameter(IConstants.REQ_PARAM_ID));
BeanUtils.copyProperties(form, groupVO);
} catch (IllegalAccessException ex) {
log.error("Error GroupAction:viewGroup()");
} catch (InvocationTargetException ex) {
log.error("Error GroupAction:viewGroup()");
} catch(DAOException e){
ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
} catch(BusinessException e){
ActionMessage message = new ActionMessage(e.getMessageKey());
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
request.getSession().setAttribute(
IConstants.SESS_ATTR_UPDATE + groupVO.getId(),
groupVO);
actionFrwd = mapping.findForward(request.getParameter("page"));
return actionFrwd;
}
/**
* This method is called when a report is updated, i.e. when a report is opened in update mode and
* the "Submit" button is pressed
*
* @param mapping
* @param form
* @param request
* @param response
*
* @return ActionForward
*
* @throws IOException
* @throws ServletException
*/
public ActionForward updateGroup(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ActionForward actionFrwd = null;
GroupVO groupVO = new GroupVO();
DynaActionForm dynaForm = (DynaActionForm) form;
try {
// Copying form objects to VO objects
BeanUtils.copyProperties(groupVO, form);
// Added by Arindam
} catch (IllegalAccessException ex) {
log.error("Error GroupAction:viewGroup()");
} catch (InvocationTargetException ex) {
log.error("Error GroupAction:viewGroup()");
}
HttpSession session = request.getSession();
SessionUserVO sessUser =
(SessionUserVO) session.getAttribute(
IConstants.SESSION_ATTR_USER_SESSION_INFO);
String dsName = sessUser.getDsName();
String dbType = sessUser.getDbType();
GroupBD groupbd =
(GroupBD) BusinessDelegateFactory.getInstance().getDelegate(
"com.eteam.ems.auth.delegates.GroupBD");
try {
groupbd.updateGroup(dsName, dbType, groupVO);
log.debug("new report id = " + groupVO.getId());
request.setAttribute(
"fwd",
"/admin/Group.do?target=viewGroup&id=" + groupVO.getId());
actionFrwd = mapping.findForward("pop_success");
} catch(DAOException e){
ActionMessage message;
log.debug("Message Key:= "+e.getMessageKey());
log.debug("Error Message := "+IErrorMessageKeys.KEY_ERROR_DUPLICATE_GROUP);
if( e.getMessageKey().equals(IErrorMessageKeys.KEY_ERROR_DUPLICATE_GROUP)){
message = new ActionMessage(IErrorMessageKeys.KEY_ERROR_DUPLICATE_GROUP);
}
else{
message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
}
//ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
//actionFrwd = new ActionForward(mapping.getInput());
actionFrwd = new ActionForward("/admin/Group.do?target=viewGroupForUpdate&id="+groupVO.getId()+"&mode=update&page=updateMain");
} catch(BusinessException e){
ActionMessage message = new ActionMessage(e.getMessageKey());
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward("/admin/Group.do?target=viewGroupForUpdate&id="+groupVO.getId()+"&mode=update&page=updateMain");
}
return actionFrwd;
}
/**
* This method is called when a report is deleted
* the "Delete" button is pressed
*
* @param mapping
* @param form
* @param request
* @param response
*
* @return ActionForward
*
* @throws IOException
* @throws ServletException
*/
public ActionForward deleteGroup(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ActionForward actionFrwd = null;
try {
GroupBD bd =
(GroupBD) BusinessDelegateFactory.getInstance().getDelegate(
"com.eteam.ems.auth.delegates.GroupBD");
HttpSession session = request.getSession();
SessionUserVO sessUser =
(SessionUserVO) session.getAttribute(
IConstants.SESSION_ATTR_USER_SESSION_INFO);
String dsName = sessUser.getDsName();
String dbType = sessUser.getDbType();
bd.delete(dsName, dbType, request.getParameter("id"));
actionFrwd = mapping.findForward(request.getParameter("target"));
} catch(DAOException e){
ActionMessage message = new ActionMessage(IErrorMessageKeys.KEY_DATABASE_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
} catch(BusinessException e){
ActionMessage message = new ActionMessage(e.getMessageKey());
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
} catch (Exception e1) {
// TODO Auto-generated catch block
// If some business error occured, will return to the input page
ActionMessage message =
new ActionMessage(IErrorMessageKeys.KEY_FATAL_ERROR);
ActionMessages messages = new ActionMessages();
messages.add(IConstants.PAGE_ERROR_MSG_ERROR_MESSAGE, message);
this.saveErrors(request, messages);
actionFrwd = new ActionForward(mapping.getInput());
}
return actionFrwd;
}
}