Exemplo n.º 1
0
 void IPlugIn.PreProcessArguments(ActionArgs args, ActionResult result, ViewPage page)
 {
     _annotations = new List <FieldValue>();
     if (args.Values != null)
     {
         foreach (FieldValue v in args.Values)
         {
             if (v.Name.StartsWith("_Annotation_") && v.Modified)
             {
                 _annotations.Add(v);
                 v.Modified = false;
             }
         }
     }
 }
Exemplo n.º 2
0
        protected virtual void ExecuteMethod(ActionArgs args, ActionResult result, ActionPhase phase)
        {
            bool match = InternalExecuteMethod(args, result, phase, true, true);

            if (!(match))
            {
                match = InternalExecuteMethod(args, result, phase, true, false);
            }
            if (!(match))
            {
                match = InternalExecuteMethod(args, result, phase, false, true);
            }
            if (!(match))
            {
                InternalExecuteMethod(args, result, phase, false, false);
            }
        }
Exemplo n.º 3
0
        public static int ExecuteNonQuery(ActionArgs args, ActionResult result, ViewPage page, DbCommand command)
        {
            TransactionManager tm = Create(args.Transaction);

            if (tm == null)
            {
                return(command.ExecuteNonQuery());
            }
            else
            if (tm.Status == "complete")
            {
                return(command.ExecuteNonQuery());
            }
            int rowsAffected = tm.ExecuteAction(args, result, page);

            tm.Arguments.Add(args);
            return(rowsAffected);
        }
Exemplo n.º 4
0
        protected virtual string GenerateOutputFileName(ActionArgs args, string outputFileName)
        {
            args.CommandArgument = args.CommandName;
            args.CommandName     = "FileName";
            var values = new List <FieldValue>();

            values.Add(new FieldValue("FileName", outputFileName));
            args.Values = values.ToArray();
            var result = ControllerFactory.CreateDataController().Execute(args.Controller, args.View, args);

            foreach (var v in result.Values)
            {
                if (v.Name == "FileName")
                {
                    outputFileName = Convert.ToString(v.Value);
                    break;
                }
            }
            return(outputFileName);
        }
Exemplo n.º 5
0
 void IActionHandler.ExecuteAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.Execute);
     ExecuteAction(args, result);
 }
Exemplo n.º 6
0
 void IActionHandler.AfterSqlAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.After);
     AfterSqlAction(args, result);
 }
Exemplo n.º 7
0
 void IActionHandler.BeforeSqlAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.Before);
     BeforeSqlAction(args, result);
 }
Exemplo n.º 8
0
 protected virtual void ExecuteAction(ActionArgs args, ActionResult result)
 {
 }
Exemplo n.º 9
0
 protected virtual void AfterSqlAction(ActionArgs args, ActionResult result)
 {
 }
Exemplo n.º 10
0
 protected virtual void BeforeSqlAction(ActionArgs args, ActionResult result)
 {
 }
Exemplo n.º 11
0
        private bool InternalExecuteMethod(ActionArgs args, ActionResult result, ActionPhase phase, bool viewMatch, bool argumentMatch)
        {
            _arguments = args;
            _result    = result;
            bool success = false;

            MethodInfo[] methods = GetType().GetMethods((BindingFlags.Public | (BindingFlags.NonPublic | BindingFlags.Instance)));
            foreach (MethodInfo method in methods)
            {
                object[] filters = method.GetCustomAttributes(typeof(ControllerActionAttribute), true);
                foreach (ControllerActionAttribute action in filters)
                {
                    if (((action.Controller == args.Controller) || (!(String.IsNullOrEmpty(args.Controller)) && Regex.IsMatch(args.Controller, action.Controller))) && ((!(viewMatch) && String.IsNullOrEmpty(action.View)) || (action.View == args.View)))
                    {
                        if ((action.CommandName == args.CommandName) && ((!(argumentMatch) && String.IsNullOrEmpty(action.CommandArgument)) || (action.CommandArgument == args.CommandArgument)))
                        {
                            if (action.Phase == phase)
                            {
                                ParameterInfo[] parameters = method.GetParameters();
                                if ((parameters.Length == 2) && ((parameters[0].ParameterType == typeof(ActionArgs)) && (parameters[1].ParameterType == typeof(ActionResult))))
                                {
                                    method.Invoke(this, new object[] {
                                        args,
                                        result
                                    });
                                }
                                else
                                {
                                    object[] arguments = new object[parameters.Length];
                                    for (int i = 0; (i < parameters.Length); i++)
                                    {
                                        ParameterInfo p = parameters[i];
                                        FieldValue    v = SelectFieldValueObject(p.Name);
                                        if (v != null)
                                        {
                                            if (p.ParameterType.Equals(typeof(FieldValue)))
                                            {
                                                arguments[i] = v;
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    arguments[i] = DataControllerBase.ConvertToType(p.ParameterType, v.Value);
                                                }
                                                catch (Exception)
                                                {
                                                }
                                            }
                                        }
                                    }
                                    method.Invoke(this, arguments);
                                    success = true;
                                }
                            }
                        }
                    }
                }
            }
            return(success);
        }
Exemplo n.º 12
0
 public virtual CommitResult Commit(JArray log)
 {
     _commitResult = new CommitResult();
     try
     {
         if (log.Count > 0)
         {
             using (DataConnection connection = new DataConnection(LoadConfig(((string)(log[0]["controller"]))).ConnectionStringName, true))
             {
                 int    index            = -1;
                 int    sequence         = -1;
                 int    lastSequence     = sequence;
                 string transactionScope = ((string)(ApplicationServices.Settings("odp.transactions.scope")));
                 for (int i = 0; (i < log.Count); i++)
                 {
                     JToken     entry       = log[i];
                     string     controller  = ((string)(entry["controller"]));
                     string     view        = ((string)(entry["view"]));
                     ActionArgs executeArgs = entry["args"].ToObject <ActionArgs>();
                     if (executeArgs.Sequence.HasValue)
                     {
                         sequence = executeArgs.Sequence.Value;
                         if ((transactionScope == "sequence") && (sequence != lastSequence && (i > 0)))
                         {
                             connection.Commit();
                             _commitResult.Sequence = lastSequence;
                             connection.BeginTransaction();
                         }
                         lastSequence = sequence;
                     }
                     ControllerConfiguration config = LoadConfig(executeArgs.Controller);
                     ProcessArguments(config, executeArgs);
                     ActionResult executeResult = ControllerFactory.CreateDataController().Execute(controller, view, executeArgs);
                     if (executeResult.Errors.Count > 0)
                     {
                         index = i;
                         _commitResult.Index  = index;
                         _commitResult.Errors = executeResult.Errors.ToArray();
                         break;
                     }
                     else
                     {
                         ProcessResult(config, executeResult);
                     }
                 }
                 if (index == -1)
                 {
                     connection.Commit();
                     _commitResult.Sequence = sequence;
                 }
                 else
                 {
                     connection.Rollback();
                     _commitResult.Index = index;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         _commitResult.Errors = new string[] {
             ex.Message
         };
         _commitResult.Index = 0;
     }
     return(_commitResult);
 }
Exemplo n.º 13
0
 public static void Execute(ActionArgs args)
 {
 }
Exemplo n.º 14
0
        public int ExecuteAction(ActionArgs args, ActionResult result, ViewPage page)
        {
            DataTable t = GetTable(args.Controller, null);

            if (args.CommandName == "Insert")
            {
                DataRow r = t.NewRow();
                foreach (FieldValue v in args.Values)
                {
                    DataField f = page.FindField(v.Name);
                    if (f.IsPrimaryKey && f.ReadOnly)
                    {
                        object key = null;
                        if (f.Type == "Guid")
                        {
                            key = Guid.NewGuid();
                        }
                        else
                        if (!(PrimaryKeys.TryGetValue(args.Controller, out key)))
                        {
                            key = -1;
                            PrimaryKeys.Add(args.Controller, key);
                        }
                        else
                        {
                            key = (Convert.ToInt32(key) - 1);
                            PrimaryKeys[args.Controller] = key;
                        }
                        r[v.Name] = key;
                        result.Values.Add(new FieldValue(v.Name, key));
                        FieldValue fv = args.SelectFieldValueObject(v.Name);
                        fv.NewValue = key;
                        fv.Modified = true;
                    }
                    else
                    if (v.Modified)
                    {
                        if (v.NewValue == null)
                        {
                            r[v.Name] = DBNull.Value;
                        }
                        else
                        {
                            r[v.Name] = v.NewValue;
                        }
                    }
                }
                t.Rows.Add(r);
                return(1);
            }
            else
            {
                DataRow targetRow = null;
                foreach (DataRow r in t.Rows)
                {
                    bool matched = true;
                    foreach (DataField f in page.Fields)
                    {
                        if (f.IsPrimaryKey)
                        {
                            object kv  = r[f.Name];
                            object kv2 = args.SelectFieldValueObject(f.Name).OldValue;
                            if (((kv == null) || (kv2 == null)) || !((kv.ToString() == kv2.ToString())))
                            {
                                matched = false;
                                break;
                            }
                        }
                    }
                    if (matched)
                    {
                        targetRow = r;
                        break;
                    }
                }
                if (targetRow == null)
                {
                    return(0);
                }
                if (args.CommandName == "Delete")
                {
                    t.Rows.Remove(targetRow);
                }
                else
                {
                    foreach (FieldValue v in args.Values)
                    {
                        if (v.Modified)
                        {
                            if (v.NewValue == null)
                            {
                                targetRow[v.Name] = DBNull.Value;
                            }
                            else
                            {
                                targetRow[v.Name] = v.NewValue;
                            }
                        }
                    }
                }
                return(1);
            }
        }
Exemplo n.º 15
0
 public static bool InTransaction(ActionArgs args)
 {
     return(!(String.IsNullOrEmpty(args.Transaction)) && !(args.Transaction.EndsWith(":complete")));
 }
Exemplo n.º 16
0
 public virtual CommitResult Commit(JArray log)
 {
     _commitResult = new CommitResult();
     try
     {
         if (log.Count > 0)
         {
             using (var tx = new DataTransaction(LoadConfig(((string)(log[0]["controller"]))).ConnectionStringName))
             {
                 var index              = -1;
                 var sequence           = -1;
                 var lastSequence       = sequence;
                 var commitedValueCount = _commitResult.Values.Count;
                 var transactionScope   = ((string)(ApplicationServices.Settings("odp.transactions.scope")));
                 for (var i = 0; (i < log.Count); i++)
                 {
                     var entry      = log[i];
                     var controller = ((string)(entry["controller"]));
                     var view       = ((string)(entry["view"]));
                     ActionArgs.Forget();
                     var executeArgs = entry["args"].ToObject <ActionArgs>();
                     if (executeArgs.Sequence.HasValue)
                     {
                         sequence = executeArgs.Sequence.Value;
                         if (transactionScope != "all" && (sequence != lastSequence && (i > 0)))
                         {
                             tx.Commit();
                             _commitResult.Sequence = lastSequence;
                             commitedValueCount     = _commitResult.Values.Count;
                             tx.BeginTransaction();
                         }
                         lastSequence = sequence;
                     }
                     var config = LoadConfig(executeArgs.Controller);
                     ProcessArguments(config, executeArgs);
                     var executeResult = ControllerFactory.CreateDataController().Execute(controller, view, executeArgs);
                     if (executeResult.Errors.Count > 0)
                     {
                         index = i;
                         _commitResult.Index  = index;
                         _commitResult.Errors = executeResult.Errors.ToArray();
                         break;
                     }
                     else
                     {
                         ProcessResult(config, executeResult);
                     }
                 }
                 if (index == -1)
                 {
                     tx.Commit();
                     _commitResult.Sequence = sequence;
                     commitedValueCount     = _commitResult.Values.Count;
                 }
                 else
                 {
                     tx.Rollback();
                     _commitResult.Index = index;
                     _commitResult.Values.RemoveRange(commitedValueCount, (_commitResult.Values.Count - commitedValueCount));
                 }
             }
         }
     }
     catch (Exception ex)
     {
         _commitResult.Errors = new string[] {
             ex.Message
         };
         _commitResult.Index = 0;
     }
     return(_commitResult);
 }
Exemplo n.º 17
0
        protected virtual void ProcessArguments(ControllerConfiguration config, ActionArgs args)
        {
            if (args.Values == null)
            {
                return;
            }
            var values = new FieldValueDictionary(args);

            _pk = null;
            // detect negative primary keys
            var pkNav = config.SelectSingleNode("/c:dataController/c:fields/c:field[@isPrimaryKey=\'true\']");

            if (pkNav != null)
            {
                FieldValue fvo = null;
                if (values.TryGetValue(pkNav.GetAttribute("name", string.Empty), out fvo))
                {
                    var value = 0;
                    if ((fvo.Value != null) && int.TryParse(Convert.ToString(fvo.Value), out value))
                    {
                        if (value < 0)
                        {
                            if (args.CommandName == "Insert")
                            {
                                // request a new row from business rules
                                var newRowRequest = new PageRequest();
                                newRowRequest.Controller       = args.Controller;
                                newRowRequest.View             = args.View;
                                newRowRequest.Inserting        = true;
                                newRowRequest.RequiresMetaData = true;
                                newRowRequest.MetadataFilter   = new string[] {
                                    "fields"
                                };
                                var page = ControllerFactory.CreateDataController().GetPage(newRowRequest.Controller, newRowRequest.View, newRowRequest);
                                if (page.NewRow != null)
                                {
                                    for (var i = 0; (i < page.NewRow.Length); i++)
                                    {
                                        var newValue = page.NewRow[i];
                                        if (newValue != null)
                                        {
                                            var field = page.Fields[i];
                                            if (field.IsPrimaryKey)
                                            {
                                                // resolve the value of the primary key
                                                ResolvePrimaryKey(args.Controller, fvo.Name, value, newValue);
                                                value        = 0;
                                                fvo.NewValue = newValue;
                                            }
                                            else
                                            {
                                                // inject a missing default value in the arguments
                                                FieldValue newFieldValue = null;
                                                if (values.TryGetValue(field.Name, out newFieldValue))
                                                {
                                                    if (!newFieldValue.Modified)
                                                    {
                                                        newFieldValue.NewValue = newValue;
                                                        newFieldValue.Modified = true;
                                                    }
                                                }
                                                else
                                                {
                                                    var newValues = new List <FieldValue>(args.Values);
                                                    newFieldValue = new FieldValue(field.Name, newValue);
                                                    newValues.Add(newFieldValue);
                                                    args.Values        = newValues.ToArray();
                                                    values[field.Name] = newFieldValue;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            // resolve the primary key after the command execution
                            if (value < 0)
                            {
                                if (args.CommandName == "Insert")
                                {
                                    if (pkNav.SelectSingleNode("c:items/@dataController", config.Resolver) == null)
                                    {
                                        _pk          = new FieldValue(fvo.Name, value);
                                        fvo.NewValue = null;
                                        fvo.Modified = false;
                                    }
                                }
                                else
                                {
                                    // otherwise try to resolve the primary key
                                    object resolvedKey = null;
                                    if (_resolvedKeys.TryGetValue(string.Format("{0}${1}", args.Controller, fvo.Value), out resolvedKey))
                                    {
                                        if (fvo.Modified)
                                        {
                                            fvo.NewValue = resolvedKey;
                                        }
                                        else
                                        {
                                            fvo.OldValue = resolvedKey;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // resolve negative foreign keys
            if (_resolvedKeys.Count > 0)
            {
                var fkIterator = config.Select("/c:dataController/c:fields/c:field[c:items/@dataController]");
                while (fkIterator.MoveNext())
                {
                    FieldValue fvo = null;
                    if (values.TryGetValue(fkIterator.Current.GetAttribute("name", string.Empty), out fvo))
                    {
                        var    itemsDataControllerNav = fkIterator.Current.SelectSingleNode("c:items/@dataController", config.Resolver);
                        object resolvedKey            = null;
                        if (_resolvedKeys.TryGetValue(string.Format("{0}${1}", itemsDataControllerNav.Value, fvo.Value), out resolvedKey))
                        {
                            if (fvo.Modified)
                            {
                                fvo.NewValue = resolvedKey;
                            }
                            else
                            {
                                fvo.OldValue = resolvedKey;
                            }
                        }
                    }
                }
            }
            // scan resolved primary keys and look for the one that are matching the keys referenced in SelectedValues, ExternalFilter, or Filter of the action
            foreach (var resolvedKeyInfo in _resolvedKeys.Keys)
            {
                var    separatorIndex     = resolvedKeyInfo.IndexOf("$");
                var    resolvedController = resolvedKeyInfo.Substring(0, separatorIndex);
                var    unresolvedKeyValue = resolvedKeyInfo.Substring((separatorIndex + 1));
                object resolvedKeyValue   = null;
                if ((args.Controller == resolvedController) && _resolvedKeys.TryGetValue(resolvedKeyInfo, out resolvedKeyValue))
                {
                    var resolvedKeyValueAsString = resolvedKeyValue.ToString();
                    // resolve primary key references in SelectedValues
                    if (args.SelectedValues != null)
                    {
                        for (var selectedValueIndex = 0; (selectedValueIndex < args.SelectedValues.Length); selectedValueIndex++)
                        {
                            var selectedKey = Regex.Split(args.SelectedValues[selectedValueIndex], ",");
                            for (var keyValueIndex = 0; (keyValueIndex < selectedKey.Length); keyValueIndex++)
                            {
                                if (selectedKey[keyValueIndex] == unresolvedKeyValue)
                                {
                                    selectedKey[keyValueIndex] = resolvedKeyValueAsString;
                                    args.SelectedValues[selectedValueIndex] = string.Join(",", selectedKey);
                                    selectedKey = null;
                                    break;
                                }
                            }
                            if (selectedKey == null)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 18
0
        private void ExecuteDataExport(ActionArgs args, ActionResult result)
        {
            if (!(string.IsNullOrEmpty(args.CommandArgument)))
            {
                var arguments = args.CommandArgument.Split(',');
                if (arguments.Length > 0)
                {
                    var sameController = (args.Controller == arguments[0]);
                    args.Controller = arguments[0];
                    if (arguments.Length == 1)
                    {
                        args.View = "grid1";
                    }
                    else
                    {
                        args.View = arguments[1];
                    }
                    if (sameController)
                    {
                        args.SortExpression = null;
                    }
                    SelectView(args.Controller, args.View);
                }
            }
            var request = new PageRequest(-1, -1, null, null);

            request.SortExpression = args.SortExpression;
            request.Filter         = args.Filter;
            request.ContextKey     = null;
            request.PageIndex      = 0;
            request.PageSize       = Int32.MaxValue;
            request.View           = args.View;
            if (args.CommandName.EndsWith("Template"))
            {
                request.PageSize = 0;
                args.CommandName = "ExportCsv";
            }
            // store export data to a temporary file
            var fileName = Path.GetTempFileName();
            var writer   = File.CreateText(fileName);

            try
            {
                var page = new ViewPage(request);
                page.ApplyDataFilter(_config.CreateDataFilter(), args.Controller, args.View, null, null, null);
                if (_serverRules == null)
                {
                    _serverRules = _config.CreateBusinessRules();
                    if (_serverRules == null)
                    {
                        _serverRules = CreateBusinessRules();
                    }
                }
                _serverRules.Page = page;
                _serverRules.ExecuteServerRules(request, ActionPhase.Before);
                using (var connection = CreateConnection(this))
                {
                    var selectCommand = CreateCommand(connection);
                    if ((selectCommand == null) && _serverRules.EnableResultSet)
                    {
                        PopulatePageFields(page);
                        EnsurePageFields(page, null);
                    }
                    ConfigureCommand(selectCommand, page, CommandConfigurationType.Select, null);
                    var reader = ExecuteResultSetReader(page);
                    if (reader == null)
                    {
                        reader = selectCommand.ExecuteReader();
                    }
                    if (args.CommandName.EndsWith("Csv"))
                    {
                        ExportDataAsCsv(page, reader, writer);
                    }
                    if (args.CommandName.EndsWith("Rss"))
                    {
                        ExportDataAsRss(page, reader, writer);
                    }
                    if (args.CommandName.EndsWith("Rowset"))
                    {
                        ExportDataAsRowset(page, reader, writer);
                    }
                    reader.Close();
                }
                _serverRules.ExecuteServerRules(request, ActionPhase.After);
            }
            finally
            {
                writer.Close();
            }
            result.Values.Add(new FieldValue("FileName", null, fileName));
        }
Exemplo n.º 19
0
        protected virtual void ProcessArguments(ControllerConfiguration config, ActionArgs args)
        {
            if (args.Values == null)
            {
                return;
            }
            FieldValueDictionary values = new FieldValueDictionary(args);

            _pk = null;
            // detect negative primary keys
            XPathNavigator pkNav = config.SelectSingleNode("/c:dataController/c:fields/c:field[@isPrimaryKey=\'true\']");

            if (pkNav != null)
            {
                FieldValue fv = null;
                if (values.TryGetValue(pkNav.GetAttribute("name", String.Empty), out fv))
                {
                    int value = 0;
                    if ((fv.NewValue != null) && int.TryParse(Convert.ToString(fv.NewValue), out value))
                    {
                        if (value < 0)
                        {
                            if (args.CommandName == "Insert")
                            {
                                // request a new row from business rules
                                PageRequest newRowRequest = new PageRequest();
                                newRowRequest.Controller       = args.Controller;
                                newRowRequest.View             = args.View;
                                newRowRequest.Inserting        = true;
                                newRowRequest.RequiresMetaData = true;
                                newRowRequest.MetadataFilter   = new string[] {
                                    "fields"
                                };
                                ViewPage page = ControllerFactory.CreateDataController().GetPage(newRowRequest.Controller, newRowRequest.View, newRowRequest);
                                if (page.NewRow != null)
                                {
                                    for (int i = 0; (i < page.NewRow.Length); i++)
                                    {
                                        object newValue = page.NewRow[i];
                                        if (newValue != null)
                                        {
                                            DataField field = page.Fields[i];
                                            if (field.IsPrimaryKey)
                                            {
                                                // resolve the value of the primary key
                                                ResolvePrimaryKey(args.Controller, fv.Name, value, newValue);
                                                value       = 0;
                                                fv.NewValue = newValue;
                                            }
                                            else
                                            {
                                                // inject a missing default value in the arguments
                                                FieldValue newFieldValue = null;
                                                if (values.TryGetValue(field.Name, out newFieldValue))
                                                {
                                                    if (!(newFieldValue.Modified))
                                                    {
                                                        newFieldValue.NewValue = newValue;
                                                        newFieldValue.Modified = true;
                                                    }
                                                }
                                                else
                                                {
                                                    List <FieldValue> newValues = new List <FieldValue>(args.Values);
                                                    newFieldValue = new FieldValue(field.Name, newValue);
                                                    newValues.Add(newFieldValue);
                                                    args.Values        = newValues.ToArray();
                                                    values[field.Name] = newFieldValue;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            // resolve the primary key after the command execution
                            if (value < 0)
                            {
                                _pk         = new FieldValue(fv.Name, value);
                                fv.NewValue = null;
                                fv.Modified = false;
                            }
                        }
                    }
                }
            }
            // resolve negative foreign keys
            if (_resolvedKeys.Count > 0)
            {
                XPathNodeIterator fkIterator = config.Select("/c:dataController/c:fields/c:field[c:items/@dataController]");
                while (fkIterator.MoveNext())
                {
                    FieldValue fv = null;
                    if (values.TryGetValue(fkIterator.Current.GetAttribute("name", String.Empty), out fv))
                    {
                        XPathNavigator itemsDataControllerNav = fkIterator.Current.SelectSingleNode("c:items/@dataController", config.Resolver);
                        object         resolvedKey            = null;
                        if (_resolvedKeys.TryGetValue(String.Format("{0}${1}", itemsDataControllerNav.Value, fv.NewValue), out resolvedKey))
                        {
                            fv.NewValue = resolvedKey;
                        }
                    }
                }
            }
        }