package wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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.BaseDetailBean;
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.utility.UserInfo;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.ThisPageVO;
import wenrgise.common.webtier.form.BaseForm;
import wenrgise.ejb.common.session.UserSession;
import wenrgise.ejb.common.utility.ParamUtil;

public class DeleteRowAction 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();
    String[] itemChecked = oBaseForm.getItemChecked();
    UserSession oUser = getUserSessionBean(request);
    String sComponentName = getComponentName(sFormName);
    checkAccessInfo(oUser, sComponentName, oBaseForm.getScreenMode(), "", 17, oBaseForm.getWorkListId());
    BaseDetailVO oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    BaseDetailBean oBaseDetailBean = null;
    boolean newRecordFlag = false;
    UserInfo oUserInfo = oUser.getUserInfo();
    SecurityBD oSecBD = new SecurityBD();
    ArrayList oDetailArray = getDetailArray(oBaseForm);
    int count = 0;
    for (int i = 0; i < itemChecked.length; i++) {
      if (itemChecked[i].equals("Y"))
        if (!((BaseDetailBean)oDetailArray.get(i)).getStatus().equals("N")) {
          count++;
          ((BaseDetailBean)oDetailArray.get(i)).setStatus("D");
          oBaseDetailVO.setRowDeleted(oBaseDetailVO.getRowDeleted() + 1);
        } else {
          oDetailArray.remove(i);
          oBaseDetailVO.setRowDeleted(oBaseDetailVO.getRowAdded() - 1);
          newRecordFlag = true;
        }  
    } 
    if (count == 0 && newRecordFlag == false)
      throw new EnrgiseApplicationException("wenrgise.common.norowselected"); 
    if (count > 0)
      oBaseForm.setDetailDataChanged(true); 
    oBaseDetailVO.setOThisPageData(oDetailArray);
    BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oBaseDetailVO.getOThisPageData());
    oUser.putBaseDetailVO(sFormName, sScreenName, oBaseDetailVO);
    BaseHeaderBean oThisBaseHeaderBean = getBaseHeaderBean(sFormName);
    BeanUtils.copyProperties(oThisBaseHeaderBean, form);
    ThisPageVO oThisPageVO = new ThisPageVO();
    oThisPageVO.setOHeaderBean(oThisBaseHeaderBean);
    oThisPageVO.setScreenMode(oBaseForm.getScreenMode());
    oThisPageVO.setScreenName(oBaseForm.getScreenName());
    oBaseDetailVO = null;
    oBaseDetailVO = oUser.getBaseDetailVO(sFormName, oBaseForm.getScreenName());
    if (null != oBaseDetailVO) {
      ArrayList oThisList = (oBaseDetailVO.getOThisPageData() != null) ? oBaseDetailVO.getOThisPageData() : new ArrayList();
      oThisPageVO.setODetailList(oThisList);
    } 
    BaseBD oBaseBD = getDetailBusinessDelegate(form, request);
    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"); 
    } 
    if (oThisPageVO.getODetailList() != null) {
      oBaseForm.setThisPageDetailCount(oThisPageVO.getODetailList().size());
      BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oThisPageVO.getODetailList());
    } 
    onLoad(form, request, 15);
    return mapping.findForward("success");
  }
}
