<%@ page import="java.io.*" %> <%@ page import="java.net.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.*" %> <%@ page import="com.kidshealth.broadcast.*" %> <%@ page session="false" %> <%@ page contentType="text/html;charset=utf-8"%> <% request.setCharacterEncoding("UTF-8"); %> <% out.println("Search Results - KidsHealth in the Classroom"); %> <% out.println(""); out.println(""); %> <% String searchedText = request.getParameter("SearchTextArea"); if(searchedText != null && searchedText.trim().length() > 0) searchedText = request.getParameter("SearchTextArea"); else searchedText = ""; %>

<%@ include file="rightcolumnprehead.html" %> <%! public String noLongStrings(String s,int maxLen) { StringBuffer sb =new StringBuffer(); StringTokenizer st = new StringTokenizer(s); while (st.hasMoreTokens()) { String txt = st.nextToken(); if (txt.length() > maxLen) { int parts = txt.length() / maxLen; StringBuffer txtSb = new StringBuffer(txt); for (int k=1; k<=parts; k++) txtSb.insert((maxLen*k)-1," "); sb.append(txtSb.toString()+" "); } else sb.append(txt+" "); } return sb.toString(); } // filter public String getResultVal(StringWriter sw ,String param) throws IOException{ StringReader sr = new StringReader(sw.toString()); BufferedReader br = new BufferedReader(sr); String line = "",retVal = ""; while ((line=br.readLine()) != null) { if (line.length() >= param.length()) { String sub = line.substring(0,param.length()); if (sub.toLowerCase().equals(param)) { retVal= line.substring(param.length()).trim(); break; } } // if } sr.close(); return retVal; } public String getResultVal(String line,String param) { String retVal = ""; if (line.trim().length() >= param.trim().length()) { String sub = line.substring(0,param.trim().length()); if (sub.toLowerCase().equals(param.trim())) { retVal = line.substring(param.trim().length()).trim(); } } return retVal; } public boolean isFound(String line,String param) { if (line.trim().length() >= param.trim().length()) { String sub = line.substring(0,param.trim().length()); if (sub.toLowerCase().equals(param.trim())) { return true; } } return false; } %> <% final String PARENTS="1"; final String TEENS ="2"; final String KIDS ="3"; final String ALLSECTION ="4"; final String NEWS = "5"; final boolean DEBUG = false; final int MAXRESULTS = 10; //Date Calendar cdate_today = Calendar.getInstance(); cdate_today.setLenient(false); int todayYear = cdate_today.get(Calendar.YEAR); ArrayList resultsAL = new ArrayList(); //ServletContext context = getServletContext(); String dbDriver = config.getInitParameter("ORACLE_DRIVER"); String dbConnectString = config.getInitParameter("ORACLE_CONNECT_STRING"); String dbUser = config.getInitParameter("ORACLE_USER"); String dbPass = config.getInitParameter("ORACLE_PASS"); String ultraSeekPort = config.getInitParameter("ULTRASEEK_PORT"); String ultraSeekHost = config.getInitParameter("ULTRASEEK_HOST"); Statement st = null; ResultSet rs = null; Connection con = null; StringBuffer radioSb = new StringBuffer(); if (ultraSeekPort==null) ultraSeekPort ="81"; if (ultraSeekPort.equals("")) ultraSeekPort = "81"; //String ultraSeek = "http://" + request.getHeader("host") + ":" + ultraSeekPort + "/query.html"; String ultraSeek = "http://" + ultraSeekHost + ":" + ultraSeekPort + "/query.html"; String displayedTotalStr = OtherUtils.getParameter(request,"DispTot"); int displayedTotal = 0; try { displayedTotal = Integer.parseInt(displayedTotalStr); } catch (NumberFormatException nfe4) { displayedTotal=0 ; } String searchText = OtherUtils.getParameter(request,"SearchTextArea"); String searchSection = OtherUtils.getParameter(request,"SearchSection"); if (searchSection.equals("")) searchSection=ALLSECTION; String searchAge = OtherUtils.getParameter(request,"SearchAge"); String mode = OtherUtils.getParameter(request,"Mode"); String nextSeekUrl = OtherUtils.getParameter(request,"NextResults"); String hitStart = OtherUtils.getParameter(request,"NextStart"); String prevStart = OtherUtils.getParameter(request,"PrevStart"); if (prevStart.equals("")) prevStart="1"; if (hitStart.equals("")) hitStart="1"; int hitStartInt = 1; try { hitStartInt = Integer.parseInt(hitStart); } catch (NumberFormatException nfe1) { hitStartInt = 1; } boolean isPrev = false, isNext=false; if (mode.toLowerCase().equals("prev")) isPrev= true; else isNext = true; if (isPrev) { try { hitStartInt = Integer.parseInt(prevStart); } catch (NumberFormatException nfe1) { hitStartInt = 1; } } String searchSubmit = OtherUtils.getParameter(request,"SearchSubmit"); if (searchSubmit.toLowerCase().indexOf("search") != -1) mode = "Search"; //out.println("submit: " + searchSubmit + "
"); if (mode.equals("")) mode="Init"; String advanced = OtherUtils.getParameter(request,"advanced"); boolean isAdvanced = (advanced.equals("yes")); String collectionId = "" ; if (searchAge.equals("")) searchAge = "ALL"; String seekUrl =""; //String qtStr = ""; if (searchSection.equals(NEWS)) collectionId = "khrserch"; if (searchSection.equals(ALLSECTION)) { collectionId = "khparent+khteen+khkid+khrserch"; if (isAdvanced) collectionId= "khparent+khteen+khkid+khrserch"; } if (searchSection.equals(PARENTS)) { collectionId = "khparent+khteen+khkid+khrserch"; if (isAdvanced) collectionId = "khparent+khrserch"; } if (searchSection.equals(TEENS)) collectionId = "khteen"; if (searchSection.equals(KIDS)) collectionId = "khkid"; boolean isHitDB = false; if (searchSection.equals(PARENTS) && !searchAge.equals("ALL")) isHitDB = true; if (searchSection.equals(ALLSECTION) && !searchAge.equals("ALL")) isHitDB = true; int totalResults = 0; int totalDBResults = 0; if (DEBUG) { out.println("mode: " +mode); out.println("
"); out.println("search text: " + searchText); out.println("
"); out.println("next result: " + nextSeekUrl); out.println("
"); out.println("prev start: " + prevStart); } if (DEBUG) { //out.println("seekUrl: " + seekUrl); //out.println("
"); } int displayCtr = 0; try { if(isHitDB){ java.lang.Class.forName(dbDriver); con = java.sql.DriverManager.getConnection(dbConnectString,dbUser,dbPass); st = con.createStatement(); } // add all ages to radioSb //radioSb.append(" All ages
\n"); //out.println("
"); %> <% if(!mode.toLowerCase().equals("init")) { %> <% } %> <% out.println(" "); if (mode.toLowerCase().equals("init")) { java.lang.Class.forName(dbDriver); con = java.sql.DriverManager.getConnection(dbConnectString,dbUser,dbPass); st = con.createStatement(); rs = st.executeQuery("SELECT * FROM age_groups ORDER BY sort"); // add all ages to radioSb radioSb.append(" All ages
\n"); while (rs.next()) { String code = OtherUtils.noNull(rs.getString("code")); String shortDesc = OtherUtils.noNull(rs.getString("short_description")); String longDesc = OtherUtils.noNull(rs.getString("long_description")); if (longDesc.toLowerCase().indexOf("pregnancy") != -1) continue; // don't show pregnancy radioSb.append(" " + longDesc + "
\n"); } st.close(); %>

Search Results

<%@ include file="rightcolumncontentheader.html" %>

Please enter a search term.

<%@ include file="footer.html" %> <% con.close(); return; } else { //out.println(" "); out.println(""); out.println(""); } PreparedStatement ps1 = null; PreparedStatement ps2 = null; if(con!=null){ ps1 = con.prepareStatement("SELECT a.article_id,a.section_id,a.url,a.title, b.code FROM articles a, article_age_group b, age_groups c WHERE a.article_id=b.article_id AND a.status_code='LIV' AND b.code=c.code AND b.code= ? AND ((LOWER(a.url)||'.html') = ? OR LOWER(a.url) = ? ) AND a.section_id= ? " ); ps2 = con.prepareStatement("SELECT a.article_id,a.section_id,a.url,a.title FROM articles a WHERE a.status_code='LIV' AND ((LOWER(a.url)||'.html') = ? OR LOWER(a.url) = ? ) AND a.section_id = ? " ); } StringBuffer parentSb = new StringBuffer("
Articles for parents
\n"); StringBuffer teenSb = new StringBuffer("
Articles for teens
\n"); StringBuffer kidSb = new StringBuffer("
TEST THIS
\n"); int parentCtr=0,teenCtr=0,kidCtr=0; StringBuffer allSb = new StringBuffer(); String parentIcon="", teenIcon="", kidIcon=""; //commented out by satish /**st = con.createStatement(); rs = st.executeQuery("SELECT * FROM sections"); while (rs.next()) { String sectionName = OtherUtils.noNull(rs.getString("section_name")); String iconName = OtherUtils.noNull(rs.getString("icon_name")); if (sectionName.toLowerCase().indexOf("parent") != -1) parentIcon = iconName; if (sectionName.toLowerCase().indexOf("teen") != -1) teenIcon = iconName; if (sectionName.toLowerCase().indexOf("kid") != -1) kidIcon = iconName; } st.close();**/ parentIcon = "parentarticle_icon.gif"; teenIcon = "teenarticle_icon.gif"; kidIcon = "kidarticle_icon.gif"; int number = 0,lowResult=0,highResult=0; String nextResults=""; String results=""; boolean ok = true; if (DEBUG) { out.println("initial displayedTotal: " + displayedTotal); out.println("
searchAge: " + searchAge); out.println("
searchSection: " + searchSection); } int firstHit = 0; firstHit = hitStartInt; // final int MAXCHARS = 3; // final String QTCHARS[][] = {{"+","%2B"}, // {" ","%20"}, // {"\"","%22"}}; //Satish Modified for search case-insensitivity //String qtStr = searchText.replace('\n',' ').replace('\r',' '); String qtStr = searchText.replace('\n',' ').replace('\r',' ').toLowerCase(); /***** int ix = 0; int loopGuard2 = 0; for (int k=0; k 99) break; int pos = qtStr.indexOf(QTCHARS[k][0],ix); if (pos != -1) { StringBuffer sb = new StringBuffer(qtStr); sb.replace(pos,pos+1,QTCHARS[k][1]); qtStr = sb.toString(); ix = pos; } else ix++; } catch (Exception e) {} } // while } // for *****/ out.println(" "); if (isPrev) { //nextSeekUrl = seekUrl = "http://"+request.getHeader("host")+":"+ultraSeekPort+"/saquery.spy?qt="+qtStr.trim()+"&nh=10&col="+collectionId+"&st="+prevStart; nextSeekUrl = seekUrl = "http://"+ultraSeekHost+":"+ultraSeekPort+"/saquery.spy?qt="+URLEncoder.encode(qtStr.trim(), "UTF-8")+"&nh=10&col=khclass&st="+prevStart; } if (!nextSeekUrl.equals("")) seekUrl = nextSeekUrl; else { if (isHitDB) { //seekUrl = "http://"+request.getHeader("host")+":"+ultraSeekPort+"/saquery.spy?qt="+qtStr.trim()+"&nh=999999&col="+collectionId+"&st=1"; seekUrl = "http://"+ultraSeekHost+":"+ultraSeekPort+"/saquery.spy?qt="+URLEncoder.encode(qtStr.trim(), "UTF-8")+"&nh=999999&col=khclass&st=1"; } else { //seekUrl = "http://"+request.getHeader("host")+":"+ultraSeekPort+"/saquery.spy?qt="+qtStr.trim()+"&nh=10&col="+collectionId+"&st=1"; seekUrl = "http://"+ultraSeekHost+":"+ultraSeekPort+"/saquery.spy?qt="+URLEncoder.encode(qtStr.trim(), "UTF-8")+"&nh=10&col=khclass&st=1"; } } //out.println(seekUrl); if (DEBUG) { out.println("seekUrl: " + seekUrl); //out.println("
searchAge: " + searchAge); //out.println("
searchSection: " + searchSection); out.println("
"); } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { URL url = new URL (seekUrl); InputStream content = (InputStream)url.getContent(); //BufferedReader in = new BufferedReader (new InputStreamReader (content,"iso-8859-1")); BufferedReader in = new BufferedReader (new InputStreamReader (content,"utf-8")); String line; while ((line = in.readLine()) != null) { pw.println (line); pw.println("
"); } content.close(); } catch (MalformedURLException e) { pw.println ("Invalid URL"); } catch (IOException e) { pw.println ("Error reading URL"); } pw.close(); hitStartInt++; number=0; nextResults=""; StringReader sr = new StringReader(sw.toString()); BufferedReader br = new BufferedReader(sr); String line = ""; String sub = ""; String prevQt = ""; try { number = Integer.valueOf(getResultVal(sw,"number:")).intValue(); } catch (NumberFormatException nfe) { if (DEBUG) { out.println("

number format exc converting number

"); } number = 0; } String urlStr = ""; String urlFile = ""; String htmlFile = ""; URL url ; File file; results = ""; if (number!=0) { totalResults = number; nextResults = getResultVal(sw,"next results:"); results = getResultVal(sw,"results:"); if (!results.equals("")) { StringTokenizer stok = new StringTokenizer(results,"-"); String loRange = stok.nextToken().trim(); String hiRange = stok.nextToken().trim(); try { lowResult = Integer.parseInt(loRange); highResult = Integer.parseInt(hiRange); } catch (NumberFormatException nfe) { lowResult=0;highResult=0; } if (DEBUG) { out.println("loRange: " + loRange); out.println("
hiRange: " + hiRange); out.println("
"); } } sr = new StringReader(sw.toString()); br = new BufferedReader(sr); //boolean isParent,isTeen,isKid,isResearch,isBreaking; boolean isParent,isTeen,isKid; boolean isInAgeGroup;; while ((line=br.readLine()) != null) { isParent=false; isKid=false; isTeen=false;isInAgeGroup=false; // parse url if (line.trim().length() >= "URL:".length()) { sub = line.substring(0,"url:".length()); if (sub.toLowerCase().equals("url:")) { urlStr = line.substring("url:".length()).trim(); // do not include printer-version html //if (urlStr.toLowerCase().indexOf("_prt") != -1) break; //do not include index.html //if (urlStr.toLowerCase().equals("index.html") ) break; //boolean isDup = resultsAL.contains(urlStr); //if (!isDup) { resultsAL.add(urlStr); } //else break; url = new URL(urlStr); urlFile = url.getFile(); file = new File(urlFile); String fileParent = file.getParent(); if (DEBUG) { out.println("
getParent: " + fileParent); out.println("
url: " + urlStr); } int sectionId = 0; if (fileParent!=null) { try { isParent = (fileParent.toLowerCase().substring(1,"parent".length()+1)).equals("parent"); if (isParent) sectionId = 1; } catch (Exception e) { isParent=false; } try { isTeen = (fileParent.toLowerCase().substring(1,"teen".length()+1)).equals("teen"); if (isTeen) sectionId = 2; } catch (Exception e) { isTeen=false; } try { isKid = (fileParent.toLowerCase().substring(1,"kid".length()+1)).equals("kid"); if (isKid) sectionId = 3; } catch (Exception e) { isKid =false; } } //else { isParent = true; } htmlFile = file.getName().trim().toLowerCase(); if (isHitDB) { if (!searchAge.equals("ALL")) { ps1.clearParameters(); ps1.setString(1,searchAge); ps1.setString(2,htmlFile); ps1.setString(3,htmlFile); ps1.setInt(4,sectionId); rs = ps1.executeQuery(); } else { ps2.clearParameters(); ps2.setString(1,htmlFile); ps2.setString(2,htmlFile); ps2.setInt(3,sectionId); rs = ps2.executeQuery(); } if (rs.next()) { isInAgeGroup = true; totalDBResults++; } else isInAgeGroup = false; rs.close(); } else isInAgeGroup = true; if (isInAgeGroup ) { String title = "", summary=""; while ((line=br.readLine()) != null) { title = getResultVal(line,"title:"); if (!title.equals("")) break; if (title.equals("") && isFound(line,"title:")) break; } while ((line=br.readLine()) != null) { summary = getResultVal(line,"summary:"); if (!summary.equals("")) break; if (summary.equals("") && isFound(line,"summary:")) break; } //////////////Displays results displayCtr++; //String icon = "misc_icon.gif"; String icon = "Nbutton.gif"; if(isParent) icon = parentIcon; if(isTeen) icon = teenIcon; if(isKid) icon = kidIcon; //////////////Actual Results allSb.append("
  • "+(icon.equals("")?"":"") + "" +StrUtils.htmlStrForSearchResults(title)+"" + (isHitDB?totalDBResults:lowResult++)+"
    "); allSb.append("" + StrUtils.htmlStrForSearchResults(noLongStrings(summary,95))+ "
  • "); if (DEBUG) out.println("
    Found! " + displayCtr + ""); ok = true; if (DEBUG) { out.println("
    "+file.getParent()+"-");out.println(urlFile);out.println(htmlFile); out.println("
    "); if (isParent) out.println("is parent
    "); if (isTeen) out.println(" is teen
    "); if (isKid) out.println(" is kid
    "); } // if debug } // isinagegroup } } // if line } // while if (number == highResult) ok = false; } // if number!=0 else ok=false; sw.close(); sr.close(); if (DEBUG) { out.println("
    number of articles found: " + number); out.println("
    results: " + results); out.println("
    next results: " + nextResults); out.println("
    hi result: " + highResult); out.println("
    "); } if(con!=null){ ps1.close(); ps2.close(); con.close(); } if (isHitDB) totalResults = totalDBResults; String prevLink=""; String nextLink=""; if (DEBUG) { out.println("displayctr: " + displayCtr); out.println("
    displayedTotal: " + displayedTotal); out.println("
    firstHit: " + firstHit); } if (displayCtr > 0 || displayedTotal !=0) { if (displayCtr==0 && displayedTotal!=0) { allSb.append("
    KidsHealth.org"); allSb.append("
    KidsHealth.org homepage
    "); } //////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////RESULTS START HERE///////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// %>

    Search Results

    <% int lowRes = lowResult - 1; if (isHitDB) lowRes = totalResults; out.println("
    (" + Integer.toString(firstHit) + " to " + Integer.toString(lowRes) + " of " + totalResults + ")" + "
    "); %> <%@ include file="rightcolumncontentheader.html" %>
      <% //out.println("   
      "); out.println(allSb.toString()); //out.println("
      "); /////////////////////////////////////////// // displays results // /////////////////////////////////////////// //out.println("\n"); //out.println("
      "); // int toHit = firstHit; //out.println("
      "); //out.println("
      "); //out.println("
      "); //out.println(""+totalResults+" results found"); boolean hasPrevious = false; if (firstHit > MAXRESULTS) { //out.println("|"); //out.println("\"Previous\"  Previous 10 results"); String href = "Search02.jsp?Mode=Prev&PrevStart="+(firstHit-MAXRESULTS); out.println(""); hasPrevious = true; } if (!nextResults.equals("")) { out.println(""); //out.println(""); String href= "Search02.jsp?Mode=Next&NextStart="+(highResult+1); if (hasPrevious) out.println(""); int remaining = number - highResult; if (remaining > MAXRESULTS) remaining = MAXRESULTS; // Next xx Results out.println(""); } out.println("
      "); } // if displayedtotal else { //out.println("

      "); %>

      Search Results

      <%@ include file="rightcolumncontentheader.html" %> <% out.println("

      Sorry, no results were found.

      "); } } catch(SQLException e) { out.println("SQL Exception" + "
      "); out.println(e + "
      "); out.println("#" + e.getErrorCode()); out.println("
      Driver==" + dbDriver + "
      "); out.println("Connect String==" + dbConnectString + "
      "); } catch(ClassNotFoundException e) { out.println("Class Not Found Exception" + "
      "); out.println(e); } catch(NullPointerException e) { out.println("Null Pointer Exception" + "
      "); out.println(e); } %>
    <%@ include file="footer.html" %>