@@ -164,8 +164,57 @@ public Bundle compositionToFhir(final FhirConnectContext context,
164164 final Set <String > createdAndAdded = new HashSet <>();
165165 final Set <String > archetypesAlreadyProcessed = new HashSet <>();
166166
167- // first the parent itself
168- // get mapper by templateid (context) + archetype id (model)
167+ if (StringUtils .isEmpty (context .getContext ().getStart ())
168+ || context .getContext ().getStart ().equals (composition .getArchetypeNodeId ())) {
169+ // fallback to the previous logic
170+ // first the parent itself
171+ // get mapper by templateid (context) + archetype id (model)
172+ handleParentCompositionMapping (webTemplate , templateId , creatingBundle , flatJsonObject ,
173+ archetypesAlreadyProcessed , isMultipleByResourceType ,
174+ intermediateCaches ,
175+ createdAndAdded , composition );
176+
177+ // all content
178+ handleContentCompositionsMapping (webTemplate , templateId , creatingBundle , flatJsonObject ,
179+ archetypesAlreadyProcessed , isMultipleByResourceType ,
180+ intermediateCaches ,
181+ createdAndAdded , composition );
182+ } else {
183+ // find where to start
184+ final ContentItem startingContentItem = composition .getContent ().stream ()
185+ .filter (c -> c .getArchetypeNodeId ().equals (context .getContext ().getStart ()))
186+ .findAny ().orElse (null );
187+ if (startingContentItem == null ) {
188+ final String availableStarts = composition .getContent ().stream ().map (c -> c .getArchetypeNodeId ())
189+ .collect (Collectors .joining (", " ));
190+ log .error (
191+ "context.start archetype '{}' not found within composition content. Available starts are: {}" ,
192+ context .getContext ().getStart (), availableStarts );
193+ return creatingBundle ;
194+ }
195+ handleContentItemMapping (webTemplate ,
196+ templateId ,
197+ creatingBundle ,
198+ flatJsonObject ,
199+ archetypesAlreadyProcessed ,
200+ isMultipleByResourceType ,
201+ intermediateCaches ,
202+ createdAndAdded ,
203+ startingContentItem );
204+ }
205+
206+ return creatingBundle ;
207+ }
208+
209+ private void handleParentCompositionMapping (final WebTemplate webTemplate ,
210+ final String templateId ,
211+ final Bundle creatingBundle ,
212+ final JsonObject flatJsonObject ,
213+ final Set <String > archetypesAlreadyProcessed ,
214+ final Map <String , Boolean > isMultipleByResourceType ,
215+ final Map <String , Map <String , Object >> intermediateCaches ,
216+ final Set <String > createdAndAdded ,
217+ final Composition composition ) {
169218 final List <OpenFhirFhirConnectModelMapper > parentMappers = openFhirTemplateRepo .getMapperForArchetype (
170219 templateId , composition .getArchetypeNodeId ());
171220 if (parentMappers != null ) {
@@ -181,41 +230,68 @@ public Bundle compositionToFhir(final FhirConnectContext context,
181230 composition .getArchetypeNodeId (),
182231 true );
183232 }
233+ }
184234
235+ private void handleContentCompositionsMapping (final WebTemplate webTemplate ,
236+ final String templateId ,
237+ final Bundle creatingBundle ,
238+ final JsonObject flatJsonObject ,
239+ final Set <String > archetypesAlreadyProcessed ,
240+ final Map <String , Boolean > isMultipleByResourceType ,
241+ final Map <String , Map <String , Object >> intermediateCaches ,
242+ final Set <String > createdAndAdded ,
243+ final Composition composition ) {
185244 // loop through top level content/archetypes within the Composition
186245 for (final ContentItem archetypesWithinContent : composition .getContent ()) {
246+ handleContentItemMapping (webTemplate ,
247+ templateId ,
248+ creatingBundle ,
249+ flatJsonObject ,
250+ archetypesAlreadyProcessed ,
251+ isMultipleByResourceType ,
252+ intermediateCaches ,
253+ createdAndAdded ,
254+ archetypesWithinContent );
255+ }
256+ }
187257
188- // elements instantiated throughout the mapping (FHIR dataelements instantiated, key'd by created object + fhir path + openehr path)
189- // instanced here so multiple archetypes can share them
190- final Map <String , Object > instantiatedIntermediateElements = new HashMap <>();
191-
192- final String archetypeNodeId = archetypesWithinContent .getArchetypeNodeId ();
193- if (archetypesAlreadyProcessed .contains (archetypeNodeId )) {
194- continue ;
195- }
196-
197- // get mapper by templateid (context) + archetype id (model)
198- final List <OpenFhirFhirConnectModelMapper > theMappers = openFhirTemplateRepo .getMapperForArchetype (
199- templateId , archetypeNodeId );
200- if (theMappers == null ) {
201- log .error ("No mappers defined for archetype within this composition: {}. No mapping possible." ,
202- archetypeNodeId );
203- continue ;
204- }
205- handleMappings (theMappers ,
206- createdAndAdded ,
207- intermediateCaches ,
208- isMultipleByResourceType ,
209- flatJsonObject ,
210- webTemplate ,
211- instantiatedIntermediateElements ,
212- creatingBundle ,
213- archetypesAlreadyProcessed ,
214- archetypeNodeId ,
215- false );
258+ private void handleContentItemMapping (final WebTemplate webTemplate ,
259+ final String templateId ,
260+ final Bundle creatingBundle ,
261+ final JsonObject flatJsonObject ,
262+ final Set <String > archetypesAlreadyProcessed ,
263+ final Map <String , Boolean > isMultipleByResourceType ,
264+ final Map <String , Map <String , Object >> intermediateCaches ,
265+ final Set <String > createdAndAdded ,
266+ final ContentItem archetypesWithinContent ) {
267+ // elements instantiated throughout the mapping (FHIR dataelements instantiated, key'd by created object + fhir path + openehr path)
268+ // instanced here so multiple archetypes can share them
269+ final Map <String , Object > instantiatedIntermediateElements = new HashMap <>();
270+
271+ final String archetypeNodeId = archetypesWithinContent .getArchetypeNodeId ();
272+ if (archetypesAlreadyProcessed .contains (archetypeNodeId )) {
273+ return ;
216274 }
217275
218- return creatingBundle ;
276+ // get mapper by templateid (context) + archetype id (model)
277+ final List <OpenFhirFhirConnectModelMapper > theMappers = openFhirTemplateRepo .getMapperForArchetype (
278+ templateId , archetypeNodeId );
279+ if (theMappers == null ) {
280+ log .error ("No mappers defined for archetype within this composition: {}. No mapping possible." ,
281+ archetypeNodeId );
282+ return ;
283+ }
284+ handleMappings (theMappers ,
285+ createdAndAdded ,
286+ intermediateCaches ,
287+ isMultipleByResourceType ,
288+ flatJsonObject ,
289+ webTemplate ,
290+ instantiatedIntermediateElements ,
291+ creatingBundle ,
292+ archetypesAlreadyProcessed ,
293+ archetypeNodeId ,
294+ false );
219295 }
220296
221297 /**
0 commit comments