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;
             }
         }
     }
 }
        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);
            }
        }
        protected virtual string GenerateOutputFileName(ActionArgs args, string outputFileName)
        {
            args.CommandArgument = args.CommandName;
            args.CommandName     = "FileName";
            List <FieldValue> values = new List <FieldValue>();

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

            foreach (FieldValue v in result.Values)
            {
                if (v.Name == "FileName")
                {
                    outputFileName = Convert.ToString(v.Value);
                    break;
                }
            }
            return(outputFileName);
        }
 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);
 }
        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;
                        }
                    }
                }
            }
        }
        private void ExecuteDataExport(ActionArgs args, ActionResult result)
        {
            if (!(String.IsNullOrEmpty(args.CommandArgument)))
            {
                string[] arguments = args.CommandArgument.Split(',');
                if (arguments.Length > 0)
                {
                    bool 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);
                }
            }
            PageRequest 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
            string       fileName = Path.GetTempFileName();
            StreamWriter writer   = File.CreateText(fileName);

            try
            {
                ViewPage 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 (DataConnection connection = CreateConnection(this))
                {
                    DbCommand selectCommand = CreateCommand(connection);
                    if ((selectCommand == null) && _serverRules.EnableResultSet)
                    {
                        PopulatePageFields(page);
                        EnsurePageFields(page, null);
                    }
                    ConfigureCommand(selectCommand, page, CommandConfigurationType.Select, null);
                    DbDataReader 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));
        }
 void IActionHandler.ExecuteAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.Execute);
     ExecuteAction(args, result);
 }
 void IActionHandler.AfterSqlAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.After);
     AfterSqlAction(args, result);
 }
 void IActionHandler.BeforeSqlAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.Before);
     BeforeSqlAction(args, result);
 }
 protected virtual void ExecuteAction(ActionArgs args, ActionResult result)
 {
 }
 protected virtual void AfterSqlAction(ActionArgs args, ActionResult result)
 {
 }
 protected virtual void BeforeSqlAction(ActionArgs args, ActionResult result)
 {
 }
        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);
        }