Tuesday, January 24, 2017

Adding EPiServer Meta Property through Code And Assigning it to MetaClass

In this post, we will go through the feature to add meta property to OrderForm, LineItem and CredtiCardPayment class.

We are going to use MetaClass and MetaField class of Mediachase.MetaDataPlus to add/update property to class

 

var metaLineItemClass = MetaClass.Load(MetaDataContext.Instance, "LineItemEx")


The above function will return the instance of LineItemEx  class. Using this instance you can add meta field to give meta class.

 

var metaProp = MetaField.Load(MetaDataContext.Instance, "GiftVoucherCode")



The above function will return the instance of field "GiftVoucherCode" meta property. Do not forget, MetaField are unique and can only be one created but can be assigned to any meta class.


 
if (metaProp == null)
                metaProp = MetaField.Create(MetaDataContext.Instance, "Mediachase.Commerce.Orders", "Gift Voucher Code", "Gift Voucher Code", "Gift Voucher Code", MetaDataType.ShortString, 100, true, false, false, false);
if (!metaLineItemClass.MetaFields.Contains(metaProp))
                metaLineItemClass.AddField(metaProp);


The above piece of code first checks if the meta field is already created. If not then it creates one.
It also checks if the meta field is already associated with meta class then it ignores it else assigns the meta field to meta class using AddField function in meta class.

Below is the complete code that I have used in my project.

We will have class MetaClassAndFieldsSyncInitializer which will add property to meta class.


 using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.Reference.Commerce.Shared.Settings;
using Mediachase.MetaDataPlus;
using Mediachase.MetaDataPlus.Configurator;

namespace EPiServer.Reference.Commerce.Shared.Infrastructure.Initialization
{
    public class MetaClassAndFieldsSyncInitializer
    {
        private readonly MetaDataContext _metaDataContext;
        private const string PRODUCT_TYPE = "ProductType";
        private const string LINE_ITEM_EX = "LineItemEx";
        private const string ORDER_FORM_EX = "OrderFormEx";
        private const string ORDER_GROUP_ADDRESS_EX = "OrderGroupAddressEx";
        private const string ORDER_CREDIT_CARD_PAYMENT = "CreditCardPayment";


        private List<string> _productTypes = Enum.GetNames(typeof(Enums.ProductType)).ToList();
            
        public MetaClassAndFieldsSyncInitializer(MetaDataContext metaDataContext)
        {
            if (metaDataContext == null)
                throw new ArgumentNullException("metaDataContext");
            _metaDataContext = metaDataContext;
        }

        public void Sync()
        {
            AddBaseProductProductType();
            AddLineItemFields();
            AddOrderFormFields();
            AddOrderGroupAddressFields();
            AddOrderPaymentFields();
        }

        private void AddBaseProductProductType()
        {
            var metaProp = AddProductTypeField();
        }

        private void AddLineItemFields()
        {
            var metaLineItemClass = MetaClass.Load(_metaDataContext, LINE_ITEM_EX);

            /* Will add them back when nedd them */
            var metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.LineItem.GiftVoucherCode);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders.System.LineItem", MetaFieldsConstants.LineItem.GiftVoucherCode, "Gift Voucher Code", "Gift Voucher Code", MetaDataType.ShortString, 20, true, false, false, false);

            if (!metaLineItemClass.MetaFields.Contains(metaProp))
                metaLineItemClass.AddField(metaProp);

            metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.LineItem.LineItemAbstract);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders", MetaFieldsConstants.LineItem.LineItemAbstract, "LineItem Abstract", "LineItem Abstract", MetaDataType.LongHtmlString, 5000, true, false, false, false);

            if (!metaLineItemClass.MetaFields.Contains(metaProp))
                metaLineItemClass.AddField(metaProp);

            metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.LineItem.ImageUrl);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders", MetaFieldsConstants.LineItem.ImageUrl, "Image Url", "Image Url", MetaDataType.ShortString, 1000, true, false, false, false);

            if (!metaLineItemClass.MetaFields.Contains(metaProp))
                metaLineItemClass.AddField(metaProp);

            metaProp = AddProductTypeField();
            if (!metaLineItemClass.MetaFields.Contains(metaProp))
                metaLineItemClass.AddField(metaProp);
        }

        private void AddOrderGroupAddressFields()
        {
            var metaOrdeGroupAddressClass = MetaClass.Load(_metaDataContext, ORDER_GROUP_ADDRESS_EX);
            var metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.OrderGroupAddress.Suburb_Field);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders", MetaFieldsConstants.OrderGroupAddress.Suburb_Field, "Suburb", "Suburb", MetaDataType.ShortString, 100, true, true, false, false);
            if (metaProp != null && !metaOrdeGroupAddressClass.MetaFields.Contains(metaProp))
                metaOrdeGroupAddressClass.AddField(metaProp);
        }

        private void AddOrderFormFields()
        {
            var metaOrderFormClass = MetaClass.Load(_metaDataContext, ORDER_FORM_EX);
            var metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.OrderForm.SubscribeToNewsletters_Field);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders", MetaFieldsConstants.OrderForm.SubscribeToNewsletters_Field, "Subscribe To NewsLetter", "Subscribe To NewsLetter", MetaDataType.Boolean, 10, true, false, false, false);
            if(metaProp != null && !metaOrderFormClass.MetaFields.Contains(metaProp))
                metaOrderFormClass.AddField(metaProp);

            metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.OrderForm.ReOrdering_Field);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders", MetaFieldsConstants.OrderForm.ReOrdering_Field, "Reordering", "Reordering", MetaDataType.Boolean, 10, true, false, false, false);
            if (metaProp != null && !metaOrderFormClass.MetaFields.Contains(metaProp))
                metaOrderFormClass.AddField(metaProp);
        }

        private void AddOrderPaymentFields()
        {
            var metaOrderFormClass = MetaClass.Load(_metaDataContext, ORDER_CREDIT_CARD_PAYMENT);
            var metaProp = MetaField.Load(_metaDataContext, MetaFieldsConstants.OrderPayment.PaymentProviderResponse_Field);
            if (metaProp == null)
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Orders", MetaFieldsConstants.OrderPayment.PaymentProviderResponse_Field, "Payment Provider Response", "Payment Provider Response", MetaDataType.LongString, 10000, true, false, false, false);
            if (metaProp != null && !metaOrderFormClass.MetaFields.Contains(metaProp))
                metaOrderFormClass.AddField(metaProp);
        }

        private MetaField AddProductTypeField()
        {
            MetaField metaProp = MetaField.Load(_metaDataContext, PRODUCT_TYPE);

            if (metaProp == null)
            {
                metaProp = MetaField.Create(_metaDataContext, "Mediachase.Commerce.Catalog", PRODUCT_TYPE, "Product Type", "Product type such as(Walker, Crawler, Toddler and etc", MetaDataType.EnumSingleValue, 1000, true, false, false, false);
            }

            foreach (var pr in _productTypes)
            {
                var existItem = metaProp.Dictionary.GetItem(pr);
                if (existItem == null)
                    metaProp.Dictionary.Add(pr, pr);

            }

            return metaProp;
        }
    }
}

We will also this SharedDependencyInitialization which will be call once before initialization of site to add/update any meta class or meta field.



using System.Reflection;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Reference.Commerce.Shared.Data.BusinessFoundation.Initialization;
using EPiServer.Reference.Commerce.Shared.Utilities;
using EPiServer.ServiceLocation;
using Mediachase.BusinessFoundation.Data;
using Mediachase.MetaDataPlus;

namespace EPiServer.Reference.Commerce.Shared.Infrastructure.Initialization
{
 [ModuleDependency(typeof(ServiceContainerInitialization))]
 [InitializableModule]
 public class SharedDependencyInitialization : IConfigurableModule
 {
  private bool _isInitialized;

  public void Initialize(InitializationEngine context)
  {
   if (_isInitialized)
    return;

      SyncMetaObjects();

                    _isInitialized = true;
  }

  public void Uninitialize(InitializationEngine context)
  {
   _isInitialized = false;
  }

  public void ConfigureContainer(ServiceConfigurationContext context)
  {
   context.Container.Configure(ctx => ctx.AddRegistry<SharedDependencyRegistry>());
  }

  private static void SyncMetaObjects()
     {
            new MetaClassAndFieldsSyncInitializer(MetaDataContext.Instance).Sync();
        }
 }
}

No comments:

Post a Comment