private async Task <bool> ValidateConstraints(List <ViewProperty> properties, Type editedComponentType, Sitecore.Commerce.Core.Component editedComponent, CommercePipelineExecutionContext context) { var result = true; var props = editedComponentType.GetProperties(); foreach (var prop in props) { var fieldValueAsString = properties.FirstOrDefault(x => x.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase))?.Value; Attribute[] propAttributes = Attribute.GetCustomAttributes(prop); var propertyAttribute = propAttributes.SingleOrDefault(attr => attr is PropertyAttribute) as PropertyAttribute; if (propertyAttribute != null) { var isValidProperty = await ValidateProperty(prop, fieldValueAsString, propAttributes, propertyAttribute, context); if (!isValidProperty) { result = false; } } } return(result); }
/// <summary> /// Sets the values from an edit view on the edited component. /// </summary> /// <param name="properties"></param> /// <param name="editedComponentType"></param> /// <param name="editedComponent"></param> /// <param name="context"></param> public void SetPropertyValuesOnEditedComponent(List <ViewProperty> properties, Type editedComponentType, Sitecore.Commerce.Core.Component editedComponent, CommerceContext context) { // Map entity view properties to component var props = editedComponentType.GetProperties(); foreach (var prop in props) { System.Attribute[] propAttributes = System.Attribute.GetCustomAttributes(prop); if (propAttributes.SingleOrDefault(attr => attr is PropertyAttribute) is PropertyAttribute propAttr) { var fieldValue = properties.FirstOrDefault(x => x.Name.Equals(prop.Name, StringComparison.OrdinalIgnoreCase))?.Value; TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType); if (converter.CanConvertFrom(typeof(string))) { try { object propValue = converter.ConvertFromString(fieldValue); prop.SetValue(editedComponent, propValue); } catch (Exception) { context.Logger.LogError($"Could not convert property '{prop.Name}' with value '{fieldValue}' to type '{prop.PropertyType}'"); } } } } }
/// <summary> /// Gets a component of specified type from the Components property of the commerceEntity or creates a new /// component and adds it to the Components property of commerceEntity if the component does not exist. /// </summary> /// <param name="commerceEntity"></param> /// <param name="editedComponentType"></param> /// <returns></returns> public Sitecore.Commerce.Core.Component GetEditedComponent(CommerceEntity commerceEntity, Type editedComponentType) { Sitecore.Commerce.Core.Component component = commerceEntity.Components.SingleOrDefault(comp => comp.GetType() == editedComponentType); if (component == null) { component = (Sitecore.Commerce.Core.Component)Activator.CreateInstance(editedComponentType); commerceEntity.Components.Add(component); } return(component); }
/// <summary> /// /// </summary> /// <param name="sellableItem"></param> /// <param name="editedComponentType"></param> /// <returns></returns> public Sitecore.Commerce.Core.Component GetEditedComponent(SellableItem sellableItem, Type editedComponentType) { Sitecore.Commerce.Core.Component component = sellableItem.Components.SingleOrDefault(comp => comp.GetType() == editedComponentType); if (component == null) { component = (Sitecore.Commerce.Core.Component)Activator.CreateInstance(editedComponentType); sellableItem.Components.Add(component); } return(component); }
/// <summary> /// /// </summary> /// <param name="sellableItem"></param> /// <param name="itemId"></param> /// <param name="editedComponentType"></param> /// <returns></returns> public Sitecore.Commerce.Core.Component GetEditedComponent(SellableItem sellableItem, string itemId, Type editedComponentType) { var components = sellableItem.Components; if (!string.IsNullOrEmpty(itemId)) { var variation = sellableItem.GetVariation(itemId); if (variation != null) { components = variation.ChildComponents; } } Sitecore.Commerce.Core.Component component = components.SingleOrDefault(comp => comp.GetType() == editedComponentType); if (component == null) { component = (Sitecore.Commerce.Core.Component)Activator.CreateInstance(editedComponentType); components.Add(component); } return(component); }
private static void AddPropertiesToView(EntityView targetView, Type componentType, Sitecore.Commerce.Core.Component component, bool isEditViewForThisComponent) { var props = componentType.GetProperties(); foreach (var prop in props) { System.Attribute[] propAttributes = System.Attribute.GetCustomAttributes(prop); if (propAttributes.SingleOrDefault(attr => attr is PropertyAttribute) is PropertyAttribute propAttr) { if (isEditViewForThisComponent || (!isEditViewForThisComponent && propAttr.ShowInList)) { var viewProperty = new ViewProperty { Name = prop.Name, DisplayName = propAttr.DisplayName, RawValue = component != null?prop.GetValue(component) : "", OriginalType = prop.PropertyType.ToString(), UiType = propAttr?.UIType, IsReadOnly = !isEditViewForThisComponent && propAttr.IsReadOnly, IsRequired = propAttr.IsRequired }; targetView.Properties.Add(viewProperty); } } } }