-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPluginControl.cs
320 lines (289 loc) · 13.4 KB
/
MyPluginControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using AshV.PortalTranslator.XTB.TranslationHelpers;
using McTools.Xrm.Connection;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XrmToolBox.Extensibility;
namespace AshV.PortalTranslator.XTB
{
public partial class MyPluginControl : PluginControlBase
{
private Settings mySettings;
private Guid CurrentWebsite;
public string CurrentSnippetName;
public Guid CurrentSnippetGuid;
public string CurrentSnippetValue;
private ITranslator translatorService;
public MyPluginControl()
{
InitializeComponent();
}
private void MyPluginControl_Load(object sender, EventArgs e)
{
ShowInfoNotification("This is a notification that can lead to XrmToolBox repository", new Uri("https://github.com/MscrmTools/XrmToolBox"));
// Loads or creates the settings for the plugin
if (!SettingsManager.Instance.TryLoad(GetType(), out mySettings))
{
mySettings = new Settings();
LogWarning("Settings not found => a new settings file has been created!");
}
else
{
LogInfo("Settings found and loaded");
}
OnLoadAppearance();
}
private void tsbClose_Click(object sender, EventArgs e)
{
CloseTool();
}
private void tsbSample_Click(object sender, EventArgs e)
{
// The ExecuteMethod method handles connecting to an
// organization if XrmToolBox is not yet connected
ExecuteMethod(GetWebsites);
}
private void GetWebsites()
{
WorkAsync(new WorkAsyncInfo
{
Message = "Retrieving portal's list...",
Work = (worker, args) =>
{
args.Result = Service.RetrieveMultiple(
new FetchExpression(
@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='adx_website'>
<attribute name='adx_websiteid' />
<attribute name='adx_primarydomainname' />
<attribute name='adx_name' />
<attribute name='adx_website_language' />
<link-entity name='adx_websitelanguage' from='adx_websitelanguageid' to='adx_defaultlanguage' visible='false' link-type='outer' alias='lang'>
<attribute name='adx_portallanguageid' />
<attribute name='adx_name' />
</link-entity>
</entity>
</fetch>"));
},
PostWorkCallBack = (args) =>
{
if (args.Error != null)
{
MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
var result = args.Result as EntityCollection;
if (result != null)
{
if (result.TotalRecordCount == 0)
MessageBox.Show("No Portals found is this environment.");
flLeftPane.Controls.Clear();
result.Entities.ToList().ForEach(website =>
{
var portalItem = new PortalItemControl();
portalItem.WebsiteGuid = website.Id;
portalItem.WebsiteName = website.GetAttributeValue<string>("adx_name");
portalItem.WebsiteLcid = website.GetAttributeValue<int>("adx_website_language").ToString();
portalItem.WebsiteLanguage = website.GetAttributeValue<AliasedValue>("lang.adx_name").Value.ToString();
portalItem.WebsiteUrl = website.GetAttributeValue<string>("adx_primarydomainname");
portalItem.Click += new EventHandler((object sender, EventArgs e) =>
{
MessageBox.Show(portalItem.WebsiteGuid.ToString());
CurrentWebsite = portalItem.WebsiteGuid;
ExecuteMethod(GetContentSnippets);
});
portalItem.Width = flLeftPane.Width - 5;
flLeftPane.Controls.Add(portalItem);
});
}
}
});
}
private void GetContentSnippets()
{
WorkAsync(new WorkAsyncInfo
{
Message = "Retrieving content snippets...",
Work = (worker, args) =>
{
args.Result = Service.RetrieveMultiple(
new FetchExpression(string.Format(
@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='adx_contentsnippet'>
<attribute name='adx_contentsnippetid' />
<attribute name='adx_name' />
<attribute name='adx_type' />
<order attribute='adx_name' descending='false' />
<filter type='and'>
<condition attribute='adx_websiteid' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>", CurrentWebsite)));
},
PostWorkCallBack = (args) =>
{
if (args.Error != null)
{
MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
var result = args.Result as EntityCollection;
if (result != null)
{
if (result.TotalRecordCount == 0)
MessageBox.Show("No content snippets found for this portal.");
flLeftPane.Controls.Clear();
result.Entities.GroupBy(gb => gb.GetAttributeValue<string>("adx_name")).ToList().ForEach(snippet =>
{
var snippetItem = new ContentSnippetItemControl();
snippetItem.SnippetName = snippet.Key;
snippetItem.LanguageCount = snippet.Count();
snippetItem.Click += new EventHandler((object sender, EventArgs e) =>
{
MessageBox.Show(snippetItem.SnippetName.ToString());
CurrentSnippetName = snippetItem.SnippetName.ToString();
ExecuteMethod(GetContentSnippetData);
});
snippetItem.Width = flLeftPane.Width - 25;
CurrentSnippetName = snippetItem.SnippetName.ToString();
flLeftPane.Controls.Add(snippetItem);
});
}
}
});
}
private void GetContentSnippetData()
{
WorkAsync(new WorkAsyncInfo
{
Message = "Retrieving selected content snippet's data...",
Work = (worker, args) =>
{
args.Result = Service.RetrieveMultiple(
new FetchExpression(string.Format(
@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='adx_contentsnippet'>
<attribute name='adx_contentsnippetid'/>
<attribute name='adx_name'/>
<attribute name='adx_value'/>
<attribute name='adx_type'/>
<link-entity name='adx_websitelanguage' from='adx_websitelanguageid' to='adx_contentsnippetlanguageid' visible='false' link-type='outer' alias='wl'>
<link-entity name='adx_portallanguage' from='adx_portallanguageid' to='adx_portallanguageid' visible='false' link-type='outer' alias='pl'>
<attribute name='adx_name'/>
<attribute name='adx_lcid'/>
</link-entity>
</link-entity>
<filter type='and'>
<condition attribute='adx_name' operator='eq' value='{0}'/>
</filter>
</entity>
</fetch>", CurrentSnippetName)));
},
PostWorkCallBack = (args) =>
{
if (args.Error != null)
{
MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
var result = args.Result as EntityCollection;
if (result != null)
{
if (result.TotalRecordCount == 0)
MessageBox.Show("Content Snippet data could not be retrieved.");
flpRightMain.Controls.Clear();
result.Entities.ToList().ForEach(cs =>
{
flpRightMain.Controls.Add(new EditSnippetControl()
{
Parent = this,
RecordGuid = cs.Id,
ValueType = cs.GetAttributeValue<OptionSetValue>("adx_type").Value.ToString(),
SnippetLanguage = cs.GetAttributeValue<AliasedValue>("pl.adx_name").Value.ToString(),
SnippetValue = cs.GetAttributeValue<string>("adx_value"),
Width = flpRightMain.Width
});
});
}
}
});
}
public void UpdateContentSnippets()
{
WorkAsync(new WorkAsyncInfo
{
Message = "Updating content snippet...",
Work = (worker, args) =>
{
args.Result = Service.Execute(new UpdateRequest()
{
Target = new Entity("adx_contentsnippet", CurrentSnippetGuid)
{
Attributes =
{
{ "adx_value", CurrentSnippetValue }
}
}
});
},
PostWorkCallBack = (args) =>
{
if (args.Error != null)
{
MessageBox.Show(args.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
var result = args.Result as OrganizationResponse;
if (result != null)
{
MessageBox.Show(JsonConvert.SerializeObject(result));
}
}
});
}
/// <summary>
/// This event occurs when the plugin is closed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyPluginControl_OnCloseTool(object sender, EventArgs e)
{
// Before leaving, save the settings
SettingsManager.Instance.Save(GetType(), mySettings);
}
/// <summary>
/// This event occurs when the connection has been updated in XrmToolBox
/// </summary>
public override void UpdateConnection(IOrganizationService newService, ConnectionDetail detail, string actionName, object parameter)
{
base.UpdateConnection(newService, detail, actionName, parameter);
if (mySettings != null && detail != null)
{
mySettings.LastUsedOrganizationWebappUrl = detail.WebApplicationUrl;
LogInfo("Connection has changed to: {0}", detail.WebApplicationUrl);
}
}
private void OnLoadAppearance()
{
// grpTranlate.Visible = false;
translatorService = new AzureTranslator();
}
private void btnTranslate_Click(object sender, EventArgs e)
{
var res = translatorService.GetTranslation(new TranslationRequest
{
BaseLanguage = 1033,
BaseText = "Hi, How are you?",
TargetLangauges = new List<int> { 1081 }
});
MessageBox.Show(res.Success.ToString());
MessageBox.Show(res.TranslatedText.First().Value);
}
}
}