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

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
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.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.vo.BaseDetailVO;
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 DecrAction 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();
    UserSession oUser = getUserSessionBean(request);
    BaseDetailVO oBaseDetailVO = oUser.getBaseDetailVO(sFormName, sScreenName);
    int count = 0;
    ArrayList oDetailArray = getDetailArray(oBaseForm);
    try {
      String[] itemChecked = oBaseForm.getItemChecked();
      String[] sStatuss = oBaseForm.getStatus();
      String[] sStartFields = BeanUtils.getArrayProperty(oBaseForm, "txtFromAmount");
      System.out.println(itemChecked.length);
      for (int i = 0; i < itemChecked.length; i++) {
        if (itemChecked[i].equals("Y")) {
          count++;
          String sStartField = sStartFields[i];
          for (int j = i + 1; j < itemChecked.length; j++) {
            String sStatus = sStatuss[j];
            if (!sStatus.equals("D")) {
              sStartFields[j] = sStartField;
              BaseDetailBean oBaseDetailBean = oDetailArray.get(j);
              oBaseDetailBean.setStatus("U");
              BeanUtils.setProperty(oBaseDetailBean, "txtFromAmount", sStartField);
              oDetailArray.set(j, oBaseDetailBean);
              break;
            } 
          } 
          ((BaseDetailBean)oDetailArray.get(i)).setStatus("D");
          ((BaseDetailBean)oDetailArray.get(i)).setItemChecked("N");
          oBaseDetailVO.setRowDeleted(oBaseDetailVO.getRowDeleted() + 1);
        } 
      } 
    } catch (NoSuchMethodException noSuchMethodException) {}
    if (count == 0)
      throw new EnrgiseApplicationException("wenrgise.common.norowselected"); 
    if (count > 0)
      oBaseForm.setDetailDataChanged(true); 
    BeanUtils.setProperty(oBaseForm, ParamUtil.getDetailArrayName(sFormName, sScreenName), oDetailArray);
    return mapping.findForward("success");
  }
}
