/// <summary>
        /// Adds the existing records classes.
        /// </summary>
        /// <param name="assemblyCode">The assembly code.</param>
        /// <param name="processView">The process view.</param>
        /// <param name="viewConfig">The view configuration.</param>
        /// <param name="process">The process.</param>
        /// <param name="assemblyGenerator">The assembly generator.</param>
        private static void AddExistingRecordsClasses(StringBuilder assemblyCode, IProcessView processView, DataCollectProcessViewConfigurator viewConfig, IProcessDefinition process, AssemblyGeneratorBase assemblyGenerator)
        {
            var dcConfig = (DataCollectViewConfig) processView.ViewConfiguration;
            var table = new TableDefinition { Name = process.Name };

            assemblyCode.AppendFormat(
               @"
    [Serializable]
    public class {0} : BaseReferenceInfo<{0}>
    {{
        {1}

        public static ReferenceFieldDefinition GetFieldDefinition()
        {{
            ReferenceFieldDefinition fieldDefinition = null;
{2}
            return fieldDefinition;
        }}
    }}
",
               GetExistingRecordsChildClassName(processView, viewConfig, process),
               AssemblyGeneratorBase.GetDisplaylistRefEditableChildProperties(table, dcConfig.ReferenceField),
               assemblyGenerator.GetReferenceDefinition(new TableDefinition {Name = process.Name}, dcConfig.ReferenceField, "fieldDefinition"));

            assemblyCode.AppendFormat(
       @"
    [Serializable]
    public class {0} : BaseReferenceInfoList<{0}, {1}>
    {{
        public static void BeginGetList(int pageNumber, int pageSize, SortList sorting, string filter, System.Action<object, IDataPortalResult> callback)
        {{
            DataPortal.BeginFetch<{0}>(new PagedCriteria {{ PageNumber = pageNumber, PageSize = pageSize, SortColumns = sorting, Filter = filter }}, (o, result) => callback(o, result));
        }}

        public void Assign(IReverseCrossReferenceItem item)
        {{
            IsReadOnly = false;
            Add(({1})item);
            IsReadOnly = true;
        }}

        public void Load(IEnumerable<IReverseCrossReferenceItem> items)
        {{
            IsReadOnly = false;

            Clear();
            AddRange(items.Cast<{1}>());

            IsReadOnly = true;
        }}
    }}
",
GetExistingRecordsListClassName(processView, viewConfig, process),
GetExistingRecordsChildClassName(processView, viewConfig, process));
        }
        /// <summary>
        /// Called when [after add process view classes].
        /// </summary>
        /// <param name="assemblyCode">The assembly code.</param>
        /// <param name="process">The process.</param>
        /// <param name="assemblyGenerator">The assembly generator.</param>
        public override void OnAfterAddProcessViewClasses(StringBuilder assemblyCode, IProcessDefinition process, AssemblyGeneratorBase assemblyGenerator)
        {
            base.OnAfterAddProcessViewClasses(assemblyCode, process, assemblyGenerator);

            foreach (var processView in process.ProcessViews)
            {
                if (processView.ViewType != ProcessViewType.DataCollect)
                    continue;

                var viewConfig = new DataCollectProcessViewConfigurator();
                viewConfig.SetConfigurationString(processView.Configuration);

                if (viewConfig.ExistingRecordFields.Count > 0)
                    AddExistingRecordsClasses(assemblyCode, processView, viewConfig, process, assemblyGenerator);
            }
        }
        public void OnInit_ShouldAdd_CurrentStateField()
        {
            var viewConfigurator = new DataCollectProcessViewConfigurator();

            var process = Mock.Create<ProcessEdit>(Behavior.Loose);

            process.IsStateEnabled = true;
            process.FilterList = new ProcessFilterList();
            process.ViewList = new ProcessViewList();

            var processVVM = new ProcessViewsViewModel { Model = process };
            var vm = new DataCollectionProcessViewConfiguratorViewModel();

            vm.Init(viewConfigurator, processVVM);

            var currentStateFieldVM = vm.AvailableFields.FirstOrDefault(f => f.Name.Contains(Constants.CurrentStateColumnName));

            Assert.IsNotNull(currentStateFieldVM);
            Assert.IsTrue(currentStateFieldVM.Model.IsBuiltIn);
        }
 /// <summary>
 /// Gets the name of the existing records child class.
 /// </summary>
 /// <param name="processView">The process view.</param>
 /// <param name="viewConfig">The view configuration.</param>
 /// <param name="process">The process.</param>
 /// <returns>System.String.</returns>
 private static string GetExistingRecordsChildClassName(IProcessView processView, DataCollectProcessViewConfigurator viewConfig, IProcessDefinition process)
 {
     return ProcessViewsCommon.GetDataCollectExsistingRecordsInfoClassName(process.Name, processView);
 }
        /// <summary>
        /// Adds the existing records classes.
        /// </summary>
        /// <param name="assemblyCode">The assembly code.</param>
        /// <param name="processView">The process view.</param>
        /// <param name="viewConfig">The view configuration.</param>
        /// <param name="process">The process.</param>
        /// <param name="assemblyGenerator">The assembly generator.</param>
        private static void AddExistingRecordsClasses(StringBuilder assemblyCode, IProcessView processView, DataCollectProcessViewConfigurator viewConfig, IProcessDefinition process, AssemblyGeneratorBase assemblyGenerator)
        {
            var dcConfig = (DataCollectViewConfig)processView.ViewConfiguration;
            var table = new TableDefinition {Name = process.Name};
            assemblyCode.AppendFormat(

                @"
    [Serializable]
    public class {0} : BaseReferenceInfo<{0}>
    {{
        {1}

        public static {0} Get{0}(SafeDataReader reader)
        {{
            return DataPortal.FetchChild<{0}>(reader);
        }}

        private void Child_Fetch(SafeDataReader reader)
        {{
            var displayName = new System.Text.StringBuilder();
{2}
        }}

        public static Cebos.Veyron.SharedTypes.ReferenceFieldDefinition GetFieldDefinition()
        {{
            Cebos.Veyron.SharedTypes.ReferenceFieldDefinition fieldDefinition = null;
{3}
            return fieldDefinition;
        }}
    }}
",
                GetExistingRecordsChildClassName(processView, viewConfig, process),
                AssemblyGeneratorBase.GetDisplaylistRefEditableChildProperties(table, dcConfig.ReferenceField),
                ((ServerAssemblyGenerator)assemblyGenerator).GetReverseCrossRefChildPropertiesLoad(dcConfig.ReferenceField),
                assemblyGenerator.GetReferenceDefinition(table, dcConfig.ReferenceField, "fieldDefinition")
            );
        }
        /// <summary>
        /// Creates the configurator.
        /// </summary>
        /// <returns>IProcessViewConfigurator.</returns>
        public IProcessViewConfigurator CreateConfigurator()
        {
            var result = new DataCollectProcessViewConfigurator();
#if SILVERLIGHT
            TheDynamicTypeManager.Value.BeginGetList<IFrequencyEventList>(
                Constants.FrequencyEventProcessName,
                (s, e) =>
                    {
                        if (e.Error == null)
                        {
                            result.FrequencyEvents = e.Object;
                            result.UpdateEvents();
                        }
                    });
#endif
            return result;
        }
 private static void Static(DataCollectProcessViewConfigurator target, EventPattern<NotifyCollectionChangedEventArgs> handler)
 {
     target.RaiseConfigurationChanged();
 }
        /// <summary>
        /// Initializes the specified view configurator.
        /// </summary>
        /// <param name="viewConfigurator">The view configurator.</param>
        /// <param name="parentViewModel">The parent view model.</param>
        public void Init(IProcessViewConfigurator viewConfigurator, ProcessViewsViewModel parentViewModel)
        {
            _viewConfigurator = (DataCollectProcessViewConfigurator) viewConfigurator;
            RaisePropertyChanged(() => ViewConfigurator);

            _parentViewModel = parentViewModel;
            RaisePropertyChanged(() => Parent);

            AvailableFields = new ObservableCollection<IDisplayFieldViewModel>();

            SelectedDisplayFields.Clear();
            foreach (var df in _viewConfigurator.ExistingRecordFields)
            {
                var vm = new SelectedFieldViewModel();
                vm.Initialize(df, this);
                SelectedDisplayFields.Add(vm);

                var node = FindNode(df.FullPath);
                if (node != null)
                {
                    node.IsSelected = false;
                }
            }

            UpdateCanMoveFields();

            SetupProcessFields();
            LoadFilters();

            SetEventListener();
        }
        /// <summary>
        /// Adds the existing records classes.
        /// </summary>
        /// <param name="assemblyCode">The assembly code.</param>
        /// <param name="processView">The process view.</param>
        /// <param name="viewConfig">The view configuration.</param>
        /// <param name="process">The process.</param>
        /// <param name="assemblyGenerator">The assembly generator.</param>
        private void AddExistingRecordsClasses(StringBuilder assemblyCode, IProcessView processView, DataCollectProcessViewConfigurator viewConfig, IProcessDefinition process, AssemblyGeneratorBase assemblyGenerator)
        {
            assemblyCode.AppendFormat(
       @"
    [Serializable]
    public class {0} : BaseReferenceInfoList<{0}, {1}>
    {{
        private void DataPortal_Fetch(PagedCriteria criteria)
        {{
            var fieldDefinition = {1}.GetFieldDefinition();
            var queryGenerator = new SearchQueryGenerator {{ PageSize = criteria.PageSize, PageNumber = criteria.PageNumber }};         

            Dynamic{2}.{2}List.AddTables(queryGenerator);          
                       
            queryGenerator.RefUpdateFields(fieldDefinition);

            var filterList = (new List<IFilterDescriptor>());
            if (!string.IsNullOrWhiteSpace(criteria.Filter))
            {{
                // new JSON filter format
                filterList.Add(Cebos.Veyron.SharedTypes.FilterDescriptor.FromJSON(criteria.Filter));
            }}
            
            if (filterList.Any())
            {{
                QueryGeneratorExtensions.AddFilters(filterList, queryGenerator.Filters, queryGenerator.RootTable, queryGenerator);
            }}           

            if (criteria.SortColumns != null) 
            {{
                foreach(var col in criteria.SortColumns)
                {{
                    queryGenerator.SortOrders.Add(new Cebos.Veyron.SharedTypes.QueryGenerator.SortDescriptor(col.Direction, queryGenerator.FindColumn(col.ColumnName), col.ColumnName));
                }}               
            }}

            var cmdText = queryGenerator.RefGetQueryForList(fieldDefinition);
            
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ServerSettings.ConnectionString, false))
            {{         
                using (var cmd = new SqlCommand(cmdText, ctx.Connection))
                {{
                    using (var reader = new SafeDataReader(cmd.ExecuteReader()))
                    {{
                        bool isFirstRow = true;

                        while (reader.Read())
                        {{
                            var item = {1}.Get{1}(reader);
                            Add(item);    

                            if (!isFirstRow) 
                            {{
                                continue;
                            }}
                            TotalRowCount = (int)reader.GetInt64(0);
                            isFirstRow = false;                     
                        }}
                    }}
                }}              
            }}

            PageNumber = criteria.PageNumber;           

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }}
    }}
",
GetExistingRecordsListClassName(processView, viewConfig, process),
GetExistingRecordsChildClassName(processView, viewConfig, process),
process.Name);
        }
        /// <summary>Refreshes the specified page number.</summary>
        /// <param name="pageNumber">The page number.</param>
        public void Refresh(int pageNumber)
        {
            IsBusy = true;
            var filter = string.Empty;

            var configurator = new DataCollectProcessViewConfigurator();
            configurator.SetConfigurationString(_processView.Configuration);

            if (!string.IsNullOrWhiteSpace(configurator.FilterDefinition))
            {
                var filterDescriptor = (FilterDescriptor)FilterDescriptor.FromJSON(configurator.FilterDefinition);
                filterDescriptor.ReplacePredefinedValues(_model);
                filter = filterDescriptor.ToJSON();
            }

            TheMethodInvokator.Invoke(_listType, "BeginGetList",new object[] { CurrentPageNumber, PageSize, _sortDescriptors, filter, new Action<object, IDataPortalResult>(SearchItemsLoaded)});
        }