@@ -144,6 +144,19 @@ public void create(String documentUri, String id, String type) throws InvalidSPD
144144 Objects .requireNonNull (documentUri , "Missing required document URI" );
145145 Objects .requireNonNull (id , "Missing required ID" );
146146 Objects .requireNonNull (type , "Missing required type" );
147+ RdfSpdxDocumentModelManager modelManager = getOrCreateModelManager (documentUri );
148+ if (modelManager .getCasesensitiveId (id ).isPresent ()) {
149+ throw new DuplicateSpdxIdException ("Id " +id +" already exists." );
150+ }
151+ modelManager .getOrCreate (id , type );
152+ }
153+
154+ /**
155+ * Gets an existing model manager or create a model manager if it does not exist
156+ * @param documentUri Document URI for the model manager
157+ * @return modelManager associated with the documentUri
158+ */
159+ private RdfSpdxDocumentModelManager getOrCreateModelManager (String documentUri ) {
147160 RdfSpdxDocumentModelManager modelManager = documentUriModelMap .get (documentUri );
148161 if (Objects .isNull (modelManager )) {
149162 Model model = ModelFactory .createDefaultModel ();
@@ -157,10 +170,7 @@ public void create(String documentUri, String id, String type) throws InvalidSPD
157170 modelManager = previousModel ;
158171 }
159172 }
160- if (modelManager .getCasesensitiveId (id ).isPresent ()) {
161- throw new DuplicateSpdxIdException ("Id " +id +" already exists." );
162- }
163- modelManager .getOrCreate (id , type );
173+ return modelManager ;
164174 }
165175
166176 /* (non-Javadoc)
@@ -427,11 +437,7 @@ public void leaveCriticalSection(IModelStoreLock lock) {
427437 @ Override
428438 public IModelStoreLock enterCriticalSection (String documentUri , boolean readLockRequested ) throws InvalidSPDXAnalysisException {
429439 Objects .requireNonNull (documentUri , "Missing required document URI" );
430- RdfSpdxDocumentModelManager modelManager = documentUriModelMap .get (documentUri );
431- if (Objects .isNull (modelManager )) {
432- logger .error ("The document " +documentUri +" has not been created. Can not enter critical section for a document that has not been created." );
433- throw new InvalidSPDXAnalysisException ("Can not enter a critical section for a document which has not been created in the RDF store." );
434- }
440+ RdfSpdxDocumentModelManager modelManager = getOrCreateModelManager (documentUri );
435441 return modelManager .enterCriticalSection (readLockRequested );
436442 }
437443
0 commit comments