groupsfile.java

来自「一个日本流行的,功能较全的开源Web办公管理(Groupware)系统。」· Java 代码 · 共 305 行

JAVA
305
字号
package jp.co.sjts.gsession.main;

/*
 * Group 僼傽僀儖憖嶌  GroupsFile.java
 * Copyright (C) 1999-2000 Japan Total System Co,LTD
 *    Satoru K  <koni@sjts.co.jp>
 */

import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import jp.co.sjts.gsession.tools.*;

public class GroupsFile extends GSTextFile implements Serializable
{
	public final static String FILENAME = "group";

	private File fGroup = null;
	private Map GroupMap=null;			// KEY:GID		VALUE:Group
	private int gidmax;					// 僌儖乕僾ID惗惉梡
	
	public GroupsFile(String DataDir) throws GSException
	{
		fGroup = new File(DataDir + FILENAME);
		GroupMap = Collections.synchronizedMap(new HashMap());

		if(!fGroup.exists())
		{// 僼傽僀儖偑側偄応崌偼丄僨僼僅儖僩抣偱嶌惉

			// 僨僼僅儖僩偱娗棟幰僌儖乕僾嶌惉
			gidmax = 0;
			Group group = new Group("0","0","娗棟僌儖乕僾","僗乕僷乕儐乕僓僌儖乕僾");
			GroupMap.put(new Integer(gidmax),group);

			try {
				Save();
			} catch(Exception e) {
				throw new GSException("\"group\"僼傽僀儖偺嶌惉偵幐攕!!");
			}
		}
	}

	protected synchronized File GetFile() { return fGroup; }

	public synchronized int getGIDMax()
	{
		return gidmax;
	}

	public synchronized int[] getAllGids()
	{
		synchronized(GroupMap) {
			if(GroupMap.size()<=0)
				return null;

			int[] gids = new int[GroupMap.size()];
			Iterator it=GroupMap.keySet().iterator();
			for(int i=0;it.hasNext();i++){
				gids[i] = ((Integer)it.next()).intValue();
			}
			return gids;
		}
	}

	protected synchronized void Read(BufferedReader bf) throws GSException
	{
		String buf;

		synchronized(GroupMap) {
			GroupMap.clear();
			try{
				gidmax = Integer.parseInt(bf.readLine());
				for(buf=bf.readLine();buf!=null;buf=bf.readLine())
				{
					String buf2;
					String gid="";
					String uids="";
					String name="";
					String note="";
					
					StringTokenizer st = new StringTokenizer(buf,";",true);
					// GID偺庢摼
					if((buf2 = st.nextToken()).equals(";"))
						throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅");
					gid = buf2;

					if(!(buf2 = st.nextToken()).equals(";"))
						throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅");

					// UID偺庢摼
					if((buf2 = st.nextToken()).equals(";"))
						uids = "";
					else
						uids = buf2;

					// 僌儖乕僾柤偺庢摼
					if(buf2.equals(";")){
						if((buf2 = st.nextToken()).equals(";"))
							throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅");
						else
							name = GSTool.delimitDecorde(";",buf2);
					}else{
						if(!(buf2 = st.nextToken()).equals(";")){
							throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅");
						}else{
							if((buf2 = st.nextToken()).equals(";"))
								throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅");
							else
								name = GSTool.delimitDecorde(";",buf2);
						}
					}

					// 旛峫偺庢摼
					if(!(buf2 = st.nextToken()).equals(";"))
						throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅");
					try{
						note = GSTool.delimitDecorde(";",GSHtml.BRtoReturn(st.nextToken()));
					}catch(NoSuchElementException e){
					}

					GroupMap.put(new Integer(gid),new Group(gid,uids,name,note));
				}
			}catch(NoSuchElementException e){
				throw new GSException("僼傽僀儖僨乕僞偑晄惓偱偡丅",e);
			}catch(IOException e){
				throw new GSException("IO僄儔乕",e);
			}
		}
	}

	protected synchronized void Write(PrintWriter pw) throws GSException
	{
		pw.println(Integer.toString(gidmax));
		synchronized(GroupMap) {
			Iterator it = GroupMap.values().iterator();
			while(it.hasNext())
				pw.println(it.next().toString());
		}
	}

	/*
	* 僌儖乕僾偺捛壛
	* in:	name(String)		僌儖乕僾柤
	* 		note(String)		旛峫
	*
	* out:	(int)				捛壛偝傟偨GID
	*/
	public synchronized int addGroup(String name,String note)
	{
		synchronized(GroupMap) {
			gidmax++;
			Group group = new Group(Integer.toString(gidmax),"",name,note);
			GroupMap.put(new Integer(gidmax),group);
			return gidmax;
		}
	}

	/*
	* 僌儖乕僾忣曬偺曄峏
	* in:	gid(int)			曄峏偡傞僌儖乕僾偺僌儖乕僾ID
	*		name(String)		僌儖乕僾柤
	* 		note(String)		旛峫
	*
	* out:	側偟
	*/
	public synchronized void setGroup(int gid,String name,String note)
	{
		synchronized(GroupMap) {
			Group group = (Group)GroupMap.get(new Integer(gid));
			group.SetName(name);
			group.SetNote(note);
		}
	}

	/*
	* 僌儖乕僾偺捛壛
	* in:	udis(String)		儐乕僓IDs	椺: "1:3"
	* 		name(String)		僌儖乕僾柤
	* 		note(String)		旛峫
	*
	* out:	(int)				捛壛偝傟偨GID
	*/
	public synchronized int addGroup(String uids,String name,String note)
	{
		synchronized(GroupMap) {
			gidmax++;
			Group group = new Group(Integer.toString(gidmax),uids,name,note);
			GroupMap.put(new Integer(gidmax),group);
			return gidmax;
		}
	}

	/*
	* 僌儖乕僾偺嶍彍
	* in:	GID(int)		僌儖乕僾ID
	*
	* out:	(int[])			懳徾僌儖乕僾偵強懏偟偰偄偨儐乕僓ID偺儕僗僩
	*/
	public synchronized int[] delGroup(int GID)
	{
		synchronized(GroupMap) {
			Group group = (Group)GroupMap.get(new Integer(GID));
			int[] UIDArray = null;
			if(group!=null)
			{
				Set userset = group.GetUserSet();
				UIDArray = new int[userset.size()];
				Iterator it = userset.iterator();
				for(int i=0;it.hasNext();i++)
				{
					Integer id = (Integer)it.next();
					UIDArray[i]=id.intValue();
				}
			}
			GroupMap.remove(new Integer(GID));
			return UIDArray;
		}
	}

	public synchronized void addUser(int[] GIDs,int UID)
		throws GSException
	{
		synchronized(GroupMap) {
			for(int i=0;i<GIDs.length;i++){
				Group g = (Group)GroupMap.get(new Integer(GIDs[i]));
				if(g != null)
					g.addUser(new Integer(UID));
			}
		}
	}

	public synchronized void delUser(int[] GIDs,int UID)
	{
		synchronized(GroupMap) {
			for(int i=0;i<GIDs.length;i++){
				Group g = (Group)GroupMap.get(new Integer(GIDs[i]));
				if(g != null)
					g.delUser(new Integer(UID));
			}
		}
	}

	/*
	* 僌儖乕僾ID偐傜強懏偟偰偄傞儐乕僓ID傪曉偡
	* in:	gid(int)		僌儖乕僾ID
	*
	* out:	(int[])			強懏偟偰偄傞儐乕僓偺儐乕僓ID
	*/
	public synchronized int[] getUserArray(int gid)
	{
		Group g;
		synchronized(GroupMap) {
			g = (Group)GroupMap.get(new Integer(gid));
			if(g == null)
				return null;
		}

		return g.GetUserArray();
	}

	/*
	* 僌儖乕僾ID偐傜Group僆僽僕僃僋僩傪庢摼
	* in:	gid(int)	僌儖乕僾ID
	*
	* out: (Group) 奩摉偡傞Group僆僽僕僃僋僩	奩摉偟側偄応崌偼(null)傪曉偡
	*/
	public synchronized Group GidToGroup(int gid) throws GSException
	{
		Group group=null;
		synchronized(GroupMap) {
			group = (Group)GroupMap.get(new Integer(gid));
		}
		return group;
	}

	/*
	* 僌儖乕僾柤偺庢摼
	*
	* in:	gid(int)		懳徾偺僌儖乕僾ID
	*
	* out:	(String)		僌儖乕僾柤
	*/
	public synchronized String getGroupName(int gid)
	{
		Group group=null;
		synchronized(GroupMap) {
			group = (Group)GroupMap.get(new Integer(gid));
		}
		if(group==null)
			return null;

		return group.GetName();
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?