Permalink
Browse files

Quick hacky service to create IODP projects

  • Loading branch information...
1 parent 690f9e2 commit fa6c79e4e3e8dd3fb6933a337402d484666ee108 @joshareed committed Mar 8, 2012
@@ -1,6 +1,7 @@
class UrlMappings {
static mappings = {
+ "/projects/$type/$id"(controller: 'project', action: 'index')
"/$controller/$action?/$id?" {
constraints {
// apply constraints here
@@ -0,0 +1,21 @@
+package coreref.services
+
+import grails.converters.JSON
+
+class ProjectController {
+ def grailsApplication
+
+ def index(String type, String id) {
+ response.setHeader("Access-Control-Allow-Origin", "*");
+ response.setHeader("Access-Control-Allow-Methods", request.getHeader("Access-Control-Request-Methods"));
+ response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
+ response.setHeader("Access-Control-Max-Age", "86400");
+
+ if (grailsApplication.mainContext.containsBean("${type}ProjectService")) {
+ def projectService = grailsApplication.mainContext.getBean("${type}ProjectService")
+ render projectService.getProject(id) as JSON
+ } else {
+ response.sendError 400, "Invalid project type: ${type}"
+ }
+ }
+}
@@ -0,0 +1,62 @@
+package coreref.services
+
+class IodpProjectService {
+
+ def getProject(String id) {
+ String site = id[0..-2]
+ String hole = id[-1]
+ def project = [
+ id: "iodp-${site}${hole}",
+ name: "IODP ${site}${hole}",
+ description: "TODO: get this from somewhere",
+ metadata: [
+ program: "iodp",
+ expedition: "TODO: get this from somewhere",
+ site: site,
+ hole: hole,
+ latitude: "TODO",
+ longitude: "TODO",
+ 'start-date': "TODO",
+ 'end-date': "TODO",
+ 'water-depth': "TODO",
+ 'terminal-depth': "TODO"
+ ],
+ "streams": [
+ [ name: "Split Core Images", type: "http://coreref.org/types/image/split-core", data: getSplitCoreImages(site, hole) ]
+ ]
+ ]
+ }
+
+ private getSplitCoreImages(String site, String hole) {
+ query("""\
+ PREFIX iodp: <http://data.oceandrilling.org/core/1/>
+ SELECT DISTINCT ?sourceURL ?top ?length
+ FROM <http://data.oceandrilling.org/coreimages/>
+ WHERE {
+ ?uri iodp:site "${site}" .
+ ?uri iodp:hole "${hole}" .
+ ?uri dc:source ?sourceURL .
+ ?uri iodp:sectiontop ?top .
+ ?uri iodp:curatedlength ?length .
+ FILTER regex(?sourceURL, "_gy.tif" )
+ }"""
+ ).collect { row ->
+ def (url, top, length) = [row[0][1..-2], row[1] as BigDecimal, row[2] as BigDecimal]
+ [url: url, top: top, base: (top + length)]
+ }
+ }
+
+ private query(sparql) {
+ def endpoint = new URL("http://data.oceandrilling.org/sparql?query=${sparql.encodeAsURL()}")
+ def connection = endpoint.openConnection()
+ connection.addRequestProperty("Accept", "text/csv")
+ connection.connect()
+ def data = []
+ boolean first = true
+ connection.inputStream.text.eachLine { line ->
+ if (!first) { data << line.split(",") }
+ first = false
+ }
+ data
+ }
+}
@@ -0,0 +1,12 @@
+package coreref.services
+
+import grails.test.mixin.*
+import org.junit.*
+
+@TestFor(IodpProjectService)
+class IodpProjectServiceTests {
+
+ void testSomething() {
+ fail "Implement me"
+ }
+}
@@ -0,0 +1,17 @@
+package coreref.services
+
+
+
+import grails.test.mixin.*
+import org.junit.*
+
+/**
+ * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
+ */
+@TestFor(ProjectController)
+class ProjectControllerTests {
+
+ void testSomething() {
+ fail "Implement me"
+ }
+}

0 comments on commit fa6c79e

Please sign in to comment.