private int _linkId; // LoanId, SavingsId or ClientId

        #endregion Fields

        #region Constructors

        public CustomizableFieldsControl(OCustomizableFieldEntities entity, int? linkId, bool showUpdateButton)
        {
            InitializeComponent();

            _entity = entity;

            if (linkId.HasValue)
            {
                _linkId = linkId.Value;
                panelUpdate.Visible = showUpdateButton;
            }

            switch (_entity)
            {
                case OCustomizableFieldEntities.Loan:
                    _entityType = 'L';
                    break;
                case OCustomizableFieldEntities.Savings:
                    _entityType = 'S';
                    break;
                default:
                    _entityType = 'C';
                    break;
            }

            _advancedFields = new CustomClass();
            _advancedFieldsCollections = new CollectionList();
            LoanAdvancedCustomizableFields();
        }
        public List<CustomizableFieldValue> CheckCustomizableFields(OCustomizableFieldEntities entity, CustomClass customFields, 
            CollectionList customizableFieldsCollections, int linkId, char linkType)
        {
            List<CustomizableFieldValue> fieldValues = new List<CustomizableFieldValue>();

            var customizableFieldsServices = ServicesProvider.GetInstance().GetCustomizableFieldsServices();
            List<CustomizableField> customizableFields = customizableFieldsServices.
                SelectCustomizableFields((int)Enum.Parse(typeof(OCustomizableFieldEntities), entity.ToString()));

            if (customizableFields != null)
            {
                foreach (CustomizableField field in customizableFields)
                {
                    CustomizableFieldValue customizableFieldValue = new CustomizableFieldValue {Field = field};

                    var fieldName = field.Name;
                    switch (field.Type)
                    {
                        case OCustomizableFieldTypes.Boolean:
                            customizableFieldValue.Value = ((bool) customFields.GetPropertyValueByName(fieldName)).ToString(CultureInfo.InvariantCulture);
                            break;
                        case OCustomizableFieldTypes.Number:
                            customizableFieldValue.Value = ((string)customFields.GetPropertyValueByName(fieldName));
                            break;
                        case OCustomizableFieldTypes.String:
                            customizableFieldValue.Value = (string)customFields.GetPropertyValueByName(fieldName);
                            break;
                        case OCustomizableFieldTypes.Date:
                            DateTime dateValue = (DateTime) customFields.GetPropertyValueByName(fieldName);
                            customizableFieldValue.Value = Converter.CustomFieldDateToString(dateValue);
                            break;
                        case OCustomizableFieldTypes.Client:
                            customizableFieldValue.Value = customFields.GetPropertyValueByName(fieldName).ToString();
                            break;
                        case OCustomizableFieldTypes.Collection:
                            {
                                int index = customizableFieldsCollections.GetItemIndexByName(fieldName, (string)customFields.GetPropertyValueByName(fieldName));
                                if (index != -1) customizableFieldValue.Value = index.ToString(CultureInfo.InvariantCulture);
                            }
                            break;
                    }

                    var fieldType = field.Type;
                    var fieldValue = customizableFieldValue.Value;
                    if (customizableFieldValue.Field.IsMandatory)
                        if (
                            (fieldType == OCustomizableFieldTypes.Number && fieldValue == string.Empty) ||
                            (fieldType == OCustomizableFieldTypes.String && fieldValue == string.Empty) ||
                            (fieldType == OCustomizableFieldTypes.Date && Converter.CustomFieldValueToDate(fieldValue) == DateTime.MinValue) ||
                            (fieldType == OCustomizableFieldTypes.Collection && fieldValue == null) ||
                            (fieldType == OCustomizableFieldTypes.Client && fieldValue == string.Empty)
                        )
                            throw new OpenCbsContractSaveException(OpenCbsContractSaveExceptionEnum.FieldIsMandatory);

                    if (fieldType == OCustomizableFieldTypes.Number)
                    {
                        if (fieldValue != string.Empty)
                        {
                            decimal result;
                            if (!Converter.CustomFieldDecimalParse(out result, fieldValue))
                                throw new OpenCbsContractSaveException(
                                    OpenCbsContractSaveExceptionEnum.NumberFieldIsNotANumber);
                        }
                    }

                    if (fieldType == OCustomizableFieldTypes.String)
                    {
                        if (fieldValue.Length>300)
                            throw new OpenCbsCustomFieldNameException(OCustomFieldExceptionEnum.FieldLimited);
                    }

                    if (field.IsUnique)
                    {
                        if (
                            fieldType == OCustomizableFieldTypes.Number ||
                            fieldType == OCustomizableFieldTypes.String ||
                            fieldType == OCustomizableFieldTypes.Date ||
                            fieldType == OCustomizableFieldTypes.Client
                        )
                        {
                            if (customizableFieldsServices.FieldValueExists(linkId, linkType, customizableFieldValue.Field.Id, fieldValue))
                                throw new OpenCbsContractSaveException(OpenCbsContractSaveExceptionEnum.FieldIsNotUnique);
                        }
                    }

                    fieldValues.Add(customizableFieldValue);
                }
            }
            return fieldValues;
        }
 public void SaveValues(OCustomizableFieldEntities entity, CustomClass customFields, CollectionList customizableFieldsCollections, int linkId, char linkType)
 {
     List<CustomizableFieldValue> fieldValues = CheckCustomizableFields(entity, customFields, customizableFieldsCollections, linkId, linkType);
     _customizableFieldsManager.SaveCustomizableFieldValues(fieldValues, linkId, linkType);
 }