2222import org .commonwl .viewer .domain .GithubDetails ;
2323import org .commonwl .viewer .domain .Workflow ;
2424import org .commonwl .viewer .domain .cwl .CWLCollection ;
25+ import org .commonwl .viewer .web .ROBundleNotFoundException ;
2526import org .slf4j .Logger ;
2627import org .slf4j .LoggerFactory ;
2728import org .springframework .beans .factory .annotation .Autowired ;
2829import org .springframework .beans .factory .annotation .Value ;
30+ import org .springframework .data .domain .Page ;
31+ import org .springframework .data .domain .Pageable ;
2932import org .springframework .stereotype .Service ;
3033
3134import java .io .File ;
@@ -62,13 +65,91 @@ public WorkflowService(GitHubService githubService,
6265 this .totalFileSizeLimit = totalFileSizeLimit ;
6366 }
6467
68+ /**
69+ * Gets a page of all workflows from the database
70+ * @param pageable The details of the page to be requested
71+ * @return The resulting page of the workflow entries
72+ */
73+ public Page <Workflow > getPageOfWorkflows (Pageable pageable ) {
74+ return workflowRepository .findAllByOrderByRetrievedOnDesc (pageable );
75+ }
76+
77+ /**
78+ * Get a workflow from the database by its ID
79+ * @param id The ID of the workflow
80+ * @return The model for the workflow
81+ */
82+ public Workflow getWorkflow (String id ) {
83+ return workflowRepository .findOne (id );
84+ }
85+
86+ /**
87+ * Get a workflow from the database, refreshing it if cache has expired
88+ * @param githubInfo Github information for the workflow
89+ * @return The workflow model associated with githubInfo
90+ */
91+ public Workflow getWorkflow (GithubDetails githubInfo ) {
92+ // Check database for existing workflow
93+ Workflow workflow = workflowRepository .findByRetrievedFrom (githubInfo );
94+
95+ // Create a new workflow if we do not have one already
96+ if (workflow != null ) {
97+ // Delete the existing workflow if the cache has expired
98+ if (cacheExpired (workflow )) {
99+ // Update by trying to add a new workflow
100+ Workflow newWorkflow = createWorkflow (workflow .getRetrievedFrom ());
101+
102+ // Only replace workflow if it could be successfully parsed
103+ if (newWorkflow == null ) {
104+ logger .error ("Could not parse updated workflow " + workflow .getID ());
105+ } else {
106+ // Delete the existing workflow
107+ removeWorkflow (workflow );
108+
109+ // Save new workflow
110+ workflowRepository .save (newWorkflow );
111+ workflow = newWorkflow ;
112+ }
113+ }
114+ }
115+
116+ return workflow ;
117+ }
118+
119+ /**
120+ * Get the RO bundle for a Workflow, triggering re-download if it does not exist
121+ * @param id The ID of the workflow
122+ * @return The file containing the RO bundle
123+ * @throws ROBundleNotFoundException If the RO bundle was not found
124+ */
125+ public File getROBundle (String id ) throws ROBundleNotFoundException {
126+ // Get workflow from database
127+ Workflow workflow = getWorkflow (id );
128+
129+ // If workflow does not exist or the bundle doesn't yet
130+ if (workflow == null || workflow .getRoBundle () == null ) {
131+ throw new ROBundleNotFoundException ();
132+ }
133+
134+ // 404 error with retry if the file on disk does not exist
135+ File bundleDownload = new File (workflow .getRoBundle ());
136+ if (!bundleDownload .exists ()) {
137+ // Clear current RO bundle link and create a new one (async)
138+ workflow .setRoBundle (null );
139+ workflowRepository .save (workflow );
140+ generateROBundle (workflow );
141+ throw new ROBundleNotFoundException ();
142+ }
143+
144+ return bundleDownload ;
145+ }
146+
65147 /**
66148 * Builds a new workflow from cwl files fetched from Github
67149 * @param githubInfo Github information for the workflow
68150 * @return The constructed model for the Workflow
69151 */
70- public Workflow newWorkflowFromGithub (GithubDetails githubInfo ) {
71-
152+ public Workflow createWorkflow (GithubDetails githubInfo ) {
72153 try {
73154 // Get the sha hash from a branch reference
74155 String latestCommit = githubService .getCommitSha (githubInfo );
@@ -89,9 +170,11 @@ public Workflow newWorkflowFromGithub(GithubDetails githubInfo) {
89170 // This is Async so cannot just call constructor, needs intermediate as per Spring framework
90171 generateROBundle (workflowModel );
91172
173+ // Save to database
174+ workflowRepository .save (workflowModel );
175+
92176 // Return this model to be displayed
93177 return workflowModel ;
94-
95178 } else {
96179 logger .error ("No workflow could be found" );
97180 }
@@ -106,7 +189,7 @@ public Workflow newWorkflowFromGithub(GithubDetails githubInfo) {
106189 * Generates the RO bundle for a Workflow and adds it to the model
107190 * @param workflow The workflow model to create a Research Object for
108191 */
109- public void generateROBundle (Workflow workflow ) {
192+ private void generateROBundle (Workflow workflow ) {
110193 try {
111194 ROBundleFactory .workflowROFromGithub (githubService , workflow .getRetrievedFrom (), workflow .getLastCommit ());
112195 } catch (Exception ex ) {
@@ -118,7 +201,7 @@ public void generateROBundle(Workflow workflow) {
118201 * Removes a workflow and its research object bundle
119202 * @param workflow The workflow to be deleted
120203 */
121- public void removeWorkflow (Workflow workflow ) {
204+ private void removeWorkflow (Workflow workflow ) {
122205 // Delete the Research Object Bundle from disk
123206 File roBundle = new File (workflow .getRoBundle ());
124207 if (roBundle .delete ()) {
@@ -139,7 +222,7 @@ public void removeWorkflow(Workflow workflow) {
139222 * @param workflow The cached workflow model
140223 * @return Whether or not there are new commits
141224 */
142- public boolean cacheExpired (Workflow workflow ) {
225+ private boolean cacheExpired (Workflow workflow ) {
143226 try {
144227 // Calculate expiration
145228 Calendar expireCal = Calendar .getInstance ();
0 commit comments