@@ -58,7 +58,7 @@ public class ModelCopyManager {
5858 * Create a ModelCopyManager with default options
5959 */
6060 public ModelCopyManager () {
61-
61+ // Required empty constructor
6262 }
6363
6464 /**
@@ -153,48 +153,86 @@ public void copy(IModelStore toStore, String toDocumentUri, String toId, IModelS
153153 List <String > propertyNames = fromStore .getPropertyValueNames (fromDocumentUri , fromId );
154154 for (String propName :propertyNames ) {
155155 if (fromStore .isCollectionProperty (fromDocumentUri , fromId , propName )) {
156- Iterator <Object > fromListIter = fromStore .listValues (fromDocumentUri , fromId , propName );
157- while (fromListIter .hasNext ()) {
158- Object listItem = fromListIter .next ();
159- Object toStoreItem ;
160- if (listItem instanceof IndividualUriValue ) {
161- toStoreItem = new SimpleUriValue ((IndividualUriValue )listItem );
162- } else if (listItem instanceof TypedValue ) {
163- TypedValue listItemTv = (TypedValue )listItem ;
164- if (toStore .equals (fromStore ) && toDocumentUri .equals (fromDocumentUri )) {
165- toStoreItem = listItemTv ;
166- } else {
167- toStoreItem = copy (toStore , toDocumentUri , fromStore , fromDocumentUri ,
168- listItemTv .getId (), listItemTv .getType ());
169- }
170- } else {
171- toStoreItem = listItem ;
172- }
173- toStore .addValueToCollection (toDocumentUri , toId , propName , toStoreItem );
174- }
156+ copyCollectionProperty (toStore , toDocumentUri , toId , fromStore , fromDocumentUri , fromId , propName );
175157 } else {
176- Optional <Object > result = fromStore .getValue (fromDocumentUri , fromId , propName );
177- if (result .isPresent ()) {
178- if (result .get () instanceof IndividualUriValue ) {
179- toStore .setValue (toDocumentUri , toId , propName , new SimpleUriValue ((IndividualUriValue )result .get ()));
180- } else if (result .get () instanceof TypedValue ) {
181- TypedValue tv = (TypedValue )result .get ();
182- if (fromStore .equals (toStore ) && fromDocumentUri .equals (toDocumentUri )) {
183- toStore .setValue (toDocumentUri , toId , propName , tv );
184- } else {
185- toStore .setValue (toDocumentUri , toId , propName ,
186- copy (toStore , toDocumentUri , fromStore , fromDocumentUri ,
187- tv .getId (), tv .getType ()));
188- }
189- } else {
190- toStore .setValue (toDocumentUri , toId , propName , result .get ());
191- }
192- }
158+ copyIndividualProperty (toStore , toDocumentUri , toId , fromStore , fromDocumentUri , fromId , propName );
193159 }
194160 }
195161 }
196162
197163 /**
164+ * Copies an individual property value (non-collection property value)
165+ * @param toStore Model Store to copy to
166+ * @param toId Id to use in the copy
167+ * @param toDocumentUri Target document URI
168+ * @param fromStore Model Store containing the source item
169+ * @param fromDocumentUri Document URI for the source item
170+ * @param fromId ID source ID
171+ * @param propName Name of the property
172+ * @throws InvalidSPDXAnalysisException
173+ */
174+ private void copyIndividualProperty (IModelStore toStore , String toDocumentUri , String toId , IModelStore fromStore ,
175+ String fromDocumentUri , String fromId , String propName ) throws InvalidSPDXAnalysisException {
176+ if (fromStore .isCollectionProperty (fromDocumentUri , fromId , propName )) {
177+ throw new InvalidSPDXAnalysisException ("Property " +propName +" is a collection type" );
178+ }
179+ Optional <Object > result = fromStore .getValue (fromDocumentUri , fromId , propName );
180+ if (result .isPresent ()) {
181+ if (result .get () instanceof IndividualUriValue ) {
182+ toStore .setValue (toDocumentUri , toId , propName , new SimpleUriValue ((IndividualUriValue )result .get ()));
183+ } else if (result .get () instanceof TypedValue ) {
184+ TypedValue tv = (TypedValue )result .get ();
185+ if (fromStore .equals (toStore ) && fromDocumentUri .equals (toDocumentUri )) {
186+ toStore .setValue (toDocumentUri , toId , propName , tv );
187+ } else {
188+ toStore .setValue (toDocumentUri , toId , propName ,
189+ copy (toStore , toDocumentUri , fromStore , fromDocumentUri ,
190+ tv .getId (), tv .getType ()));
191+ }
192+ } else {
193+ toStore .setValue (toDocumentUri , toId , propName , result .get ());
194+ }
195+ }
196+ }
197+
198+ /**
199+ * Copies a property which is is a collection
200+ * @param toStore Model Store to copy to
201+ * @param toId Id to use in the copy
202+ * @param toDocumentUri Target document URI
203+ * @param fromStore Model Store containing the source item
204+ * @param fromDocumentUri Document URI for the source item
205+ * @param fromId ID source ID
206+ * @param propName Name of the property
207+ * @throws InvalidSPDXAnalysisException
208+ */
209+ private void copyCollectionProperty (IModelStore toStore , String toDocumentUri , String toId , IModelStore fromStore ,
210+ String fromDocumentUri , String fromId , String propName ) throws InvalidSPDXAnalysisException {
211+ if (!fromStore .isCollectionProperty (fromDocumentUri , fromId , propName )) {
212+ throw new InvalidSPDXAnalysisException ("Property " +propName +" is not a collection type" );
213+ }
214+ Iterator <Object > fromListIter = fromStore .listValues (fromDocumentUri , fromId , propName );
215+ while (fromListIter .hasNext ()) {
216+ Object listItem = fromListIter .next ();
217+ Object toStoreItem ;
218+ if (listItem instanceof IndividualUriValue ) {
219+ toStoreItem = new SimpleUriValue ((IndividualUriValue )listItem );
220+ } else if (listItem instanceof TypedValue ) {
221+ TypedValue listItemTv = (TypedValue )listItem ;
222+ if (toStore .equals (fromStore ) && toDocumentUri .equals (fromDocumentUri )) {
223+ toStoreItem = listItemTv ;
224+ } else {
225+ toStoreItem = copy (toStore , toDocumentUri , fromStore , fromDocumentUri ,
226+ listItemTv .getId (), listItemTv .getType ());
227+ }
228+ } else {
229+ toStoreItem = listItem ;
230+ }
231+ toStore .addValueToCollection (toDocumentUri , toId , propName , toStoreItem );
232+ }
233+ }
234+
235+ /**
198236 * Copy an item from one Model Object Store to another using the source ID for the target unless it is anonymous
199237 * @param toStore Model Store to copy to
200238 * @param toDocumentUri Target document URI
0 commit comments