Skip to content

Commit

Permalink
Fixed CopyLocalToGlobal when updating category
Browse files Browse the repository at this point in the history
  • Loading branch information
MscrmTools committed Jul 3, 2017
1 parent 95aa7f7 commit 8a922a3
Showing 1 changed file with 53 additions and 26 deletions.
79 changes: 53 additions & 26 deletions McTools.Parameters.Plugins/EntityHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@ public void Execute(IServiceProvider serviceProvider)
case PluginMessage.Create:
{
var inputData =
(Entity) ps.Context.InputParameters[PluginInputParameters.Target];
(Entity)ps.Context.InputParameters[PluginInputParameters.Target];

if (ParameterWithSameLogicalNameExists(
inputData["mctools_logicalname"].ToString(),
ps.GetIOrganizationService(true)))
throw new InvalidPluginExecutionException(
"Un paramètre avec ce nom existe déjà!");

CopyLocalToGlobal(inputData, null, ps.GetIOrganizationService(true));
CopyLocalToGlobal(inputData, null, ps.GetIOrganizationService(true), ps);
CleanOtherAttributesValue(inputData);
}
break;

case PluginMessage.Update:
{
var inputData = (Entity) ps.Context.InputParameters[PluginInputParameters.Target];
var inputData = (Entity)ps.Context.InputParameters[PluginInputParameters.Target];
var preImage = ps.Context.PreEntityImages["Image"];

CopyLocalToGlobal(inputData, preImage, ps.GetIOrganizationService(true));
CopyLocalToGlobal(inputData, preImage, ps.GetIOrganizationService(true), ps);

if(inputData.Contains("mctools_valuetype"))
if (inputData.Contains("mctools_valuetype"))
{
CleanOtherAttributesValue(inputData, preImage);
}
Expand All @@ -78,62 +80,81 @@ private static bool ParameterWithSameLogicalNameExists(string logicalName, IOrga
return service.RetrieveMultiple(qba).Entities.Count > 0;
}

private static void CopyLocalToGlobal(Entity inputData, Entity preImage, IOrganizationService service)
private static void CopyLocalToGlobal(Entity inputData, Entity preImage, IOrganizationService service, PluginServices ps)
{
var amd = ((RetrieveAttributeResponse) service.Execute(new RetrieveAttributeRequest
var amd = ((RetrieveAttributeResponse)service.Execute(new RetrieveAttributeRequest
{
EntityLogicalName = "mctools_parameter",
LogicalName = "mctools_globalvalue"
})).AttributeMetadata as StringAttributeMetadata;


var type = inputData.Contains("mctools_valuetype")
? ((OptionSetValue) inputData["mctools_valuetype"]).Value
? ((OptionSetValue)inputData["mctools_valuetype"]).Value
: ((OptionSetValue)preImage["mctools_valuetype"]).Value;

ps.Trace($"Type option set value is {type}");

switch (type)
{
case 1:
inputData["mctools_globalvalue"] = inputData["mctools_textvalue"];
if (!inputData.Contains("mctools_textvalue")) return;
string sValue = inputData.GetAttributeValue<string>("mctools_textvalue");
if (sValue != null && sValue.Length > amd.MaxLength.Value)
{
sValue = sValue.Substring(0, amd.MaxLength.Value - 3);
sValue += "...";
}
inputData["mctools_globalvalue"] = sValue;
break;

case 2:
{
string value = inputData.GetAttributeValue<string>("mctools_memovalue");
if (value.Length > amd.MaxLength.Value)
{
value = value.Substring(0, amd.MaxLength.Value - 3);
value += "...";
if (!inputData.Contains("mctools_memovalue")) return;
string mValue = inputData.GetAttributeValue<string>("mctools_memovalue");
if (mValue.Length > amd.MaxLength.Value)
{
mValue = mValue.Substring(0, amd.MaxLength.Value - 3);
mValue += "...";
}
inputData["mctools_globalvalue"] = mValue;
}
inputData["mctools_globalvalue"] = value;
}
break;

case 3:
inputData["mctools_globalvalue"] = (bool)inputData["mctools_boolvalue"] ? "Oui" : "Non";
if (!inputData.Contains("mctools_boolvalue")) return;
inputData["mctools_globalvalue"] = inputData.GetAttributeValue<bool>("mctools_boolvalue") ? "Oui" : "Non";
break;

case 4:
if (!inputData.Contains("mctools_integervalue")) return;
inputData["mctools_globalvalue"] =
((int)inputData["mctools_integervalue"]).ToString();
inputData.GetAttributeValue<int>("mctools_integervalue").ToString();
break;

case 5:
if (!inputData.Contains("mctools_decimalvalue")) return;
inputData["mctools_globalvalue"] =
((decimal)inputData["mctools_decimalvalue"]).ToString();
inputData.GetAttributeValue<decimal>("mctools_decimalvalue").ToString();
break;

case 6:
if (!inputData.Contains("mctools_floatvalue")) return;
inputData["mctools_globalvalue"] =
((float)inputData["mctools_floatvalue"]).ToString();
inputData.GetAttributeValue<double>("mctools_floatvalue").ToString();
break;

case 7:
inputData["mctools_globalvalue"] =
((DateTime)inputData["mctools_datevalue"]).ToString("dd/MM/yyyy");
if (!inputData.Contains("mctools_datevalue")) return;
inputData["mctools_globalvalue"] = $"{inputData.GetAttributeValue<DateTime>("mctools_datevalue").ToString("G")} (UTC)";
break;
}
}

private static void CleanOtherAttributesValue(Entity inputData, Entity preImage = null)
{
var type = inputData.Contains("mctools_valuetype")
? ((OptionSetValue)inputData["mctools_valuetype"]).Value
: ((OptionSetValue)preImage["mctools_valuetype"]).Value;
? inputData.GetAttributeValue<OptionSetValue>("mctools_valuetype").Value
: preImage?.GetAttributeValue<OptionSetValue>("mctools_valuetype")?.Value ?? -1;

switch (type)
{
Expand All @@ -145,6 +166,7 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
inputData["mctools_integervalue"] = null;
inputData["mctools_memovalue"] = null;
break;

case 2:
inputData["mctools_boolvalue"] = null;
inputData["mctools_datevalue"] = null;
Expand All @@ -153,6 +175,7 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
inputData["mctools_integervalue"] = null;
inputData["mctools_textvalue"] = null;
break;

case 3:
inputData["mctools_datevalue"] = null;
inputData["mctools_decimalvalue"] = null;
Expand All @@ -161,6 +184,7 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
inputData["mctools_memovalue"] = null;
inputData["mctools_textvalue"] = null;
break;

case 4:
inputData["mctools_boolvalue"] = null;
inputData["mctools_datevalue"] = null;
Expand All @@ -169,6 +193,7 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
inputData["mctools_memovalue"] = null;
inputData["mctools_textvalue"] = null;
break;

case 5:
inputData["mctools_boolvalue"] = null;
inputData["mctools_datevalue"] = null;
Expand All @@ -177,6 +202,7 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
inputData["mctools_memovalue"] = null;
inputData["mctools_textvalue"] = null;
break;

case 6:
inputData["mctools_boolvalue"] = null;
inputData["mctools_datevalue"] = null;
Expand All @@ -185,6 +211,7 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
inputData["mctools_memovalue"] = null;
inputData["mctools_textvalue"] = null;
break;

case 7:
inputData["mctools_boolvalue"] = null;
inputData["mctools_decimalvalue"] = null;
Expand All @@ -196,4 +223,4 @@ private static void CleanOtherAttributesValue(Entity inputData, Entity preImage
}
}
}
}
}

0 comments on commit 8a922a3

Please sign in to comment.