/*
 * Created on 29-Apr-2009
 *
 * TODO 
 */
package uk.ac.roe.wfau;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * @author MAR
 *
 */
public class VSAQueryRetrieverThread extends QueryRetrieverThread{
    static String startCell="<td align=\"right\" nowrap=\"nowrap\">";
    static String endCell="</td> \n";
    static String displayURL=WSAHTMLSchema.baseCGIURL+"display.cgi?";
    static String GIURL=WSAHTMLSchema.baseJSPURL+"GetImage?mode=show&";
    /**
     * @param dbURL
     */
    public String getFromEmail(){
        return "vsa-support@roe.ac.uk";
    }
    static String VSATITLE ="VSA - SQL Query Results";
    public String getTitle() {
        return VSATITLE;
    }
    public VSAQueryRetrieverThread(String dbURL) {
        super(dbURL);
        // TODO Auto-generated constructor stub
    }

    public static String [][] showArray = { {"multiframeid","filename","numdetectors","compfile"},
            {"framesetID","ra","dec"},{"multiframeID","ra","dec"} };
   // boolean [] showBoolean = new boolean [] {false};
    
    
    public StringBuffer getLinkInfo () {
        StringBuffer st=new StringBuffer("");
        if (showBoolean[1]) {
            st.append("The mfLink column can be used to display and download the associated multiframe.<p>");
        }
        if (showBoolean[2]) {
            st.append("The getFSLink column can be used to display 1 arcmin image cut-outs around the RA/Dec of the object.<p>");
        }
        if (showBoolean[3]) {
            st.append("The getMFLink column can be used to display 1 arcmin image cut-outs around the RA/Dec of the object.<p>");
        }        
        return st;
    }
    
    public StringBuffer getShowTitles () {
        StringBuffer st=new StringBuffer("");
            if (showBoolean[1]) {
                st.append("<th nowrap=\"nowrap\">mfLink</th>");
            }
            if (showBoolean[2]) {
                st.append("<th nowrap=\"nowrap\">getFSLink</th>");
            }
            if (showBoolean[3]) {
                st.append("<th nowrap=\"nowrap\">getMFLink</th>");
            }
            
        
        return st;
    }
    public StringBuffer getShowLinks (ResultSet rs) {
        StringBuffer st=new StringBuffer("");
        
        try {
            if (showBoolean[1]) {
                st.append(startCell);
                String compFile=rs.getString("compfile");
        String comp=compFile.substring(0,Math.max(0,compFile.lastIndexOf('_')));
        st.append("<a href=\""+displayURL+"file="+rs.getString("filename")+"&multiframeid="+
                rs.getString("multiframeid")+"&noExt="+rs.getString("numDetectors")+
                "&comp="+comp+"\" target=display>view<a/>");
        st.append(endCell);
            }
            
            if (showBoolean[2]) {
                st.append(startCell);
                st.append("<a href=\""+GIURL+"ra="+rs.getString("ra")+"&dec="+
                        rs.getString("dec")+"&fsid="+rs.getString("framesetID")+"&database="+database+"\" target=getI>view1<a/>");
                st.append(endCell);
            }
            
            if (showBoolean[3]) {
                st.append(startCell);
                st.append("<a href=\""+GIURL+"ra="+rs.getString("ra")+"&dec="+
                        rs.getString("dec")+"&mfid="+rs.getString("multiframeID")+"&database="+database+"\" target=getI>view<a/>");
                st.append(endCell);
             }
            
            
            
        }
        catch (Exception e){
            return EMPTYSTRINGBUFFER;  
        }
        
        return st;
    }
    
    public void setShowBoolean(ResultSetMetaData rsmd) {
        
        showBoolean=showArrayPresent(rsmd,showArray);
        //logger.info(showBoolean[0]+" "+showBoolean[1]);
    }

}
