Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #224 from MITLibraries/play_24
Browse files Browse the repository at this point in the history
Play 24
  • Loading branch information
Richard Rodgers committed Jun 24, 2015
2 parents 6f9d645 + ecaccdb commit 8ee6325
Show file tree
Hide file tree
Showing 50 changed files with 210 additions and 206 deletions.
5 changes: 3 additions & 2 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import play.api.libs.json._
import play.api.mvc._
import play.api.mvc.Security._
import play.api.Play.current
import play.api.i18n.Messages.Implicits._

import models._
import services.contentModelJson._
Expand Down Expand Up @@ -101,7 +102,7 @@ object Application extends Controller with Security {
if (cancel) {
sub.subscriptionFor(topic.id).map(sc => { sc.cancel; sc.unlinkInterest } )
// remove backing interest if non-template
sub.interestWithValue(topic.scheme.get.tag, topic.tag).map(i => Interest.delete(i.id))
sub.interestWithValue(topic.scheme.tag, topic.tag).map(i => Interest.delete(i.id))
} else {
conveyor ! sub.subscribeTo(topic)
}
Expand Down Expand Up @@ -1059,7 +1060,7 @@ object Application extends Controller with Security {
def removeSubscriberInterest(sid: Int, iid: Int) = Action { implicit request =>
Subscriber.findById(sid).map( sub =>
Interest.findById(iid).map( interest => {
sub.removeInterest(interest.scheme.get, interest.intValue)
sub.removeInterest(interest.scheme, interest.intValue)
Redirect(routes.Application.interestBrowse("scheme", interest.schemeTag))
}).getOrElse(NotFound(views.html.static.trouble("No such interest: " + iid)))
).getOrElse(NotFound(views.html.static.trouble("No such subscriber: " + sid)))
Expand Down
3 changes: 1 addition & 2 deletions app/models/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import play.api._
import play.api.Play.current

import anorm.SqlParser._
import anorm.~
import anorm.SQL
import anorm.{SQL, ~}

/** Channel contains coordinates and credentials to effect a content transfer
* or notification between a hub and a specified address or endpoint of a subscriber
Expand Down
33 changes: 15 additions & 18 deletions app/models/Interest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ import play.api.db.DB
import play.api.Play.current

import anorm.SqlParser._
import anorm.~
import anorm.SQL
import anorm.{SQL, ~}

/** Interest represents a subscriber intent toward a value in a namespace (scheme).
* The value may be exact, or represent a 'template' or pattern that
* describes a value space.
* describes a range of values.
*
* @author richardrodgers
*/

case class Interest(id: Int, subscriberId: Int, schemeTag: String,
intValue: String, template: Boolean, created: Date) {
require(schemeTag != null && intValue != null)

def subscriber = {
DB.withConnection { implicit c =>
SQL("select * from subscriber where id = {subscriber_id}")
.on('subscriber_id -> subscriberId).as(Subscriber.sub.singleOpt)
.on('subscriber_id -> subscriberId).as(Subscriber.sub.single)
}
}

def scheme = {
DB.withConnection { implicit c =>
SQL("select * from scheme where tag = {scheme_tag}")
.on('scheme_tag -> schemeTag).as(Scheme.scheme.singleOpt)
.on('scheme_tag -> schemeTag).as(Scheme.scheme.single)
}
}
}
Expand Down Expand Up @@ -64,9 +64,8 @@ object Interest {
// of schemes... which would always be 1 as we pass in the Scheme to count.
def schemeCount(subscriberId: Int, schemeTag: String) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from interest where interest.subscriber_id = {sub_id} and interest.scheme_tag = {sch_tag}")
.on('sub_id -> subscriberId, 'sch_tag -> schemeTag).apply.head
count[Long]("c")
SQL("select count(*) from interest where interest.subscriber_id = {sub_id} and interest.scheme_tag = {sch_tag}")
.on('sub_id -> subscriberId, 'sch_tag -> schemeTag).as(scalar[Long].single)
}
}

Expand All @@ -88,16 +87,15 @@ object Interest {
// of schemes... which would always be 1 as we pass in the Scheme to count.
def planCount(subscriberId: Int, planId: Int) = {
DB.withConnection { implicit c =>
val count = SQL(
SQL(
"""
select count(interest.*) as c from interest, scheme, plan_scheme
select count(interest.*) from interest, scheme, plan_scheme
where interest.subscriber_id = {sub_id}
and interest.scheme_tag = scheme.tag
and plan_scheme.scheme_id = scheme.id
and plan_scheme.plan_id = {plan_id}
"""
).on('sub_id -> subscriberId, 'plan_id -> planId).apply.head
count[Long]("c")
).on('sub_id -> subscriberId, 'plan_id -> planId).as(scalar[Long].single)
}
}

Expand All @@ -122,21 +120,20 @@ object Interest {
// of schemes... which would always be 1 as we pass in the Scheme to count.
def matchCount(subscriberId: Int, mType: String) = {
DB.withConnection { implicit c =>
val count = if (mType == "sub") SQL(
if (mType == "sub") SQL(
"""
select count(*) as c from interest
select count(*) from interest
where subscriber_id = {sub_id}
and exists (select 1 from interest_subscription where interest_id = interest.id)
"""
).on('sub_id -> subscriberId).apply.head
).on('sub_id -> subscriberId).as(scalar[Long].single)
else SQL(
"""
select count(*) as c from interest
select count(*) from interest
where subscriber_id = {sub_id}
and not exists (select 1 from interest_subscription where interest_id = interest.id)
"""
).on('sub_id -> subscriberId).apply.head
count[Long]("c")
).on('sub_id -> subscriberId).as(scalar[Long].single)
}
}

Expand Down
33 changes: 11 additions & 22 deletions app/models/Item.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import play.api._
import play.api.Play.current

import anorm.SqlParser._
import anorm.~
import anorm.SQL
import anorm.Row
import anorm.{Row, SQL, ~}

/** Item represents a distinct content aggregation, typically containing
* a prinary artifcat, and metadata or other auxillary files. While opaque
Expand All @@ -33,9 +31,8 @@ case class Item(id: Int, // DB key

def hasTopic(topic: Topic): Boolean = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from item_topic where topic_id = {topic_id} and item_id = {item_id}")
.on('topic_id -> topic.id, 'item_id -> id).apply.head
count[Long]("c") > 0
SQL("select count(*) from item_topic where topic_id = {topic_id} and item_id = {item_id}")
.on('topic_id -> topic.id, 'item_id -> id).as(scalar[Long].single) > 0
}
}

Expand Down Expand Up @@ -65,7 +62,7 @@ case class Item(id: Int, // DB key
def contentType = {
DB.withConnection { implicit c =>
SQL("select * from content_type where id = {ctype_id}")
.on('ctype_id -> ctypeId).as(ContentType.ctype.singleOpt)
.on('ctype_id -> ctypeId).as(ContentType.ctype.single)
}
}

Expand All @@ -79,28 +76,21 @@ case class Item(id: Int, // DB key
def metadataValue(mdname: String) = {
DB.withConnection { implicit c =>
SQL("select mdvalue from metadata where item_id = {item_id} and mdname = {mdname}")
.on('item_id -> id, 'mdname -> mdname).apply().headOption match {
case Some(x) => x[String]("mdvalue")
case None => "Unknown Value"
}
.on('item_id -> id, 'mdname -> mdname).as(scalar[String] *).headOption.getOrElse("Unknown Value")
}
}

def hasMetadata(mdname: String) = {
DB.withConnection { implicit c =>
SQL("select mdvalue from metadata where item_id = {item_id} and mdname = {mdname}")
.on('item_id -> id, 'mdname -> mdname).apply().headOption match {
case Some(x) => true
case None => false
}
.on('item_id -> id, 'mdname -> mdname).as(scalar[String] *).headOption.isDefined
}
}

def metadataValues(mdname: String) = {
DB.withConnection { implicit c =>
val rows = SQL("select mdvalue from metadata where item_id = {item_id} and mdname = {mdname}")
.on('item_id -> id, 'mdname -> mdname)
rows().map(row => row[String]("mdvalue")).toList
SQL("select mdvalue from metadata where item_id = {item_id} and mdname = {mdname}")
.on('item_id -> id, 'mdname -> mdname).as(scalar[String] *)
}
}

Expand Down Expand Up @@ -316,8 +306,7 @@ object Item {

def collectionCount(coll_id: Int) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from item where collection_id = {id}").on('id -> coll_id).apply.head
count[Long]("c")
SQL("select count(*) from item where collection_id = {id}").on('id -> coll_id).as(scalar[Long].single)
}
}

Expand All @@ -331,8 +320,8 @@ object Item {

def deleteBefore(date: Date) {
DB.withConnection { implicit c =>
val rows = SQL("select id from item where created <= {created}").on('created -> date)
rows().map(row => row[Int]("id")).toList.foreach(delete)
SQL("select id from item where created <= {created}").on('created -> date).as(scalar[Int] *).foreach(delete)
// NB: stream-based approach may be preferable
}
}
}
16 changes: 5 additions & 11 deletions app/models/Publisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,25 @@ case class Publisher(id: Int, // DB key
link: Option[String], // Optional URL to publisher site
logo: Option[String], // Optional URL to publisher logo
created: Date) {
require(tag != null && name != null && description != null)

def collectionCount = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from collection where publisher_id = {id}").on('id -> id).apply.head
count[Long]("c")
SQL("select count(*) from collection where publisher_id = {id}").on('id -> id).as(scalar[Long].single)
}
}

def itemCount = {
DB.withConnection { implicit c =>
if (collectionCount > 0L) {
val sum = SQL("select sum(deposits) as sm from collection where publisher_id = {id}").on('id -> id).apply.head
sum match {
case sum: Row => sum[Long]("sm")
case _ => 0
}
SQL("select sum(deposits) from collection where publisher_id = {id}").on('id -> id).as(scalar[Long].single)
} else 0
}
}

def harvestCount = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from harvest where publisher_id = {id}").on('id -> id).apply.head
count[Long]("c")
SQL("select count(*) from harvest where publisher_id = {id}").on('id -> id).as(scalar[Long].single)
}
}
}
Expand Down Expand Up @@ -96,8 +91,7 @@ object Publisher {

def categoryCount(category: String) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from publisher where category = {category}").on('category -> category).apply.head
count[Long]("c")
SQL("select count(*) from publisher where category = {category}").on('category -> category).as(scalar[Long].single)
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/models/Scheme.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ case class Scheme(id: Int, tag: String, gentype: String, category: String, descr

def topicCount = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from topic where scheme_id = {id}").on('id -> id).apply.head
count[Long]("c")
SQL("select count(*) as c from topic where scheme_id = {id}").on('id -> id).as(scalar[Long].single)
}
}

Expand Down
21 changes: 8 additions & 13 deletions app/models/Subscriber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import play.api._
import play.api.Play.current

import anorm.SqlParser._
import anorm.~
import anorm.SQL
import anorm.Row
import anorm.{Row, SQL, ~}

/** Subscriber is a consumer of content on a hub, where consumption may
* range from email notifications, SWORD delivery of packages, etc
Expand Down Expand Up @@ -90,9 +88,9 @@ case class Subscriber(id: Int, // DB key

def newItemCountFor(topicId: Int) = {
DB.withConnection { implicit c =>
val count = SQL(
SQL(
"""
select count(*) as c from item_topic
select count(*) from item_topic
where topic_id = {topic_id}
and item_created > {created}
and not exists (
Expand All @@ -101,8 +99,7 @@ case class Subscriber(id: Int, // DB key
and item_id = item_topic.item_id
)
"""
).on('topic_id -> topicId, 'created -> created, 'sub_id -> id).apply.head
count[Long]("c")
).on('topic_id -> topicId, 'created -> created, 'sub_id -> id).as(scalar[Long].single)
}
}

Expand Down Expand Up @@ -140,8 +137,7 @@ case class Subscriber(id: Int, // DB key

def holdCount = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from hold where subscriber_id = {id}").on('id -> id).apply.head
count[Long]("c")
SQL("select count(*) from hold where subscriber_id = {id}").on('id -> id).as(scalar[Long].single)
}
}

Expand All @@ -167,8 +163,7 @@ case class Subscriber(id: Int, // DB key

def pickCount = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from topic_pick where subscriber_id = {id}").on('id -> id).apply.head
count[Long]("c")
SQL("select count(*) from topic_pick where subscriber_id = {id}").on('id -> id).as(scalar[Long].single)
}
}

Expand Down Expand Up @@ -405,8 +400,8 @@ object Subscriber {

def categoryCount(category: String) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from subscriber where category = {category}").on('category -> category).apply.head
count[Long]("c")
SQL("select count(*) from subscriber where category = {category}")
.on('category -> category).as(scalar[Long].single)
}
}

Expand Down
12 changes: 5 additions & 7 deletions app/models/Subscription.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ case class Subscription(id: Int, // DB key

def topic = {
DB.withConnection { implicit c =>
SQL("select * from topic where id = {topic_id}").on('topic_id -> topicId).as(Topic.topic.singleOpt)
SQL("select * from topic where id = {topic_id}").on('topic_id -> topicId).as(Topic.topic.single)
}
}

def subscriber = {
DB.withConnection { implicit c =>
SQL("select * from subscriber where id = {sid}").on('sid -> subscriberId).as(Subscriber.sub.singleOpt)
SQL("select * from subscriber where id = {sid}").on('sid -> subscriberId).as(Subscriber.sub.single)
}
}

def transferCount = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from transfer where subscription_id = {id}").on('id -> id).apply.head
count[Long]("c")
SQL("select count(*) from transfer where subscription_id = {id}").on('id -> id).as(scalar[Long].single)
}
}

Expand Down Expand Up @@ -105,9 +104,8 @@ object Subscription {
// of schemes... which would always be 1 as we pass in the Scheme to count.
def schemeCount(subscriberId: Int, schemeId: Int) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from subscription, topic where subscription.subscriber_id = {sub_id} and subscription.topic_id = topic.id and topic.scheme_id = {sch_id}")
.on('sub_id -> subscriberId, 'sch_id -> schemeId).apply.head
count[Long]("c")
SQL("select count(*) from subscription, topic where subscription.subscriber_id = {sub_id} and subscription.topic_id = topic.id and topic.scheme_id = {sch_id}")
.on('sub_id -> subscriberId, 'sch_id -> schemeId).as(scalar[Long].single)
}
}

Expand Down
Loading

0 comments on commit 8ee6325

Please sign in to comment.