package com.fuse;

/*
	FUSE Light Server - multiuser server application
	Copyright (C) 2000 Aapo Kyrola / Sulake Oy  Helsinki, Finland

	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 2
	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, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.rmi.*;

import com.fuse.net.*;
import com.fuse.storage.*;
import com.fuse.storage.data_objects.*;

/**
  * FUSERegister handles new user registrations and data updates.
  */
public class FUSERegister {
	
	public SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
	protected File registryDir;
	
	protected FUSEUser utilityUser = null;
	
	FUSERegister() throws Exception {
	}
	
	/**
	  * Stores a new user.
	  * @param newUser the new user object
	  * @throws UserExistsException if user with the same name already exists
	  * @throws IOException if the user could not be written to disk
	  */
	public FUSEUser saveUser(FUSEUser newUser) throws UserExistsException, DatabaseException {
		try {
			Log.status("Saving user:" +  newUser.getName());
			FUSEDatabase database = FUSEEnvironment.getDatabaseProxy().getDatabase();
			return (FUSEUser) database.insert(newUser);
		} catch (RemoteException re) {
			Log.error(re);
			return null;
		}
	}
	
	/**
	  * Construcst a new empty user instance
	  */
	public FUSEUser createNewUser() {
		try {
			FUSEDatabase database = FUSEEnvironment.getDatabaseProxy().getDatabase();
			return (FUSEUser) database.createNew("FUSEUser");
		} catch (Exception e) {
			Log.error(e);
			return null;
		}
	}
	
		
 	/**
	  * Stores an existing user
	  * @param newUser the new user object
	  * @throws UserExistsException if user with the same name already exists
	  * @throws IOException if the user could not be written to disk
	  */
	public FUSEUser updateUser(FUSEUser oldUser) throws UserExistsException {
		try {
			FUSEDatabase database = FUSEEnvironment.getDatabaseProxy().getDatabase();
			return (FUSEUser) database.update(oldUser);
		} catch (Exception re) {
			Log.error(re);
			return null;
		}
	}
	
	/**
	  * Retrieves an user from the disk (or database)
	  * @param userName
	  * @throws IOException if the user cannot be loaded
	  */
	public FUSEUser loadUser(String userName) throws DatabaseException {
		try {
			FUSEDatabase database = FUSEEnvironment.getDatabaseProxy().getDatabase();
			if (utilityUser == null) {
				 utilityUser = (FUSEUser) database.createNew("FUSEUser");
			}
			/* UtilityUser is used for making the query */
			Log.status("Utility user:" + utilityUser.getClass().getName());
			return utilityUser.getUserWithName(userName);
		} catch (Exception re) {
			Log.error(re);
			return null;
		}
	}
	
	
	public void save(FUSEUser user) throws IOException {
	}
}

