PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/dataset/app/controllers/organization/DataSets.scala

https://github.com/delving/culture-hub
Scala | 80 lines | 64 code | 10 blank | 6 comment | 4 complexity | 9d5a172533823db49c51746e699d3f8d MD5 | raw file
  1. package controllers.organization
  2. import play.api.mvc.{ WebSocket, Action }
  3. import models.DataSet
  4. import play.api.i18n.Messages
  5. import com.mongodb.casbah.Imports._
  6. import controllers.{ Token, OrganizationController }
  7. import java.util.regex.Pattern
  8. import play.api.libs.json.{ JsString, JsValue }
  9. import play.api.libs.concurrent.Promise
  10. import play.api.libs.iteratee.{ Concurrent, Enumerator, Done, Input }
  11. import util.OrganizationConfigurationHandler
  12. import com.escalatesoft.subcut.inject.BindingModule
  13. /**
  14. *
  15. * @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
  16. */
  17. class DataSets(implicit val bindingModule: BindingModule) extends OrganizationController {
  18. def list = OrganizationMember {
  19. MultitenantAction {
  20. implicit request =>
  21. render {
  22. case Accepts.Html() => Ok(Template('title -> listPageTitle("dataset"), 'canAdministrate -> DataSet.dao.canAdministrate(connectedUser)))
  23. case Accepts.Json() =>
  24. val sets = DataSet.dao.findAll()
  25. val smallSets = sets.map { set =>
  26. Map("key" -> set.spec, "name" -> set.details.name)
  27. }
  28. Json(smallSets)
  29. }
  30. }
  31. }
  32. def dataSet(spec: String) = OrganizationMember {
  33. MultitenantAction {
  34. implicit request =>
  35. val maybeDataSet = DataSet.dao.findBySpecAndOrgId(spec, configuration.orgId)
  36. if (maybeDataSet.isEmpty) {
  37. NotFound(Messages("dataset.DatasetWasNotFound", spec))
  38. } else {
  39. val ds = maybeDataSet.get
  40. Ok(Template('spec -> ds.spec))
  41. }
  42. }
  43. }
  44. def feed(clientId: String, spec: Option[String]) = WebSocket.async[JsValue] { implicit request =>
  45. if (request.session.get("userName").isDefined) {
  46. OrganizationConfigurationHandler.getByDomain(request.domain) map { implicit configuration =>
  47. DataSetEventFeed.subscribe(configuration.orgId, clientId, session.get("userName").get, configuration.orgId, spec)
  48. } getOrElse {
  49. Promise.pure((Done[JsValue, JsValue](JsString(""), Input.Empty), Concurrent.broadcast._1))
  50. }
  51. } else {
  52. // return a fake pair
  53. // TODO perhaps a better way here ?
  54. Promise.pure((Done[JsValue, JsValue](JsString(""), Input.Empty), Concurrent.broadcast._1))
  55. }
  56. }
  57. def listAsTokens(q: String, maybeFormats: Option[String]) = Root {
  58. MultitenantAction {
  59. implicit request =>
  60. val formats = maybeFormats.map(_.split(",").toSeq.map(_.trim).filterNot(_.isEmpty)).getOrElse(Seq.empty)
  61. val query = MongoDBObject("spec" -> Pattern.compile(q, Pattern.CASE_INSENSITIVE))
  62. val sets = DataSet.dao.find(query).filter { set =>
  63. formats.isEmpty ||
  64. formats.toSet.subsetOf(set.getPublishableMappingSchemas.map(_.getPrefix).toSet)
  65. }
  66. val asTokens = sets.map(set => Token(set.spec, set.spec)).toList
  67. Json(asTokens)
  68. }
  69. }
  70. }