예제 #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;
             }
         }
     }
 }
예제 #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);
            }
        }
예제 #3
0
        public void UpdateManyToManyField(string fieldName, string primaryKeyField, string targetController, string targetForeignKey1, string targetForeignKey2)
        {
            FieldValue field = SelectFieldValueObject(fieldName);

            if (field == null)
            {
                return;
            }
            object        primaryKey = SelectFieldValue(primaryKeyField);
            List <string> oldValues  = ValueToList(((string)(field.OldValue)));
            List <string> newValues  = ValueToList(((string)(field.NewValue)));

            if (!(ListsAreEqual(oldValues, newValues)))
            {
                IDataController controller = ControllerFactory.CreateDataController();
                foreach (string s in oldValues)
                {
                    if (!(newValues.Contains(s)))
                    {
                        ActionArgs args = new ActionArgs();
                        args.CommandName     = "Delete";
                        args.LastCommandName = "Select";
                        args.Values          = new FieldValue[] {
                            new FieldValue(targetForeignKey1, primaryKey, null),
                            new FieldValue(targetForeignKey2, s, null)
                        };
                        controller.Execute(targetController, null, args);
                    }
                }
                foreach (string s in newValues)
                {
                    if (!(oldValues.Contains(s)))
                    {
                        ActionArgs args = new ActionArgs();
                        args.CommandName     = "Insert";
                        args.LastCommandName = "New";
                        args.Values          = new FieldValue[] {
                            new FieldValue(targetForeignKey1, primaryKey),
                            new FieldValue(targetForeignKey2, s)
                        };
                        controller.Execute(targetController, null, args);
                    }
                }
            }
        }
예제 #4
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);
        }
예제 #5
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);
            }
        }
예제 #6
0
 public static bool InTransaction(ActionArgs args)
 {
     return(!(String.IsNullOrEmpty(args.Transaction)) && !(args.Transaction.EndsWith(":complete")));
 }
예제 #7
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);
 }
예제 #8
0
 void IActionHandler.AfterSqlAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.After);
     AfterSqlAction(args, result);
 }
예제 #9
0
 void IActionHandler.ExecuteAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.Execute);
     ExecuteAction(args, result);
 }
예제 #10
0
 protected virtual void ExecuteAction(ActionArgs args, ActionResult result)
 {
 }
예제 #11
0
 void IActionHandler.BeforeSqlAction(ActionArgs args, ActionResult result)
 {
     ExecuteMethod(args, result, ActionPhase.Before);
     BeforeSqlAction(args, result);
 }
예제 #12
0
 protected virtual void AfterSqlAction(ActionArgs args, ActionResult result)
 {
 }
예제 #13
0
 protected virtual void BeforeSqlAction(ActionArgs args, ActionResult result)
 {
 }
예제 #14
0
        void IPlugIn.ProcessArguments(ActionArgs args, ActionResult result, ViewPage page)
        {
            if (_annotations.Count == 0)
            {
                return;
            }
            string p = AnnotationPlugIn.GenerateDataRecordPath(args.Controller, page, args.Values, 0);

            if (!(Directory.Exists(p)))
            {
                Directory.CreateDirectory(p);
            }
            foreach (FieldValue v in _annotations)
            {
                Match m = Regex.Match(v.Name, "^_Annotation_(Note)(New|\\w+)$", RegexOptions.Compiled);
                if (m.Success)
                {
                    if (m.Groups[1].Value == "Note")
                    {
                        string fileName = m.Groups[2].Value;
                        if (fileName == "New")
                        {
                            fileName = DateTime.Now.ToString("u");
                            fileName = Regex.Replace(fileName, "[\\W]", String.Empty, RegexOptions.Compiled);
                        }
                        fileName = Path.Combine(p, (fileName + ".xml"));
                        if (!(String.IsNullOrEmpty(Convert.ToString(v.NewValue))))
                        {
                            XmlWriterSettings settings = new XmlWriterSettings();
                            settings.CloseOutput = true;
                            XmlWriter writer = XmlWriter.Create(new FileStream(fileName, FileMode.Create), settings);
                            try
                            {
                                writer.WriteStartElement("note");
                                writer.WriteAttributeString("timestamp", DateTime.Now.ToString("o"));
                                writer.WriteAttributeString("username", HttpContext.Current.User.Identity.Name);
                                //MembershipUser user = Membership.GetUser();
                                if (SessionCache.CurrentUser == null)
                                {
                                    WebUtil.LoginUser();
                                }
                                User user = SessionCache.CurrentUser;
                                if (user == null)
                                {
                                    user = new User();
                                }

                                if (String.IsNullOrEmpty(user.Email))
                                {
                                    user.Email = "*****@*****.**";
                                }

                                writer.WriteAttributeString("email", user.Email);
                                writer.WriteString(Convert.ToString(v.NewValue));
                                writer.WriteEndElement();
                            }
                            finally
                            {
                                writer.Close();
                            }
                        }
                        else
                        {
                            File.Delete(fileName);
                            if (Directory.GetFiles(p).Length == 0)
                            {
                                Directory.Delete(p);
                            }
                        }
                    }
                }
            }
        }
예제 #15
0
 public static void Execute(ActionArgs args)
 {
     Process(args);
 }
예제 #16
0
        public virtual void Process(string fileName, string controller, string view, string notify, List <string> userMapping)
        {
            BeforeProcess(fileName, controller, view, notify, userMapping);
            string       logFileName = Path.GetTempFileName();
            StreamWriter log         = File.CreateText(logFileName);

            log.WriteLine("{0:s} Import process started.", DateTime.Now);
            // retrieve metadata
            PageRequest request = new PageRequest();

            request.Controller       = controller;
            request.View             = view;
            request.RequiresMetaData = true;
            ViewPage page = ControllerFactory.CreateDataController().GetPage(controller, view, request);
            // open data reader and enumerate fields
            OleDbDataReader        reader  = OpenRead(fileName, "*");
            ImportMapDictionary    map     = new ImportMapDictionary();
            ImportLookupDictionary lookups = new ImportLookupDictionary();

            EnumerateFields(reader, page, map, lookups, userMapping);
            // resolve lookup data value field and data text fields
            ResolveLookups(lookups);
            // insert records from the file
            int recordCount      = 0;
            int errorCount       = 0;
            NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
            Regex            numberCleanupRegex = new Regex(String.Format("[^\\d\\{0}\\{1}\\{2}]", nfi.CurrencyDecimalSeparator, nfi.NegativeSign, nfi.NumberDecimalSeparator));

            while (reader.Read())
            {
                ActionArgs args = new ActionArgs();
                args.Controller      = controller;
                args.View            = view;
                args.LastCommandName = "New";
                args.CommandName     = "Insert";
                List <FieldValue> values = new List <FieldValue>();
                foreach (int index in map.Keys)
                {
                    DataField field = map[index];
                    object    v     = reader[index];
                    if (DBNull.Value.Equals(v))
                    {
                        v = null;
                    }
                    else
                    if (field.Type != "String" && (v is string))
                    {
                        string s = ((string)(v));
                        if (field.Type == "Boolean")
                        {
                            v = s.ToLower();
                        }
                        else
                        if (!(field.Type.StartsWith("Date")) && field.Type != "Time")
                        {
                            v = numberCleanupRegex.Replace(s, String.Empty);
                        }
                    }
                    if (v != null)
                    {
                        DataField lookupField = null;
                        if (lookups.TryGetValue(field.Name, out lookupField))
                        {
                            if (lookupField.Items.Count > 0)
                            {
                                // copy static values
                                foreach (object[] item in lookupField.Items)
                                {
                                    if (Convert.ToString(item[1]).Equals(Convert.ToString(v), StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        values.Add(new FieldValue(lookupField.Name, item[0]));
                                    }
                                }
                            }
                            else
                            {
                                PageRequest lookupRequest = new PageRequest();
                                lookupRequest.Controller       = lookupField.ItemsDataController;
                                lookupRequest.View             = lookupField.ItemsDataView;
                                lookupRequest.RequiresMetaData = true;
                                lookupRequest.PageSize         = 1;
                                lookupRequest.Filter           = new string[] {
                                    String.Format("{0}:={1}{2}", lookupField.ItemsDataTextField, v, Convert.ToChar(0))
                                };
                                ViewPage vp = ControllerFactory.CreateDataController().GetPage(lookupRequest.Controller, lookupRequest.View, lookupRequest);
                                if (vp.Rows.Count > 0)
                                {
                                    values.Add(new FieldValue(lookupField.ItemsDataValueField, vp.Rows[0][vp.Fields.IndexOf(vp.FindField(lookupField.ItemsDataValueField))]));
                                }
                            }
                        }
                        else
                        {
                            values.Add(new FieldValue(field.Name, v));
                        }
                    }
                }
                recordCount++;
                if (values.Count > 0)
                {
                    args.Values = values.ToArray();
                    ActionResult r = ControllerFactory.CreateDataController().Execute(controller, view, args);
                    if (r.Errors.Count > 0)
                    {
                        log.WriteLine("{0:s} Error importing record #{1}.", DateTime.Now, recordCount);
                        log.WriteLine();
                        foreach (string s in r.Errors)
                        {
                            log.WriteLine(s);
                        }
                        foreach (FieldValue v in values)
                        {
                            if (v.Modified)
                            {
                                log.WriteLine("{0}={1};", v.Name, v.Value);
                            }
                        }
                        log.WriteLine();
                        errorCount++;
                    }
                }
                else
                {
                    log.WriteLine("{0:s} Record #1 has been ignored.", DateTime.Now, recordCount);
                    errorCount++;
                }
            }
            reader.Close();
            log.WriteLine("{0:s} Processed {1} records. Detected {2} errors.", DateTime.Now, recordCount, errorCount);
            log.Close();
            if (!(String.IsNullOrEmpty(notify)))
            {
                string[]   recipients = notify.Split(',');
                SmtpClient client     = new SmtpClient();
                foreach (string s in recipients)
                {
                    string address = s.Trim();
                    if (!(String.IsNullOrEmpty(address)))
                    {
                        MailMessage message = new MailMessage();
                        try
                        {
                            message.To.Add(new MailAddress(address));
                            message.Subject = String.Format("Import of {0} has been completed", controller);
                            message.Body    = File.ReadAllText(logFileName);
                            client.Send(message);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
            File.Delete(logFileName);
            AfterProcess(fileName, controller, view, notify, userMapping);
        }