@@ -8,6 +8,7 @@ namespace Microsoft.Graph.ODataTemplateWriter.Extensions
88 using Microsoft . Graph . ODataTemplateWriter . Settings ;
99 using Vipr . Core . CodeModel ;
1010 using NLog ;
11+ using Microsoft . Graph . ODataTemplateWriter . TemplateProcessor ;
1112
1213 public static class OdcmModelExtensions
1314 {
@@ -137,6 +138,85 @@ public static IEnumerable<OdcmMethod> GetMethods(this OdcmModel model)
137138 return model . GetEntityTypes ( ) . SelectMany ( entityType => entityType . Methods ) ;
138139 }
139140
141+ /// <summary>
142+ /// This extension method determines whether the current type needs to be disambiguated.
143+ /// If the current type needs dismabiguation, we then provide a fully qualified
144+ /// import statement for the model.
145+ /// This currently operates only on entities whose name ends in "Request". We do this
146+ /// because every entity results in a request object. For example, assume we have the following
147+ /// two entities: timeOff and timeOffRequest. The timeOff entity will trigger the generation
148+ /// of classes named model.timeOff and request.timeOffRequest. The timeOffRequest entity
149+ /// will trigger the generation of a model.timeOffRequest and request.timeOffRequestRequest.
150+ /// The request.timeOffRequest and model.timeOffRequest classes will result in a name collision in
151+ /// a few files.
152+ /// Assumptions: 1) host.CurrentType is an OdcmProperty.
153+ /// </summary>
154+ /// <param name="host">The T4Host that orchestrates applying templates to the OdcmModel.</param>
155+ /// <returns>A boolean value that indicates whether the current type needs to be disambiguated.</returns>
156+ public static bool DoesCurrentTypeNeedDisambiguation ( this CustomT4Host host )
157+ {
158+ // At this point this is only applicable to OdcmProperty.
159+ // Challenging this assumption will require a lot more investigation.
160+ if ( ! ( host . CurrentType is OdcmProperty ) )
161+ return false ;
162+
163+ // We only support "Request" dismabiguation at this point. Check whether the
164+ // current type ends in "Request".
165+ var requestSuffix = "Request" ;
166+ var currentTypeName = ( host . CurrentType as OdcmProperty ) . Type . Name ;
167+ int index = currentTypeName . IndexOf ( requestSuffix ) ;
168+ if ( index == - 1 || ! currentTypeName . EndsWith ( requestSuffix ) )
169+ return false ; // Doesn't need disambiguation
170+
171+ // If it does end in "Request", let's capture the base name to check if an entity of that name
172+ // exists in the schema.
173+ string entityNameToCheckForCollision = currentTypeName . Remove ( index , requestSuffix . Length ) ;
174+
175+ // Search across namespaces, looking only at EntityType, to determine whether this type requires
176+ // disambiguation. This needs to be supported across namespaces.
177+ var classes = host . CurrentModel . Namespaces . SelectMany ( n => n . Classes ) ;
178+ var shouldDisambiguate = classes . Where ( entity => entity . Kind == OdcmClassKind . Entity
179+ && entity . Name == entityNameToCheckForCollision ) . Any ( ) ;
180+
181+ return shouldDisambiguate ;
182+ }
183+
184+ /// <summary>
185+ /// An extension method to get an import statement for the fully qualified name of the current type.
186+ /// Assumptions: 1) host.CurrentType is an OdcmProperty. 2) the generated namespace of the current type
187+ /// is in models.generated output namespace (in the generated file, not in the metadata).
188+ /// This method should support multiple namespaces.
189+ /// This currently (6/2020) applies to the following templates:
190+ /// BaseEntityCollectionRequest.java.tt
191+ /// IBaseEntityCollectionRequest.java.tt
192+ /// IBaseEntityCollectionPage.java.tt
193+ /// This currently unintentionally applies to the following templates when the disambiguation condition is met.
194+ /// This is not an issue as there is already a wild card import for the namespace that we should address first.
195+ /// IBaseEntityCollectionRequestBuilder.java.tt
196+ /// BaseEntityCollectionRequestBuilder.java.tt
197+ /// </summary>
198+ /// <param name="host">The T4Host that orchestrates applying templates to the OdcmModel.</param>
199+ /// <returns>A string that represents the import statement of the fully qualified name of the current type.</returns>
200+ public static string GetFullyQualifiedImportStatementForModel ( this CustomT4Host host )
201+ {
202+ // By default, we don't need to disambiguate the model in the generated code file.
203+ // This will be the general case.
204+ var importStatement = "" ;
205+
206+ // Check whether we need to disambiguate the current type for generation of the model in the code file.
207+ var shouldDisambiguate = host . DoesCurrentTypeNeedDisambiguation ( ) ;
208+
209+ if ( shouldDisambiguate )
210+ {
211+ // Form the import statement to disambiguate the model in the generated code file.
212+ var thisNamespace = host . CurrentModel . NamespaceName ( ) ;
213+ var thisTypeName = ( host . CurrentType as OdcmProperty ) . Type . Name . ToUpperFirstChar ( ) ;
214+ importStatement = $ "\n import { thisNamespace } .models.generated.{ thisTypeName } ;";
215+ }
216+
217+ return importStatement ;
218+ }
219+
140220 /// <summary>
141221 /// Get the service collection navigation property for the given property type
142222 ///
0 commit comments