package WEB-INF.classes.wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.businessdelegate.BaseBD;
import wenrgise.common.businessdelegate.SecurityBD;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.common.vo.BaseQueryVO;
import wenrgise.common.vo.ThisPageVO;
import wenrgise.common.webtier.action.BaseAction;
import wenrgise.common.webtier.form.BaseForm;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.utility.ParamUtil;

public class DeleteAction extends BaseAction {
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    BaseForm oBaseForm = (BaseForm)form;
    String sFormName = form.getClass().getName();
    String sScreenName = oBaseForm.getScreenName();
    BaseBD oBaseBD = getHeaderBusinessDelegate(form, request);
    UserSession oUser = getUserSessionBean(request);
    String sComponentName = getComponentName(sFormName);
    checkAccessInfo(oUser, sComponentName, oBaseForm.getScreenMode(), "", 17, oBaseForm.getWorkListId());
    BaseHeaderBean oBaseHeaderBean = getBaseHeaderBean(sFormName);
    ArrayList oDetailBeanArray = null;
    BaseDetailVO oBaseDetailVO = null;
    BaseHeaderVO oBaseHeaderVO = null;
    Timestamp oHeaderPicked = null;
    Timestamp oDetailPicked = null;
    oBaseHeaderVO = oUser.getBaseHeaderVO(sFormName);
    if (oBaseHeaderVO != null) {
      oHeaderPicked = oBaseHeaderVO.getOWhenPicked();
      oBaseHeaderBean = getBaseHeaderBean(sFormName);
      if (oBaseHeaderBean == null)
        oBaseHeaderBean = getBaseHeaderBean(sFormName); 
    } 
    BeanUtils.copyProperties(oBaseHeaderBean, form);
    oBaseForm.setHeaderDataChanged(true);
    String sHeaderPrimaryKey = oBaseBD.saveRecord(oBaseHeaderBean, oHeaderPicked, oBaseForm.getScreenName(), "D", oBaseForm.isHeaderDataChanged(), oDetailBeanArray, oBaseForm.isDetailDataChanged(), oDetailPicked);
    BaseQueryVO oBaseQueryVO = null;
    if (oBaseForm.getPositionRequested() > 0L && sHeaderPrimaryKey == null) {
      if (oBaseForm.getTotalHeaderRecord() == 1L) {
        oBaseForm.setScreenMode("Q");
        changeMode(oBaseForm, request);
      } else {
        oBaseHeaderVO = oBaseBD.getNextHeaderRecord(oBaseQueryVO, oBaseForm.getPositionRequested(), sFormName, true, oUser);
        int iRelativePosition = (int)(oBaseHeaderVO.getPositionRequested() - oBaseHeaderVO.getHeaderStartPosition() + 1L);
        BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(iRelativePosition));
        BeanUtils.copyProperties(form, oBaseHeaderVO);
        oBaseForm.setTotalHeaderRecord(((int)oBaseForm.getTotalHeaderRecord() - 1));
        oBaseHeaderVO.setTotalCount(oBaseHeaderVO.getTotalCount() - 1L);
        oBaseForm.setHeaderDataChanged(false);
        oBaseForm.setDetailDataChanged(false);
        oBaseForm.setScreenMode("U");
      } 
    } else {
      oBaseQueryVO = getQueryVO(form);
      oBaseQueryVO.setHeaderPrimaryKey(sHeaderPrimaryKey);
      oBaseQueryVO.setMaxHeaderSize(ParamUtil.getHeaderSize(sFormName));
      oBaseHeaderVO = oBaseBD.getHeaderRecord(oBaseQueryVO, sFormName, oUser);
      BeanUtils.copyProperties(form, oBaseHeaderVO.getHeaderRecord(1));
      BeanUtils.copyProperties(form, oBaseHeaderVO);
      oBaseForm.setScreenMode("U");
    } 
    enableAll(oBaseForm);
    enableDisable(oBaseForm, saveClicked(), "D");
    controlHeaderNavigation(oBaseForm);
    SecurityBD oSecBD = new SecurityBD();
    Map oSecMap = oSecBD.getDisabledFields(String.valueOf(oBaseForm.getScreenName()).concat(String.valueOf(".jsp")), "ALL", oBaseForm.getScreenMode(), oBaseForm.getHeaderStatus());
    if (null != oSecMap)
      enableDisable(oBaseForm, (ArrayList)oSecMap.get("D"), "D"); 
    BaseHeaderBean oThisBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oThisBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setOHeaderBean(oThisBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    oThisPageVO.setActionName(17);
    oBaseDetailVO = null;
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, oBaseForm.getScreenName());
    if (null != oBaseDetailVO) {
      ArrayList oDetailList = (oBaseDetailVO.getOThisPageData() != null) ? oBaseDetailVO.getOThisPageData() : new ArrayList();
      oThisPageVO.setODetailList(oDetailList);
    } 
    Map oMap = oBaseBD.getDisabledFields(oThisPageVO);
    if (oMap != null) {
      if (oMap.containsKey("D"))
        enableDisable(oBaseForm, (ArrayList)oMap.get("D"), "D"); 
      if (oMap.containsKey("E"))
        enableDisable(oBaseForm, (ArrayList)oMap.get("E"), "E"); 
    } 
    onLoad(form, request, 17);
    return mapping.findForward("success");
  }
}
