Exemplo n.º 1
0
        public override void DeleteContent(int moduleId, Guid moduleGuid)
        {
            ItemFieldValue.DeleteByModule(moduleGuid);
            Item.DeleteByModule(moduleGuid);

            Module module = new Module(moduleGuid);
            ModuleConfiguration config = new ModuleConfiguration(module);

            //having field definitions in the database when the fields aren't used by any module is just clutter.
            //we'll delete the field definitions from the database when they aren't used but of course we don't touch the actual field definition XML files.
            bool deleteOrphanFields = ConfigHelper.GetBoolProperty("SuperFlexi:DeleteFieldDefinitionsWhenNotUsed", true);

            //we didn't implement the delete handler for a year or so after building superflexi so we have a lot of instances in the wild that probably have orphaned items
            //if we upgrade those to this version, create a module instance, and then delete it, these orphaned items will be removed
            bool deleteOrphanItems = ConfigHelper.GetBoolProperty("SuperFlexi:DeleteOrphanedItemsWhenDeletingModules", true);

            //clean up search definitions
            bool deleteOrphanSearchDefinitions = ConfigHelper.GetBoolProperty("SuperFlexi:DeleteOrphanedSearchDefinitions", true);

            if (deleteOrphanFields || deleteOrphanItems || deleteOrphanSearchDefinitions)
            {
                List <Item> sflexiItems     = Item.GetForModule(module.ModuleId);
                List <Guid> definitionGuids = new List <Guid>();


                foreach (Item item in sflexiItems)
                {
                    //add definitionGuids here b/c we'll need them for all three checks
                    definitionGuids.Add(item.DefinitionGuid);

                    if (deleteOrphanItems)
                    {
                        Module checkModule = new Module(item.ModuleGuid);
                        if (checkModule == null)
                        {
                            ItemFieldValue.DeleteByModule(item.ModuleGuid);
                            Item.DeleteByModule(item.ModuleGuid);
                        }
                    }
                }

                if (deleteOrphanFields)
                {
                    definitionGuids = definitionGuids.Distinct().ToList();

                    foreach (Guid guid in definitionGuids)
                    {
                        List <Item> definitionItems = Item.GetForDefinition(guid, module.SiteGuid);
                        if (definitionItems.Count == 0)
                        {
                            //delete field definitions when there are no more modules using them
                            Field.DeleteByDefinition(guid);

                            if (deleteOrphanSearchDefinitions)
                            {
                                //delete search definitions when there are no more modules using their definition
                                SearchDef.DeleteByFieldDefinition(guid);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
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();
            }
        }