GridProperty.cs
- Last UpdatedMar 12, 2021
- 2 minute read
using System.Linq;
using Kendo.Custom.Controls;
using Skelta.Forms2.Web;
namespace Kendo.Custom.ControlProperty
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Xml;
using System.Xml.XPath;
using Skelta.Forms.Core.Controls;
using Skelta.Forms.Core.Interfaces;
/// <summary>
/// Class for showing the control property as Grid
/// </summary>
public class GridProperty : IControlProperty
{
/// <summary>
/// Get the grid form control for rendering the property
/// </summary>
/// <param name="mainControl">Main control</param>
/// <param name="propertyInformation">property information</param>
/// <returns>Grid form with 2 text inputs</returns>
public BaseControl GetPropertyControl(BaseControl mainControl, PropertyInfo propertyInformation)
{
if (propertyInformation == null || mainControl == null)
{
return null;
}
var propertyDetails = string.Empty;
var designerProperties = propertyInformation.GetCustomAttributes(typeof(DesignerProperty), false);
foreach (var designerProperty in designerProperties.Cast<DesignerProperty>().Where(designerProperty => designerProperty.DesignerType.Equals(CoreDesigner.NextGenDesigner)))
{
propertyDetails = designerProperty.ControlPropertyDetails;
break;
}
if (string.IsNullOrEmpty(propertyDetails))
{
propertyDetails = Helper.GetResourceTextFile("BaseFormWithValidationScript.xml");
}
var localized = CommonFunctions.ProcessLocalization(propertyDetails, CommonFunctions.GetCurrentECCulture);
var baseF = BaseForm.LoadDefinitionXml(localized);
return baseF.Controls.FirstOrDefault();
}
/// <summary>
/// Read the property information from XML document
/// </summary>
/// <param name="propertyInformation">Property information</param>
/// <param name="xmlDocument">Instance xml</param>
/// <param name="propertyValue">Value of the property</param>
public void ReadProperty(PropertyInfo propertyInformation, IXPathNavigable xmlDocument, object propertyValue)
{
var xmlDoc = xmlDocument as XmlDocument;
var values = propertyValue as Dictionary<string, string>;
if (xmlDoc == null || values == null || propertyInformation == null)
{
return;
}
foreach (var value in values)
{
var xmlElement = xmlDoc.CreateElement(propertyInformation.Name);
var propText = xmlElement.AppendChild(xmlDoc.CreateElement(propertyInformation.Name + "Text"));
var propValue = xmlElement.AppendChild(xmlDoc.CreateElement(propertyInformation.Name + "Value"));
propText.InnerText = value.Key;
propValue.InnerText = value.Value;
xmlDoc.FirstChild.AppendChild(xmlElement);
}
}
/// <summary>
/// Set the value to property of the control
/// </summary>
/// <param name="xmlDocument">Instance XML</param>
/// <param name="propertyControl">Control which is rendered for the property</param>
/// <param name="mainControl">Main control</param>
public void WriteProperty(IXPathNavigable xmlDocument, BaseDataControl propertyControl, BaseControl mainControl)
{
var paramterName = string.Empty;
var paramterControl = string.Empty;
var propertyValues = new Dictionary<string, string>();
var grid = propertyControl as BaseForm;
var xmlDoc = xmlDocument as XmlDocument;
if (xmlDoc == null || grid == null || mainControl == null)
{
return;
}
var xmlNodeList = xmlDoc.SelectNodes("//" + propertyControl.XmlNodeBoundTo);
var prop = mainControl.GetType().GetProperty(propertyControl.XmlNodeBoundTo);
if (xmlNodeList != null)
{
for (var i = 0; i < xmlNodeList.Count; i++)
{
foreach (var gridControl in grid.Controls)
{
foreach (XmlNode node in xmlNodeList[i].ChildNodes)
{
var baseDataControl = gridControl as BaseDataControl;
if (IsParameterPartAvailable(baseDataControl, node, prop.Name + "Text"))
{
paramterName = node.InnerText;
break;
}
if (IsParameterPartAvailable(baseDataControl, node, prop.Name + "Value"))
{
paramterControl = node.InnerText;
break;
}
}
}
if (!string.IsNullOrEmpty(paramterName) && !string.IsNullOrEmpty(paramterControl))
{
propertyValues.Add(paramterName, paramterControl);
}
}
}
prop.SetValue(mainControl, Convert.ChangeType(propertyValues, prop.PropertyType, CultureInfo.InvariantCulture), null);
}
/// <summary>
/// Get parameter value based on the XMLNodeBoundTo
/// </summary>
/// <param name="baseDataControl">Base data control</param>
/// <param name="node">Iterative node</param>
/// <param name="parameterPart">Value for XMLNodeBoundTo</param>
/// <returns>Whether the parameter item is found</returns>
private static bool IsParameterPartAvailable(BaseDataControl baseDataControl, XmlNode node, string parameterPart)
{
return baseDataControl != null && node.Name == baseDataControl.XmlNodeBoundTo && node.Name == parameterPart;
}
}
}