package com.fuse.projects.fuse_light.examples;

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

import com.fuse.*;
import com.fuse.net.*;
import com.fuse.storage.*;
import com.fuse.projects.fuse_light.net.*;


/**
  * Example how to extend FUSELightConnection to add own functionality.
  * To use this class, you have to set in properties-file
  * property "connection.class=com.fuse.projects.fuse_light.examples.ExampleConnection"
  *
  * This connection makes a sql-query to the database. Client can
  * send any SQL-query by sending command "QUERYSQL select xx,xx .... FROM ..."
  *
  * @see com.fuse.project.fuse_light.net.FUSELightConnectionFactory
  * @author Aapo Kyrola
  */
public class ExampleConnection extends FUSELightConnection {
	
	protected static final String FUSECMD_QUERYSQL = "QUERYSQL";
	
	/**
	  * Constructor : only calls super(fcm)
	  */
	public ExampleConnection(FUSEConnectionManager fcm) {
		super(fcm);	
		this.setName("ExampleConnection id = " + id);
	}
	
	
	/**
	  * Override this if you want to handle some special messages
	  * in your project. Return true if you did handle the message, false otherwise
	  */
	protected boolean projectSpecificCommand(String command, String msg) throws Exception {
		if (command.equals(FUSECMD_QUERYSQL)) {
			/* Parse the query from the message */
			String sqlQuery = msg.substring(msg.indexOf(" ")+1);
			
			Log.status("ExampleConnection: Making query: " + sqlQuery);
			
			/* Obtain reference to database */
			FUSEDatabase db = FUSEEnvironment.getDatabaseProxy().getDatabase();
			
			/* Perform the query */
			ExampleSQLQuery queryBean = new ExampleSQLQuery(sqlQuery);
			Vector results = db.query(queryBean);			
			
			Log.status("Got " + results.size() + " hits.");
			
			/* Send the	results back to client in tab-delimited format */
			StringBuffer sb = new StringBuffer(results.size() * 200);		
			
			for(int i=0; i<results.size(); i++) {
				Vector row = (Vector) results.elementAt(i);
				for(int j=0; j<row.size(); j++) {
					sb.append(row.elementAt(j).toString());
					sb.append("\t");
				}
				sb.append("\r");
			}
			
			/* Send the results back to client */
			processFuseMessage("SQLQUERY_RESULTS", sb.toString());						
			return true;
		}
		return false;
	}
}
