package com.fuse.storage.data_objects;

/*
	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.util.*;
import java.io.Serializable;

import com.fuse.*;
import com.fuse.storage.*;
/**
  * FUSEUser represents a registered user of a FUSE-service.
  * Name implementation classes <ImplType>FUSEUser, like SQLFUSEUser
  * @author Aapo Kyrola
  */
public abstract class FUSEUser implements FUSEDataObject, Serializable {
	
	/* Interface: DataObject */
	public String getType() {
		return "FUSEUser";
	}	
	
	/**
	  * Returns the cache key of this object or null
	  * if the object is not cacheable.
	  * NOTE that the cache key must be unique only between objects
	  * of same class: the database cache implementation should differ
	  * between classes.
	  */
	public String getCacheKey() {
		return this.getName();
	}
	
	/* Returns an unique id for the user*/
	public abstract long getId();
	
	/* User name - is unique */
	public abstract void setName(String name);
	
	public abstract String getName();
	
	/* Password */
	public abstract void setPassword(String s);
	
	public abstract String getPassword();
	
	/* Figure: format is "bottomPart,middlePart,topPart" */
	public abstract void setFigure(String s);
	
	public abstract String getFigure();
	
	
	/* Sex: "Male" or "Female" */
	public abstract String getSex();
	
	/* If true, then set the user to be male */
	public abstract void setSex(boolean male);
	
	public abstract boolean isMale();
	
	public abstract boolean isFemale();
	
	
	/* Real name */
	public abstract void setRealName(String firstName, String lastname);
	
	public abstract void setRealFirstName(String s);
	
	public abstract void setRealLastName(String s);
	
	public String getRealName() {
		return getRealFirstName() + " " + getRealLastName();
	}
	
	public abstract String getRealLastName();
	
	public abstract String getRealFirstName();
	
			
	/* Phone number */
	public abstract void setPhoneNumber(String s);
	
	public abstract String getPhoneNumber();
	
	/* Address */
	public abstract void setAddress(String s);
	
	public abstract String getAddress();
	
	public abstract void setZipCode(String s);
	
	public abstract String getZipCode();
	
	/* Toimipaikka.. */
	public abstract void setPostLocation(String s);
	
	public abstract String getPostLocation();
	
	
	/* E-mail */
	public abstract void setEmail(String s);
	
	public abstract String getEmail();
	
	/* Age */
	public abstract void setAge(int age);
	
	public abstract int getAge();
	
	/* Custom text (motto) */
	public abstract void setCustomData(String s);
	
	public abstract String getCustomData();
	
	
	/* Free properties / attributes (like "allow_direct_advertisement" or "last_access_time")*/
	public abstract void setProperty(String type, String value);
	
	public abstract String getProperty(String type);
	
	public abstract void removeProperty(String type);
	
	/* Utility / query methods */
	public abstract FUSEUser getUserWithName(String name) throws DatabaseException;
	
	public abstract FUSEUser getUserById(long id) throws DatabaseException;
	
	/* FUSE-protocol representation of the user */
	public String toFusePString() 
	 {
		StringBuffer sb = new StringBuffer(400);
		// user password email figure bandwidth opinionOnMusic sex mailinfo age customData .. 
		sb.append("name=" + this.getName());
		sb.append("\rpassword=" + this.getPassword());
		sb.append("\remail=" + this.getEmail());
		try {sb.append("\rage=" + this.getAge());} catch (NumberFormatException nfe) {;}
		sb.append("\rfirstName=" + this.getRealFirstName());
		sb.append("\rlastName=" + this.getRealLastName());
		sb.append("\rfigure=" + this.getFigure());
		sb.append("\raddress=" + this.getAddress());
		sb.append("\rzipcode=" + this.getZipCode());
		sb.append("\rphoneNumber=" + this.getPhoneNumber());
		sb.append("\rpostLocation=" + this.getPostLocation());
		sb.append("\rcustomData=" + this.getCustomData());
		return sb.toString();
	}
	
	/* Handles a client requested registration */
	public void handleFusePRegistration(String data) {
		StringTokenizer zt = new StringTokenizer(data, "\r");
		String ln, key, value;
		while(zt.hasMoreTokens()) {
			ln = zt.nextToken();
			int keyOffset = ln.indexOf("=");
			if (keyOffset < 1) continue;
			key = ln.substring(0, keyOffset);
			value = ln.substring(keyOffset+1);
			System.out.println(key + "*=*" + value);
			
			try {
				if (key.equals("name")) this.setName(value);
				else if (key.equals("password")) this.setPassword(value);
				else if (key.equals("customData")) this.setCustomData(value);
				else if (key.equals("age")) this.setAge(Integer.parseInt(value));
				else if (key.equals("email")) this.setEmail(value);
				else if (key.equals("firstName")) this.setRealFirstName(value);
				else if (key.equals("lastName")) this.setRealLastName(value);
				else if (key.equals("figure")) this.setFigure(value);
				else if (key.equals("sex")) this.setSex(value.equals("Male"));
				else if (key.equals("address")) this.setAddress(value);
				else if (key.equals("zipcode")) this.setZipCode(value);
				else if (key.equals("postLocation")) this.setPostLocation(value);
				else if (key.equals("phoneNumber")) this.setPhoneNumber(value);
				else this.setProperty(key,value);
			} catch (Exception e) {;}
		}
	}
	
	public String toString() {
		return "User of name=" + getName() + "; implementation=" + this.getClass().getName();
	}
}
