FEATURE #371 - Implement serverside functionality for Candidate List.

This commit is contained in:
Marco Vermeulen
2015-10-10 23:38:24 +03:00
parent 67238be220
commit ceca6900ea
2 changed files with 72 additions and 15 deletions

View File

@@ -0,0 +1,15 @@
================================================================================
Available Candidates
================================================================================
<%
candidates.each { candidate, detail ->
println detail.header
println ""
println detail.description
println ""
println detail.footer
println "--------------------------------------------------------------------------------"
}
%>

View File

@@ -38,14 +38,13 @@ def config = [
container.deployModule 'vertx.mongo-persistor-v1.2', config
def templateEngine = new SimpleTemplateEngine()
def templateBase = "build/templates"
def listVersionsTemplateFile = "${templateBase}/list_versions.gtpl" as File
def listVersionsTemplate = templateEngine.createTemplate(listVersionsTemplateFile)
def broadcastTemplateFile = "${templateBase}/broadcast.gtpl" as File
def broadcastTemplate = templateEngine.createTemplate(broadcastTemplateFile)
def listCandidatesTemplateFile = "${templateBase}/list_candidates.gtpl" as File
def listCandidatesTemplate = templateEngine.createTemplate(listCandidatesTemplateFile)
//
@@ -96,6 +95,60 @@ rm.get("/candidates") { req ->
}
}
rm.get("/candidates/list") { req ->
def cmd = [action:"find",
collection:"candidates",
matcher:[:],
keys:[candidate:1,
default:1,
description:1,
websiteUrl:1,
name:1]]
vertx.eventBus.send("mongo-persistor", cmd){ msg ->
TreeMap candidates = [:]
msg.body.results.each {
candidates.put(
it.candidate,
[
header: header(it.name, it.default, it.websiteUrl),
description: compose(it.description.tokenize(" ")).join("\n"),
footer: footer(it.candidate)
]
)
}
addPlainTextHeader req
def binding = [candidates: candidates]
def template = listCandidatesTemplate.make(binding)
req.response.end template.toString()
}
}
PARAGRAPH_WIDTH = 80
def header(String name, String version, String websiteUrl) {
int padding = PARAGRAPH_WIDTH - websiteUrl.length()
"${name} (${version})".padRight(padding, " ") + websiteUrl
}
def footer(String candidate) {
"\$ sdk install $candidate".padLeft(PARAGRAPH_WIDTH)
}
def compose(List words) {
def lineWords = [], lineLength = 0, idx = 0
for(word in words) {
idx++
lineLength += word.size() + 1
lineWords << word
if(lineLength > PARAGRAPH_WIDTH){
def lineText = lineWords.take(lineWords.size()-1).join(" ")
def remainingWords = [word] + words.drop(idx)
return [lineText] + compose(remainingWords)
}
}
[words.join(" ")]
}
rm.get("/candidates/:candidate") { req ->
def candidate = req.params['candidate']
def cmd = [action:"find", collection:"versions", matcher:[candidate:candidate], keys:["version":1]]
@@ -235,17 +288,6 @@ def versionHandler = { req ->
rm.get("/app/version", versionHandler)
rm.get("/api/version", versionHandler)
def broadcastHandler = { req ->
addPlainTextHeader req
req.response.end "This Broadcast API is being discontinued. \nPlease upgrade to the latest version of SDKMAN!"
}
rm.get("/broadcast", broadcastHandler)
rm.get("/broadcast/:version", broadcastHandler)
rm.get("/api/broadcast", broadcastHandler)
rm.get("/api/broadcast/:version", broadcastHandler)
//
// private methods
//
@@ -270,7 +312,7 @@ private log(command, candidate, version, req){
date:date
]
def cmd = [action:'save', collection:'audit', document:document]
def cmd = [action:'save', collection:'audit', document:document]
vertx.eventBus.send 'mongo-persistor', cmd
}