@@ -103,11 +103,49 @@ public static void upgrade(Model model, String documentNamespace) throws Invalid
103103 upgradeArtifactOf (model , documentNamespace );
104104 upgradeReviewers (model , documentNamespace );
105105 upgradeExternalDocumentRefs (model , documentNamespace );
106+ upgradeHasFiles (model , documentNamespace );
106107 } finally {
107108 model .leaveCriticalSection ();
108109 }
109110 }
110111
112+ /**
113+ * Changes all hasFile properties to CONTAINS relationships
114+ * @param model
115+ * @param documentNamespace
116+ */
117+ private static void upgradeHasFiles (Model model , String documentNamespace ) {
118+ List <Statement > statementsToRemove = new ArrayList <>();
119+ Property hasFileProperty = model .createProperty ("http://spdx.org/rdf/terms#hasFile" );
120+ Property relationshipProperty = model .createProperty (SpdxConstants .SPDX_NAMESPACE + SpdxConstants .PROP_RELATIONSHIP );
121+ String query = "SELECT ?s ?o WHERE { ?s <http://spdx.org/rdf/terms#hasFile> ?o }" ;
122+ try (QueryExecution qe = QueryExecutionFactory .create (query , model )) {
123+ ResultSet result = qe .execSelect ();
124+ while (result .hasNext ()) {
125+ QuerySolution qs = result .next ();
126+ Resource pkg = qs .get ("s" ).asResource ();
127+ Resource file = qs .get ("o" ).asResource ();
128+ statementsToRemove .add (model .createStatement (pkg , hasFileProperty , file ));
129+ // check for existing contains relationships - avoids duplication
130+ List <Statement > foundContainsRelationships = new ArrayList <>();
131+ pkg .listProperties (relationshipProperty ).forEach ((stmt ) -> {
132+ Resource existingRelationship = stmt .getObject ().asResource ();
133+ Resource relatedElement = existingRelationship .getPropertyResourceValue (model .createProperty (SpdxConstants .SPDX_NAMESPACE + SpdxConstants .PROP_RELATED_SPDX_ELEMENT ));
134+ Resource relationshipType = existingRelationship .getPropertyResourceValue (model .createProperty (SpdxConstants .SPDX_NAMESPACE + SpdxConstants .PROP_RELATIONSHIP_TYPE ));
135+ if (relatedElement .getURI ().equals (file .getURI ()) &&
136+ relationshipType .getURI ().equals (RelationshipType .CONTAINS .getIndividualURI ())) {
137+ foundContainsRelationships .add (stmt );
138+ }
139+ });
140+ if (foundContainsRelationships .size () == 0 ) {
141+ Resource relationship = createRelationship (model , file , RelationshipType .CONTAINS );
142+ pkg .addProperty (relationshipProperty , relationship );
143+ }
144+ }
145+ model .remove (statementsToRemove );
146+ }
147+ }
148+
111149 /**
112150 * Make sure all external document Ref's have a URI with proper ID rather than using the externalDocumentId property
113151 * @param model
0 commit comments