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

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.jetspeed.modules.parameters.ListBox;
import org.apache.turbine.util.RunData;

import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALEipGroup;
import com.aimluck.eip.common.ALEipManager;
import com.aimluck.eip.common.ALEipPost;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 部署とMyグループをListBoxで返すクラスです。 <br />
 * 
 */
public class GroupListBox extends ListBox {

  public static final String INITIAL_VALUE = "initialvalue";

  private String DEF_INITIAL_VALUE = "（全体）";

  /**
   * 表示オプションを初期化します。
   * 
   * @param data
   */
  protected void init(RunData data) {

    // 部署の取得
    Map postMap = ALEipManager.getInstance().getPostMap();
    Collection postCollection = postMap.values();
    try {
      // Myグループの取得
      List mygroupList = ALEipUtils.getMyGroups(data);

      int length = postCollection.size() + mygroupList.size() + 1;
      String[] groupKeys = new String[length];
      String[] groupValues = new String[length];

      groupKeys[0] = "";
      groupValues[0] = (String) this.getParm(INITIAL_VALUE, DEF_INITIAL_VALUE);
      int count = 1;

      // 部署の登録
      ALEipPost post = null;
      Iterator iter = postCollection.iterator();
      while (iter.hasNext()) {
        post = (ALEipPost) iter.next();
        groupKeys[count] = post.getGroupName().toString();
        groupValues[count] = post.getPostName().toString();
        count++;
      }

      // Myグループの登録
      ALEipGroup group = null;
      iter = mygroupList.iterator();
      while (iter.hasNext()) {
        group = (ALEipGroup) iter.next();
        groupKeys[count] = group.getName().toString();
        groupValues[count] = group.getAliasName().toString();
        count++;
      }

      this.layout = (String) this.getParm(LAYOUT, LAYOUT_COMBO);
      this.items = groupKeys;
      this.values = groupValues;
      this.size = Integer.toString(length);
      this.multiple = Boolean.valueOf(
          (String) this.getParm(MULTIPLE_CHOICE, "false")).booleanValue();
    } catch (ALDBErrorException e) {
      ALEipUtils.redirectPageNotFound(data);
    }

  }
}
