34
34
import org .eclipse .egit .github .core .User ;
35
35
import org .slf4j .Logger ;
36
36
import org .slf4j .LoggerFactory ;
37
+ import org .springframework .beans .factory .annotation .Autowired ;
38
+ import org .springframework .beans .factory .annotation .Value ;
39
+ import org .springframework .stereotype .Service ;
37
40
38
41
import java .io .IOException ;
39
42
import java .net .URI ;
48
51
import java .util .regex .Pattern ;
49
52
50
53
/**
51
- * Represents a Workflow Research Object Bundle
54
+ * Service handling Research Object Bundles
52
55
*/
53
- public class ROBundle {
56
+ @ Service
57
+ public class ROBundleService {
54
58
55
59
private final Logger logger = LoggerFactory .getLogger (this .getClass ());
56
60
61
+ // Services
57
62
private GitHubService githubService ;
58
63
59
- private Bundle bundle ;
60
- private GithubDetails githubInfo ;
61
- private Agent thisApp ;
64
+ // Configuration variables
65
+ private Agent appAgent ;
62
66
private int singleFileSizeLimit ;
63
- private Set < HashableAgent > authors = new HashSet <>() ;
67
+ private Path bundleStorage ;
64
68
65
69
// Pattern for extracting version from a cwl file
66
70
private final String CWL_VERSION_REGEX = "cwlVersion:\\ s*\" ?(?:cwl:)?([^\\ s\" ]+)\" ?" ;
67
71
private final Pattern cwlVersionPattern = Pattern .compile (CWL_VERSION_REGEX );
68
72
69
73
/**
70
- * Creates a new research object bundle for a workflow from a Github repository
71
- * @param githubService The service for handling Github functionality
72
- * @param githubInfo The information necessary to access the Github directory associated with the RO
74
+ * Creates an instance of this service which handles Research Object Bundles
75
+ * @param bundleStorage The configured storage location for bundles
73
76
* @param appName The name of the application from properties, for attribution
74
77
* @param appURL The URL of the application from properties, for attribution
75
- * @throws IOException Any API errors which may have occurred
78
+ * @param singleFileSizeLimit The file size limit for the RO bundle
79
+ * @param githubService The service for handling Github functionality
80
+ * @throws URISyntaxException Error in creating URI for appURL
76
81
*/
77
- public ROBundle (GitHubService githubService , GithubDetails githubInfo ,
78
- String appName , String appURL , int singleFileSizeLimit ) throws IOException {
79
- // File size limits
82
+ @ Autowired
83
+ public ROBundleService (@ Value ("${bundleStorage}" ) Path bundleStorage ,
84
+ @ Value ("${applicationName}" ) String appName ,
85
+ @ Value ("${applicationURL}" ) String appURL ,
86
+ @ Value ("${singleFileSizeLimit}" ) int singleFileSizeLimit ,
87
+ GitHubService githubService ) throws URISyntaxException {
88
+ this .bundleStorage = bundleStorage ;
89
+ this .appAgent = new Agent (appName );
90
+ appAgent .setUri (new URI (appURL ));
80
91
this .singleFileSizeLimit = singleFileSizeLimit ;
81
-
82
- // Create a new RO bundle
83
- this .bundle = Bundles .createBundle ();
84
- this .githubInfo = githubInfo ;
85
92
this .githubService = githubService ;
93
+ }
94
+
95
+ /**
96
+ * Creates a new research object bundle for a workflow from a Github repository
97
+ * @param githubInfo The information to access the repository
98
+ * @return The constructed bundle
99
+ */
100
+ public Bundle newBundleFromGithub (GithubDetails githubInfo ) throws IOException {
86
101
102
+ // Create a new RO bundle
103
+ Bundle bundle = Bundles .createBundle ();
87
104
Manifest manifest = bundle .getManifest ();
88
105
89
106
// Simplified attribution for RO bundle
90
107
try {
91
108
// Tool attribution in createdBy
92
- thisApp = new Agent (appName );
93
- thisApp .setUri (new URI (appURL ));
94
- manifest .setCreatedBy (thisApp );
109
+ manifest .setCreatedBy (appAgent );
95
110
96
111
// Retrieval Info
97
112
// TODO: Make this importedBy/On/From
98
- manifest .setRetrievedBy (thisApp );
113
+ manifest .setRetrievedBy (appAgent );
99
114
manifest .setRetrievedOn (manifest .getCreatedOn ());
100
115
manifest .setRetrievedFrom (new URI (githubInfo .getURL ()));
101
116
@@ -109,18 +124,40 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo,
109
124
110
125
// Add the files from the Github repo to this workflow
111
126
List <RepositoryContents > repoContents = githubService .getContents (githubInfo );
112
- addFiles (repoContents , bundleFiles );
127
+ Set <HashableAgent > authors = new HashSet <HashableAgent >();
128
+ addFilesToBundle (bundle , githubInfo , repoContents , bundleFiles , authors );
113
129
114
130
// Add combined authors
115
131
manifest .setAuthoredBy (new ArrayList <>(authors ));
132
+
133
+ // Return the completed bundle
134
+ return bundle ;
135
+
136
+ }
137
+
138
+ /**
139
+ * Save the Research Object bundle to disk
140
+ * @param roBundle The bundle to be saved
141
+ * @return The path to the research object
142
+ * @throws IOException Any errors in saving
143
+ */
144
+ public Path saveToFile (Bundle roBundle ) throws IOException {
145
+ String fileName = "bundle-" + java .util .UUID .randomUUID () + ".zip" ;
146
+ Path bundleLocation = Files .createFile (bundleStorage .resolve (fileName ));
147
+ Bundles .closeAndSaveBundle (roBundle , bundleLocation );
148
+ return bundleLocation ;
116
149
}
117
150
118
151
/**
119
152
* Add files to this bundle from a list of Github repository contents
153
+ * @param bundle The RO bundle to add files/directories to
154
+ * @param githubInfo The information used to access the repository
120
155
* @param repoContents The contents of the Github repository
121
156
* @param path The path in the Research Object to add the files
122
157
*/
123
- private void addFiles (List <RepositoryContents > repoContents , Path path ) throws IOException {
158
+ private void addFilesToBundle (Bundle bundle , GithubDetails githubInfo ,
159
+ List <RepositoryContents > repoContents , Path path ,
160
+ Set <HashableAgent > authors ) throws IOException {
124
161
125
162
// Loop through repo contents and add them
126
163
for (RepositoryContents repoContent : repoContents ) {
@@ -138,7 +175,7 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
138
175
Files .createDirectory (subdirPath );
139
176
140
177
// Add the files in the subdirectory to this new folder
141
- addFiles ( subdirectory , subdirPath );
178
+ addFilesToBundle ( bundle , githubInfo , subdirectory , subdirPath , authors );
142
179
143
180
// Otherwise this is a file so add to the bundle
144
181
} else if (repoContent .getType ().equals (GitHubService .TYPE_FILE )) {
@@ -214,7 +251,7 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
214
251
215
252
// Set retrieved information for this file in the manifest
216
253
aggregation .setRetrievedFrom (rawURI );
217
- aggregation .setRetrievedBy (thisApp );
254
+ aggregation .setRetrievedBy (appAgent );
218
255
aggregation .setRetrievedOn (aggregation .getCreatedOn ());
219
256
220
257
} catch (URISyntaxException ex ) {
@@ -224,24 +261,4 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
224
261
}
225
262
}
226
263
227
- /**
228
- * Save the Research Object bundle to disk
229
- * @param directory The directory in which the RO will be saved
230
- * @return The path to the research object
231
- * @throws IOException Any errors in saving
232
- */
233
- public Path saveToFile (Path directory ) throws IOException {
234
- String fileName = "bundle-" + java .util .UUID .randomUUID () + ".zip" ;
235
- Path bundleLocation = Files .createFile (directory .resolve (fileName ));
236
- Bundles .closeAndSaveBundle (bundle , bundleLocation );
237
- return bundleLocation ;
238
- }
239
-
240
- /**
241
- * Getter method for the bundle
242
- * @return The RO bundle
243
- */
244
- public Bundle getBundle () {
245
- return bundle ;
246
- }
247
264
}
0 commit comments