Skip to content

Commit ad93b89

Browse files
authored
Merge pull request #33 from common-workflow-language/permalinks
Permalinks
2 parents 63cd35a + 75881df commit ad93b89

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

src/main/java/org/commonwl/viewer/domain/WorkflowForm.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
public class WorkflowForm {
2727

28+
public WorkflowForm() {}
29+
30+
public WorkflowForm(String githubURL) {
31+
this.githubURL = githubURL;
32+
}
33+
2834
private String githubURL;
2935

3036
public String getGithubURL() {

src/main/java/org/commonwl/viewer/web/PageController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.stereotype.Controller;
2424
import org.springframework.ui.Model;
2525
import org.springframework.web.bind.annotation.GetMapping;
26+
import org.springframework.web.bind.annotation.RequestParam;
2627

2728
@Controller
2829
public class PageController {
@@ -33,8 +34,8 @@ public class PageController {
3334
* @return The view for this page
3435
*/
3536
@GetMapping("/")
36-
public String homePage(Model model) {
37-
model.addAttribute("workflowForm", new WorkflowForm());
37+
public String homePage(Model model, @RequestParam(value = "url", required = false) String defaultURL) {
38+
model.addAttribute("workflowForm", new WorkflowForm(defaultURL));
3839
return "index";
3940
}
4041

src/main/java/org/commonwl/viewer/web/WorkflowController.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.commonwl.viewer.web;
2121

22+
import org.apache.commons.lang.StringUtils;
2223
import org.commonwl.viewer.domain.GithubDetails;
2324
import org.commonwl.viewer.domain.Workflow;
2425
import org.commonwl.viewer.domain.WorkflowForm;
@@ -32,8 +33,10 @@
3233
import org.springframework.stereotype.Controller;
3334
import org.springframework.validation.BindingResult;
3435
import org.springframework.web.bind.annotation.*;
36+
import org.springframework.web.servlet.HandlerMapping;
3537
import org.springframework.web.servlet.ModelAndView;
3638

39+
import javax.servlet.http.HttpServletRequest;
3740
import javax.servlet.http.HttpServletResponse;
3841
import javax.validation.Valid;
3942
import java.io.File;
@@ -79,36 +82,30 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
7982
// Go back to index if there are validation errors
8083
return new ModelAndView("index");
8184
} else {
82-
// The ID of the workflow to be redirected to
83-
String workflowID;
84-
8585
// Check database for existing workflow
86-
Workflow existingWorkflow = workflowRepository.findByRetrievedFrom(githubInfo);
87-
if (existingWorkflow != null) {
86+
Workflow workflow = workflowRepository.findByRetrievedFrom(githubInfo);
87+
if (workflow != null) {
8888
logger.info("Fetching existing workflow from DB");
89-
90-
// Get the ID from the existing workflow
91-
workflowID = existingWorkflow.getID();
9289
} else {
9390
// New workflow from Github URL
94-
Workflow newWorkflow = workflowFactory.workflowFromGithub(githubInfo);
91+
workflow = workflowFactory.workflowFromGithub(githubInfo);
9592

9693
// Runtime error
97-
if (newWorkflow == null) {
94+
if (workflow == null) {
9895
bindingResult.rejectValue("githubURL", "githubURL.parsingError");
9996
return new ModelAndView("index");
10097
}
10198

10299
// Save to the MongoDB database
103100
logger.info("Adding new workflow to DB");
104-
workflowRepository.save(newWorkflow);
105-
106-
// Get the ID from the new workflow
107-
workflowID = newWorkflow.getID();
101+
workflowRepository.save(workflow);
108102
}
109103

110104
// Redirect to the workflow
111-
return new ModelAndView("redirect:/workflows/" + workflowID);
105+
GithubDetails githubDetails = workflow.getRetrievedFrom();
106+
return new ModelAndView("redirect:/workflows/github.com/" + githubDetails.getOwner()
107+
+ "/" + githubDetails.getRepoName() + "/tree/" + githubDetails.getBranch()
108+
+ "/" + githubDetails.getPath());
112109
}
113110
}
114111

@@ -118,7 +115,7 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
118115
* @return The workflow view with the workflow as a model
119116
*/
120117
@RequestMapping(value="/workflows/{workflowID}")
121-
public ModelAndView getWorkflow(@PathVariable String workflowID){
118+
public ModelAndView getWorkflowByID(@PathVariable String workflowID){
122119

123120
// Get workflow from database
124121
Workflow workflowModel = workflowRepository.findOne(workflowID);
@@ -133,6 +130,41 @@ public ModelAndView getWorkflow(@PathVariable String workflowID){
133130

134131
}
135132

133+
/**
134+
* Display a page for a particular workflow from Github details
135+
* @param owner The owner of the Github repository
136+
* @param repoName The name of the repository
137+
* @param branch The branch of repository
138+
* @return The workflow view with the workflow as a model
139+
*/
140+
@RequestMapping(value="/workflows/github.com/{owner}/{repoName}/tree/{branch}/**")
141+
public ModelAndView getWorkflowByGithubDetails(@PathVariable("owner") String owner,
142+
@PathVariable("repoName") String repoName,
143+
@PathVariable("branch") String branch,
144+
HttpServletRequest request) {
145+
146+
// The wildcard end of the URL is the path
147+
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
148+
int pathStartIndex = StringUtils.ordinalIndexOf(path, "/", 7) + 1;
149+
path = path.substring(pathStartIndex);
150+
151+
// Construct a GithubDetails object to search for in the database
152+
GithubDetails githubDetails = new GithubDetails(owner, repoName, branch, path);
153+
154+
// Get workflow from database
155+
Workflow workflowModel = workflowRepository.findByRetrievedFrom(githubDetails);
156+
157+
// Redirect to index with form autofilled if workflow does not already exist
158+
if (workflowModel == null) {
159+
return new ModelAndView("redirect:/?url=https://github.com/" +
160+
owner + "/" + repoName + "/tree/" + branch + "/" + path);
161+
}
162+
163+
// Display this model along with the view
164+
return new ModelAndView("workflow", "workflow", workflowModel);
165+
166+
}
167+
136168
/**
137169
* Download the Research Object Bundle for a particular workflow
138170
* @param workflowID The ID of the workflow to download

src/main/resources/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h2>Workflow from Github URL</h2>
5050
<strong>Error:</strong> <span th:errors="*{githubURL}">Error message here</span>
5151
</div>
5252
<div class="input-group">
53-
<input type="text" class="form-control" placeholder="Github URL" id="githubURL" name="githubURL" th:field="*{githubURL}" />
53+
<input type="text" class="form-control" placeholder="Github URL" id="githubURL" name="githubURL" th:field="*{githubURL}" th:value="${formURL}" />
5454
<span class="input-group-btn">
5555
<button class="btn btn-primary" type="submit">Parse Workflow</button>
5656
</span>

src/main/resources/templates/workflow.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ <h4 class="modal-title" id="dotGraphLabel">Workflow DOT Graph</h4>
9898

9999
<div class="container">
100100
<div class="row">
101-
<div class="col-md-12" role="main" id="main">
101+
<div class="col-md-12" role="main" id="main" th:with="workflowURL=@{'github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.retrievedFrom.branch} + '/' + ${workflow.retrievedFrom.path}}">
102102
<h2>Workflow: <span th:text="${workflow.label}">Workflow Name</span></h2>
103103
<p>
104-
<a th:href="@{'https://github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.retrievedFrom.branch} + '/' + ${workflow.retrievedFrom.path}}" href="#" rel="noopener" target="_blank">
104+
<a th:href="@{'https://' + ${workflowURL}}" href="#" rel="noopener" target="_blank">
105105
<img id="githubLogo" src="../static/img/GitHub-Mark-32px.png" th:src="@{/img/GitHub-Mark-32px.png}" width="24" height="24" />
106106
</a>
107107
<i>Fetched <span th:text="${{workflow.retrievedOn}}">01/12/16 at 21:00</span></i>

0 commit comments

Comments
 (0)