Example #1
0
 ActionResult IDataController.Execute(string controller, string view, ActionArgs args)
 {
     ActionResult result = new ActionResult();
     SelectView(controller, view);
     try
     {
         _serverRules = _config.CreateBusinessRules();
         if (_serverRules == null)
             _serverRules = CreateBusinessRules();
         IActionHandler handler = ((IActionHandler)(_serverRules));
         if (_config.PlugIn != null)
             _config.PlugIn.PreProcessArguments(args, result, CreateViewPage());
         if (args.SqlCommandType != CommandConfigurationType.None)
             using (DbConnection connection = CreateConnection())
             {
                 DbCommand command = CreateCommand(connection, args);
                 if ((args.SelectedValues != null) && (((args.LastCommandName == "BatchEdit") && (args.CommandName == "Update")) || ((args.CommandName == "Delete") && (args.SelectedValues.Length > 1))))
                 {
                     ViewPage page = CreateViewPage();
                     PopulatePageFields(page);
                     string originalCommandText = command.CommandText;
                     foreach (string sv in args.SelectedValues)
                     {
                         result.Canceled = false;
                         _serverRules.ClearBlackAndWhiteLists();
                         string[] key = sv.Split(',');
                         int keyIndex = 0;
                         foreach (FieldValue v in args.Values)
                         {
                             DataField field = page.FindField(v.Name);
                             if (field != null)
                                 if (!(field.IsPrimaryKey))
                                     v.Modified = true;
                                 else
                                     if (v.Name == field.Name)
                                     {
                                         v.OldValue = key[keyIndex];
                                         v.Modified = false;
                                         keyIndex++;
                                     }
                         }
                         ExecutePreActionCommands(args, result, connection);
                         if (handler != null)
                             handler.BeforeSqlAction(args, result);
                         else
                             _serverRules.ExecuteServerRules(args, result, ActionPhase.Before);
                         if ((result.Errors.Count == 0) && !(result.Canceled))
                         {
                             ConfigureCommand(command, null, args.SqlCommandType, args.Values);
                             result.RowsAffected = (result.RowsAffected + TransactionManager.ExecuteNonQuery(command));
                             if (handler != null)
                                 handler.AfterSqlAction(args, result);
                             else
                                 _serverRules.ExecuteServerRules(args, result, ActionPhase.After);
                             command.CommandText = originalCommandText;
                             command.Parameters.Clear();
                             if (_config.PlugIn != null)
                                 _config.PlugIn.ProcessArguments(args, result, page);
                         }
                     }
                 }
                 else
                 {
                     ExecutePreActionCommands(args, result, connection);
                     if (handler != null)
                         handler.BeforeSqlAction(args, result);
                     else
                         _serverRules.ExecuteServerRules(args, result, ActionPhase.Before);
                     if ((result.Errors.Count == 0) && !(result.Canceled))
                     {
                         if (ConfigureCommand(command, null, args.SqlCommandType, args.Values))
                         {
                             result.RowsAffected = TransactionManager.ExecuteNonQuery(args, result, CreateViewPage(), command);
                             if (result.RowsAffected == 0)
                             {
                                 result.RowNotFound = true;
                                 result.Errors.Add(Localizer.Replace("RecordChangedByAnotherUser", "The record has been changed by another user."));
                             }
                             else
                                 ExecutePostActionCommands(args, result, connection);
                         }
                         if (handler != null)
                             handler.AfterSqlAction(args, result);
                         else
                             _serverRules.ExecuteServerRules(args, result, ActionPhase.After);
                         if (_config.PlugIn != null)
                             _config.PlugIn.ProcessArguments(args, result, CreateViewPage());
                     }
                 }
             }
         else
             if (args.CommandName.StartsWith("Export"))
                 ExecuteDataExport(args, result);
             else
                 if (args.CommandName.Equals("PopulateDynamicLookups"))
                     PopulateDynamicLookups(args, result);
                 else
                     if (args.CommandName.Equals("ProcessImportFile"))
                         ImportProcessor.Execute(args);
                     else
                         if (args.CommandName.Equals("Execute"))
                             using (DbConnection connection = CreateConnection())
                             {
                                 DbCommand command = CreateCommand(connection, args);
                                 TransactionManager.ExecuteNonQuery(command);
                             }
                         else
                             _serverRules.ProcessSpecialActions(args, result);
     }
     catch (Exception ex)
     {
         if (ex.GetType() == typeof(System.Reflection.TargetInvocationException))
             ex = ex.InnerException;
         HandleException(ex, args, result);
     }
     result.EnsureJsonCompatibility();
     return result;
 }