/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2008 Aimluck,Inc.
 * http://aipostyle.com/
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aimluck.eip.modules.screens;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.cashflow.AccountPlanFormData;
import com.aimluck.eip.cashflow.AccountPlanMultiDelete;
import com.aimluck.eip.cashflow.AccountPlanResultData;
import com.aimluck.eip.cayenne.om.portlet.ImwMItem;
import com.aimluck.eip.cayenne.om.portlet.ImwMItemSubcategory;

/**
 * 入出金予定をJSONデータとして出力するクラスです。 <br />
 * 
 */
public class AccountPlanFormJSONScreen extends ALJSONScreen {
  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(AccountPlanFormJSONScreen.class.getName());

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

    try {

      if (ALEipConstants.MODE_INSERT.equals(mode)) {
        //
        AccountPlanFormData formData = new AccountPlanFormData();
        formData.initField();
        formData.loadComboList(rundata, context);
        //formData.loadItemList(rundata, context);
        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)) {

        AccountPlanFormData formData = new AccountPlanFormData();
        formData.initField();
        formData.loadComboList(rundata, context);
        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)) {

        AccountPlanFormData formData = new AccountPlanFormData();
        formData.initField();
        formData.loadComboList(rundata, context);
        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 ("multi_delete".equals(mode)) {

        AccountPlanMultiDelete delete = new AccountPlanMultiDelete();
        if (delete.doMultiAction(this, rundata, context)) {
        } else {
          ArrayList list = (ArrayList) context
              .get(ALEipConstants.ERROR_MESSAGE_LIST);
          JSONArray json = JSONArray.fromObject(list);
          result = json.toString();
        }
      } else if ("catChanged".equals(mode)) {
        Integer value = Integer.valueOf(
          rundata.getParameters().getStrings("value")[0]);
        AccountPlanFormData formData = new AccountPlanFormData();
        Map map = new HashMap();

        formData.initField();
        formData.loadComboList(rundata, context);
        formData.loadCategoryList(rundata, context);
//        formData.loadSubCategoryList(rundata, context);
//        formData.loadItemList(rundata, context);
        List subCats = formData.getSubCategoryList(value);
        StringBuffer strbuf = new StringBuffer();
        for(Object obj : subCats){
          ImwMItemSubcategory subcat = (ImwMItemSubcategory)obj;
          strbuf.append(subcat.getItemSubcategoryId()).append(",")
            .append(subcat.getItemSubcategoryName()).append(",");
        }
        map.put("subcats", strbuf.toString());

        List items = formData.getItemList(value, -1);
        strbuf.setLength(0);
        for(Object obj : items){
          ImwMItem item = (ImwMItem)obj;
          strbuf.append(item.getItemId()).append(",")
            .append(item.getItemName()).append(",");
        }
        map.put("items", strbuf.toString());

        JSONArray json = JSONArray.fromObject(map);
        result = json.toString();
      } else if ("subcatChanged".equals(mode)) {
        Integer value = Integer.valueOf(
          rundata.getParameters().getStrings("value")[0]);
        Integer catId = Integer.valueOf(
            rundata.getParameters().getStrings("value")[1]);
        
        AccountPlanFormData formData = new AccountPlanFormData();
        Map map = new HashMap();

        formData.initField();
        formData.loadComboList(rundata, context);
        formData.loadCategoryList(rundata, context);

        List items = formData.getItemList(catId, value);
        StringBuffer strbuf = new StringBuffer();
        for(Object obj : items){
          ImwMItem item = (ImwMItem)obj;
          strbuf.append(item.getItemId()).append(",")
            .append(item.getItemName()).append(",");
        }
        map.put("items", strbuf.toString());

        JSONArray json = JSONArray.fromObject(map);
        result = json.toString();
      } else if ("clientChanged".equals(mode)) {
        Integer value = Integer.valueOf(
          rundata.getParameters().getStrings("value")[0]);
        
        AccountPlanFormData formData = new AccountPlanFormData();
        Map map = new HashMap();

        formData.initField();
        formData.setType(inout);
        formData.loadComboList(rundata, context);

        AccountPlanResultData client = formData.getClientInfo(value);
        map.put("clientPayClassId", client.getClientPayClassId());
        map.put("clientPayClassName", client.getClientPayClassName());
        JSONArray json = JSONArray.fromObject(map);
        result = json.toString();
      }
    } catch (Exception e) {
      logger.error("[AccountPlanFormJSONScreen]", e);
    }

    return result;
  }
}

