import java.io.*;
import java.net.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Jstore extends HttpServlet {
    static TheLockableStore mysql_store;
    public void init(){
        mysql_store=new TheLockableStore();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String [] the_data=new String[4];
        String[] mysql_response;
        int the_task;
        the_data[0]=request.getParameter("the_task");
        the_task=Integer.parseInt(the_data[0]);
        the_data[1]=request.getParameter("the_name");
        switch (the_task) {
        case 0:
            the_data[2]=request.getParameter("the_class");
            the_data[3]=request.getParameter("the_notes");
            break;
        case 1:
            the_data[2]=request.getParameter("the_version");
            break;
        case 2:
            the_data[2]=request.getParameter("the_version");
            the_data[3]=request.getParameter("the_notes");
            break;
        default:
            break;
        }        

        String[]   connector_response;
        mysql_store.lock();
        try {
            connector_response=mysql_store.TheDBCall(the_data);
        } finally {
            mysql_store.unlock();
        }
        PrintWriter out = response.getWriter();
        switch (the_task) {
        case 0:
            out.println("File Name:"+ the_data[1]+"   Version:  "+connector_response[0]+ "  was stored.");
            break;
        case 1:
            out.println(connector_response[0]);
            //out.println(the_data[0]+" "+ the_data[1]+"  "+the_data[2]);
        case 2:
            out.println(connector_response[0]);
            //out.println(the_data[0]+" "+ the_data[1]+"  "+the_data[2]+"  "+the_data[3]);
            break;
        default:
            out.println("bad result");
        }


        out.close();
    }
}

    class TheLockableStore extends ReentrantLock {

        public TheLockableStore(){       
        }
        public String[] TheDBCall(String[] this_data)
        throws ServletException, IOException{
            String[]   the_response=new Jstore_connector().the_call(this_data);
            return the_response;
        }
    }