/// <summary>
        /// Function to Load the relation Source from database.
        /// </summary>
        /// <param name="connectionWidget">ConnectionWidgetEntity parent</param>
        /// <exception cref="ArgumentNullException">
        /// if <paramref name="connectionWidget"/> is not a <c>ConnectionWidgetEntity</c>.
        /// </exception>
        public void LoadRelationSource(ConnectionWidgetEntity connectionWidget, Dictionary <string, IEntity> scope)
        {
            if (connectionWidget == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            bool closeConnection = false;

            try
            {
                // Create a new connection if needed
                if (dbConnection == null || dbConnection.State.CompareTo(ConnectionState.Closed) == 0)
                {
                    closeConnection = true;
                    dbConnection    = dataAccess.GetNewConnection();
                    dbConnection.Open();
                }
                // Create a command

                string           cmdText    = "SELECT idSource FROM [ConnectionWidget] WHERE idConnectionWidget = @idConnectionWidget";
                IDbCommand       sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                IDbDataParameter parameter  = dataAccess.GetNewDataParameter("@idConnectionWidget", DbType.Int32);
                // Set command parameters values

                parameter.Value = connectionWidget.Id;
                sqlCommand.Parameters.Add(parameter);
                // Execute commands

                object idRelation = sqlCommand.ExecuteScalar();
                if (idRelation != null && ((int)idRelation) > 0)
                {
                    // Create data access objects and set connection objects
                    ConnectionPointDataAccess connectionPointDataAccess = new ConnectionPointDataAccess();
                    connectionPointDataAccess.SetConnectionObjects(dbConnection, dbTransaction);
                    // Load related object

                    connectionWidget.Source = connectionPointDataAccess.Load(((int)idRelation), true, scope);
                }
            }
            catch (DbException dbException)
            {
                // Catch and rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if initiated by me
                if (closeConnection)
                {
                    dbConnection.Close();
                }
            }
        }
        private void FillSaveParameters(ConnectionWidgetEntity connectionWidget, IDbCommand sqlCommand)
        {
            IDbDataParameter parameter;

            parameter = dataAccess.GetNewDataParameter("@idTarget", DbType.Int32);

            parameter.Value = connectionWidget.IdTarget;
            sqlCommand.Parameters.Add(parameter);
            parameter = dataAccess.GetNewDataParameter("@idSource", DbType.Int32);

            parameter.Value = connectionWidget.IdSource;
            sqlCommand.Parameters.Add(parameter);
            parameter = dataAccess.GetNewDataParameter("@idCustomerServiceData", DbType.Int32);

            parameter.Value = connectionWidget.IdCustomerServiceData;
            sqlCommand.Parameters.Add(parameter);
        }
示例#3
0
        public static string Relation(ConnectionWidgetEntity connection)
        {
            // Reemplaza elementos de plantillas
            string tpl = RelationTemplate;

            // Fuente
            if (connection.Source.ParentComponent.ComponentType == (int)ComponentType.DataSource)
            {
                tpl = tpl.Replace("%SOURCE%", Utilities.GetValidIdentifier(connection.Source.ParentComponent.InputDataContext.Name, true));
            }
            else if (connection.Source.ParentComponent.ComponentType == (int)ComponentType.FormMenuItem)
            {
                tpl = tpl.Replace("%SOURCE%", GetNameForComponent(connection.Source.ParentComponent.ParentComponent, true));
            }
            else
            {
                tpl = tpl.Replace("%SOURCE%", GetNameForComponent(connection.Source.ParentComponent, true));
            }
            // Opción
            if (connection.Source.ParentComponent.ComponentType == (int)ComponentType.FormMenuItem)
            {
                tpl = tpl.Replace("%OPTION%", "Option = \"" + Utilities.GetValidIdentifier(connection.Source.ParentComponent.Text, true) + "\";");
            }
            else
            {
                tpl = tpl.Replace("%OPTION%", "");
            }
            // Destino
            if (connection.Target.ParentComponent.ComponentType == (int)ComponentType.DataSource)
            {
                tpl = tpl.Replace("%TARGET%", Utilities.GetValidIdentifier(connection.Target.ParentComponent.OutputDataContext.Name, true));
            }
            else
            {
                tpl = tpl.Replace("%TARGET%", GetNameForComponent(connection.Target.ParentComponent, true));
            }
            return(tpl);
        }
        /// <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);
        }
        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>
        /// Function to load a ConnectionWidgetEntity from database.
        /// </summary>
        /// <param name="id">The ID of the record to load</param>
        /// <param name="loadRelation">if is true load the relation</param>
        /// <param name="scope">Internal structure used to avoid circular reference locks, must be provided if calling from other data access object</param>
        /// <returns>The entity instance</returns>
        /// <exception cref="UtnEmallDataAccessException">
        /// If a DbException occurs while accessing the database.
        /// </exception>
        public ConnectionWidgetEntity Load(int id, bool loadRelation, Dictionary <string, IEntity> scope)
        {
            // Build a key for internal scope object
            string scopeKey = id.ToString(NumberFormatInfo.InvariantInfo) + "ConnectionWidget";

            if (scope != null)
            {
                // If scope contains the object it was already loaded,
                // return it to avoid circular references
                if (scope.ContainsKey(scopeKey))
                {
                    return((ConnectionWidgetEntity)scope[scopeKey]);
                }
            }
            else
            {
                // If there isn't a current scope create one
                scope = new Dictionary <string, IEntity>();
            }

            ConnectionWidgetEntity connectionWidget = null;

            // Check if the entity was already loaded by current data access object
            // and return it if that is the case

            if (inMemoryEntities.ContainsKey(id))
            {
                connectionWidget = inMemoryEntities[id];
                // Add current object to current load scope

                scope.Add(scopeKey, connectionWidget);
            }
            else
            {
                bool closeConnection = false;
                try
                {
                    // Open a new connection if it isn't on a transaction
                    if (dbConnection == null || dbConnection.State.CompareTo(ConnectionState.Closed) == 0)
                    {
                        closeConnection = true;
                        dbConnection    = dataAccess.GetNewConnection();
                        dbConnection.Open();
                    }

                    string cmdText = "SELECT idConnectionWidget, idTarget, idSource, idCustomerServiceData, timestamp FROM [ConnectionWidget] WHERE idConnectionWidget = @idConnectionWidget";
                    // Create the command

                    IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                    // Create the Id parameter for the query

                    IDbDataParameter parameter = dataAccess.GetNewDataParameter("@idConnectionWidget", DbType.Int32);
                    parameter.Value = id;
                    sqlCommand.Parameters.Add(parameter);
                    // Use a DataReader to get data from db

                    IDataReader reader = sqlCommand.ExecuteReader();
                    connectionWidget = new ConnectionWidgetEntity();

                    if (reader.Read())
                    {
                        // Load fields of entity
                        connectionWidget.Id = reader.GetInt32(0);

                        connectionWidget.IdTarget = reader.GetInt32(1);
                        connectionWidget.IdSource = reader.GetInt32(2);
                        connectionWidget.IdCustomerServiceData = reader.GetInt32(3);
                        // Add current object to the scope

                        scope.Add(scopeKey, connectionWidget);
                        // Add current object to cache of loaded entities

                        inMemoryEntities.Add(connectionWidget.Id, connectionWidget);
                        // Read the timestamp and set new and changed properties

                        connectionWidget.Timestamp = reader.GetDateTime(4);
                        connectionWidget.IsNew     = false;
                        connectionWidget.Changed   = false;
                        // Close the reader

                        reader.Close();
                        // Load related objects if required

                        if (loadRelation)
                        {
                            LoadRelationTarget(connectionWidget, scope);
                            LoadRelationSource(connectionWidget, scope);
                        }
                    }
                    else
                    {
                        reader.Close();
                    }
                }
                catch (DbException dbException)
                {
                    // Catch DBException and rethrow as custom exception
                    throw new UtnEmallDataAccessException(dbException.Message, dbException);
                }
                finally
                {
                    // Close connection if it was opened by ourself
                    if (closeConnection)
                    {
                        dbConnection.Close();
                    }
                }
            }
            // Return the loaded entity
            return(connectionWidget);
        }
        /// <summary>
        /// Function to Delete a ConnectionWidgetEntity from database.
        /// </summary>
        /// <param name="connectionWidget">ConnectionWidgetEntity to delete</param>
        /// <param name="scope">Internal structure to avoid circular reference locks. Must provide an instance while calling from other data access object.</param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="connectionWidget"/> is not a <c>ConnectionWidgetEntity</c>.
        /// </exception>
        /// <exception cref="UtnEmallDataAccessException">
        /// If an DbException occurs in the try block while accessing the database.
        /// </exception>
        public void Delete(ConnectionWidgetEntity connectionWidget, Dictionary <string, IEntity> scope)
        {
            if (connectionWidget == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            try
            {
                // Open connection and initialize a transaction if needed
                if (!isGlobalTransaction)
                {
                    dbConnection = dataAccess.GetNewConnection();
                    dbConnection.Open();
                    dbTransaction = dbConnection.BeginTransaction();
                }
                // Reload the entity to ensure deletion of older data

                connectionWidget = this.Load(connectionWidget.Id, true);
                if (connectionWidget == null)
                {
                    throw new UtnEmallDataAccessException("Error retrieving data while trying to delete.");
                }
                // Create a command for delete
                string     cmdText    = "DeleteConnectionWidget";
                IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                // Add values to parameters

                IDbDataParameter parameterID = dataAccess.GetNewDataParameter("@idConnectionWidget", DbType.Int32);
                parameterID.Value = connectionWidget.Id;
                sqlCommand.Parameters.Add(parameterID);
                // Execute the command

                sqlCommand.ExecuteNonQuery();
                // Delete related objects
                // Commit transaction if is mine
                if (!isGlobalTransaction)
                {
                    dbTransaction.Commit();
                }
                // Remove entity from loaded objects

                inMemoryEntities.Remove(connectionWidget.Id);
                // Remove entity from current internal scope

                if (scope != null)
                {
                    string scopeKey = connectionWidget.Id.ToString(NumberFormatInfo.InvariantInfo) + "ConnectionWidget";
                    scope.Remove(scopeKey);
                }
            }
            catch (DbException dbException)
            {
                // Rollback transaction
                if (!isGlobalTransaction)
                {
                    dbTransaction.Rollback();
                }
                // Rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if it was initiated by this instance
                if (!isGlobalTransaction)
                {
                    dbConnection.Close();
                    dbConnection  = null;
                    dbTransaction = null;
                }
            }
        }
 /// <summary>
 /// Function to Delete a ConnectionWidgetEntity from database.
 /// </summary>
 /// <param name="connectionWidget">ConnectionWidgetEntity to delete</param>
 /// <exception cref="ArgumentNullException">
 /// If <paramref name="connectionWidget"/> is not a <c>ConnectionWidgetEntity</c>.
 /// </exception>
 /// <exception cref="UtnEmallDataAccessException">
 /// If an DbException occurs in the try block while accessing the database.
 /// </exception>
 public void Delete(ConnectionWidgetEntity connectionWidget)
 {
     Delete(connectionWidget, null);
 }
        /// <summary>
        /// Function to Save a ConnectionWidgetEntity in the database.
        /// </summary>
        /// <param name="connectionWidget">ConnectionWidgetEntity to save</param>
        /// <param name="scope">Interna structure to avoid circular reference locks. Provide an instance when calling from other data access object.</param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="connectionWidget"/> is not a <c>ConnectionWidgetEntity</c>.
        /// </exception>
        /// <exception cref="UtnEmallDataAccessException">
        /// If an DbException occurs in the try block while accessing the database.
        /// </exception>
        public void Save(ConnectionWidgetEntity connectionWidget, Dictionary <string, IEntity> scope)
        {
            if (connectionWidget == null)
            {
                throw new ArgumentException("The argument can't be null");
            }
            // Create a unique key to identify the object in the internal scope
            string scopeKey = connectionWidget.Id.ToString(NumberFormatInfo.InvariantInfo) + "ConnectionWidget";

            if (scope != null)
            {
                // If it's on the scope return it, don't save again
                if (scope.ContainsKey(scopeKey))
                {
                    return;
                }
            }
            else
            {
                // Create a new scope if it's not provided
                scope = new Dictionary <string, IEntity>();
            }

            try
            {
                // Open a DbConnection and a new transaction if it isn't on a higher level one
                if (!isGlobalTransaction)
                {
                    dbConnection = dataAccess.GetNewConnection();
                    dbConnection.Open();
                    dbTransaction = dbConnection.BeginTransaction();
                }

                string commandName = "";
                bool   isUpdate    = false;
                // Check if it is an insert or update command

                if (connectionWidget.IsNew || !DataAccessConnection.ExistsEntity(connectionWidget.Id, "ConnectionWidget", "idConnectionWidget", dbConnection, dbTransaction))
                {
                    commandName = "SaveConnectionWidget";
                }
                else
                {
                    isUpdate    = true;
                    commandName = "UpdateConnectionWidget";
                }
                // Create a db command
                IDbCommand sqlCommand = dataAccess.GetNewCommand(commandName, dbConnection, dbTransaction);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                // Add parameters values to current command

                IDbDataParameter parameter;
                if (isUpdate)
                {
                    parameter       = dataAccess.GetNewDataParameter("@idConnectionWidget", DbType.Int32);
                    parameter.Value = connectionWidget.Id;
                    sqlCommand.Parameters.Add(parameter);
                }

                FillSaveParameters(connectionWidget, sqlCommand);
                // Execute the command
                if (isUpdate)
                {
                    sqlCommand.ExecuteNonQuery();
                }
                else
                {
                    IDbDataParameter parameterIdOutput = dataAccess.GetNewDataParameter("@idConnectionWidget", DbType.Int32);
                    parameterIdOutput.Direction = ParameterDirection.ReturnValue;
                    sqlCommand.Parameters.Add(parameterIdOutput);

                    sqlCommand.ExecuteNonQuery();
                    connectionWidget.Id = Convert.ToInt32(parameterIdOutput.Value, NumberFormatInfo.InvariantInfo);
                }

                scopeKey = connectionWidget.Id.ToString(NumberFormatInfo.InvariantInfo) + "ConnectionWidget";
                // Add entity to current internal scope

                scope.Add(scopeKey, connectionWidget);
                // Save collections of related objects to current entity
                // Save objects related to current entity
                // Update
                // Close transaction if initiated by me
                if (!isGlobalTransaction)
                {
                    dbTransaction.Commit();
                }
                // Update new and changed flags

                connectionWidget.IsNew   = false;
                connectionWidget.Changed = false;
            }
            catch (DbException dbException)
            {
                // Rollback transaction
                if (!isGlobalTransaction)
                {
                    dbTransaction.Rollback();
                }
                // Rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if initiated by me
                if (!isGlobalTransaction)
                {
                    dbConnection.Close();
                    dbConnection  = null;
                    dbTransaction = null;
                }
            }
        }
 /// <summary>
 /// Function to Save a ConnectionWidgetEntity in the database.
 /// </summary>
 /// <param name="connectionWidget">ConnectionWidgetEntity to save</param>
 /// <exception cref="ArgumentNullException">
 /// if <paramref name="connectionWidget"/> is not a <c>ConnectionWidgetEntity</c>.
 /// </exception>
 /// <exception cref="UtnEmallDataAccessException">
 /// If an DbException occurs in the try block while accessing the database.
 /// </exception>
 public void Save(ConnectionWidgetEntity connectionWidget)
 {
     Save(connectionWidget, null);
 }