/*
 * 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.cashflow.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Comparator;
import java.util.Collections;

import org.apache.cayenne.DataRow;
import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.SQLTemplate;
import org.apache.cayenne.query.SelectQuery;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.resources.JetspeedResources;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.cayenne.om.portlet.EipMFacility;
import com.aimluck.eip.cayenne.om.portlet.ImwMHanyo;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.orm.DatabaseOrmService;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 帳票のユーティリティクラスです。 <BR>
 *
 */
public class CashFlowUtils {

  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(CashFlowUtils.class.getName());

  /** タイムカードファイルを一時保管するディレクトリの指定 */
  public static final String FOLDER_TMP_FOR_CASH_FLOW_FILES = JetspeedResources
      .getString("aipo.tmp.cashflow.directory", "");
  
  /**
   * 一時ファイル保存先のユーザのルートフォルダ
   * 
   * @param org_id
   * @param userId
   * @return
   */
  public static File getCashFlowRootFolder(String org_id, int userId) {
    String rootPath = CashFlowUtils.FOLDER_TMP_FOR_CASH_FLOW_FILES
        + File.separator + org_id + File.separator + userId + File.separator;
    File folder = new File(rootPath);
    if (!folder.exists()) {
      if (!folder.mkdirs()) {
        return null;
      }
    }
    return folder;
  }

  public static Calendar getCompanyStart(RunData rundata, Context context){
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
      .getDataContext();
    
      SelectQuery query = new SelectQuery(ImwMHanyo.class);
    
      Expression exp = ExpressionFactory.matchExp(
          ImwMHanyo.PARENT_HANYO_ID_PROPERTY, 10);
      query.setQualifier(exp);
    
      List list = dataContext.performQuery(query);
      
      ImwMHanyo obj = (ImwMHanyo)list.get(0);
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
      Calendar companyStart = Calendar.getInstance();
      companyStart.setTime(dateFormat.parse(obj.getValue()));
      
      return companyStart;
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return null;
    }
  
  }

}
