<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.net.*"%>
<%@ page import = "ajax.*"%>
<%
 
  String band_id="-1";  
  String bandname= "";  
  if (request.getParameter("data")==null) 
   {
   bandname = request.getParameter("bandname");
   band_id = request.getParameter("band_id");
   out.println("<div class=\"invis\" id=\"band_id\">"+band_id+"</div>");  
   out.println("<div class=\"invis\" id=\"bandname\">"+bandname+"</div>");  
 %>
<i>Add new Album:</i>
<br><br>

Album Name:    <br><input id="albumnameField" type="text" size="25" maxlength="40"><br>
Release Date:    <br><input id="yearField" type="text" size="25" maxlength="40"><br>
Tracklist:<br><textarea cols="60" rows="8" id="tracksField">
</textarea>
<br>
<button onclick="javascript:addAlbum()">add</button>
<div id="isPost"></div>
<br>      
<br>      

<% }

else{


String data = (request.getParameter("data")); 
String albumname="";
String albumyear="";
String albumtracks="";

System.out.println(request.getCharacterEncoding());
System.out.println(request.getContentLength());
System.out.println(request.getContentType());
System.out.println(request.getParameterNames());

//data= URLDecoder.decode(data,"UTF-8");

int delimiter = data.indexOf("||");
int delimiter2 = data.indexOf("|||");
int delimiter3 = data.indexOf("||||");




band_id= data.substring(0,delimiter);
albumname = data.substring(delimiter+2,delimiter2);
albumyear = data.substring(delimiter2+3,delimiter3);
albumtracks = data.substring(delimiter3+4);


albumname = albumname.replaceAll("'","\\\\'");
albumname = albumname.replaceAll("\"","\\\\''");
albumtracks = albumtracks.replaceAll("'","\\\\'");
albumtracks = albumtracks.replaceAll("\"","\\\\''");



System.out.println(band_id);
System.out.println(albumname);
System.out.println(albumyear);
System.out.println(albumtracks);


		Connection conn = null;  
           try
           {
               String userName = "root";
               String password = "";
               String url = "jdbc:mysql://localhost/band";
               Class.forName ("com.mysql.jdbc.Driver").newInstance();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
               
               Statement s;
               String insert_statement="";
               String query="";
               ResultSet rs=null;
               int album_id=-1;
               
               
               int m;
               s = conn.createStatement();
               insert_statement = ("insert into albums(name, band_id, date) VALUES ('"+albumname+"','"+band_id+"','"+albumyear+"');");
               System.out.println(insert_statement);
               m = s.executeUpdate(insert_statement);
               
               s = conn.createStatement();
               query = ("select id from albums where name ='"+albumname+"';");
               System.out.println(query);
               s.executeQuery(query);
               rs = s.getResultSet();
               
               while(rs.next()){
               album_id = rs.getInt("id");
               }
               
               
               
               
               
               ArrayList<Track> tlist= new ArrayList<Track>();
               String temp="";
               
               for(int i=0; i<albumtracks.length();i++){
               if(albumtracks.charAt(i)!='\n')
                  temp+=albumtracks.charAt(i);
               else{
               tlist.add(new Track(temp));
               temp="";               
                }
               }
               
               tlist.add(new Track(temp));
               
               System.out.println("no. of tracks: "+tlist.size());
               
               for(int i=0; i<tlist.size();i++){
               String track = tlist.get(i).getTrack();
               s = conn.createStatement();
               insert_statement = ("insert into tracks(name, album_id) VALUES ('"+track+"','"+album_id+"');");
               System.out.println(insert_statement);
               m = s.executeUpdate(insert_statement);
               }
               
                      /*        
               query =("select name from bands where id='"+band_id+"'");
               
               System.out.println(query);
               s = conn.createStatement();
               s.executeQuery(query);               
               rs = s.getResultSet();
               
                              
               while(rs.next())
               bandname= rs.getString("name");
               */
                              
                              
               out.println("Added?");
               //out.println("<div class=\"invis\" id=\"bandname\">"+bandname+"</div>"); 
               
               
          }        
          
          
           catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
               out.println("Error");
               return;
           }
           
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
           
    




}
%>
