/*
 * $Id: DisplayGroupInfoForm.java,v 1.4 2005/09/15 23:16:43 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: DisplayGroupInfoForm.java,v $
 * Revision 1.4  2005/09/15 23:16:43  marbobo
 * + Sprachabhängiges Sortieren implementiert
 *
 * Revision 1.3  2005/09/15 13:43:58  marbobo
 * + Sprachabhängige Sortierung implementiert
 *
 * Revision 1.2  2004/07/20 08:53:51  jonyp
 * added copyright info + cvs log
 *
 */
package vkc.view.form;

import java.text.CollationKey;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCGroup;
import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.util.VKCStringUtils;
import vkc.view.bean.UserInfo;

public final class DisplayGroupInfoForm extends BasicActionForm {
	private Integer groupId = null;
	
	private String  creationDate = null;
	private String  lastEditingDate = null;

	private UserInfo creator = null;
	private UserInfo lastEditor = null;
	
	private VKCGroup group = null;

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);

		groupId = null;
		
		creationDate = null;
		lastEditingDate = null;

		creator = null;
		lastEditor = null;
		
		group = null;
	}
	
	public String getCreationDate() {
		return creationDate;
	}

	public UserInfo getCreator() {
		return creator;
	}

	public VKCGroup getGroup() {
		return group;
	}

	public Integer getGroupId() {
		return groupId;
	}

	public List getGroupUsers() throws DBException {
		List groupUsers = new ArrayList();
		Integer[] userIds = (Integer[])VKCGroupSerializer.getGroupUserIds(getGroupId()).toArray(new Integer[0]);
		
		if (userIds.length > 0) {
			List users = VKCUserSerializer.retrieveUserInfos();
			
			for (int i = 0; i < userIds.length; i++) {
				for (int j = 0; i < users.size(); j++) {
					UserInfo u = (UserInfo)users.get(j);
					
					if (userIds[i].intValue() == u.getUserId().intValue()) {
						groupUsers.add(u);
						break;
					}
				}
			}

			final Collator collator = Collator.getInstance(getUser().getLanguage().getLocale());

			Comparator c = new Comparator() {
				public int compare(Object o1, Object o2) {
					UserInfo u1 = (UserInfo)o1;
					UserInfo u2 = (UserInfo)o2;
					
                	CollationKey key1 = collator.getCollationKey(VKCStringUtils.decodeXmlEntities(u1.getLastname()));
                	CollationKey key2 = collator.getCollationKey(VKCStringUtils.decodeXmlEntities(u2.getLastname()));
                	
                	return key1.compareTo(key2);
				}
			};

			Collections.sort(groupUsers, c);
		}
		
		return groupUsers;
	}

	public String getLastEditingDate() {
		return lastEditingDate;
	}

	public UserInfo getLastEditor() {
		return lastEditor;
	}

	public void setCreationDate(String string) {
		creationDate = string;
	}

	public void setCreator(UserInfo info) {
		creator = info;
	}

	public void setGroup(VKCGroup group) {
		this.group = group;
	}

	public void setGroupId(Integer integer) {
		groupId = integer;
	}

	public void setLastEditingDate(String string) {
		lastEditingDate = string;
	}

	public void setLastEditor(UserInfo info) {
		lastEditor = info;
	}
}