Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

content api query

  • Loading branch information...
commit c1ed1e6de39e9c09038cc74808596cc218b56b3e 1 parent f391fc6
authored February 03, 2012
3  build.sbt
@@ -32,6 +32,3 @@ libraryDependencies ++= Seq(
32 32
 // and use this version of jetty for jetty run
33 33
 libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "container"
34 34
 
35  
-//scalateTemplateDirectories in Compile <<= (baseDirectory) {
36  
-//  (basedir) => Seq(new File(basedir, "/src/main/webapp/templates"))
37  
-//}
64  src/main/scala/com/gu/personalization/dispatcher/dispatcher.scala
@@ -2,8 +2,13 @@ package com.gu.personalization
2 2
 package dispatcher
3 3
 
4 4
 import org.scalatra.{ScalatraKernel, ScalatraFilter}
  5
+import com.gu.openplatform.contentapi.Api
  6
+import scala.collection.JavaConversions._
  7
+import com.gu.openplatform.contentapi.model.Content
  8
+import com.gu.openplatform.contentapi.connection.JavaNetHttp
  9
+import org.scalatra.scalate.ScalateSupport
5 10
 
6  
-class Dispatcher extends ScalatraFilter with ScalatraKernel with Logging {
  11
+class Dispatcher extends ScalatraFilter with ScalateSupport with Logging {
7 12
 
8 13
   get("/"){
9 14
     "HELLO WORLD"
@@ -12,9 +17,64 @@ class Dispatcher extends ScalatraFilter with ScalatraKernel with Logging {
12 17
 
13 18
   post("/saveProfile") {
14 19
     val userId = params.getOrElse("userId", throw new Exception("missing userid"))
15  
-    val terms = params.getOrElse("terms","")
  20
+    val terms = multiParams.get("terms").getOrElse(Seq()).mkString(",").toLowerCase
16 21
 
17 22
     model.Personalization.save(userId, terms)
18 23
   }
  24
+
  25
+  get("/personalization/:userId"){
  26
+    val userId = params.getOrElse("userId", throw new Exception("missing userid"))
  27
+    val terms = model.Personalization.get(userId).split(",")
  28
+    try{
  29
+      val tags = terms.map(ApiClient.getTagId(_)).mkString("|")
  30
+      val content = ApiClient.searchApi(tags)
  31
+      val renderParams = Map("content" -> content, "userid" -> userId)
  32
+      render("templates/personalizedPage", renderParams)
  33
+      // renderTemplate("templates/personalizedPage.ssp", "content" -> content, "userid" -> userId)
  34
+
  35
+    }catch{
  36
+      case e => {
  37
+        log.error(e.getMessage)
  38
+        log.error(e.getStackTraceString)
  39
+        terms
  40
+      }
  41
+    }
  42
+  }
  43
+
  44
+
  45
+  def render(file: String, renderParams: Map[String, Any] = Map(), contentTypeHeader: String = "text/html;charset=UTF-8", cacheMaxAge: Int = 0) = {
  46
+    response.setHeader("Cache-Control", "public, max-age=%d" format cacheMaxAge)
  47
+    contentType = contentTypeHeader
  48
+    //templateEngine.layout("/WEB-INF/scalate/%s.ssp" format file, renderParams)
  49
+    val htmlString = "<html><head><title></title></head><body>" +
  50
+      renderParams.get("content").get.asInstanceOf[List[com.gu.openplatform.contentapi.model.Content]].map(_.webTitle).mkString(" <br> ") +
  51
+      "</body></html>"
  52
+    htmlString
  53
+  }
  54
+
19 55
 }
20 56
 
  57
+
  58
+object ApiClient extends Api with JavaNetHttp with Logging  {
  59
+
  60
+  apiKey = Some("techdev-internal")
  61
+
  62
+  def searchApi(tagstring : String) : List[Content]  = {
  63
+
  64
+    log.info(tagstring)
  65
+    val apiRequest = Api.search
  66
+                      .tag(tagstring)
  67
+                      .showFields("all")
  68
+
  69
+    apiRequest.results
  70
+
  71
+  }
  72
+
  73
+  def getTagId(term : String) : String = {
  74
+    val apiRequest = Api.tags
  75
+                        .tagType(term)
  76
+    val firstResult : Option[com.gu.openplatform.contentapi.model.Tag] = apiRequest.results.headOption
  77
+    firstResult.map(_.id).getOrElse("")
  78
+  }
  79
+
  80
+}
31  src/main/scala/com/gu/personalization/model/Personalization.scala
@@ -2,24 +2,37 @@ package com.gu.personalization
2 2
 package model
3 3
 
4 4
 import com.google.appengine.api.datastore._
  5
+import scala.collection.JavaConversions._
5 6
 
6 7
 object Personalization {
7 8
 
8 9
   val datastore = DatastoreServiceFactory.getDatastoreService();
9 10
 
10  
-  def save(userProfileId: String,  terms: String) {
11  
-
12  
-    val profile = new Entity("Profile")
13  
-    profile.setProperty("userid", userProfileId)
14  
-    profile.setProperty("terms", terms)
15  
-    datastore.put(profile)
16  
-  }
17 11
 
18  
-  def get(userProfileId: String) : String = {
  12
+  private def getEntity(userProfileId : String) : Option[Entity] = {
19 13
     val q = new Query("Profile");
20 14
     q.addFilter("userid", Query.FilterOperator.EQUAL, userProfileId);
21 15
     val pq = datastore.prepare(q);
22  
-    pq.asSingleEntity.getProperty("terms").asInstanceOf[String]
  16
+    pq.asIterable.headOption
23 17
   }
24 18
 
  19
+  def save(userProfileId: String,  terms: String) {
  20
+    val entityOption = getEntity(userProfileId)
  21
+    if (entityOption.isEmpty){
  22
+      val profile = new Entity("Profile")
  23
+      profile.setProperty("userid", userProfileId)
  24
+      profile.setProperty("terms", terms)
  25
+      datastore.put(profile)
  26
+    }else{
  27
+      val profile = entityOption.get
  28
+      profile.setProperty("terms", terms)
  29
+      datastore.put(profile)
  30
+    }
  31
+  }
  32
+
  33
+  def get(userProfileId: String) : String = {
  34
+    getEntity(userProfileId).map(_.getProperty("terms").asInstanceOf[String]).getOrElse("")
  35
+  }
  36
+
  37
+
25 38
 }
14  src/main/webapp/WEB-INF/scalate/templates/personalizedPage.ssp
... ...
@@ -0,0 +1,14 @@
  1
+<%@ val userid : String %>
  2
+<%@ val content: List[com.gu.openplatform.contentapi.model.Content] %>
  3
+
  4
+<html>
  5
+<head>
  6
+<title>Personalized page for ${userid}</title>
  7
+</head>
  8
+<body>
  9
+#for (c <- content)
  10
+    ${c.webTitle}
  11
+    <img src="${c.thumbnail}" />
  12
+#end
  13
+</body>
  14
+</html>
14  src/main/webapp/WEB-INF/src/WEB-INF/scalate/templates/personalizedPage.ssp
... ...
@@ -0,0 +1,14 @@
  1
+<%@ val userid : String %>
  2
+<%@ val content: List[com.gu.openplatform.contentapi.model.Content] %>
  3
+
  4
+<html>
  5
+<head>
  6
+<title>Personalized page for ${userid}</title>
  7
+</head>
  8
+<body>
  9
+#for (c <- content)
  10
+    ${c.webTitle}
  11
+    <img src="${c.thumbnail}" />
  12
+#end
  13
+</body>
  14
+</html>

0 notes on commit c1ed1e6

Please sign in to comment.
Something went wrong with that request. Please try again.