context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Dynamic;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using MixERP.Net.Api.Framework;
  using MixERP.Net.ApplicationState.Cache;
  using MixERP.Net.Common.Extensions;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using Newtonsoft.Json;
  using Newtonsoft.Json.Linq;
  using PetaPoco;
  using MixERP.Net.Schemas.Policy.Data;
  
  namespace MixERP.Net.Api.Policy
  {
      /// <summary>
      ///     Provides a direct HTTP access to perform various tasks such as adding, editing, and removing Menu Policies.
      /// </summary>
      [RoutePrefix("api/v1.5/policy/menu-policy")]
      public class MenuPolicyController : ApiController
      {
          /// <summary>
          ///     The MenuPolicy repository.
          /// </summary>
          private readonly IMenuPolicyRepository MenuPolicyRepository;
  
          public MenuPolicyController()
          {
              this._LoginId = AppUsers.GetCurrent().View.LoginId.ToLong();
              this._UserId = AppUsers.GetCurrent().View.UserId.ToInt();
              this._OfficeId = AppUsers.GetCurrent().View.OfficeId.ToInt();
              this._Catalog = AppUsers.GetCurrentUserDB();
  
              this.MenuPolicyRepository = new MixERP.Net.Schemas.Policy.Data.MenuPolicy
              {
                  _Catalog = this._Catalog,
                  _LoginId = this._LoginId,
                  _UserId = this._UserId
              };
          }
  
          public MenuPolicyController(IMenuPolicyRepository repository, string catalog, LoginView view)
          {
              this._LoginId = view.LoginId.ToLong();
              this._UserId = view.UserId.ToInt();
              this._OfficeId = view.OfficeId.ToInt();
              this._Catalog = catalog;
  
              this.MenuPolicyRepository = repository;
          }
  
          public long _LoginId { get; }
          public int _UserId { get; private set; }
          public int _OfficeId { get; private set; }
          public string _Catalog { get; }
  
          /// <summary>
          ///     Creates meta information of "menu policy" entity.
          /// </summary>
          /// <returns>Returns the "menu policy" meta information to perform CRUD operation.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("meta")]
          [Route("~/api/policy/menu-policy/meta")]
          public EntityView GetEntityView()
          {
              if (this._LoginId == 0)
              {
                  return new EntityView();
              }
  
              return new EntityView
              {
                  PrimaryKey = "policy_id",
                  Columns = new List<EntityColumn>()
                                  {
                                          new EntityColumn { ColumnName = "policy_id",  PropertyName = "PolicyId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = true,  IsSerial = true,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "menu_id",  PropertyName = "MenuId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "office_id",  PropertyName = "OfficeId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "inherit_in_child_offices",  PropertyName = "InheritInChildOffices",  DataType = "bool",  DbDataType = "bool",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "role_id",  PropertyName = "RoleId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "user_id",  PropertyName = "UserId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "scope",  PropertyName = "Scope",  DataType = "string",  DbDataType = "varchar",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 12 }
                                  }
              };
          }
  
          /// <summary>
          ///     Counts the number of menu policies.
          /// </summary>
          /// <returns>Returns the count of the menu policies.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count")]
          [Route("~/api/policy/menu-policy/count")]
          public long Count()
          {
              try
              {
                  return this.MenuPolicyRepository.Count();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns all collection of menu policy.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("all")]
          [Route("~/api/policy/menu-policy/all")]
          public IEnumerable<MixERP.Net.Entities.Policy.MenuPolicy> GetAll()
          {
              try
              {
                  return this.MenuPolicyRepository.GetAll();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns collection of menu policy for export.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("export")]
          [Route("~/api/policy/menu-policy/export")]
          public IEnumerable<dynamic> Export()
          {
              try
              {
                  return this.MenuPolicyRepository.Export();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns an instance of menu policy.
          /// </summary>
          /// <param name="policyId">Enter PolicyId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("{policyId}")]
          [Route("~/api/policy/menu-policy/{policyId}")]
          public MixERP.Net.Entities.Policy.MenuPolicy Get(int policyId)
          {
              try
              {
                  return this.MenuPolicyRepository.Get(policyId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          [AcceptVerbs("GET", "HEAD")]
          [Route("get")]
          [Route("~/api/policy/menu-policy/get")]
          public IEnumerable<MixERP.Net.Entities.Policy.MenuPolicy> Get([FromUri] int[] policyIds)
          {
              try
              {
                  return this.MenuPolicyRepository.Get(policyIds);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns the first instance of menu policy.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("first")]
          [Route("~/api/policy/menu-policy/first")]
          public MixERP.Net.Entities.Policy.MenuPolicy GetFirst()
          {
              try
              {
                  return this.MenuPolicyRepository.GetFirst();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns the previous instance of menu policy.
          /// </summary>
          /// <param name="policyId">Enter PolicyId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("previous/{policyId}")]
          [Route("~/api/policy/menu-policy/previous/{policyId}")]
          public MixERP.Net.Entities.Policy.MenuPolicy GetPrevious(int policyId)
          {
              try
              {
                  return this.MenuPolicyRepository.GetPrevious(policyId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns the next instance of menu policy.
          /// </summary>
          /// <param name="policyId">Enter PolicyId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("next/{policyId}")]
          [Route("~/api/policy/menu-policy/next/{policyId}")]
          public MixERP.Net.Entities.Policy.MenuPolicy GetNext(int policyId)
          {
              try
              {
                  return this.MenuPolicyRepository.GetNext(policyId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns the last instance of menu policy.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("last")]
          [Route("~/api/policy/menu-policy/last")]
          public MixERP.Net.Entities.Policy.MenuPolicy GetLast()
          {
              try
              {
                  return this.MenuPolicyRepository.GetLast();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a paginated collection containing 10 menu policies on each page, sorted by the property PolicyId.
          /// </summary>
          /// <returns>Returns the first page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("")]
          [Route("~/api/policy/menu-policy")]
          public IEnumerable<MixERP.Net.Entities.Policy.MenuPolicy> GetPaginatedResult()
          {
              try
              {
                  return this.MenuPolicyRepository.GetPaginatedResult();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a paginated collection containing 10 menu policies on each page, sorted by the property PolicyId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset.</param>
          /// <returns>Returns the requested page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("page/{pageNumber}")]
          [Route("~/api/policy/menu-policy/page/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Policy.MenuPolicy> GetPaginatedResult(long pageNumber)
          {
              try
              {
                  return this.MenuPolicyRepository.GetPaginatedResult(pageNumber);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Counts the number of menu policies using the supplied filter(s).
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns the count of filtered menu policies.</returns>
          [AcceptVerbs("POST")]
          [Route("count-where")]
          [Route("~/api/policy/menu-policy/count-where")]
          public long CountWhere([FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.MenuPolicyRepository.CountWhere(f);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a filtered and paginated collection containing 10 menu policies on each page, sorted by the property PolicyId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns the requested page from the collection using the supplied filters.</returns>
          [AcceptVerbs("POST")]
          [Route("get-where/{pageNumber}")]
          [Route("~/api/policy/menu-policy/get-where/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Policy.MenuPolicy> GetWhere(long pageNumber, [FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.MenuPolicyRepository.GetWhere(pageNumber, f);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Counts the number of menu policies using the supplied filter name.
          /// </summary>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns the count of filtered menu policies.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count-filtered/{filterName}")]
          [Route("~/api/policy/menu-policy/count-filtered/{filterName}")]
          public long CountFiltered(string filterName)
          {
              try
              {
                  return this.MenuPolicyRepository.CountFiltered(filterName);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a filtered and paginated collection containing 10 menu policies on each page, sorted by the property PolicyId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns the requested page from the collection using the supplied filters.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("get-filtered/{pageNumber}/{filterName}")]
          [Route("~/api/policy/menu-policy/get-filtered/{pageNumber}/{filterName}")]
          public IEnumerable<MixERP.Net.Entities.Policy.MenuPolicy> GetFiltered(long pageNumber, string filterName)
          {
              try
              {
                  return this.MenuPolicyRepository.GetFiltered(pageNumber, filterName);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Displayfield is a lightweight key/value collection of menu policies.
          /// </summary>
          /// <returns>Returns an enumerable key/value collection of menu policies.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("display-fields")]
          [Route("~/api/policy/menu-policy/display-fields")]
          public IEnumerable<DisplayField> GetDisplayFields()
          {
              try
              {
                  return this.MenuPolicyRepository.GetDisplayFields();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     A custom field is a user defined field for menu policies.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection of menu policies.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("custom-fields")]
          [Route("~/api/policy/menu-policy/custom-fields")]
          public IEnumerable<PetaPoco.CustomField> GetCustomFields()
          {
              try
              {
                  return this.MenuPolicyRepository.GetCustomFields(null);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     A custom field is a user defined field for menu policies.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection of menu policies.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("custom-fields/{resourceId}")]
          [Route("~/api/policy/menu-policy/custom-fields/{resourceId}")]
          public IEnumerable<PetaPoco.CustomField> GetCustomFields(string resourceId)
          {
              try
              {
                  return this.MenuPolicyRepository.GetCustomFields(resourceId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Adds or edits your instance of MenuPolicy class.
          /// </summary>
          /// <param name="menuPolicy">Your instance of menu policies class to add or edit.</param>
          [AcceptVerbs("POST")]
          [Route("add-or-edit")]
          [Route("~/api/policy/menu-policy/add-or-edit")]
          public object AddOrEdit([FromBody]Newtonsoft.Json.Linq.JArray form)
          {
              dynamic menuPolicy = form[0].ToObject<ExpandoObject>(JsonHelper.GetJsonSerializer());
              List<EntityParser.CustomField> customFields = form[1].ToObject<List<EntityParser.CustomField>>(JsonHelper.GetJsonSerializer());
  
              if (menuPolicy == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  return this.MenuPolicyRepository.AddOrEdit(menuPolicy, customFields);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Adds your instance of MenuPolicy class.
          /// </summary>
          /// <param name="menuPolicy">Your instance of menu policies class to add.</param>
          [AcceptVerbs("POST")]
          [Route("add/{menuPolicy}")]
          [Route("~/api/policy/menu-policy/add/{menuPolicy}")]
          public void Add(MixERP.Net.Entities.Policy.MenuPolicy menuPolicy)
          {
              if (menuPolicy == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  this.MenuPolicyRepository.Add(menuPolicy);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Edits existing record with your instance of MenuPolicy class.
          /// </summary>
          /// <param name="menuPolicy">Your instance of MenuPolicy class to edit.</param>
          /// <param name="policyId">Enter the value for PolicyId in order to find and edit the existing record.</param>
          [AcceptVerbs("PUT")]
          [Route("edit/{policyId}")]
          [Route("~/api/policy/menu-policy/edit/{policyId}")]
          public void Edit(int policyId, [FromBody] MixERP.Net.Entities.Policy.MenuPolicy menuPolicy)
          {
              if (menuPolicy == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  this.MenuPolicyRepository.Update(menuPolicy, policyId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          private List<ExpandoObject> ParseCollection(JArray collection)
          {
              return JsonConvert.DeserializeObject<List<ExpandoObject>>(collection.ToString(), JsonHelper.GetJsonSerializerSettings());
          }
  
          /// <summary>
          ///     Adds or edits multiple instances of MenuPolicy class.
          /// </summary>
          /// <param name="collection">Your collection of MenuPolicy class to bulk import.</param>
          /// <returns>Returns list of imported policyIds.</returns>
          /// <exception cref="MixERPException">Thrown when your any MenuPolicy class in the collection is invalid or malformed.</exception>
          [AcceptVerbs("POST")]
          [Route("bulk-import")]
          [Route("~/api/policy/menu-policy/bulk-import")]
          public List<object> BulkImport([FromBody]JArray collection)
          {
              List<ExpandoObject> menuPolicyCollection = this.ParseCollection(collection);
  
              if (menuPolicyCollection == null || menuPolicyCollection.Count.Equals(0))
              {
                  return null;
              }
  
              try
              {
                  return this.MenuPolicyRepository.BulkImport(menuPolicyCollection);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
  
gt:                     Content = new StringContent(ex.Message),