private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            if (!Functions.ExistsItem(this.TridionObject.TcmId))
                return;

            List<ResultInfo> results = Functions.CheckBrokenComponentsAndMetadata(this.TridionObject.TcmId);

            //show results
            ResultsWindow dialog = new ResultsWindow();
            dialog.Host = this.txtHost.Text;
            dialog.ListBoxReport.ItemsSource = this.CheckResults(results);
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();
        }
        private void btnCheckDelete_Click(object sender, RoutedEventArgs e)
        {
            if (!Functions.ExistsItem(this.TridionObject.TcmId))
                return;

            if (this._CheckResults == null)
            {
                this._CheckResults = new List<ResultInfo>();
                Functions.Delete(this.TridionObject.TcmId, false, this._SchemaUri, this._Criterias, this._CheckResults);
            }

            //show results
            ResultsWindow dialog = new ResultsWindow();
            dialog.Host = this.txtHost.Text;
            dialog.ListBoxReport.ItemsSource = this.CheckResults(this._CheckResults);
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();
        }
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (!Functions.ExistsItem(this.TridionObject.TcmId))
                return;

            if (MessageBox.Show("Delete selected item and all related items?", "Are you sure?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) != MessageBoxResult.Yes)
                return;

            if (this._CheckResults == null)
            {
                this._CheckResults = new List<ResultInfo>();
                Functions.Delete(this.TridionObject.TcmId, false, this._SchemaUri, this._Criterias, this._CheckResults);
            }

            ResultsWindow dialog;

            if (this._CheckResults.Any(x => x.Status == Status.Warning || x.Status == Status.Error))
            {
                if (MessageBox.Show("Some items probably won't be deleted. \n\nTry to delete anyway?", "Problem items detected", MessageBoxButton.YesNo, MessageBoxImage.Stop, MessageBoxResult.No) != MessageBoxResult.Yes)
                {
                    //show results
                    dialog = new ResultsWindow();
                    dialog.Host = this.txtHost.Text;
                    dialog.ListBoxReport.ItemsSource = this.CheckResults(this._CheckResults);
                    dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
                    dialog.Show();

                    return;
                }
            }

            List<ResultInfo> results = new List<ResultInfo>();
            Functions.Delete(this.TridionObject.TcmId, true, this._SchemaUri, this._Criterias, results);

            //show results
            dialog = new ResultsWindow();
            dialog.Host = this.txtHost.Text;
            dialog.ListBoxReport.ItemsSource = this.CheckResults(results);
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();

            //reload tree
            List<ItemInfo> publications = Functions.GetPublications().Expand(TridionSelectorMode, this.TridionObject.TcmIdPath, this.TridionObject.TcmId).MakeExpandable();
            this.treeTridionObject.ItemsSource = publications;

            this._CheckResults = null;
        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            string sourceTable = this.cbSourceTable.SelectedValue as string;
            string sourceDatabase = this.cbSourceDatabase.SelectedValue as string;
            ItemInfo targetSchema = this.cbTargetSchema.SelectedValue as ItemInfo;

            if (sourceDatabase == null || sourceTable == null || targetSchema == null)
                return;

            this.SetCustomImporters();
            this.SetCustomNameTransformers();

            if (this.HistoryMapping == null)
            {
                this.HistoryMapping = Functions.GetHistoryMapping(Functions.GetId(this.txtHost.Text, sourceDatabase, sourceTable, targetSchema.TcmId));
            }

            if (this.HistoryMapping == null && (this.CustomComponentImporter == null || this.CustomMetadataImporter == null))
            {
                MessageBox.Show("Neither field mapping nor custom importers is set. Please set mapping or select custom importer and try again.", "Field Mapping", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            //inform that custom transfromation is selected
            if (this.CustomComponentImporter != null || this.CustomMetadataImporter != null)
            {
                string messsage = "";
                if (this.CustomComponentImporter != null && this.CustomMetadataImporter != null)
                {
                    messsage = String.Format("Custom imports {0} and {1} are used.\n\nContinue?", this.CustomComponentImporter.Title, this.CustomMetadataImporter.Title);
                }
                else if (this.CustomComponentImporter != null)
                {
                    messsage = String.Format("Custom component import {0} is used.\n\nContinue?", this.CustomComponentImporter.Title);
                }
                else if (this.CustomMetadataImporter != null)
                {
                    messsage = String.Format("Custom metadata import {0} is used.\n\nContinue?", this.CustomMetadataImporter.Title);
                }

                if (MessageBox.Show(messsage, "Custom import", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    return;
            }

            if (this.HistoryMapping != null)
            {
                foreach (HistoryItemMappingInfo historyMapping in this.HistoryMapping)
                {
                    foreach (FieldMappingInfo mapping in historyMapping.Mapping)
                    {
                        mapping.SourceFields = this._SourceSchemaFields;
                        mapping.TargetFields = this._TargetSchemaFields;
                    }
                }

                Functions.SetHistoryMappingTree(this.HistoryMapping);
            }

            List<ResultInfo> results = new List<ResultInfo>();

            bool localize = this.chkLocalize.IsChecked == true;
            Functions.SaveToIsolatedStorage(Functions.GetId(this.txtDbHost.Text, sourceDatabase, sourceTable, "Localize"), localize.ToString());

            // Import components from database
            Functions.ImportComponents(this.txtDbHost.Text, this.txtDbUsername.Text, this.txtDbPassword.Password, sourceDatabase, sourceTable, this.Sql, this.TargetTridionFolder.TcmId, targetSchema.TcmId, this._FormatString, this._Replacements, localize, this.HistoryMapping, this.CustomComponentImporter, this.CustomMetadataImporter, results);

            if (results.Any(x => x.Status == Status.Success))
            {
                ResultInfo resultFinish = new ResultInfo();
                resultFinish.Status = Status.Info;
                resultFinish.Message = "Finished";
                results.Add(resultFinish);
            }
            else
            {
                ResultInfo resultFinish = new ResultInfo();
                resultFinish.Status = Status.Info;
                resultFinish.Message = "No Actions Made";
                results.Add(resultFinish);
            }

            //show results
            ResultsWindow dialog = new ResultsWindow();
            dialog.Host = this.txtHost.Text;
            dialog.ListBoxReport.ItemsSource = results.Where(x => x != null && x.Status != Status.None);
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();

            //reload target tree
            this._Publications = Functions.GetPublications();
            this.treeTargetTridionFolder.ItemsSource = this._Publications.Expand(this.TargetTridionSelectorMode, this.TargetTridionFolder.TcmIdPath, this.TargetTridionFolder.TcmId).MakeExpandable();
        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            ItemInfo schema = cbSchema.SelectedValue as ItemInfo;
            if (schema == null)
                return;

            List<Criteria> criterias = this.SearchCriteriasControl1.GetCriterias();

            //get results
            List<ItemInfo> res = Functions.GetComponentsByCriterias(this.TridionFolder.TcmId, schema.TcmId, criterias);

            //show results
            ResultsWindow dialog = new ResultsWindow();
            dialog.Host = this.txtHost.Text;
            dialog.ListBoxReport.ItemsSource = this.CheckResults(res.Select(itemInfo => new ResultInfo { Item = itemInfo, Status = Status.Info, Message = string.Format("{0} ({1})", itemInfo.Title, itemInfo.TcmId) }).ToList());
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();
        }
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            ItemInfo sourceSchema = this.cbSourceSchema.SelectedValue as ItemInfo;
            ItemInfo targetSchema = this.cbTargetSchema.SelectedValue as ItemInfo;

            if (sourceSchema == null || targetSchema == null)
                return;

            this.SetCustomTransformers();
            this.SetCustomNameTransformers();

            if (this.HistoryMapping == null)
            {
                this.HistoryMapping = Functions.GetHistoryMapping(Functions.GetId(this.txtHost.Text, sourceSchema.TcmId, targetSchema.TcmId));
            }

            if (this.HistoryMapping == null && (this.CustomComponentTransformer == null || this.CustomMetadataTransformer == null))
            {
                MessageBox.Show("Neither field mapping nor custom transformers is set. Please set mapping or select custom transformer and try again.", "Field Mapping", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            //inform that custom transfromation is selected
            if (this.CustomComponentTransformer != null || this.CustomMetadataTransformer != null)
            {
                string messsage = "";
                if (this.CustomComponentTransformer != null && this.CustomMetadataTransformer != null)
                {
                    messsage = String.Format("Custom transformations {0} and {1} are used.\n\nContinue?", this.CustomComponentTransformer.Title, this.CustomMetadataTransformer.Title);
                }
                else if (this.CustomComponentTransformer != null)
                {
                    messsage = String.Format("Custom component transformation {0} is used.\n\nContinue?", this.CustomComponentTransformer.Title);
                }
                else if (this.CustomMetadataTransformer != null)
                {
                    messsage = String.Format("Custom metadata transformation {0} is used.\n\nContinue?", this.CustomMetadataTransformer.Title);
                }

                if (MessageBox.Show(messsage, "Custom Transformations", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    return;
            }

            if (this.HistoryMapping != null)
            {
                foreach (HistoryItemMappingInfo historyMapping in this.HistoryMapping)
                {
                    string sourceSchemaVersionTcmId = historyMapping.TcmId;
                    if (historyMapping.Current && sourceSchemaVersionTcmId.Contains("-v"))
                    {
                        sourceSchemaVersionTcmId = sourceSchemaVersionTcmId.Replace("-" + sourceSchemaVersionTcmId.Split('-')[3], "");
                    }

                    List<ItemFieldDefinitionData> sourceComponentFields = Functions.GetSchemaFields(sourceSchemaVersionTcmId);
                    List<ItemFieldDefinitionData> sourceMetadataFields = Functions.GetSchemaMetadataFields(sourceSchemaVersionTcmId);
                    List<FieldInfo> sourceSchemaFields = Functions.GetAllFields(sourceComponentFields, sourceMetadataFields, true, false);

                    foreach (FieldMappingInfo mapping in historyMapping.Mapping)
                    {
                        mapping.SourceFields = sourceSchemaFields;
                        mapping.TargetFields = this._TargetSchemaFields;
                    }
                }

                Functions.SetHistoryMappingTree(this.HistoryMapping);
            }

            List<ResultInfo> results = new List<ResultInfo>();

            ItemType sourceItemType = Functions.GetItemType(this.SourceTridionObject.TcmId);

            string sourceSchemaTcmId = sourceSchema.TcmId;

            string targetFolderUri = this.chkSameFolder.IsChecked == true ? this.SourceTridionObject.TcmId : this.TargetTridionFolder.TcmId;
            bool sameFolder = this.chkSameFolder.IsChecked == true || this.SourceTridionObject.TcmId == this.TargetTridionFolder.TcmId;

            bool localize = this.chkLocalize.IsChecked == true;
            Functions.SaveToIsolatedStorage(Functions.GetId(this.txtHost.Text.GetDomainName(), sourceSchema.TcmId, "Localize"), localize.ToString());

            // same folder selected: schema change and component fix functionality
            if (sameFolder)
            {
                // single component
                if (sourceItemType == ItemType.Component)
                {
                    if (sourceSchema.TcmId.GetId() == targetSchema.TcmId.GetId())
                    {
                        // fix single component
                        Functions.FixComponent(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                    }
                    else
                    {
                        // change schema for single component
                        Functions.ChangeSchemaForComponent(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetSchema.TcmId, targetFolderUri, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                    }
                }

                // folder
                if (sourceItemType == ItemType.Folder)
                {
                    if (sourceSchema.TcmId.GetId() == targetSchema.TcmId.GetId())
                    {
                        // fix all componnets in folder
                        Functions.FixComponentsInFolder(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, this._Criterias, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                    }
                    else
                    {
                        // change schema for all components in folder
                        Functions.ChangeSchemasForComponentsInFolder(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, targetSchema.TcmId, this._Criterias, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                    }
                }

                // single page
                if (sourceItemType == ItemType.Page)
                {
                    if (sourceSchema.TcmId.GetId() == targetSchema.TcmId.GetId())
                    {
                        // fix single page metadata
                        Functions.FixTridionObjectMetadata(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, localize, this.HistoryMapping, this.CustomMetadataTransformer, results);
                    }
                    else
                    {
                        // change metadata schema for single page
                        Functions.ChangeMetadataSchemaForTridionObject(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, targetSchema.TcmId, localize, this.HistoryMapping, this.CustomMetadataTransformer, results);
                    }
                }

                // structure group
                if (sourceItemType == ItemType.StructureGroup)
                {
                    if (sourceSchema.TcmId.GetId() == targetSchema.TcmId.GetId())
                    {
                        // fix metadata for all pages in structure group
                        Functions.FixMetadataForTridionObjectsInContainer(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, localize, this.HistoryMapping, this.CustomMetadataTransformer, results);
                    }
                    else
                    {
                        // change metadata schema for all pages in structure group
                        Functions.ChangeMetadataSchemasForTridionObjectsInContainer(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetFolderUri, targetSchema.TcmId, localize, this.HistoryMapping, this.CustomMetadataTransformer, results);
                    }
                }
            }

            // transform and copy to destination
            else
            {
                string targetTridionObjectContainerUri = this.chkSameFolder.IsChecked == true ? this.SourceTridionObject.TcmId : this.TargetTridionFolder.TcmId;

                // single component
                if (sourceItemType == ItemType.Component)
                {
                    // copy / transform component
                    Functions.TransformComponent(this.SourceTridionObject.TcmId, string.Empty, sourceSchemaTcmId, targetTridionObjectContainerUri, targetSchema.TcmId, this._FormatString, this._Replacements, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                }
                // folder
                if (sourceItemType == ItemType.Folder)
                {
                    // copy / transform all components in folder
                    Functions.TransformComponentsInFolder(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetTridionObjectContainerUri, targetSchema.TcmId, this._Criterias, this._FormatString, this._Replacements, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                }

                // single page
                if (sourceItemType == ItemType.Page)
                {
                    // copy / transform metadata
                    Functions.TransformTridionObjectMetadata(this.SourceTridionObject.TcmId, string.Empty, sourceSchemaTcmId, targetTridionObjectContainerUri, targetSchema.TcmId, this._FormatString, this._Replacements, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                }
                // structure group
                if (sourceItemType == ItemType.StructureGroup)
                {
                    // copy / transform all pages in structure group
                    Functions.TransformMetadataForTridionObjectsInContainer(this.SourceTridionObject.TcmId, sourceSchemaTcmId, targetTridionObjectContainerUri, targetSchema.TcmId, this._FormatString, this._Replacements, localize, this.HistoryMapping, this.CustomComponentTransformer, this.CustomMetadataTransformer, results);
                }
            }

            if (results.Any(x => x.Status == Status.Success))
            {
                ResultInfo resultFinish = new ResultInfo();
                resultFinish.Status = Status.Info;
                resultFinish.Message = "Finished";
                results.Add(resultFinish);
            }
            else
            {
                ResultInfo resultFinish = new ResultInfo();
                resultFinish.Status = Status.Info;
                resultFinish.Message = "No Actions Made";
                results.Add(resultFinish);
            }

            //show results
            ResultsWindow dialog = new ResultsWindow();
            dialog.Host = this.txtHost.Text;
            dialog.ListBoxReport.ItemsSource = results.Where(x => x != null && x.Status != Status.None);
            dialog.ListBoxReport.MouseDoubleClick += lbReport_OnMouseDoubleClick;
            dialog.Show();

            //reload source and target tree
            this._Publications1 = Functions.GetPublications();
            this.treeSourceTridionObject.ItemsSource = this._Publications1.Expand(this.SourceTridionSelectorMode, this.SourceTridionObject.TcmIdPath, this.SourceTridionObject.TcmId).MakeExpandable();

            this._Publications2 = Functions.GetPublications();
            this.treeTargetTridionFolder.ItemsSource = this._Publications2.Expand(this.TargetTridionSelectorMode, this.TargetTridionFolder.TcmIdPath, this.TargetTridionFolder.TcmId).MakeExpandable();
        }