public void writeBundle()

in taverna-prov/src/main/java/org/apache/taverna/prov/W3ProvenanceExport.java [831:955]


    public void writeBundle(WorkflowBundle wfBundle) throws IOException {

        Bundle dataBundle = getBundle();

        // Workflow
        DataBundles.setWorkflowBundle(dataBundle, wfBundle);

        // Generate Manifest
        // TODO: This should be done automatically on close/save
        Manifest manifest = new Manifest(dataBundle);
        manifest.populateFromBundle();

        Path workflowRunProvenance = DataBundles
                .getWorkflowRunProvenance(dataBundle);
        // Additional metadata
        manifest.getAggregation(workflowRunProvenance).setMediatype(
                "text/turtle");

        Agent provPlugin = new Agent();
        provPlugin.setName("Taverna-PROV plugin, " + applicationConfig.getTitle() + " " + applicationConfig.getName());
        provPlugin.setUri(getPluginIdentifier(getClass()));
        manifest.getAggregation(workflowRunProvenance).setCreatedBy(
                provPlugin);        
        manifest.setCreatedBy(provPlugin);
        
        
        // Media types:
        for (Entry<URI, String> e : mediaTypes.entrySet()) {
            URI uri = e.getKey();
            String mediatype = e.getValue();
            PathMetadata aggregation = manifest.getAggregation(uri);
            if (aggregation == null) {
                // An external reference? Add it.
		aggregation = manifest.getAggregation(uri);
                //aggregation = new PathMetadata();
                //aggregation.setUri(uri);
                //manifest.getAggregates().add(aggregation);
            }
            aggregation.setMediatype(mediatype);
        }

        // Add annotations

        // This RO Bundle is about a run
        PathAnnotation bundleAboutRun = new PathAnnotation();
        bundleAboutRun.setAbout(runURI);
        bundleAboutRun.setContent(URI.create("/"));
        manifest.getAnnotations().add(bundleAboutRun);
        
        // Also aggregate the run by ID, and that it was done by taverna
		Agent taverna = new Agent();
		taverna.setName(applicationConfig.getTitle());
		taverna.setUri(getTavernaVersion());
        manifest.getAggregation(runURI).setCreatedBy(taverna);

        // TODO: Do we need both the "history" link and the annotation below?
        manifest.setHistory(Arrays.asList(workflowRunProvenance));

        // This RO Bundle is described in the provenance file
        PathAnnotation provenanceAboutBundle = new PathAnnotation();
        provenanceAboutBundle.setAbout(URI.create("/"));
        provenanceAboutBundle.setContent(URI.create(workflowRunProvenance
                .toUri().getPath()));
        manifest.getAnnotations().add(provenanceAboutBundle);

        // The wfdesc is about the workflow definition
        Path workflow = DataBundles.getWorkflow(dataBundle);
        // String workflowType = Files.probeContentType(workflow);
        manifest.getAggregation(workflow).setMediatype(WORKFLOW_BUNDLE);
        Path wfdesc = DataBundles.getWorkflowDescription(dataBundle);
        if (Files.exists(wfdesc)) {
            PathAnnotation wfdescAboutWfBundle = new PathAnnotation();
            wfdescAboutWfBundle
                    .setAbout(URI.create(workflow.toUri().getPath()));
            wfdescAboutWfBundle
                    .setContent(URI.create(wfdesc.toUri().getPath()));
            manifest.getAnnotations().add(wfdescAboutWfBundle);
        }

        // And the workflow definition is about the workflow
        PathAnnotation wfBundleAboutWf = new PathAnnotation();
        URITools uriTools = new URITools();
        URI mainWorkflow = uriTools.uriForBean(wfBundle.getMainWorkflow());
        wfBundleAboutWf.setAbout(mainWorkflow);
        URI wfBundlePath = URI.create(workflow.toUri().getPath());
        wfBundleAboutWf.setContent(wfBundlePath);
        manifest.getAnnotations().add(wfBundleAboutWf);
        manifest.getAggregation(mainWorkflow);

        // hasWorkflowDefinition
        PathAnnotation hasWorkflowDefinition = new PathAnnotation();
        hasWorkflowDefinition.setAbout(wfBundlePath);
        UUID uuid = UUID.randomUUID();
        hasWorkflowDefinition.setUri(URI.create("urn:uuid:" + uuid));
        Path annotationBody = DataBundles.getAnnotations(dataBundle).resolve(
                uuid + ".ttl");
        hasWorkflowDefinition.setContent(URI.create(annotationBody.toUri()
                .getPath()));
        Model model = ModelFactory.createDefaultModel();
        URI relPathToWfBundle = uriTools.relativePath(annotationBody.toUri(),
                workflow.toUri());
        model.setNsPrefix("wfdesc", WFDESC);
        model.add(model.createResource(mainWorkflow.toASCIIString()),
                model.createProperty(WFDESC + "hasWorkflowDefinition"),
                model.createResource(relPathToWfBundle.toASCIIString()));
        try (OutputStream out = Files.newOutputStream(annotationBody)) {
            model.write(out, "TURTLE", annotationBody.toUri().toASCIIString());
        }
        manifest.getAnnotations().add(hasWorkflowDefinition);

        PathAnnotation wfBundleAboutWfB = new PathAnnotation();
        wfBundleAboutWfB.setAbout(wfBundle.getGlobalBaseURI());
        wfBundleAboutWfB.setContent(URI.create(workflow.toUri().getPath()));
        manifest.getAnnotations().add(wfBundleAboutWfB);

        manifest.writeAsJsonLD();

        // // Saving a data bundle:
        // Path bundleFile = runPath.getParent().resolve(runPath.getFileName() +
        // ".bundle.zip");
        // DataBundles.closeAndSaveBundle(dataBundle, bundleFile);
        // NOTE: From now dataBundle and its Path's are CLOSED
        // and can no longer be accessed

    }