/*
 * SearchPortlet.java
 *
 * Created on August 13, 2001, 8:37 PM
 */

package com.ibm.raleigh.saroehr.portlets;
// import portal packages
import org.apache.jetspeed.portlet.*;
import org.apache.jetspeed.portlets.DefaultPortletAction;
import org.apache.jetspeed.portlets.AbstractPortlet;
// import utility classes
import com.ibm.raleigh.saroehr.beans.SoapAccessBean;
import com.ibm.raleigh.saroehr.beans.PortletDataStoreBean;

/**
 * SearchPortlet extends AbstractPortlet and implements
 * a web services based search portlet.
 * @author  Scott A. Roehrig@IBM Corporation
 * @version 1.0
 */
public class SearchPortlet extends AbstractPortlet {
    // declare member variables
    private PortletLog logger;
    // declare methods

    /** initializes portlet */
    public void init(PortletConfig config) throws UnavailableException {
        super.init(config);
        logger = config.getContext().getLog();
        
    }
    
    /** handler for view requests */
    public void doView(PortletRequest request, PortletResponse response) throws PortletException, java.io.IOException {
        if (logger.isInfoEnabled() == true) {
            logger.info("doView() called...");
        }
        // obtain output stream
        java.io.PrintWriter output = response.getWriter();
        // obtain PortletURI
        PortletURI viewURI = response.createReturnURI();
        // create actionHandler
        PortletAction actionHandler = new DefaultPortletAction("Submit");
        // register eventHandler
        viewURI.addAction(actionHandler);
        // check request for search attribute
        Object results = request.getAttribute("searchResults");
        String provider = (String)request.getSession().getAttribute("provider");
        if (provider == null) {
            provider = "Yahoo";
        }
        if (results == null || results.equals("")) {
            output.println("<p>Please enter desired search string and select Submit: </p>");
            output.println("<form name=\"searchForm\" action="+"\""+viewURI.toString()+"\" method=\"POST\">");
            output.println("<input name=\"search\" type=\"text\"/>");
            output.println("<input name=\"provider\" value=\""+provider+"\" type=\"hidden\"/>");
            output.println("<p><input name=\"submit\" value=\"Submit\" type=\"Submit\"/> <input name=\"reset\" value=\"Reset\" type=\"Reset\"/></p>");
            output.println("</form>");
            
        }
        else if (results.getClass() == org.apache.soap.SOAPException.class) {
            output.println("<p>There has been an error processing your request. Please retry. If the problem persists, "+
                                      "please contact your system administrator and inform them of the issue.");
            output.println(results);
            output.println("<p>Please click <a href=\""+viewURI.toString()+"\">here </a> to retry your request.</p>"); 
            
            
        }
        else {
            output.println("<p><h3>Thank you for using the Soap WebServices WebSearch!</h3></p>");
            output.println("<p>Your results include the following. To perform another search, please click <a href=\""+viewURI.toString()+"\">here</a>.</p>");
            output.println("<hr />");
            output.println(results);
        }
            
    }
    
    /** edit request handler */
    public void doEdit(PortletRequest request, PortletResponse response) throws PortletException, java.io.IOException {
        // obtain output stream
        java.io.PrintWriter output = response.getWriter();
        // create return URI
        PortletURI viewURI = response.createReturnURI();
        // declare eventHandler
        PortletAction actionHandler = new DefaultPortletAction("submit");
        // register listener
        viewURI.addAction(actionHandler);
        // create output fragment
        output.println("<p><h5 align=\"left\">Please select your desired search engine provider: </h5></p>");
        output.println("<form id=\"provider\" action=\""+viewURI.toString()+"\" method=\"post\">");
        output.println("<input type=\"radio\" name=\"provider\" value=\"Yahoo\"/> Yahoo");
        output.println("<input type=\"radio\" name=\"provider\" value=\"Excite\"/> Excite");
        output.println("<input type=\"radio\" name=\"provider\" value=\"AltaVista\"/> AltaVista");
        output.println("<input type=\"radio\" name=\"provider\" value=\"Google\"/> Google");
        output.println("<input type=\"submit\" name=\"Submit\" value=\"Submit\"/> <input type=\"reset\" name=\"Reset\" value=\"Reset\"/>");
      
    }
    
    /** handler for help requests */
    public void doHelp(PortletRequest request, PortletResponse response) throws PortletException, java.io.IOException {
        // obtain output stream
        java.io.PrintWriter output = response.getWriter();
        // create fragment
        output.println("<p>SearchPortlet provides a soap WebServices based search portlet. This portlet allows web searching against various "+
                                  "search engines. The portlet interacts with the XMethods web search webservice to perform the search and return the "+
                                  "result. For further information, please review the source code to SearchPortlet provided.");
        
        
    }
        
    /** destroys the portlet */
    public void destroy() {
        logger = null;
        
    }
    
}
