Exemplo n.º 1
0
        protected override ActionContext LoadInternal(ISqlConnectionInfo connection, int id)
        {
            IDatabase database = connection.Database;

            if (database == null)
            {
                throw new ArgumentNullException("database", "Error initializing database connection.");
            }
            string sqlCmdText = string.Empty;

            try
            {
                sqlCmdText = "SELECT " +
                             ActionContextTable.GetColumnNames("[ac]") +
                             (this.Depth > 0 ? "," + ActionContextGroupTable.GetColumnNames("[ac_acg]") : string.Empty) +
                             " FROM [core].[ActionContext] AS [ac] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[ActionContextGroup] AS [ac_acg] ON [ac].[ActionContextGroupID] = [ac_acg].[ActionContextGroupID] ";
                }
                sqlCmdText += "WHERE [ac].[ActionContextID] = @ActionContextID;";

                SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand;
                sqlCmd.Parameters.AddWithValue("@ActionContextID", id);
                SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader;

                if (!sqlReader.HasRows || !sqlReader.Read())
                {
                    IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "loadinternal", "notfound"), "ActionContext could not be loaded by id as it was not found.", sqlCmdText, this, connection, id);
                    if (this.Logger.IsWarnEnabled)
                    {
                        this.Logger.Warn(builder.ToString());
                    }
                    sqlReader.Close();
                    return(null);
                }

                SqlQuery query = new SqlQuery(sqlReader);

                ActionContextTable      acTable     = new ActionContextTable(query);
                ActionContextGroupTable ac_acgTable = (this.Depth > 0) ? new ActionContextGroupTable(query) : null;


                ActionContextGroup ac_acgObject = (this.Depth > 0) ? ac_acgTable.CreateInstance() : null;
                ActionContext      acObject     = acTable.CreateInstance(ac_acgObject);
                sqlReader.Close();

                return(acObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "loadinternal", "exception"), "ActionContext could not be loaded by id. See exception for details.", sqlCmdText, ex, this, connection, id);
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.Error(builder.ToString(), ex);
                }
                throw new DataOperationException(DataOperation.Load, "ActionContext", "Exception while loading ActionContext object from database. See inner exception for details.", ex);
            }
        }
Exemplo n.º 2
0
 public ActionContext CreateInstance(ActionContextGroup actionContextGroup)
 {
     if (!this.HasData)
     {
         return(null);
     }
     return(new ActionContext(this.ActionContextID, actionContextGroup ?? new ActionContextGroup(this.ActionContextGroupID), this.Updated, this.Created));
 }
Exemplo n.º 3
0
        public ActionContext Load(ISqlConnectionInfo connection, SqlQueryParameters parameters)
        {
            IDatabase database = connection.Database;

            if (database == null)
            {
                throw new ArgumentNullException("database", "Error initializing database connection.");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            string sqlCmdText = string.Empty;

            try
            {
                sqlCmdText = "SELECT {0} " +
                             ActionContextTable.GetColumnNames("[ac]") +
                             (this.Depth > 0 ? "," + ActionContextGroupTable.GetColumnNames("[ac_acg]") : string.Empty) +
                             " FROM [core].[ActionContext] AS [ac] ";
                if (this.Depth > 0)
                {
                    sqlCmdText += "INNER  JOIN [core].[ActionContextGroup] AS [ac_acg] ON [ac].[ActionContextGroupID] = [ac_acg].[ActionContextGroupID] ";
                }


                parameters.Top = 1;
                sqlCmdText     = parameters.BuildQuery(sqlCmdText);
                SqlCommand sqlCmd = database.Add(sqlCmdText) as SqlCommand;
                foreach (KeyValuePair <string, object> argument in parameters.Arguments)
                {
                    sqlCmd.Parameters.AddWithValue("@" + argument.Key, argument.Value);
                }

                SqlDataReader sqlReader = database.Add(sqlCmd) as SqlDataReader;

                if (!sqlReader.HasRows || !sqlReader.Read())
                {
                    IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "customload", "notfound"), "ActionContext could not be loaded using custom logic as it was not found.", sqlCmdText, this, connection, parameters);
                    if (this.Logger.IsDebugEnabled)
                    {
                        this.Logger.Debug(builder.ToString());
                    }
                    sqlReader.Close();
                    return(null);
                }

                SqlQuery query = new SqlQuery(sqlReader);

                ActionContextTable      acTable     = new ActionContextTable(query);
                ActionContextGroupTable ac_acgTable = (this.Depth > 0) ? new ActionContextGroupTable(query) : null;


                ActionContextGroup ac_acgObject = (this.Depth > 0) ? ac_acgTable.CreateInstance() : null;
                ActionContext      acObject     = acTable.CreateInstance(ac_acgObject);
                sqlReader.Close();

                return(acObject);
            }
            catch (Exception ex)
            {
                database.HandleException(ex);
                IMessageBuilder builder = new DbLogMessageBuilder(new LogErrorCode("ac", "customload", "exception"), "ActionContext could not be loaded using custom logic. See exception for details.", sqlCmdText, ex, this, connection, parameters);
                if (this.Logger.IsErrorEnabled)
                {
                    this.Logger.Error(builder.ToString(), ex);
                }
                throw new DataOperationException(DataOperation.Load, "ActionContext", "Exception while loading (custom/single) ActionContext object from database. See inner exception for details.", ex);
            }
        }