Beispiel #1
0
        private void ImportBtn_Click(object sender, EventArgs e)
        {
            var results            = new StringBuilder();
            var importCount        = 0;
            var partialImportCount = 0;
            var updateCount        = 0;
            var partialUpdateCount = 0;
            var failCount          = 0;

            if (uploader.HasFile)
            {
                var recordsToImport = ImportHelper.GetDynamicListFromCSV(uploader.FileContent);

                if (recordsToImport != null)
                {
                    if (chkDelete.Checked)
                    {
                        _ = ItemFieldValue.DeleteByModule(module.ModuleGuid);
                        _ = Item.DeleteByModule(module.ModuleGuid);
                    }

                    foreach (IDictionary <string, object> record in recordsToImport)
                    {
                        var existingGuid = Guid.Empty;
                        var hasGuid      = false;

                        if (record.ContainsKey("Guid"))
                        {
                            hasGuid = Guid.TryParse(Convert.ToString(record["Guid"]), out existingGuid);
                        }

                        var  isUpdate     = true;
                        Item importedItem = null;

                        if (hasGuid && chkUpdate.Checked)
                        {
                            importedItem = new Item(existingGuid);
                        }

                        if (importedItem == null || importedItem.DefinitionGuid != config.FieldDefinitionGuid || importedItem.ModuleGuid != module.ModuleGuid)
                        {
                            //todo: report to user why record isn't being updated
                            isUpdate     = false;
                            importedItem = new Item
                            {
                                SiteGuid       = siteSettings.SiteGuid,
                                FeatureGuid    = config.FeatureGuid,
                                ModuleGuid     = module.ModuleGuid,
                                ModuleID       = module.ModuleId,
                                DefinitionGuid = config.FieldDefinitionGuid,
                                ItemGuid       = Guid.NewGuid()
                            };
                        }

                        var sortOrder = 500;

                        if (record.ContainsKey("SortOrder"))
                        {
                            var sortOrderStr = Convert.ToString(record["SortOrder"]);

                            if (!string.IsNullOrWhiteSpace(sortOrderStr))
                            {
                                sortOrder = int.Parse(sortOrderStr);
                            }
                        }

                        importedItem.SortOrder  = sortOrder;
                        importedItem.LastModUtc = DateTime.UtcNow;
                        //we don't want to do this on each item that has been imported because that's a lot of work during the import process
                        //importedItem.ContentChanged += new ContentChangedEventHandler(sflexiItem_ContentChanged);

                        if (importedItem.Save())
                        {
                            var fullyImported = true;
                            //partialCount++;
                            List <Field> fields = null;

                            if (config.FieldDefinitionGuid != Guid.Empty)
                            {
                                fields = Field.GetAllForDefinition(config.FieldDefinitionGuid);
                            }
                            else
                            {
                                //todo: need to show a message about definition guid missing
                                log.ErrorFormat("definitionGuid is missing from the field configuration file named {0}.", config.FieldDefinitionSrc);

                                return;
                            }

                            if (fields == null)
                            {
                                return;
                            }

                            foreach (Field field in fields)
                            {
                                foreach (var kvp in record)
                                {
                                    if (field.Name == kvp.Key.Replace(" ", string.Empty))
                                    {
                                        List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(importedItem.ItemGuid);
                                        ItemFieldValue        fieldValue  = null;

                                        try
                                        {
                                            fieldValue = fieldValues.Where(saved => saved.FieldGuid == field.FieldGuid).Single();
                                        }
                                        catch (InvalidOperationException)
                                        {
                                            //field is probably new

                                            fieldValue = new ItemFieldValue();
                                        }

                                        //ItemFieldValue fieldValue = new ItemFieldValue(item.ItemGuid, field.FieldGuid);
                                        fieldValue.FieldGuid   = field.FieldGuid;
                                        fieldValue.SiteGuid    = field.SiteGuid;
                                        fieldValue.FeatureGuid = field.FeatureGuid;
                                        fieldValue.ModuleGuid  = module.ModuleGuid;
                                        fieldValue.ItemGuid    = importedItem.ItemGuid;

                                        fieldValue.FieldValue = kvp.Value.ToString();

                                        if (!fieldValue.Save())
                                        {
                                            fullyImported = false;
                                            _             = results.AppendLine(string.Format("<div><strong>Partial Failure:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value))));
                                        }
                                    }
                                }
                            }

                            if (fullyImported)
                            {
                                if (isUpdate)
                                {
                                    updateCount++;
                                }
                                else
                                {
                                    importCount++;
                                }
                            }
                            else
                            {
                                if (isUpdate)
                                {
                                    partialUpdateCount++;
                                }
                                else
                                {
                                    partialImportCount++;
                                }
                            }
                        }
                        else
                        {
                            failCount++;
                            _ = results.AppendFormat("<div><strong>Failed:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value)));
                        }
                    }

                    _ = results.Insert(0, string.Format(@"
                        <div><strong>Imported</strong>&nbsp;{0}</div>
                        <div><strong>Partially Imported</strong>&nbsp;{1}</div>
						<div><strong>Updated</strong>&nbsp;{2}</div>
						<div><strong>Partially Updated</strong>&nbsp;{3}</div>
                        <div><strong>Failed</strong>&nbsp;{4}</div>"
                                                        , importCount.ToString()
                                                        , partialImportCount.ToString()
                                                        , updateCount.ToString()
                                                        , partialUpdateCount.ToString()
                                                        , failCount.ToString()));
                }
                else
                {
                    _ = results.Insert(0, "<div class=\"alert alert-danger\">No records found in CSV file</div>");
                }

                litResults.Text = results.ToString();

                CacheHelper.ClearModuleCache(moduleId);
                SuperFlexiIndexBuilderProvider indexBuilder = new SuperFlexiIndexBuilderProvider();
                indexBuilder.RebuildIndex(CacheHelper.GetCurrentPage(), IndexHelper.GetSearchIndexPath(siteSettings.SiteId));
                SiteUtils.QueueIndexing();
            }
        }
Beispiel #2
0
        private void ImportBtn_Click(Object sender, EventArgs e)
        {
            StringBuilder results            = new StringBuilder();
            int           importCount        = 0;
            int           partialImportCount = 0;
            int           updateCount        = 0;
            int           partialUpdateCount = 0;
            int           failCount          = 0;

            if (uploader.HasFile)
            {
                var recordsToImport = ImportHelper.GetDynamicListFromCSV(uploader.FileContent);

                if (recordsToImport != null)
                {
                    foreach (IDictionary <string, object> record in recordsToImport)
                    {
                        var  existingGuid = record.ContainsKey("Guid") ? Guid.Parse(record["Guid"].ToString()) : Guid.Empty;
                        bool isUpdate     = true;
                        Item importedItem = null;

                        if (existingGuid != Guid.Empty && chkUpdate.Checked)
                        {
                            importedItem = new Item(existingGuid);
                        }

                        if (importedItem == null || importedItem.DefinitionGuid != config.FieldDefinitionGuid || importedItem.ModuleGuid != module.ModuleGuid)
                        {
                            //todo: report to use why record isn't being updated
                            isUpdate                    = false;
                            importedItem                = new Item();
                            importedItem.SiteGuid       = siteSettings.SiteGuid;
                            importedItem.FeatureGuid    = config.FeatureGuid;
                            importedItem.ModuleGuid     = module.ModuleGuid;
                            importedItem.ModuleID       = module.ModuleId;
                            importedItem.DefinitionGuid = config.FieldDefinitionGuid;
                            importedItem.ItemGuid       = Guid.NewGuid();
                        }


                        int sortOrder = record.ContainsKey("SortOrder") ? int.Parse(record["SortOrder"].ToString()) : 500;

                        importedItem.SortOrder       = sortOrder;
                        importedItem.LastModUtc      = DateTime.UtcNow;
                        importedItem.ContentChanged += new ContentChangedEventHandler(sflexiItem_ContentChanged);

                        if (importedItem.Save())
                        {
                            bool fullyImported = true;
                            //partialCount++;
                            List <Field> fields = null;
                            if (config.FieldDefinitionGuid != Guid.Empty)
                            {
                                fields = Field.GetAllForDefinition(config.FieldDefinitionGuid);
                            }
                            else
                            {
                                //todo: need to show a message about definition guid missing
                                log.ErrorFormat("definitionGuid is missing from the field configuration file named {0}.", config.FieldDefinitionSrc);
                                return;
                            }

                            if (fields == null)
                            {
                                return;
                            }
                            foreach (Field field in fields)
                            {
                                foreach (var kvp in record)
                                {
                                    if (field.Name == kvp.Key.Replace(" ", string.Empty))
                                    {
                                        List <ItemFieldValue> fieldValues = ItemFieldValue.GetItemValues(importedItem.ItemGuid);
                                        ItemFieldValue        fieldValue;

                                        try
                                        {
                                            fieldValue = fieldValues.Where(saved => saved.FieldGuid == field.FieldGuid).Single();
                                        }
                                        catch (System.InvalidOperationException ex)
                                        {
                                            //field is probably new

                                            fieldValue = new ItemFieldValue();
                                        }

                                        //ItemFieldValue fieldValue = new ItemFieldValue(item.ItemGuid, field.FieldGuid);
                                        fieldValue.FieldGuid   = field.FieldGuid;
                                        fieldValue.SiteGuid    = field.SiteGuid;
                                        fieldValue.FeatureGuid = field.FeatureGuid;
                                        fieldValue.ModuleGuid  = module.ModuleGuid;
                                        fieldValue.ItemGuid    = importedItem.ItemGuid;

                                        fieldValue.FieldValue = kvp.Value.ToString();

                                        if (!fieldValue.Save())
                                        {
                                            fullyImported = false;
                                            results.AppendLine(String.Format("<div><strong>Partial Failure:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value))));
                                        }
                                    }
                                }
                            }

                            CacheHelper.ClearModuleCache(importedItem.ModuleID);
                            SiteUtils.QueueIndexing();

                            if (fullyImported)
                            {
                                if (isUpdate)
                                {
                                    updateCount++;
                                }
                                else
                                {
                                    importCount++;
                                }
                            }
                            else
                            {
                                if (isUpdate)
                                {
                                    partialUpdateCount++;
                                }
                                else
                                {
                                    partialImportCount++;
                                }
                            }
                        }
                        else
                        {
                            failCount++;
                            results.AppendFormat("<div><strong>Failed:</strong> {0}</div>", string.Join(";", record.Select(x => x.Key + "=" + x.Value)));
                        }
                    }

                    results.Insert(0, String.Format(@"
                        <div><strong>Imported</strong>&nbsp;{0}</div>
                        <div><strong>Partially Imported</strong>&nbsp;{1}</div>
						<div><strong>Updated</strong>&nbsp;{2}</div>
						<div><strong>Partially Updated</strong>&nbsp;{3}</div>
                        <div><strong>Failed</strong>&nbsp;{4}</div>"
                                                    , importCount.ToString()
                                                    , partialImportCount.ToString()
                                                    , updateCount.ToString()
                                                    , partialUpdateCount.ToString()
                                                    , failCount.ToString()));
                }
                else
                {
                    results.Insert(0, "<div class=\"alert alert-danger\">No records found in CSV file</div>");
                }

                litResults.Text = results.ToString();
            }
        }