public ActionResult Index(FindAndReplaceViewModel model)
        {
            model.ServerErrorMessage = string.Empty;

            if (ModelState.IsValid)
            {
                ReadOptions ro = new ReadOptions {
                    LoadFlags = LoadFlags.None
                };

                OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData()
                {
                    ItemTypes      = new[] { ItemType.Component },
                    ComponentTypes = new[] { ComponentType.Normal },
                    Recursive      = true,
                };

                try
                {
                    XElement xmlList = Client.GetListXml(model.FolderId, filter);

                    // Load all components based on the folderId
                    List <ComponentData> componentsList = (from itemId in xmlList.Elements().Attributes(XName.Get("ID", String.Empty))
                                                           select(ComponentData) Client.Read(itemId.Value, ro)).ToList <ComponentData>();

                    model.ComponentIdsMatchedListForFindAndReplace = new List <Component>();
                    foreach (var component in componentsList.Where(x => x.Content != "" && x.BluePrintInfo.IsShared == false && x.ComponentType.Value != ComponentType.Multimedia))
                    {
                        if (FindText(component.Content, model.SearchText, model.Matchcase))
                        {
                            log.Info(string.Format("Find and Replace Text Match component ID {0}, Title {1}", component.Id, component.Title));
                            model.ComponentIdsMatchedListForFindAndReplace.Add(new Component {
                                Id = component.Id, Title = component.Title
                            });
                        }
                    }

                    log.Info(string.Format("No of matched components {0}", model.ComponentIdsMatchedListForFindAndReplace.Count()));

                    Session["ComponentsListReplace"] = model.ComponentIdsMatchedListForFindAndReplace;
                    Session["SearchText"]            = model.SearchText;
                    Session["ReplaceText"]           = model.ReplaceText;
                    Session["Matchcase"]             = model.Matchcase;

                    model.IsSuccess          = true;
                    model.IsReplaceCompleted = false;
                    this.SetModel <FindAndReplaceViewModel>(model);
                    return(Redirect(Request.Url.AbsolutePath));
                }
                catch (Exception ex)
                {
                    //model.ServerErrorMessage = "Error processing the items...";
                    log.Error("Index - Find and Replace Post Method Error", ex);
                }
            }

            this.SetModel <FindAndReplaceViewModel>(model);
            return(Redirect(Request.Url.PathAndQuery));
        }
        public ActionResult ReplaceAll(FindAndReplaceViewModel model)
        {
            ReadOptions ro = new ReadOptions {
                LoadFlags = LoadFlags.None
            };

            model.ComponentIdsMatchedListForFindAndReplace = (List <Component>)Session["ComponentsListReplace"];

            model.SearchText              = Session["SearchText"].ToString();
            model.ReplaceText             = Session["ReplaceText"].ToString();
            model.Matchcase               = (bool)Session["Matchcase"];
            model.ComponentIdsNotReplaced = new List <Component>();

            foreach (var component in model.ComponentIdsMatchedListForFindAndReplace)
            {
                try
                {
                    var           readOptions = new ReadOptions();
                    ComponentData comp        = (ComponentData)Client.CheckOut(component.Id, true, ro);
                    comp.Content = ReplaceAll(comp.Content, model.SearchText, model.ReplaceText, model.Matchcase);
                    Client.Save(comp, ro);
                    Client.CheckIn(component.Id, true, "", ro);
                    log.Info(string.Format("Successfully replaced on component ID {0}, Title {1}", comp.Id, comp.Title));
                }
                catch (Exception ex)
                {
                    model.ComponentIdsNotReplaced.Add(new Component {
                        Id = component.Id, Title = component.Title
                    });
                    log.Error("Unable to update the component", ex);
                }
            }

            model.IsSuccess          = true;
            model.IsReplaceCompleted = true;
            this.SetModel <FindAndReplaceViewModel>(model);
            return(Redirect("/Home/Index?success=true"));
        }
Exemple #3
0
 public FindAndReplace(MainViewModel mainViewModel)
 {
     InitializeComponent();
     DataContext = new FindAndReplaceViewModel(mainViewModel);
 }