<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.net.*"%>
<%@ page import = "ajax.*"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

<link rel="stylesheet" href="css/mysite.css" type="text/css"/>
<div align="right">
<button onclick="javascript:getAddTag()">Add Tag</button>
<button onclick="javascript:getAddAlbum()">Add Album</button>
</div>
<%

		Connection conn = null;
    String bandname="";
    Statement s=null;
    String query="";
    ResultSet rs=null;
    int tracknum=-1;
    
           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");
           
			String band = "";
		/*
		 if (request.getParameter("album")!=null)
		   {
		  s = conn.createStatement ();
			query= ("select name from bands where id in (select band_id from albums where name='"+request.getParameter("album")+"')");
			System.out.println(query);			
			s.executeQuery (query);
			rs = s.getResultSet ();
		 while (rs.next ())
      {
      band = rs.getString("name");
      } 
      
      
		 }
		 
			else if (request.getParameter("track")!=null)
		   {
		  s = conn.createStatement ();
			query= ("select name from bands where id in (select band_id from albums where id in (select album_id from tracks where name='"+request.getParameter("track")+"' ))");
			System.out.println(query);			
			s.executeQuery (query);
			rs = s.getResultSet ();
		 while (rs.next ())
      {
      band = rs.getString("name");
      } 
      
      
		 } 
		 
       else
       {
		*/
            if (request.getParameter("band").equals("")) {
            System.out.println("empty.."); 
            return;
            
        } else {
            System.out.println(request.getParameter("band"));
           band=request.getParameter("band");
        
           }
           
 /*       }   */
/****************Band***********************/
           
      s = conn.createStatement ();
			query= ("Select * from bands where name = \""+band+"\"");
			System.out.println(query);			
			s.executeQuery (query);
			rs = s.getResultSet ();
              
              
              ArrayList ar = new ArrayList();
      
      int idVal=-1;
      String desc="";
      String picurl="";
                           
		while (rs.next ())
		{
			idVal = rs.getInt ("id");
			bandname = rs.getString ("name");
			desc=rs.getString ("description");
			picurl=rs.getString ("picurl");
			System.out.println(idVal+" "+bandname);
			
		}
		rs.close ();
		s.close ();
      out.println("<div class=\"invis\" id=\"band_id\">"+idVal+"</div>");            
      out.println("<H2><div id=\"t_b_n\">"+bandname+"</div></H2>");  
      out.println("<img src=\""+picurl+"\"/>");         
      out.println("<br>");
      out.println(desc);
  
             
      
/****************Players**********************/
   
out.println("<div id=\"player_place\">");
out.println("<img src=\"img/ajax_anim.gif\"/>");
out.println("</div>");
out.println("<div id=\"player_place2\">");
out.println("<img src=\"img/ajax_anim.gif\"/>");
out.println("</div>");
out.println("<br>");
out.println("<div id=\"player_place3\">");
out.println("<img src=\"img/ajax_anim.gif\"/>");
out.println("</div>");
/****************Tags*************************/
//at the and
 /****************Albums*************************/

       s = conn.createStatement ();
      String  albumname="";
      java.sql.Date albumdate = null;
      int albumid=-1;
      
			 query= ("select * from albums where band_id in (select id from bands where name='"+bandname+"') order by date");
			System.out.println(query);			
			s.executeQuery (query);
			rs = s.getResultSet ();
              
      
      boolean first=true;
      
      
      out.println("<div class=\"albums\">");
      
      
      
		while (rs.next ())
		{
		  
			if (first) {first=false;out.println("<br><div class=\"bbg2\"><b>Artist albums:</b></div><br>");}
			
			
			albumname = rs.getString ("name");
			albumid =  rs.getInt("id");
			albumdate = rs.getDate("date");
			//out.println(albumid+" "+albumname+" "+albumdate.toString().substring(0,4));
			
			Statement s2 = conn.createStatement ();
			String query2= ("select * from tracks where album_id ="+albumid);
			System.out.println(query2);			
			s2.executeQuery (query2);
			ResultSet rs2 = s2.getResultSet ();
			ArrayList<Track> songs=null;
			songs = new ArrayList<Track>();
			String albuminfo = ("<b><u>"+albumname+" ("+albumdate.toString().substring(0,4)+"):</b></u>");
			songs.add(new Track(albuminfo));
			tracknum=1;
			
			while (rs2.next ())					
        {
                
        songs.add(new Track(""+(tracknum++)+". "+rs2.getString("name")));
        }
      
      request.setAttribute( ("album") , songs );
      

      %>
      
      <display:table name="album" class="table" >         
        <display:column property="track" /> 
        <display:setProperty name="basic.show.header" value="false"/>
      </display:table>

			  
			<%			

			
			
			 out.println("<br>");
			
		}
		
		out.println("</div>");
		
		
		
		rs.close ();
		s.close ();
   
   
   
           
	}                   catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
           }
           

         out.println("<br>");  

%>







<script type="text/javascript" src="js/ajax.js"></script>
<script language="JavaScript">
<% //out.println("javascript:getAjax('myspaceplayer.jsp?band="+bandname+"','player_place');"); %>
</script>

<div id="simil">

</div>

<%
try{
       s = conn.createStatement ();
      
			query= ("select name from tags where id in (select tag_id from tagged where band_id in (select id from bands where name = \""+bandname+"\"))");
			System.out.println(query);			
			s.executeQuery (query);
			rs = s.getResultSet ();
      
      String tagname="";
      
      out.println("<br><div class=\"bbg\"><b>Artist tags:</b></div>");
      out.println("<br><div class=\"bandtags\">");      
		while (rs.next ())
		{
			tagname = rs.getString ("name");
			out.println("<div class=\"suggest_link\"onmouseover=\"javascript:suggestOver(this);\" onmouseout=\"javascript:suggestOut(this);\" onclick=\"javascript:getBandsFromTag('"+tagname+"','main_window'); disableForum();\"   >");
      out.println(tagname+"<br>");      
      out.println("</div>");      
		}
		
		out.println("</div>");
		
		rs.close ();
		s.close ();
		}      
		
		          catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
           }
           
		
		           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
   
 %>  
