11using Microsoft . Xrm . Sdk ;
22using Microsoft . Xrm . Sdk . Metadata ;
3+ using Rappen . XRM . Helpers . Extensions ;
34using Rappen . XRM . Helpers . FetchXML ;
45using Rappen . XTB . FetchXmlBuilder . Builder ;
56using Rappen . XTB . FetchXmlBuilder . ControlsClasses ;
@@ -15,6 +16,8 @@ namespace Rappen.XTB.FetchXmlBuilder.Controls
1516 public partial class linkEntityControl : FetchXmlElementControlBase
1617 {
1718 private int relationshipWidth ;
19+ private bool aliasset ;
20+ private string lastalias ;
1821
1922 public linkEntityControl ( ) : this ( null , null , null )
2023 {
@@ -26,6 +29,9 @@ public linkEntityControl(TreeNode node, FetchXmlBuilder fetchXmlBuilder, TreeBui
2629 BeginInit ( ) ;
2730 chkOnlyLpks . Checked = fetchXmlBuilder . settings . LinkEntityIdAttributesOnly ;
2831 InitializeFXB ( null , fetchXmlBuilder , tree , node ) ;
32+ picAliasRegen . Enabled = fxb . settings . LinkEntityAliasGenerate != Settings . LinkEntityAlias . None ;
33+ aliasset = ! string . IsNullOrWhiteSpace ( txtAlias . Text ) ;
34+ lastalias = txtAlias . Text ;
2935 EndInit ( ) ;
3036 RefreshAttributes ( ) ;
3137 }
@@ -78,6 +84,87 @@ private void cmbEntity_SelectedIndexChanged(object sender, EventArgs e)
7884 {
7985 RefreshAttributes ( ) ;
8086 }
87+ GenerateAlias ( ) ;
88+ }
89+
90+ private void GenerateAlias ( )
91+ {
92+ if ( aliasset )
93+ {
94+ return ;
95+ }
96+ if ( ! IsInitialized )
97+ {
98+ return ;
99+ }
100+ if ( fxb . settings . LinkEntityAliasGenerate == Settings . LinkEntityAlias . None )
101+ {
102+ return ;
103+ }
104+ var aliasentities = fxb . dockControlBuilder . GetEntityNodes ( needsalias : true ) . Where ( ae => ae . Node != Node ) ;
105+ switch ( fxb . settings . LinkEntityAliasGenerate )
106+ {
107+ case Settings . LinkEntityAlias . Acronym :
108+ var entitymeta = fxb . GetEntity ( cmbEntity . Text ) ;
109+ if ( entitymeta == null )
110+ {
111+ return ;
112+ }
113+ var displayname = entitymeta . ToDisplayName ( ) ;
114+
115+ // Finding unique acronym
116+ var acronym = string . Empty ;
117+ var acronymlength = 1 ;
118+ while ( string . IsNullOrEmpty ( acronym ) )
119+ {
120+ var tempacronym = displayname . ToAcronym ( acronymlength , includeAllWordInitials : true ) ;
121+ if ( acronymlength >= displayname . Length )
122+ {
123+ acronym = tempacronym ;
124+ }
125+ else if ( ! aliasentities . Any ( ae => ae . Alias . RemoveTrailingDigits ( ) == tempacronym ) )
126+ { // Not used by other link-entities
127+ acronym = tempacronym ;
128+ }
129+ else if ( aliasentities . Any ( ae => ae . Alias . RemoveTrailingDigits ( ) == tempacronym && ae . LogicalName == entitymeta . LogicalName ) )
130+ { // Used by this link-entity
131+ acronym = tempacronym ;
132+ }
133+ else
134+ { // Try longer acronym
135+ acronymlength ++ ;
136+ }
137+ }
138+
139+ // Finding possible number
140+ var number = 0 ;
141+ while ( number < 1000 )
142+ {
143+ var tempacronym = acronym + ( number > 0 ? number . ToString ( ) : "" ) ;
144+ if ( ! aliasentities . Any ( ae => ae . Alias == tempacronym ) )
145+ { // Not used by other link-entities
146+ txtAlias . Text = tempacronym ;
147+ return ;
148+ }
149+ number ++ ;
150+ }
151+ break ;
152+
153+ case Settings . LinkEntityAlias . LogicalName :
154+ var logicalname = cmbEntity . Text ;
155+ number = 1 ;
156+ while ( number < 1000 )
157+ {
158+ var tempalias = logicalname + ( number > 0 ? number . ToString ( ) : "" ) ;
159+ if ( ! aliasentities . Any ( ae => ae . Alias == tempalias ) )
160+ { // Not used by other link-entities
161+ txtAlias . Text = tempalias ;
162+ return ;
163+ }
164+ number ++ ;
165+ }
166+ break ;
167+ }
81168 }
82169
83170 private void RefreshRelationships ( )
@@ -468,5 +555,13 @@ public override void Focus()
468555 {
469556 cmbRelationship . Focus ( ) ;
470557 }
558+
559+ private void picAliasRegen_Click ( object sender , EventArgs e )
560+ {
561+ Cursor = Cursors . WaitCursor ;
562+ aliasset = false ;
563+ GenerateAlias ( ) ;
564+ Cursor = Cursors . Default ;
565+ }
471566 }
472567}
0 commit comments