Exemple #1
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);
            }
        }
Exemple #2
0
 private bool InternalExecuteMethod(ActionArgs args, ActionResult result, ActionPhase phase, bool viewMatch, bool argumentMatch)
 {
     _arguments = args;
     _result    = result;
     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) && ((!(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 = args[p.Name];
                                 if (v != null)
                                 {
                                     if (p.ParameterType.Equals(typeof(FieldValue)))
                                     {
                                         arguments[i] = v;
                                     }
                                     else
                                     {
                                         try
                                         {
                                             if (p.ParameterType.Equals(typeof(Guid)))
                                             {
                                                 arguments[i] = new Guid(Convert.ToString(v.Value));
                                             }
                                             else
                                             if (p.ParameterType.IsGenericType)
                                             {
                                                 object argumentValue = v.Value;
                                                 argumentValue = Convert.ChangeType(argumentValue, p.ParameterType.GetProperty("Value").PropertyType);
                                                 arguments[i]  = argumentValue;
                                             }
                                             else
                                             {
                                                 object argumentValue = v.Value;
                                                 argumentValue = Convert.ChangeType(argumentValue, p.ParameterType);
                                                 arguments[i]  = argumentValue;
                                             }
                                         }
                                         catch (Exception)
                                         {
                                         }
                                     }
                                 }
                             }
                             method.Invoke(this, arguments);
                             return(true);
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }