19
19
20
20
package org .commonwl .viewer .web ;
21
21
22
+ import org .apache .commons .lang .StringUtils ;
22
23
import org .commonwl .viewer .domain .GithubDetails ;
23
24
import org .commonwl .viewer .domain .Workflow ;
24
25
import org .commonwl .viewer .domain .WorkflowForm ;
32
33
import org .springframework .stereotype .Controller ;
33
34
import org .springframework .validation .BindingResult ;
34
35
import org .springframework .web .bind .annotation .*;
36
+ import org .springframework .web .servlet .HandlerMapping ;
35
37
import org .springframework .web .servlet .ModelAndView ;
36
38
39
+ import javax .servlet .http .HttpServletRequest ;
37
40
import javax .servlet .http .HttpServletResponse ;
38
41
import javax .validation .Valid ;
39
42
import java .io .File ;
@@ -79,36 +82,30 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
79
82
// Go back to index if there are validation errors
80
83
return new ModelAndView ("index" );
81
84
} else {
82
- // The ID of the workflow to be redirected to
83
- String workflowID ;
84
-
85
85
// 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 ) {
88
88
logger .info ("Fetching existing workflow from DB" );
89
-
90
- // Get the ID from the existing workflow
91
- workflowID = existingWorkflow .getID ();
92
89
} else {
93
90
// New workflow from Github URL
94
- Workflow newWorkflow = workflowFactory .workflowFromGithub (githubInfo );
91
+ workflow = workflowFactory .workflowFromGithub (githubInfo );
95
92
96
93
// Runtime error
97
- if (newWorkflow == null ) {
94
+ if (workflow == null ) {
98
95
bindingResult .rejectValue ("githubURL" , "githubURL.parsingError" );
99
96
return new ModelAndView ("index" );
100
97
}
101
98
102
99
// Save to the MongoDB database
103
100
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 );
108
102
}
109
103
110
104
// 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 ());
112
109
}
113
110
}
114
111
@@ -118,7 +115,7 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
118
115
* @return The workflow view with the workflow as a model
119
116
*/
120
117
@ RequestMapping (value ="/workflows/{workflowID}" )
121
- public ModelAndView getWorkflow (@ PathVariable String workflowID ){
118
+ public ModelAndView getWorkflowByID (@ PathVariable String workflowID ){
122
119
123
120
// Get workflow from database
124
121
Workflow workflowModel = workflowRepository .findOne (workflowID );
@@ -133,6 +130,41 @@ public ModelAndView getWorkflow(@PathVariable String workflowID){
133
130
134
131
}
135
132
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
+
136
168
/**
137
169
* Download the Research Object Bundle for a particular workflow
138
170
* @param workflowID The ID of the workflow to download
0 commit comments