namespace
Develop1.Plugins
{
using
System;
using
System.ServiceModel;
using
Microsoft.Xrm.Sdk;
using
System.Text;
using
Microsoft.Xrm.Sdk.Query;
///
/// Plugin to support Multi Language Product Names
///
public
class
MultiLanguageProductPlugin: Plugin
{
private
readonly
string
preImageAlias =
"PreImage"
;
private
readonly
string
[] languages =
new
string
[] {
"en"
,
"de"
};
private
readonly
int
[] locales =
new
int
[] { 1033, 1031 };
///
/// Initializes a new instance of the class.
///
public
MultiLanguageProductPlugin()
:
base
(
typeof
(MultiLanguageProductPlugin))
{
base
.RegisteredEvents.Add(
new
Tuple>(20,
"Create"
,
"product"
,
new
Action(PackNameTranslations)));
base
.RegisteredEvents.Add(
new
Tuple>(20,
"Update"
,
"product"
,
new
Action(PackNameTranslations)));
base
.RegisteredEvents.Add(
new
Tuple>(40,
"Retrieve"
,
"product"
,
new
Action(UnpackNameOnRetrieve)));
base
.RegisteredEvents.Add(
new
Tuple>(40,
"RetrieveMultiple"
,
"product"
,
new
Action(UnpackNameOnRetrieveMultiple)));
base
.RegisteredEvents.Add(
new
Tuple>(40,
"Retrieve"
,
"opportunityproduct"
,
new
Action(UnpackNameOnRetrieveRelated)));
base
.RegisteredEvents.Add(
new
Tuple>(40,
"RetrieveMultiple"
,
"opportunityproduct"
,
new
Action(UnpackNameOnRetrieveMultipleRelated)));
}
///
/// Pack the translations into the name field when a Product is Created or Updated
/// Each translated name is packed into a comma separated string
/// This field is unpacked when the product entity is retrieved or related records are retrieved
///
protected
void
PackNameTranslations(LocalPluginContext localContext)
{
IPluginExecutionContext context = localContext.PluginExecutionContext;
Entity target = (Entity)localContext.PluginExecutionContext.InputParameters[
"Target"
];
Entity preImageEntity = (context.PreEntityImages !=
null
&& context.PreEntityImages.Contains(
this
.preImageAlias)) ? context.PreEntityImages[
this
.preImageAlias] :
null
;
string
[] names =
new
string
[languages.Length];
for
(
int
i = 0; i < languages.Length; i++)
{
names[i] = GetAttributeValue(
"new_name_"
+ languages[i], preImageEntity, target);
}
target[
"name"
] =
string
.Join(
","
, names);
}
///
/// Unpack the name field when a Product is Retreived
///
protected
void
UnpackNameOnRetrieve(LocalPluginContext localContext)
{
IPluginExecutionContext context = localContext.PluginExecutionContext;
Entity target = (Entity)context.OutputParameters[
"BusinessEntity"
];
target[
"name"
] = UnpackName(localContext, target.GetAttributeValue(
"name"
));
}
///
/// Unpack the name field when Products are retrieved via Lookup Search or Advanced Find
///
protected
void
UnpackNameOnRetrieveMultiple(LocalPluginContext localContext)
{
IPluginExecutionContext context = localContext.PluginExecutionContext;
EntityCollection collection = (EntityCollection) localContext.PluginExecutionContext.OutputParameters[
"BusinessEntityCollection"
];
foreach
(Entity e
in
collection.Entities)
{
if
(e.Attributes.ContainsKey(
"name"
))
{
e[
"name"
] = UnpackName(localContext, e.GetAttributeValue(
"name"
));
}
}
}
///
/// Unpack the product lookup name when an Opportunity Producs is Retrieved
///
protected
void
UnpackNameOnRetrieveMultipleRelated(LocalPluginContext localContext)
{
IPluginExecutionContext context = localContext.PluginExecutionContext;
EntityCollection collection = (EntityCollection)localContext.PluginExecutionContext.OutputParameters[
"BusinessEntityCollection"
];
foreach
(Entity e
in
collection.Entities)
{
if
(e.Attributes.ContainsKey(
"productid"
))
{
((EntityReference)e[
"productid"
]).Name = UnpackName(localContext, e.GetAttributeValue(
"productid"
).Name);
}
}
}
///
/// Unpack the product lookup name when Opportunity Products are retrieved via lookup searches or advanced find
///
protected
void
UnpackNameOnRetrieveRelated(LocalPluginContext localContext)
{
IPluginExecutionContext context = localContext.PluginExecutionContext;
Entity target = (Entity)context.OutputParameters[
"BusinessEntity"
];
if
(target.Attributes.ContainsKey(
"productid"
))
{
((EntityReference)target[
"productid"
]).Name = UnpackName(localContext, target.GetAttributeValue(
"productid"
).Name);
}
}
///
/// Unpack the product name field
///
protected
string
UnpackName(LocalPluginContext localContext,
string
name)
{
int
userLanguageId = 0;
if
(localContext.PluginExecutionContext.SharedVariables.ContainsKey(
"UserLocaleId"
))
{
userLanguageId = (
int
)localContext.PluginExecutionContext.SharedVariables[
"UserLocaleId"
];
}
else
{
Entity userSettings = localContext.OrganizationService.Retrieve(
"usersettings"
,
localContext.PluginExecutionContext.InitiatingUserId,
new
ColumnSet(
"uilanguageid"
));
userLanguageId = userSettings.GetAttributeValue(
"uilanguageid"
);
localContext.PluginExecutionContext.SharedVariables[
"uilanguageid"
] = userLanguageId;
}
string
[] labels = name.Split(
','
);
int
labelIndex = Array.IndexOf(locales, userLanguageId);
return
labels[labelIndex];
}
///
/// Get a value from the target if present, otherwise from the preImage
///
private
T GetAttributeValue(
string
attributeName, Entity preImage, Entity targetImage)
{
if
(targetImage.Contains(attributeName))
{
return
targetImage.GetAttributeValue(attributeName);
}
else
if
(preImage !=
null
)
return
preImage.GetAttributeValue(attributeName);
else
return
default
(T);
}
}
}