Exemple #1
0
        public string CreateInitialFieldMap(string fileName, string controller, string view)
        {
            // retreive metadata
            PageRequest request = new PageRequest();

            request.Controller       = controller;
            request.View             = view;
            request.RequiresMetaData = true;
            ViewPage page = ControllerFactory.CreateDataController().GetPage(controller, view, request);
            // create initial map
            StringBuilder   sb     = new StringBuilder();
            OleDbDataReader reader = OpenRead(fileName, "*");

            try
            {
                ImportMapDictionary    map     = new ImportMapDictionary();
                ImportLookupDictionary lookups = new ImportLookupDictionary();
                EnumerateFields(reader, page, map, lookups, null);
                for (int i = 0; (i < reader.FieldCount); i++)
                {
                    sb.AppendFormat("{0}=", reader.GetName(i));
                    DataField field = null;
                    if (map.TryGetValue(i, out field))
                    {
                        string fieldName = field.Name;
                        foreach (DataField lookupField in lookups.Values)
                        {
                            if (lookupField.AliasName == field.Name)
                            {
                                fieldName = lookupField.Name;
                                break;
                            }
                        }
                        sb.Append(fieldName);
                    }
                    sb.AppendLine();
                }
            }
            finally
            {
                reader.Close();
            }
            return(sb.ToString());
        }
Exemple #2
0
        string[] IAutoCompleteManager.GetCompletionList(string prefixText, int count, string contextKey)
        {
            if (contextKey == null)
            {
                return(null);
            }
            string[] arguments = contextKey.Split(',');
            if (arguments.Length != 3)
            {
                return(null);
            }
            DistinctValueRequest request = new DistinctValueRequest();

            request.FieldName = arguments[2];
            string filter = (request.FieldName + ":");

            foreach (string s in prefixText.Split(',', ';'))
            {
                string query = Controller.ConvertSampleToQuery(s);
                if (!(String.IsNullOrEmpty(query)))
                {
                    filter = (filter + query);
                }
            }
            request.Filter = new string[] {
                filter
            };
            request.AllowFieldInFilter = true;
            request.MaximumValueCount  = count;
            request.Controller         = arguments[0];
            request.View = arguments[1];
            object[]      list   = ControllerFactory.CreateDataController().GetListOfValues(arguments[0], arguments[1], request);
            List <string> result = new List <string>();

            foreach (object o in list)
            {
                result.Add(Convert.ToString(o));
            }
            return(result.ToArray());
        }
Exemple #3
0
 private void ResolveLookups(ImportLookupDictionary lookups)
 {
     foreach (string fieldName in lookups.Keys)
     {
         DataField lookupField = lookups[fieldName];
         if ((lookupField.Items.Count == 0) && (String.IsNullOrEmpty(lookupField.ItemsDataValueField) || String.IsNullOrEmpty(lookupField.ItemsDataTextField)))
         {
             PageRequest lookupRequest = new PageRequest();
             lookupRequest.Controller       = lookupField.ItemsDataController;
             lookupRequest.View             = lookupField.ItemsDataView;
             lookupRequest.RequiresMetaData = true;
             ViewPage lp = ControllerFactory.CreateDataController().GetPage(lookupRequest.Controller, lookupRequest.View, lookupRequest);
             if (String.IsNullOrEmpty(lookupField.ItemsDataValueField))
             {
                 foreach (DataField f in lp.Fields)
                 {
                     if (f.IsPrimaryKey)
                     {
                         lookupField.ItemsDataValueField = f.Name;
                         break;
                     }
                 }
             }
             if (String.IsNullOrEmpty(lookupField.ItemsDataTextField))
             {
                 foreach (DataField f in lp.Fields)
                 {
                     if ((!(f.IsPrimaryKey) && !(f.Hidden)) && (!(f.AllowNulls) || (f.Type == "String")))
                     {
                         lookupField.ItemsDataTextField = f.Name;
                         break;
                     }
                 }
             }
         }
     }
 }
Exemple #4
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);
        }
Exemple #5
0
 public bool PopulateStaticItems(DataField field, FieldValue[] contextValues)
 {
     if (field.SupportsStaticItems() && (String.IsNullOrEmpty(field.ContextFields) || ((contextValues != null) || (field.ItemsStyle == "CheckBoxList"))))
     {
         if (PopulatingStaticItems)
         {
             return(true);
         }
         PopulatingStaticItems = true;
         try
         {
             string[] filter = null;
             if (!(String.IsNullOrEmpty(field.ContextFields)))
             {
                 List <string> contextFilter = new List <string>();
                 Match         m             = Regex.Match(field.ContextFields, "(\\w+)=(.+?)($|,)");
                 while (m.Success)
                 {
                     Match vm = Regex.Match(m.Groups[2].Value, "^\'(.+?)\'$");
                     if (vm.Success)
                     {
                         contextFilter.Add(String.Format("{0}:={1}", m.Groups[1].Value, vm.Groups[1].Value));
                     }
                     else
                     if (contextValues != null)
                     {
                         foreach (FieldValue cv in contextValues)
                         {
                             if (cv.Name == m.Groups[2].Value)
                             {
                                 contextFilter.Add(String.Format("{0}:={1}", m.Groups[1].Value, cv.NewValue));
                                 break;
                             }
                         }
                     }
                     m = m.NextMatch();
                 }
                 filter = contextFilter.ToArray();
             }
             PageRequest request             = new PageRequest(-1, 1000, field.ItemsDataTextField, filter);
             ViewPage    page                = ControllerFactory.CreateDataController().GetPage(field.ItemsDataController, field.ItemsDataView, request);
             int         dataValueFieldIndex = page.Fields.IndexOf(page.FindField(field.ItemsDataValueField));
             if (dataValueFieldIndex == -1)
             {
                 foreach (DataField aField in page.Fields)
                 {
                     if (aField.IsPrimaryKey)
                     {
                         dataValueFieldIndex = page.Fields.IndexOf(aField);
                         break;
                     }
                 }
             }
             int dataTextFieldIndex = page.Fields.IndexOf(page.FindField(field.ItemsDataTextField));
             if (dataTextFieldIndex == -1)
             {
                 int i = 0;
                 while ((dataTextFieldIndex == -1) && (i < page.Fields.Count))
                 {
                     DataField f = page.Fields[i];
                     if (!(f.Hidden) && (f.Type == "String"))
                     {
                         dataTextFieldIndex = i;
                     }
                     i++;
                 }
                 if (dataTextFieldIndex == -1)
                 {
                     dataTextFieldIndex = 0;
                 }
             }
             List <int> fieldIndexes = new List <int>();
             fieldIndexes.Add(dataValueFieldIndex);
             fieldIndexes.Add(dataTextFieldIndex);
             if (!(String.IsNullOrEmpty(field.Copy)))
             {
                 Match m = Regex.Match(field.Copy, "(\\w+)=(\\w+)");
                 while (m.Success)
                 {
                     int copyFieldIndex = page.Fields.IndexOf(page.FindField(m.Groups[2].Value));
                     if (copyFieldIndex >= 0)
                     {
                         fieldIndexes.Add(copyFieldIndex);
                     }
                     m = m.NextMatch();
                 }
             }
             foreach (object[] row in page.Rows)
             {
                 object[] values = new object[fieldIndexes.Count];
                 for (int i = 0; (i < fieldIndexes.Count); i++)
                 {
                     int copyFieldIndex = fieldIndexes[i];
                     if (copyFieldIndex >= 0)
                     {
                         values[i] = row[copyFieldIndex];
                     }
                 }
                 field.Items.Add(values);
             }
             return(true);
         }
         finally
         {
             PopulatingStaticItems = false;
         }
     }
     return(false);
 }