package ch.ethz.syslab.telesto.server.controller;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;

import ch.ethz.syslab.telesto.protocol.ComplexTestPacket;
import ch.ethz.syslab.telesto.protocol.CreateQueuePacket;
import ch.ethz.syslab.telesto.protocol.DeleteQueuePacket;
import ch.ethz.syslab.telesto.protocol.GetActiveQueuesPacket;
import ch.ethz.syslab.telesto.protocol.GetMessagesPacket;
import ch.ethz.syslab.telesto.protocol.GetQueueIdPacket;
import ch.ethz.syslab.telesto.protocol.GetQueueNamePacket;
import ch.ethz.syslab.telesto.protocol.GetQueuesPacket;
import ch.ethz.syslab.telesto.protocol.IdentifyClientPacket;
import ch.ethz.syslab.telesto.protocol.Packet;
import ch.ethz.syslab.telesto.protocol.PingPacket;
import ch.ethz.syslab.telesto.protocol.PutMessagePacket;
import ch.ethz.syslab.telesto.protocol.ReadMessagePacket;
import ch.ethz.syslab.telesto.protocol.RegisterClientPacket;
import ch.ethz.syslab.telesto.server.db.Database;

public class PacketHandler implements IPacketHandler {

    private Database db;

    public PacketHandler(Database database) {
        db = database;
    }

    @Override
    public Packet handle(Packet packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(PingPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(RegisterClientPacket packet) throws PacketProcessingException {

        CallableStatement stm = db.prepareCall("request_id", 2, true);

        try {
            stm.registerOutParameter(1, Types.INTEGER);

            stm.setObject(2, packet.clientName, Types.VARCHAR);

            stm.setString(2, packet.clientName);
            stm.setByte(3, packet.mode);

            stm.execute();
            stm.getResultSet();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Packet handle(IdentifyClientPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(CreateQueuePacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(DeleteQueuePacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(GetQueueIdPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(GetQueueNamePacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(GetQueuesPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(GetActiveQueuesPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(GetMessagesPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(PutMessagePacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(ReadMessagePacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Packet handle(ComplexTestPacket packet) throws PacketProcessingException {
        // TODO Auto-generated method stub
        return null;
    }

}

@@ -48,97 +48,8 @@
 r;%0A%0A
-import java.sql.CallableStatement;%0Aimport java.sql.SQLException;%0Aimport java.sql.Types;%0A%0A
 impo
@@ -879,25 +879,151 @@
 sto.
-server.db.Databas
+protocol.RegisterClientResponsePacket;%0Aimport ch.ethz.syslab.telesto.server.db.Database;%0Aimport ch.ethz.syslab.telesto.server.db.StoredProcedur
 e;%0A%0A
@@ -1607,25 +1607,24 @@
 ption %7B%0A
-%0A
         
 Callable
@@ -1619,504 +1619,283 @@
     
-CallableStatement stm = db.prepareCall(%22request_id%22, 2, true);%0A%0A        try %7B%0A            stm.registerOutParameter(1, Types.INTEGER);%0A%0A            stm.setObject(2, packet.
+// TODO: should return whole information; i.e. %5Bclient_id, 
 client
-N
+_n
 ame, 
-Types.VARCHAR);%0A%0A            stm.setString(2, packet.clientName);%0A            stm.setByte(3, packet.mode);%0A%0A            stm.execute();%0A            stm.getResultSet();%0A%0A        %7D catch (SQLException e) %7B%0A            // TODO Auto-generated catch block%0A            e.printStackTrace();%0A        %7D%0A%0A        return null
+operation_mode%5D%0A%0A        int clientId = db.callSimpleProcedure(StoredProcedure.REQUEST_ID, packet.clientName, packet.mode);%0A%0A        return new RegisterClientResponsePacket(packet.packetId + 1, clientId)
 ;%0A  

implement first packet as demo