/**
 * 新規
 * 2011/1/25 
 * 孫奇全
 * 
 */

package com.aimluck.eip.modules.screens;

import java.util.ArrayList;

import net.sf.json.JSONArray;

import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.exttimecard.ExtTimecardSystemDetailFormData;
import com.aimluck.eip.exttimecard.ExtTimecardSystemDetailMultiDelete;

/**
 * タイムカード集計をJSONデータとして出力するクラスです。 <br />
 *
 */
public class ExtTimecardSystemDetailFormJSONScreen extends ALJSONScreen {
  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(ExtTimecardSystemDetailFormJSONScreen.class.getName());

  @Override
  protected String getJSONString(RunData rundata, Context context)
      throws Exception {
    String result = new JSONArray().toString();
    String mode = this.getMode();

    try {
      if (ALEipConstants.MODE_INSERT.equals(mode)) {
        ExtTimecardSystemDetailFormData formData = new ExtTimecardSystemDetailFormData();
        formData.initField();
        if (formData.doInsert(this, rundata, context)) {
        } else {
          ArrayList list = (ArrayList) context
              .get(ALEipConstants.ERROR_MESSAGE_LIST);
          JSONArray json = JSONArray.fromObject(list);
          result = json.toString();
        }
      } else if (ALEipConstants.MODE_UPDATE.equals(mode)) {
    	  ExtTimecardSystemDetailFormData formData = new ExtTimecardSystemDetailFormData();
        formData.initField();
        if (formData.doUpdate(this, rundata, context)) {
        } else {
          ArrayList list = (ArrayList) context
              .get(ALEipConstants.ERROR_MESSAGE_LIST);
          JSONArray json = JSONArray.fromObject(list);
          result = json.toString();
        }
      } else if (ALEipConstants.MODE_DELETE.equals(mode)) {
    	  ExtTimecardSystemDetailFormData formData = new ExtTimecardSystemDetailFormData();
        formData.initField();
        if (formData.doDelete(this, rundata, context)) {
        } else {
          ArrayList list = (ArrayList) context
              .get(ALEipConstants.ERROR_MESSAGE_LIST);
          JSONArray json = JSONArray.fromObject(list);
          result = json.toString();
        }
      } else if (ALEipConstants.MODE_MULTI_DELETE.equals(mode)) {
        ExtTimecardSystemDetailMultiDelete delete = new ExtTimecardSystemDetailMultiDelete();
        if (delete.doMultiAction(this, rundata, context)) {
        } else {
          ArrayList list = (ArrayList) context
              .get(ALEipConstants.ERROR_MESSAGE_LIST);
          JSONArray json = JSONArray.fromObject(list);
          result = json.toString();
        }
      }
    } catch (Exception e) {
      logger.error("[ExtTimecardSystemDetailFormJSONScreen]", e);
    }

    return result;
  }
}
