/// <summary>
        /// Busca el componente origen
        /// </summary>
        /// <param name="connectableComponents"></param>
        /// <param name="connectionWidgetEntity"></param>
        /// <param name="serviceDocumentWpf"></param>
        /// <returns></returns>
        private static Component SearchForSourceComponent(Dictionary <Component, ComponentEntity> connectableComponents, ConnectionWidgetEntity connectionWidgetEntity, ServiceDocumentWpf serviceDocumentWpf)
        {
            bool found = false;

            ComponentEntity relatedEntity;
            Component       relatedSourceWpf = null;

            // Busca el indice del origen
            for (int i = 0; !found; i++)
            {
                // Verifica que la entidad tiene el id correcto
                if ((connectableComponents.TryGetValue(serviceDocumentWpf.Components[i], out relatedEntity)) && (relatedEntity.Id == connectionWidgetEntity.Source.IdParentComponent))
                {
                    relatedSourceWpf = serviceDocumentWpf.Components[i];
                    found            = true;
                }

                // Si un MenuForm fue encontrado, Se itera en sus items.
                else if (serviceDocumentWpf.Components[i] is MenuFormWpf)
                {
                    foreach (FormMenuItem menuItem in (serviceDocumentWpf.Components[i] as MenuFormWpf).MenuItems)
                    {
                        if ((connectableComponents.TryGetValue(menuItem, out relatedEntity)) && (relatedEntity.Id == connectionWidgetEntity.Source.IdParentComponent))
                        {
                            relatedSourceWpf = serviceDocumentWpf.Components[i];
                            found            = true;
                        }
                    }
                }
            }

            return(relatedSourceWpf);
        }
        private static Component SearchForTargetComponent(Dictionary <Component, ComponentEntity> connectableComponents, ConnectionWidgetEntity connectionWidgetEntity, ServiceDocumentWpf serviceDocumentWpf)
        {
            bool found = false;

            ComponentEntity relatedEntity;
            Component       relatedTargetWpf = null;

            for (int i = 0; !found; i++)
            {
                if ((connectableComponents.TryGetValue(serviceDocumentWpf.Components[i], out relatedEntity)) && (relatedEntity.Id == connectionWidgetEntity.Target.IdParentComponent))
                {
                    relatedTargetWpf = serviceDocumentWpf.Components[i];
                    found            = true;
                }

                // Si un MenuForm fue encontrado, Se itera en sus items.
                else if (serviceDocumentWpf.Components[i] is MenuFormWpf)
                {
                    foreach (FormMenuItem menuItem in (serviceDocumentWpf.Components[i] as MenuFormWpf).MenuItems)
                    {
                        // Si uno de los items del menu ocurre debe estar conectado
                        if ((connectableComponents.TryGetValue(menuItem, out relatedEntity)) && (relatedEntity.Id == connectionWidgetEntity.Target.IdParentComponent))
                        {
                            relatedTargetWpf = serviceDocumentWpf.Components[i];
                            found            = true;
                        }
                    }
                }
            }

            return(relatedTargetWpf);
        }
        /// <summary>
        /// Convierte un CustomerServiceDataEntity guardado en la base de datos en un ServiceDocumentWpf para ser usado en un proyecto wpf.
        /// </summary>
        /// <param name="documentEntity"></param>
        /// <param name="serviceDocumentWpf"></param>
        /// <param name="session"></param>
        public static void ConvertEntityToServiceModelWithStatistics(CustomerServiceDataEntity documentEntity, ServiceDocumentWpf serviceDocumentWpf, string session)
        {
            Dictionary <Component, ComponentEntity> connectableComponents = new Dictionary <Component, ComponentEntity>();

            foreach (ComponentEntity componentEntity in documentEntity.Components)
            {
                StatisticsWpf statisticsCanvas;

                switch (componentEntity.ComponentType)
                {
                case (int)ComponentType.DataSource:

                    // Genera e inserta un componente de origen de dato en un documento de servicio.
                    DataSourceWpf dataSource = Utilities.ConvertEntityToDataSource(componentEntity, serviceDocumentWpf.DataModel);
                    dataSource.MakeCanvas();
                    serviceDocumentWpf.Components.Add(dataSource);
                    connectableComponents.Add(dataSource, componentEntity);
                    break;

                case (int)ComponentType.EnterSingleDataFrom:

                    // Genera e inserta el componente de entrada de datos en el documento del servicio.
                    EnterSingleDataFormWpf enterSingleData = Utilities.ConvertEntityToEnterSingleData(componentEntity);
                    enterSingleData.MakeCanvas();
                    serviceDocumentWpf.Components.Add(enterSingleData);
                    connectableComponents.Add(enterSingleData, componentEntity);

                    // Genera el componente de estadistica para el formulario y lo agrega
                    statisticsCanvas = new StatisticsWpf(enterSingleData, serviceDocumentWpf.DrawArea, StatisticsWpf.GenerateStatisticSummary(componentEntity, session));
                    serviceDocumentWpf.Components.Add(statisticsCanvas);
                    break;

                case (int)ComponentType.ListForm:

                    // Genera e inserta el componente de origen de datos en el documento del servicio
                    ListFormWpf listFormWpf = Utilities.ConvertEntityToListForm(componentEntity);
                    listFormWpf.MakeCanvas();
                    serviceDocumentWpf.Components.Add(listFormWpf);
                    connectableComponents.Add(listFormWpf, componentEntity);

                    // Asigna el formulario de listas como el componente de inicio si lo es.
                    if (documentEntity.InitComponent != null && documentEntity.InitComponent.Id == componentEntity.Id)
                    {
                        serviceDocumentWpf.StartWidget = listFormWpf;
                    }

                    // Genera el componente de estadistica y lo agrega
                    statisticsCanvas = new StatisticsWpf(listFormWpf, serviceDocumentWpf.DrawArea, StatisticsWpf.GenerateStatisticSummary(componentEntity, session));
                    serviceDocumentWpf.Components.Add(statisticsCanvas);
                    break;

                case (int)ComponentType.MenuForm:

                    // Genera e inserta el componetne de origen de datos en el documento del servicio
                    MenuFormWpf menuForm = Utilities.ConvertEntityToMenuForm(componentEntity);
                    menuForm.MakeCanvas();
                    serviceDocumentWpf.Components.Add(menuForm);

                    // Agrega el formulario de menu y todos los items del menu
                    connectableComponents.Add(menuForm, componentEntity);
                    for (int i = 0; i < menuForm.MenuItems.Count; i++)
                    {
                        connectableComponents.Add(menuForm.MenuItems[i], componentEntity.MenuItems[i]);
                    }

                    // Asigna el formulario de menu como componente de inicio
                    if (documentEntity.InitComponent != null && documentEntity.InitComponent.Id == componentEntity.Id)
                    {
                        serviceDocumentWpf.StartWidget = menuForm;
                    }

                    // Genera el componente de estadistica y lo agrega
                    statisticsCanvas = new StatisticsWpf(menuForm, serviceDocumentWpf.DrawArea, StatisticsWpf.GenerateStatisticSummary(componentEntity, session));
                    serviceDocumentWpf.Components.Add(statisticsCanvas);
                    break;

                case (int)ComponentType.ShowDataForm:

                    // Genera e inserta el componente de origen de datos en el documento de servicio.
                    ShowDataFormWpf showDataFormWpf = Utilities.ConvertEntityToShowDataForm(componentEntity);
                    showDataFormWpf.MakeCanvas();
                    serviceDocumentWpf.Components.Add(showDataFormWpf);
                    connectableComponents.Add(showDataFormWpf, componentEntity);

                    // Genera el componente de estadistica y lo agrega
                    statisticsCanvas = new StatisticsWpf(showDataFormWpf, serviceDocumentWpf.DrawArea, StatisticsWpf.GenerateStatisticSummary(componentEntity, session));
                    serviceDocumentWpf.Components.Add(statisticsCanvas);
                    break;
                }
            }

            // Convierte cada una de las conexiones
            foreach (ConnectionWidgetEntity connectionWidgetEntity in documentEntity.Connections)
            {
                try
                {
                    ConnectionWpf connectionWpf = ConvertEntityToConnection(connectableComponents, connectionWidgetEntity, serviceDocumentWpf);
                    serviceDocumentWpf.AddConnection(connectionWpf);
                }
                catch (ArgumentNullException)
                {
                    Util.ShowErrorDialog(UtnEmall.ServerManager.Properties.Resources.NonFatalErrorLoadingConnection);
                }
            }
        }
        public static ConnectionWpf ConvertEntityToConnection(Dictionary <Component, ComponentEntity> connectableComponents,
                                                              ConnectionWidgetEntity connectionWidgetEntity, ServiceDocumentWpf serviceDocumentWpf)
        {
            Component relatedSourceWpf = null;
            Component relatedTargetWpf = null;

            ConnectionPointWpf source = null;
            ConnectionPointWpf target = null;

            // Busca por el componente origen
            relatedSourceWpf = SearchForSourceComponent(connectableComponents, connectionWidgetEntity, serviceDocumentWpf);

            // Busca el componente destino
            relatedTargetWpf = SearchForTargetComponent(connectableComponents, connectionWidgetEntity, serviceDocumentWpf);


            DataSourceWpf          dataSourceWpf          = relatedSourceWpf as DataSourceWpf;
            ListFormWpf            listFormWpf            = relatedSourceWpf as ListFormWpf;
            MenuFormWpf            menuFormWpf            = relatedSourceWpf as MenuFormWpf;
            EnterSingleDataFormWpf enterSingleDataFormWpf = relatedSourceWpf as EnterSingleDataFormWpf;
            ShowDataFormWpf        showDataFormWpf        = relatedSourceWpf as ShowDataFormWpf;

            // Si el Source es de entrada
            //obtengo y guardo el connectionpoint del widget correspondiente
            if (connectionWidgetEntity.Source.ConnectionType == (int)ConnectionPointType.Input)
            {
                if (dataSourceWpf != null)
                {
                    source = dataSourceWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (listFormWpf != null)
                {
                    source = listFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (menuFormWpf != null)
                {
                    source = menuFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (enterSingleDataFormWpf != null)
                {
                    source = enterSingleDataFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (showDataFormWpf != null)
                {
                    source = showDataFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
            }
            // El Source es de Output obtengo y guardo el Widget correpondiente
            else
            {
                if (dataSourceWpf != null)
                {
                    source = dataSourceWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
                if (listFormWpf != null)
                {
                    source = listFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
                if (menuFormWpf != null)
                {
                    foreach (FormMenuItemWpf menuItem in menuFormWpf.MenuItems)
                    {
                        if (String.CompareOrdinal(menuItem.Text, connectionWidgetEntity.Source.ParentComponent.Text) == 0)
                        {
                            source = menuItem.OutputConnectionPoint as ConnectionPointWpf;
                        }
                    }
                }
                if (enterSingleDataFormWpf != null)
                {
                    source = enterSingleDataFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
                if (showDataFormWpf != null)
                {
                    source = showDataFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
            }

            dataSourceWpf          = relatedTargetWpf as DataSourceWpf;
            listFormWpf            = relatedTargetWpf as ListFormWpf;
            menuFormWpf            = relatedTargetWpf as MenuFormWpf;
            enterSingleDataFormWpf = relatedTargetWpf as EnterSingleDataFormWpf;
            showDataFormWpf        = relatedTargetWpf as ShowDataFormWpf;

            // Si el Target es input
            if (connectionWidgetEntity.Target.ConnectionType == (int)ConnectionPointType.Input)
            {
                if (dataSourceWpf != null)
                {
                    target = dataSourceWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (listFormWpf != null)
                {
                    target = listFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (menuFormWpf != null)
                {
                    target = menuFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (enterSingleDataFormWpf != null)
                {
                    target = enterSingleDataFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
                if (showDataFormWpf != null)
                {
                    target = showDataFormWpf.InputConnectionPoint as ConnectionPointWpf;
                }
            }
            // El Target es de Output obtengo y guardo el Widget correpondiente
            else
            {
                if (dataSourceWpf != null)
                {
                    target = dataSourceWpf.OutputConnectionPoint as ConnectionPointWpf;
                }

                if (listFormWpf != null)
                {
                    target = listFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
                if (menuFormWpf != null)
                {
                    target = menuFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }

                if (enterSingleDataFormWpf != null)
                {
                    target = enterSingleDataFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
                if (showDataFormWpf != null)
                {
                    target = showDataFormWpf.OutputConnectionPoint as ConnectionPointWpf;
                }
            }

            ConnectionWpf connectionWpf = new ConnectionWpf(source, target);

            return(connectionWpf);
        }
        /// <summary>
        /// Convierte un CustomerServiceDataEntity almacenado en la base de datos en un ServiceDocumentWpf para ser usado en un proyecto wpf
        /// </summary>
        /// <param name="documentEntity"></param>
        /// <param name="serviceDocumentWpf"></param>
        public static void ConvertEntityToServiceModel(CustomerServiceDataEntity documentEntity, ServiceDocumentWpf serviceDocumentWpf)
        {
            Dictionary <Component, ComponentEntity> connectableComponents = new Dictionary <Component, ComponentEntity>();

            foreach (ComponentEntity componentEntity in documentEntity.Components)
            {
                switch (componentEntity.ComponentType)
                {
                // Convierte un DataSourceEntity en un DataSourceWpf
                case (int)ComponentType.DataSource:
                    DataSourceWpf dataSource = Utilities.ConvertEntityToDataSource(componentEntity, serviceDocumentWpf.DataModel);
                    dataSource.MakeCanvas();
                    serviceDocumentWpf.Components.Add(dataSource);
                    connectableComponents.Add(dataSource, componentEntity);
                    break;

                // Convierte un EnterSingleDataFormEntity en un EnterSingleDataFormWpf
                case (int)ComponentType.EnterSingleDataFrom:
                    EnterSingleDataFormWpf enterSingleData = Utilities.ConvertEntityToEnterSingleData(componentEntity);
                    enterSingleData.MakeCanvas();
                    serviceDocumentWpf.Components.Add(enterSingleData);
                    connectableComponents.Add(enterSingleData, componentEntity);
                    break;

                // Convierte un ListFormEntity en un ListFormWpf
                case (int)ComponentType.ListForm:
                    ListFormWpf listFormWpf = Utilities.ConvertEntityToListForm(componentEntity);
                    listFormWpf.MakeCanvas();
                    serviceDocumentWpf.Components.Add(listFormWpf);
                    connectableComponents.Add(listFormWpf, componentEntity);

                    if (documentEntity.InitComponent != null && documentEntity.InitComponent.Id == componentEntity.Id)
                    {
                        serviceDocumentWpf.StartWidget = listFormWpf;
                    }
                    break;

                // Convierte un MenuFormEntity en un MenuFormWpf
                case (int)ComponentType.MenuForm:
                    MenuFormWpf menuForm = Utilities.ConvertEntityToMenuForm(componentEntity);
                    menuForm.MakeCanvas();
                    serviceDocumentWpf.Components.Add(menuForm);

                    connectableComponents.Add(menuForm, componentEntity);
                    for (int i = 0; i < menuForm.MenuItems.Count; i++)
                    {
                        connectableComponents.Add(menuForm.MenuItems[i], componentEntity.MenuItems[i]);
                    }

                    if (documentEntity.InitComponent != null && documentEntity.InitComponent.Id == componentEntity.Id)
                    {
                        serviceDocumentWpf.StartWidget = menuForm;
                    }
                    break;

                // Convierte un ShowDataFormEntity en un ShowDataFormWpf
                case (int)ComponentType.ShowDataForm:
                    ShowDataFormWpf showDataFormWpf = Utilities.ConvertEntityToShowDataForm(componentEntity);
                    showDataFormWpf.MakeCanvas();
                    serviceDocumentWpf.Components.Add(showDataFormWpf);
                    connectableComponents.Add(showDataFormWpf, componentEntity);
                    break;

                default:
                    break;
                }
            }

            // Convierte cada una de las conexiones
            foreach (ConnectionWidgetEntity connectionWidgetEntity in documentEntity.Connections)
            {
                ConnectionWpf connectionWpf = ConvertEntityToConnection(connectableComponents, connectionWidgetEntity, serviceDocumentWpf);
                serviceDocumentWpf.AddConnection(connectionWpf);
            }
        }