Skip to content

Commit

Permalink
Now supports multiple pages when marking snippets in factextract
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Ennals committed May 8, 2009
1 parent 6b75c0e commit a1abb9c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 53 deletions.
6 changes: 2 additions & 4 deletions scala/src/com/intel/thinkscala/MainServlet.scala
Expand Up @@ -80,8 +80,7 @@ class MainServlet extends HttpServlet {
val title = claim("text") + "Find Instances with Think Link"
var query = c.arg("query")
if(query == null) query = claim.str("text")
val bossUrls = SnipSearch.searchBoss(query)
c.outputHtml(title,Page.findsnippets(claim,query,bossUrls))
c.outputHtml(title,Page.findsnippets(claim,query))
}),
UrlHandler("/claim/new",c => {
c.outputHtml("Create New Claim - Think Link",Page.newClaim(c,c.arg("query")))
Expand Down Expand Up @@ -112,8 +111,7 @@ class MainServlet extends HttpServlet {

new UrlHandler("/fragment/snipsearch", c => {
implicit val ctx = c
val bossUrls = SnipSearch.searchBoss(c.arg("query"))
c.outputFragment(flatMapWithIndex(bossUrls,Render.bossUrl(_ : BossUrl,_,c.arg("query"))))
c.outputFragment(Render.snipSearchResults(c.arg("query")))
})
)

Expand Down
7 changes: 4 additions & 3 deletions scala/src/com/intel/thinkscala/SnipSearch.scala
Expand Up @@ -39,13 +39,14 @@ object SnipSearch {
val bossKey = "NpeiOwLV34E5KHWPTxBix1HTRHe4zIj2LfTtyyDKvBdeQHOzlC_RIv4SmAPuBh3E";
val bossSvr = "http://boss.yahooapis.com/ysearch/web/v1";

def searchBoss(claim : String) : Seq[BossUrl] = {
val url = bossSvr + "/"+encode(claim)+"?appid="+bossKey+"&format=xml&abstract=long"
def searchBoss(claim : String, page : Int) : (Int,Seq[BossUrl]) = {
val url = bossSvr + "/"+encode(claim)+"?appid="+bossKey+"&format=xml&abstract=long&start="+(page*10)
val xmltext = download(url)
val parser = ConstructingParser.fromSource(Source.fromString(xmltext),false)
val doc = parser.document
val results = doc \\ "result"
return results map absForResult
val totalhits = doc \\ "resultset_web" \ "@totalhits"
return (Integer.parseInt(totalhits.toString)/10,results map absForResult)
}

def searchYahoo(claim : String) : Seq[SnipUrlRes] = {
Expand Down
27 changes: 22 additions & 5 deletions scala/src/com/intel/thinkscala/UrlHandler.scala
Expand Up @@ -10,17 +10,35 @@ import com.intel.thinkscala._
import scala.xml.NodeSeq;
import com.intel.thinkscala.view.Template
import com.intel.thinkscala.view.Page
import scala.collection.immutable.HashMap
//import scala.collection.Map
// import java.util.Iterator;

class NotFound extends Exception
class NoLogin extends Exception

class ReqContext(val store : Datastore, m : Match, req : HttpServletRequest, res : HttpServletResponse){
class Enum[T](it:java.util.Enumeration[T]) extends Iterator[T]{
def hasNext = it.hasMoreElements
def next = it.nextElement
}

class ReqContext(val store : Datastore, m : Match, req : HttpServletRequest, res : HttpServletResponse, path : String){
def urlInt(i : Int) = Integer.parseInt(m.group(i))
def argInt(name : String) = Integer.parseInt(req.getParameter(name))
def argIntDflt(name : String, dflt : Int) = if(req.getParameter(name) != null) argInt(name) else dflt
def arg(name : String) = req.getParameter(name)
lazy val user = store.getUser(getCookie("email"), getCookie("password"));
def userid = if(user.realuser) user.userid else throw new NoLogin
def maybe_userid = user.userid
lazy val params = getParams

def getParams : Map[String,String] = {
val keys : Iterator[String] = new Enum(req.getParameterNames.asInstanceOf[java.util.Enumeration[String]])
val maps : Iterator[(String,String)] = keys map (key => (key,req.getParameterValues(key)(0)))
return HashMap(maps.collect : _*)
}

def modifiedUrl(key : String, value : Any) = mkUrl("/thinklink"+path,params.update(key,value))

def output(obj : Any) {
res.setContentType("text/html; charset=UTF-8") // TODO: set this correctly
Expand Down Expand Up @@ -50,8 +68,7 @@ class ReqContext(val store : Datastore, m : Match, req : HttpServletRequest, res
def outputFragment(html : NodeSeq){
UrlHandler.outputHtml(res,html,false)
}



def notFound() {
res.setStatus(HttpServletResponse.SC_NOT_FOUND)
outputHtml("Not found",Page.notfound)
Expand Down Expand Up @@ -101,9 +118,9 @@ class UrlHandler(pat : String, func : ReqContext => unit){
def tryForUrl(store : Datastore, path : String,req : HttpServletRequest, res : HttpServletResponse) : Boolean = {
r.findFirstMatchIn(path) match {
case Some(m) =>
val c = new ReqContext(store,m,req,res)
val c = new ReqContext(store,m,req,res,path)
try{
func(new ReqContext(store,m,req,res))
func(new ReqContext(store,m,req,res,path))
}catch{
case e : NotFound => c.notFound
case e : NoLogin => c.needLogin
Expand Down
4 changes: 4 additions & 0 deletions scala/src/com/intel/thinkscala/Util.scala
Expand Up @@ -18,6 +18,10 @@ object Util {
def encode(claim : String) = URLEncoder.encode(claim,"UTF-8")
def decode(claim : String) = URLDecoder.decode(claim,"UTF-8")

def mkUrl(path : String, args : Map[String,Any]) =
path + (args map {case (key,value) => key + "=" + encode(value.toString)}).mkString("?","&","")


def readToString(reader : BufferedReader) : String = {
val buf = new StringBuffer("");
var line = reader.readLine();
Expand Down
4 changes: 2 additions & 2 deletions scala/src/com/intel/thinkscala/view/Page.scala
Expand Up @@ -87,7 +87,7 @@ object Page {
</div>


def findsnippets(row : SqlRow, query : String, bossurls : Seq[BossUrl])(implicit c : ReqContext) =
def findsnippets(row : SqlRow, query : String)(implicit c : ReqContext) =
<div id="findsnippets">
<input type="hidden" id="data-query" value={query}/>
<input type="hidden" id="data-claim" value={""+row("id")}/>
Expand All @@ -98,7 +98,7 @@ object Page {
{searchQueryList(c,row.int("id"))}
</div>
{simpleSearch("snipsearch", Urls.findsnippets(row("id")), query,
flatMapWithIndex(bossurls,(bossUrl(_ : BossUrl,_,query))))
Render.snipSearchResults(query))
}
</div>

Expand Down
48 changes: 9 additions & 39 deletions scala/src/com/intel/thinkscala/view/Render.scala
Expand Up @@ -90,51 +90,21 @@ object Render {
}
</div>
}


def bossResults(query : String,page : Int)(implicit c : ReqContext) : (Int,NodeSeq) = {
val (max,bossUrls) = SnipSearch.searchBoss(query,page)
return (max,Util.flatMapWithIndex(bossUrls,Render.bossUrl(_ : BossUrl,_,query)))
}

def snipSearchResults(query : String)(implicit c : ReqContext) =
Widgets.pagedList(bossResults(query,_))

def topicref(row : SqlRow) =
<a href={Urls.topic(row.int("id"))}>{row("text")}</a>
}


object Widgets {
def greyInput(cls : String, id : String, previewtext : String) =
<input id={id} name={id} class={cls} style="color:grey" onfocus="ungrey(this)" value={previewtext}/>

def action(row : SqlRow, action : String, name : String) =
<a class={"action-"+action} href={"/thinklink/api/action?id="+row("id")}>{name}</a>

def tabs(param : String, options : Array[String], selected : String) =
<div class="tabs">
options map (s => if(s equals selected){
<a class="selected">s</a>
}else{
<a>s</a>
})
</div>

def ajaxSearch(id : String, fragurl : String, initquery : String, content : NodeSeq) =
<div id={id}>
<form onsubmit={"ajaxSearch("+fragurl+","+id+")"}>
<input type="text" class="query" name="query" value={initquery}/>
<input type="submit" class="submit" value="Search"/>
</form>
<div class="ajaxcontent">
{content}
</div>
</div>

def simpleSearch(id : String, url : String, initquery : String, content : NodeSeq) =
<div>
<form id={id} method="GET" action={url}>
<input type="text" class="query" name="query" value={initquery}/>
<input type="submit" class="submit" value="Search"/>
</form>
<div class="searchcontent">
{content}
</div>
</div>

}


object Images {
Expand Down
5 changes: 5 additions & 0 deletions webapps/node/stylesheets/normal.css
Expand Up @@ -373,3 +373,8 @@ h1 {
.snippet-ignored .ignore{
color: black;
}

.pageselector {
text-align: right;
padding: 8px;
}

0 comments on commit a1abb9c

Please sign in to comment.