/*
 * 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 org.apache.jetspeed.portal.portlets.VelocityPortlet;
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.ALAbstractSelectData;
import com.aimluck.eip.officecalendar.OfficecalendarMonthlySelectData;
import com.aimluck.eip.officecalendar.util.OfficecalendarUtils;
import com.aimluck.eip.util.ALEipUtils;

/**
 * スケジュールの一覧を処理するクラスです。 <br />
 * 
 */
public class OfficecalendarScreen extends ALVelocityScreen {
	/** logger */
	private static final JetspeedLogger logger = JetspeedLogFactoryService
			.getLogger(OfficecalendarScreen.class.getName());

	/**
	 * @see org.apache.turbine.modules.screens.RawScreen#doOutput(org.apache.turbine.util.RunData)
	 */
	@Override
	protected void doOutput(RunData rundata, Context context) throws Exception {

		VelocityPortlet portlet = ALEipUtils.getPortlet(rundata, context);
		String tab = rundata.getParameters().getString("tab");
		try {
			// 自ポートレットからのリクエストであれば、パラメータを展開しセッションに保存する。
			if (ALEipUtils.isMatch(rundata, context)) {
				// 現在選択されているタブ
				// oneday : １日表示
				// weekly : 週間表示
				// monthly: 月間表示
				if (rundata.getParameters().containsKey("tab")) {
					ALEipUtils.setTemp(rundata, context, "tab", rundata
							.getParameters().getString("tab"));
				}
			}

			ALAbstractSelectData listData = null;
			// ポートレット ID を取得する．
			String portletId = portlet.getID();
			String currentTab;
			String tmpCurrentTab = ALEipUtils.getTemp(rundata, context, "tab");
			if (tmpCurrentTab == null
					|| !(tmpCurrentTab.equals("oneday")
							|| tmpCurrentTab.equals("weekly")
							|| tmpCurrentTab.equals("monthly")
							|| tmpCurrentTab.equals("oneday-group") || tmpCurrentTab
							.equals("weekly-group"))) {
				currentTab = "oneday";
			} else {
				currentTab = tmpCurrentTab;
			}

			int tab_count = 0;
			// Velocity テンプレートを読み込む
			String template = "";
			String _template = portlet.getPortletConfig().getInitParameter(
					"pba-template");
			boolean done = false;

			// アクセスコントロール
			String has_acl_self = OfficecalendarUtils.hasAuthSelf(rundata);
			String has_acl_other = OfficecalendarUtils.hasAuthOther(rundata);
			String tab_flg_monthly = ALEipUtils.getPortlet(rundata, context)
					.getPortletConfig().getInitParameter("p8a-tab");
			if ("0".equals(tab_flg_monthly) && ("T".equals(has_acl_self))) {
				tab_count++;
				if (("".equals(template)) || (!done)) {
					template = "officecalendar-monthly";
					if (template.equals(_template)) {
						done = true;
					}
				}
			}
			tab = "monthly";
			if ("T".equals(has_acl_self)) {
				if (!"0".equals(tab_flg_monthly)) {
					tab_flg_monthly = "0";
					tab_count++;
				}
			}
			listData = new OfficecalendarMonthlySelectData();
			((OfficecalendarMonthlySelectData) listData)
					.setPortletId(portletId);

			if ("T".equals(has_acl_self)) {
				context.put("tab-monthly", tab_flg_monthly);
			}
			if ("T".equals(has_acl_other)) {
			}

			context.put("widthALL", Integer.toString(tab_count * 120 + 40)
					+ "px");

			listData.initField();
			listData.doViewList(this, rundata, context);
			String layout_template = "portlets/html/ja/ajax-officecalendar.vm";

			setTemplate(rundata, context, layout_template);

		} catch (Exception ex) {
			logger.error("[ToDoScreen] Exception.", ex);
			ALEipUtils.redirectDBError(rundata);
		}
	}

}
