Permalink
Browse files

content api query

  • Loading branch information...
1 parent f391fc6 commit c1ed1e6de39e9c09038cc74808596cc218b56b3e Sheena Luu committed Feb 3, 2012
View
@@ -32,6 +32,3 @@ libraryDependencies ++= Seq(
// and use this version of jetty for jetty run
libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "container"
-//scalateTemplateDirectories in Compile <<= (baseDirectory) {
-// (basedir) => Seq(new File(basedir, "/src/main/webapp/templates"))
-//}
@@ -2,8 +2,13 @@ package com.gu.personalization
package dispatcher
import org.scalatra.{ScalatraKernel, ScalatraFilter}
+import com.gu.openplatform.contentapi.Api
+import scala.collection.JavaConversions._
+import com.gu.openplatform.contentapi.model.Content
+import com.gu.openplatform.contentapi.connection.JavaNetHttp
+import org.scalatra.scalate.ScalateSupport
-class Dispatcher extends ScalatraFilter with ScalatraKernel with Logging {
+class Dispatcher extends ScalatraFilter with ScalateSupport with Logging {
get("/"){
"HELLO WORLD"
@@ -12,9 +17,64 @@ class Dispatcher extends ScalatraFilter with ScalatraKernel with Logging {
post("/saveProfile") {
val userId = params.getOrElse("userId", throw new Exception("missing userid"))
- val terms = params.getOrElse("terms","")
+ val terms = multiParams.get("terms").getOrElse(Seq()).mkString(",").toLowerCase
model.Personalization.save(userId, terms)
}
+
+ get("/personalization/:userId"){
+ val userId = params.getOrElse("userId", throw new Exception("missing userid"))
+ val terms = model.Personalization.get(userId).split(",")
+ try{
+ val tags = terms.map(ApiClient.getTagId(_)).mkString("|")
+ val content = ApiClient.searchApi(tags)
+ val renderParams = Map("content" -> content, "userid" -> userId)
+ render("templates/personalizedPage", renderParams)
+ // renderTemplate("templates/personalizedPage.ssp", "content" -> content, "userid" -> userId)
+
+ }catch{
+ case e => {
+ log.error(e.getMessage)
+ log.error(e.getStackTraceString)
+ terms
+ }
+ }
+ }
+
+
+ def render(file: String, renderParams: Map[String, Any] = Map(), contentTypeHeader: String = "text/html;charset=UTF-8", cacheMaxAge: Int = 0) = {
+ response.setHeader("Cache-Control", "public, max-age=%d" format cacheMaxAge)
+ contentType = contentTypeHeader
+ //templateEngine.layout("/WEB-INF/scalate/%s.ssp" format file, renderParams)
+ val htmlString = "<html><head><title></title></head><body>" +
+ renderParams.get("content").get.asInstanceOf[List[com.gu.openplatform.contentapi.model.Content]].map(_.webTitle).mkString(" <br> ") +
+ "</body></html>"
+ htmlString
+ }
+
}
+
+object ApiClient extends Api with JavaNetHttp with Logging {
+
+ apiKey = Some("techdev-internal")
+
+ def searchApi(tagstring : String) : List[Content] = {
+
+ log.info(tagstring)
+ val apiRequest = Api.search
+ .tag(tagstring)
+ .showFields("all")
+
+ apiRequest.results
+
+ }
+
+ def getTagId(term : String) : String = {
+ val apiRequest = Api.tags
+ .tagType(term)
+ val firstResult : Option[com.gu.openplatform.contentapi.model.Tag] = apiRequest.results.headOption
+ firstResult.map(_.id).getOrElse("")
+ }
+
+}
@@ -2,24 +2,37 @@ package com.gu.personalization
package model
import com.google.appengine.api.datastore._
+import scala.collection.JavaConversions._
object Personalization {
val datastore = DatastoreServiceFactory.getDatastoreService();
- def save(userProfileId: String, terms: String) {
-
- val profile = new Entity("Profile")
- profile.setProperty("userid", userProfileId)
- profile.setProperty("terms", terms)
- datastore.put(profile)
- }
- def get(userProfileId: String) : String = {
+ private def getEntity(userProfileId : String) : Option[Entity] = {
val q = new Query("Profile");
q.addFilter("userid", Query.FilterOperator.EQUAL, userProfileId);
val pq = datastore.prepare(q);
- pq.asSingleEntity.getProperty("terms").asInstanceOf[String]
+ pq.asIterable.headOption
}
+ def save(userProfileId: String, terms: String) {
+ val entityOption = getEntity(userProfileId)
+ if (entityOption.isEmpty){
+ val profile = new Entity("Profile")
+ profile.setProperty("userid", userProfileId)
+ profile.setProperty("terms", terms)
+ datastore.put(profile)
+ }else{
+ val profile = entityOption.get
+ profile.setProperty("terms", terms)
+ datastore.put(profile)
+ }
+ }
+
+ def get(userProfileId: String) : String = {
+ getEntity(userProfileId).map(_.getProperty("terms").asInstanceOf[String]).getOrElse("")
+ }
+
+
}
@@ -0,0 +1,14 @@
+<%@ val userid : String %>
+<%@ val content: List[com.gu.openplatform.contentapi.model.Content] %>
+
+<html>
+<head>
+<title>Personalized page for ${userid}</title>
+</head>
+<body>
+#for (c <- content)
+ ${c.webTitle}
+ <img src="${c.thumbnail}" />
+#end
+</body>
+</html>
@@ -0,0 +1,14 @@
+<%@ val userid : String %>
+<%@ val content: List[com.gu.openplatform.contentapi.model.Content] %>
+
+<html>
+<head>
+<title>Personalized page for ${userid}</title>
+</head>
+<body>
+#for (c <- content)
+ ${c.webTitle}
+ <img src="${c.thumbnail}" />
+#end
+</body>
+</html>

0 comments on commit c1ed1e6

Please sign in to comment.