Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions Templates/Java/BaseJavaModel.template.tt
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,153 @@
}
return "<" + BaseTypeCollectionResponse(c) + ", " + ITypeCollectionPage(c) + ">";
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide comments for the functions you add.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments

//Creating package definition for Enum.java.tt template file
public string CreatePackageDefForEnum(CustomT4Host host)
{
var sb = new StringBuilder();
var packageFormat = @"package {0}.{1};";
sb.AppendFormat(packageFormat,
host.CurrentModel.NamespaceName(),
host.TemplateInfo.OutputParentDirectory.Replace("_", "."));
sb.Append("\n");
return sb.ToString();
}

//Getting import prefix using property name for model classes
public string getPrefixForModels(string propertyName)
{
if(propertyName.StartsWith("Base"))
{
if(propertyName == "BaseItem" || propertyName == "BaseItemVersion")
return "models.extensions";
else
return "models.generated";
}

return "models.extensions";
}

//Getting import prefix using property name for Request classes
public string getPrefixForRequests(string propertyName)
{
if(propertyName == "BaseItemCollectionPage")
return "requests.extensions";

if(propertyName.StartsWith("Base") || propertyName.StartsWith("IBase"))
return "requests.generated";

return "requests.extensions";
}

//Get package prefix using OdcmProperty for model classes
public string getPackagePrefix(OdcmProperty property)
{
var propertyType = property.GetTypeString();

if(property.Type is OdcmEnum)
return "models.generated";

return getPrefixForModels(propertyType);
}

//Fixing package and import statement for model classes
public string CreatePackageDefForEntity(CustomT4Host host)
{
IEnumerable<OdcmProperty> properties = ((OdcmClass)host.CurrentType).Properties;
var sb = new StringBuilder();
var packageFormat = @"package {0}.{1};";
sb.AppendFormat(packageFormat,
host.CurrentModel.NamespaceName(),
host.TemplateInfo.OutputParentDirectory.Replace("_", "."));
sb.Append("\n");

sb.AppendFormat(@"import {0}.concurrency.*;
import {0}.core.*;
import {0}.http.*;
import {0}.options.*;
import {0}.serializer.*;
import java.util.Arrays;
import java.util.EnumSet;", host.CurrentModel.NamespaceName());

sb.Append("\n");
var importFormat = @"import {0}.{1}.{2};";

foreach (var property in properties.Where(p => !p.Projection.Type.Name.Equals("Stream")))
{
var propertyType = property.GetTypeString();
if(property.Type is OdcmPrimitiveType)
continue;

if(propertyType == "com.google.gson.JsonElement" || propertyType == "com.google.gson.JsonElement" || propertyType.StartsWith("com.microsoft.graph.models"))
continue;

if(propertyType.StartsWith("EnumSet"))
propertyType = propertyType.Substring("EnumSet<".Length, propertyType.Length-("EnumSet<".Length+1));

string prefixValue = getPackagePrefix(property);
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
prefixValue,
propertyType);
sb.Append("\n");

}

string baseClassNameType = BaseClassName(host.CurrentType);
if(baseClassNameType != "")
{
string prefixValue = getPrefixForModels(baseClassNameType);
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
prefixValue,
baseClassNameType);
sb.Append("\n");
}

string baseTypeNameStr = BaseTypeName(host.CurrentType);
if(baseTypeNameStr == "BasePlannerAssignments")
{
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"models.extensions",
"PlannerAssignment");
sb.Append("\n");
}
if(baseTypeNameStr == "BasePlannerChecklistItems")
{
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
"models.extensions",
"PlannerChecklistItem");
sb.Append("\n");
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we special casing BasePlannerAssignments and BasePlannerChecklistItems? What are we getting here that we don't get from within the if(baseClassNameType != ""). Assuming there is good reason for this, we should consider maintaining a list of extension files outside of the template logic code and then document the use of this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are due to overrides present in CustomOverwrites.cs file. Needed to handle these cases separately.


if (properties != null)
{
foreach (var property in properties.Where(p => p.IsCollection() && p.IsNavigation()))
{
if(property.Type is OdcmPrimitiveType)
continue;

var propertyType = BaseTypeCollectionResponse(property);

sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
getPrefixForRequests(propertyType),
propertyType);
sb.Append("\n");

string propertyValue = TypeCollectionPage(property);
sb.AppendFormat(importFormat,
host.CurrentModel.NamespaceName(),
getPrefixForRequests(propertyValue),
propertyValue);
sb.Append("\n");
}
}
return sb.ToString();
}

public string CreatePackageDef(CustomT4Host host)
{
Expand Down
2 changes: 1 addition & 1 deletion Templates/Java/models_generated/BaseEntity.java.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#@ include file="BaseJavaModel.template.tt"#>
<#@ output extension="\\" #>
<#=writer.WriteHeader()#>
<#=CreatePackageDef(host)#>
<#=CreatePackageDefForEntity(host)#>

import com.google.gson.JsonObject;
import com.google.gson.JsonElement;
Expand Down
2 changes: 1 addition & 1 deletion Templates/Java/models_generated/Enum.java.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#@ include file="BaseJavaModel.template.tt"#>
<#@ output extension="\\" #>
<#=writer.WriteHeader()#>
<#=CreatePackageDef(host)#>
<#=CreatePackageDefForEnum(host)#>

/**
* The Enum <#=c.Name.ToUpperFirstChar().SplitCamelCase()#>.
Expand Down