PageRenderTime 155ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/com/mycotrack/snippet/ManageSpecies.scala

http://github.com/JanxSpirit/mycotrack_mongo
Scala | 169 lines | 141 code | 22 blank | 6 comment | 4 complexity | 4800ffe1fbea13f7077d0434e60fc460 MD5 | raw file
  1. /*
  2. * User: gregg
  3. * Date: 3/26/11
  4. * Time: 1:32 PM
  5. */
  6. package com.mycotrack.snippet
  7. import _root_.net.liftweb.util.Helpers
  8. import Helpers._
  9. import net.liftweb.http.SHtml._
  10. import com.mycotrack.model.{Species, User}
  11. import org.bson.types.ObjectId
  12. import net.liftweb.common.Box._
  13. import xml.{Group, NodeSeq, Text}
  14. import com.mycotrack.db.MycoMongoDb
  15. import net.liftweb.http._
  16. import net.liftweb.common.{Full, Box, Empty}
  17. import com.mongodb.casbah.Imports._
  18. import java.awt.image.BufferedImage
  19. import javax.imageio.ImageIO
  20. import com.thebuzzmedia.imgscalr.Scalr
  21. import java.io.ByteArrayOutputStream
  22. import java.io.ByteArrayInputStream
  23. class ManageSpecies {
  24. def add(xhtml: NodeSeq): NodeSeq = {
  25. val species = Species.createRecord
  26. var scientificName = species.scientificName.is
  27. var commonName = species.commonName.is
  28. var infoUrl = species.infoUrl.is
  29. Helpers.bind("species", xhtml,
  30. "scientificName" -> SHtml.text(scientificName, scientificName = _),
  31. "commonName" -> SHtml.text(commonName, commonName = _),
  32. "infoUrl" -> SHtml.text(infoUrl, infoUrl = _),
  33. "submit" -> SHtml.submit("Save", () => {
  34. species.scientificName.set(scientificName)
  35. species.commonName.set(commonName)
  36. species.infoUrl.set(infoUrl)
  37. species.save
  38. S.redirectTo("/manageSpecies")
  39. }))
  40. }
  41. def info(xhtml: NodeSeq): NodeSeq = {
  42. val species = theSpecies.get.open_!
  43. var scientificName = species.scientificName.is
  44. var commonName = species.commonName.is
  45. var infoUrl = species.infoUrl.is
  46. Helpers.bind("species", xhtml,
  47. "scientificName" -> scientificName,
  48. "commonName" -> commonName,
  49. "infoUrl" -> infoUrl
  50. )
  51. }
  52. def list(xhtml: NodeSeq): NodeSeq = {
  53. Helpers.bind("species", xhtml,
  54. "allSpecies" -> {
  55. for (species <- Species.findAll) yield <li>
  56. <a href={"speciesInfo/" + species.id.toString}>
  57. {species.commonName.get}
  58. </a>
  59. </li>
  60. }
  61. )
  62. }
  63. def imageUpload(xhtml: Group): NodeSeq = {
  64. val species = theSpecies.get.open_!
  65. if (S.get_?) bind("ul", chooseTemplate("choose", "get", xhtml),
  66. "file_upload" -> fileUpload(ul => {
  67. theUpload(Full(ul))
  68. //resize image
  69. val orig = ImageIO.read(ul.fileStream)
  70. val thumbnail = Scalr.resize(Scalr.resize(orig, Scalr.Mode.FIT_TO_WIDTH, 125, 75), Scalr.Mode.FIT_TO_HEIGHT, 125, 75)
  71. val display = Scalr.resize(Scalr.resize(orig, Scalr.Mode.FIT_TO_WIDTH, 500, 300), Scalr.Mode.FIT_TO_HEIGHT, 500, 300)
  72. MycoMongoDb.gridFs(ul.fileStream) {
  73. fh =>
  74. fh.metaData.put("species_id", species.id)
  75. fh.metaData.put("size", "original")
  76. fh.filename = ul.name
  77. }
  78. val bos = new ByteArrayOutputStream
  79. ImageIO.write(thumbnail, "gif", bos)
  80. MycoMongoDb.gridFs(new ByteArrayInputStream(bos.toByteArray)) {
  81. fh =>
  82. fh.metaData.put("species_id", species.id)
  83. fh.metaData.put("size", "thumbnail")
  84. fh.filename = ul.name
  85. }
  86. val bos_disp = new ByteArrayOutputStream
  87. ImageIO.write(display, "gif", bos_disp)
  88. MycoMongoDb.gridFs(new ByteArrayInputStream(bos_disp.toByteArray)) {
  89. fh =>
  90. fh.metaData.put("species_id", species.id)
  91. fh.metaData.put("size", "display")
  92. fh.filename = ul.name
  93. }
  94. }))
  95. else bind("ul", chooseTemplate("choose", "post", xhtml),
  96. "scientific_name" -> Text(species.scientificName.get),
  97. "common_name" -> Text(species.commonName.get),
  98. "file_name" -> theUpload.is.map(v => Text(v.fileName)),
  99. "mime_type" -> theUpload.is.map(v => Box.legacyNullTest(v.mimeType).map(Text).openOr(Text("No mime type supplied"))), // Text(v.mimeType)),
  100. "length" -> theUpload.is.map(v => Text(v.file.length.toString)),
  101. "md5" -> theUpload.is.map(v => Text(hexEncode(md5(v.file))))
  102. );
  103. }
  104. def speciesImage(xhtml: NodeSeq): NodeSeq = {
  105. val species = theSpecies.get.open_!
  106. Helpers.bind("species", xhtml,
  107. "image" -> {
  108. val photo = MycoMongoDb.gridFs.findOne(MongoDBObject("species_id" -> species.id))
  109. photo match {
  110. case None => Text("No photo found")
  111. case Some(photo) => {
  112. val url = "/images/%s" format photo.id
  113. <img src={url}/>
  114. }
  115. }
  116. }
  117. )
  118. }
  119. def speciesImages(xhtml: NodeSeq): NodeSeq = {
  120. val species = theSpecies.get.open_!
  121. Helpers.bind("species", xhtml,
  122. "image" -> {
  123. println(species.id)
  124. val files = MycoMongoDb.gridFs.find(MongoDBObject("species_id" -> species.id)).map(_.get("filename")).distinct
  125. println(files)
  126. for (fn <- files) yield {
  127. println(species.id)
  128. println(fn)
  129. val thumbnail_id = MycoMongoDb.gridFs.findOne(MongoDBObject("filename" -> fn, "species_id" -> species.id, "size" -> "thumbnail")).map(_.id).getOrElse("404")
  130. val original_id = MycoMongoDb.gridFs.findOne(MongoDBObject("filename" -> fn, "species_id" -> species.id, "size" -> "original")).map(_.id).getOrElse("404")
  131. val display_id = MycoMongoDb.gridFs.findOne(MongoDBObject("filename" -> fn, "species_id" -> species.id, "size" -> "display")).map(_.id).getOrElse("404")
  132. val thumbnail_url = "/images/%s" format thumbnail_id
  133. val display_url = "/images/%s" format display_id
  134. <li>
  135. <h3>{species.commonName.get}</h3>
  136. <span>{display_url}</span>
  137. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ut urna. Mauris nulla. Donec nec mauris. Proin nulla dolor, bibendum et, dapibus in, euismod ut, felis.</p>
  138. <a href="#"><img src={thumbnail_url} alt={species.commonName.get} /></a>
  139. </li>
  140. }
  141. }
  142. )
  143. }
  144. private object theUpload extends RequestVar[Box[FileParamHolder]](Empty)
  145. }
  146. object theSpecies extends RequestVar[Box[Species]](Empty)
  147. case class SpeciesImage(original: String, thumbnail: String, display: String)