Ejemplo n.º 1
0
        public BusinessObjectParameters(params Object[] values)
        {
            string parameterMarker = null;

            for (int i = 0; (i < values.Length); i++)
            {
                object v = values[i];
                if (v is FieldValue)
                {
                    FieldValue fv = ((FieldValue)(v));
                    Add(fv.Name, fv.Value);
                }
                else
                {
                    if (String.IsNullOrEmpty(parameterMarker))
                    {
                        parameterMarker = SqlStatement.GetParameterMarker(String.Empty);
                    }
                    Add((parameterMarker
                         + ("p" + i.ToString())), v);
                }
            }
        }
        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;
                        }
                    }
                }
            }
        }
 public override void ValidateFieldValue(FieldValue fv)
 {
 }
 protected virtual void ExecuteRule(string ruleId)
 {
     MethodInfo[] methods = GetType().GetMethods((BindingFlags.Public | (BindingFlags.NonPublic | BindingFlags.Instance)));
     foreach (MethodInfo method in methods)
     {
         object[] ruleBindings = method.GetCustomAttributes(typeof(RuleAttribute), true);
         foreach (RuleAttribute ra in ruleBindings)
         {
             if (ra.Id == ruleId)
             {
                 BlockRule(ruleId);
                 ParameterInfo[] parameters = method.GetParameters();
                 object[]        arguments  = new object[parameters.Length];
                 for (int i = 0; (i < parameters.Length); i++)
                 {
                     ParameterInfo p = parameters[i];
                     if ((parameters.Length == 1) && p.ParameterType.IsSubclassOf(typeof(BusinessRulesObjectModel)))
                     {
                         object self = p.ParameterType.Assembly.CreateInstance(p.ParameterType.FullName, true, BindingFlags.CreateInstance, null, new object[] {
                             this
                         }, System.Globalization.CultureInfo.CurrentCulture, null);
                         System.Reflection.FieldInfo[] fields = self.GetType().GetFields((BindingFlags.Instance | BindingFlags.NonPublic));
                         foreach (System.Reflection.FieldInfo fi in fields)
                         {
                             string fieldName = fi.Name.Substring("_".Length);
                             if (fieldName.Length == 1)
                             {
                                 fieldName = fieldName.ToUpper();
                             }
                             else
                             {
                                 fieldName = (char.ToUpperInvariant(fieldName[0]) + fieldName.Substring(1));
                             }
                             FieldValue v = SelectFieldValueObject(fieldName);
                             if (v != null)
                             {
                                 try
                                 {
                                     self.GetType().InvokeMember(fi.Name, (BindingFlags.SetField | (BindingFlags.Instance | BindingFlags.NonPublic)), null, self, new object[] {
                                         DataControllerBase.ConvertToType(fi.FieldType, v.Value)
                                     });
                                 }
                                 finally
                                 {
                                     // release resources here
                                 }
                             }
                         }
                         arguments[i] = self;
                     }
                     else
                     {
                         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);
             }
         }
     }
 }
        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);
        }