public void DeleteItem(Guid parentId, string itemName, MetadataTemplate metadataTemplate) { var textItem = _destMasterRepository.GetSitecoreChildrenItems(metadataTemplate.destTemplateId, parentId) .FirstOrDefault(item => string.Equals(item.Name, itemName, StringComparison.InvariantCultureIgnoreCase)); if (textItem != null) { _destMasterRepository.DeleteSitecoreItem(textItem); } }
private SCItem CreateDescendantItem(MetadataTemplate.DescendantItem descendantItem, SCItem destParentItem) { if (_appSettings.enableOnlyAnalysisByDefault && destParentItem == null) { destParentItem = CreateDummyItem(); } var descendantItemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateName(descendantItem.destTemplateName); var children = _destMasterRepository.GetSitecoreChildrenItems(descendantItemMetadataTemplate.destTemplateId, destParentItem.ID); if (children != null && children.Any(i => string.Equals(i.Name, descendantItem.itemName, StringComparison.InvariantCultureIgnoreCase))) { return(children.FirstOrDefault(i => string.Equals(i.Name, descendantItem.itemName, StringComparison.InvariantCultureIgnoreCase))); } return(Create(descendantItemMetadataTemplate.destTemplateId, destParentItem, descendantItem.itemName)); }
private void ConvertSubmitMode(SCItem form, SCItem buttonItem) { buttonItem = CheckItemNotNullForAnalysis(buttonItem); var submitMode = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormSubmitModeFieldId)); if (submitMode != null && submitMode.Value == FormConstants.FormSubmitModeField_RedirectModeValue) { var successPage = form.Fields.FirstOrDefault(field => field.FieldId == new Guid(FormConstants.FormSuccessPageFieldId)); if (successPage != null && !string.IsNullOrEmpty(successPage.Value)) { var successPageLink = XmlHelper.GetXmlElementNode(successPage.Value, "link"); if (successPageLink != null) { var successPageId = successPageLink.Attributes["id"]?.Value; if (successPageId != null) { if (!_appSettings.enableReferencedItemCheck || _destMasterRepository.ItemExists(new Guid(successPageId))) { // Redirect To Page Action var redirectToPageValues = new Dictionary <Guid, string>(); redirectToPageValues.Add(new Guid(SubmitActionConstants.SubmitActionFieldId), SubmitActionConstants.SubmitActionField_RedirectToPageActionValue); redirectToPageValues.Add(new Guid(SubmitActionConstants.ParametersFieldId), string.Format("{{\"referenceId\":\"{0}\"}}", successPageId)); redirectToPageValues.Add(new Guid(BaseTemplateConstants.SortOrderFieldId), "5000"); ConvertFieldsToSubmitActionItem("Redirect to Page", redirectToPageValues, buttonItem); } } } } } else if (submitMode == null || submitMode.Value == FormConstants.FormSubmitModeField_ShowMessageValue) // Show Message Mode { // Create success page var successPageItemName = "Success Page"; var pageMetadata = _metadataProvider.GetItemMetadataByTemplateName("Page"); SCItem successPageItem = _destMasterRepository.GetSitecoreChildrenItems(pageMetadata.destTemplateId, form.ID).FirstOrDefault(item => string.Equals(item.Name, successPageItemName, StringComparison.InvariantCultureIgnoreCase)); Guid successPageId; if (successPageItem == null) { successPageId = WriteNewItem(pageMetadata.destTemplateId, form, successPageItemName); successPageItem = _destMasterRepository.GetSitecoreItem(successPageId); } else { successPageId = successPageItem.ID; } // Create Text item with message stored in Success Message field var textMetadata = _metadataProvider.GetItemMetadataByTemplateName("Text"); var successMessageItem = _destMasterRepository.GetSitecoreChildrenItems(textMetadata.destTemplateId, successPageId) .FirstOrDefault(item => string.Equals(item.Name, "Success Message", StringComparison.InvariantCultureIgnoreCase)); if (successMessageItem != null) { _destMasterRepository.DeleteSitecoreItem(successMessageItem); } ConvertTextField(form, successPageId); // Configure Navigation field in Submit button to go to next page if (buttonItem.Fields.Any()) { buttonItem.Fields.First(field => field.FieldId == new Guid(ButtonConstants.ButtonNavigationFieldId)).Value = "1"; } UpdateItem(buttonItem); } }
private List <SCItem> ConvertFields(SCItem destItem, SCItem lastDescendantItem) { var destFields = new List <SCField>(); var destItems = new List <SCItem>(); var sourceFields = destItem.Fields; var itemId = sourceFields.First().ItemId; IEnumerable <Tuple <string, int> > langVersions = sourceFields.Where(f => f.Version != null && f.Language != null).Select(f => new Tuple <string, int>(f.Language, (int)f.Version)).Distinct(); var languages = sourceFields.Where(f => f.Language != null).Select(f => f.Language).Distinct(); // Migrate existing fields if (_itemMetadataTemplate.fields.existingFields != null) { var filteredExistingFields = sourceFields.Where(f => _itemMetadataTemplate.fields.existingFields.Select(mf => mf.fieldId).Contains(f.FieldId)); foreach (var filteredExistingField in filteredExistingFields) { var existingField = _itemMetadataTemplate.fields.existingFields.FirstOrDefault(mf => mf.fieldId == filteredExistingField.FieldId); if (existingField != null) { destFields.Add(filteredExistingField); } } } // Convert fields if (_itemMetadataTemplate.fields.convertedFields != null) { // Select only fields that are mapped var filteredConvertedFields = sourceFields.Where(f => _itemMetadataTemplate.fields.convertedFields.Select(mf => mf.sourceFieldId).Contains(f.FieldId)); foreach (var filteredConvertedField in filteredConvertedFields) { var convertedField = _itemMetadataTemplate.fields.convertedFields.FirstOrDefault(mf => mf.sourceFieldId == filteredConvertedField.FieldId); if (convertedField != null) { // Process fields that have multiple dest fields if (convertedField.destFields != null && convertedField.destFields.Any()) { var valueElements = new List <string>(); try { valueElements = XmlHelper.GetXmlElementNames(filteredConvertedField.Value); } catch (Exception ex) { _logger.Log(new LogEntry(LoggingEventType.Error, string.Format("ItemConverter - Failed to parse Xml value for form field item. ItemID = {0} - FieldID = {1} - FieldValue_Decoded = {2}", itemId, filteredConvertedField.Id, filteredConvertedField.Value), ex)); } var filteredValueElementsToMany = convertedField.destFields.Where(f => valueElements.Contains(f.sourceElementName.ToLower(), StringComparer.InvariantCultureIgnoreCase) && (f.destFieldId == null || f.destFieldId == Guid.Empty)); foreach (var valueXmlElementMapping in filteredValueElementsToMany) { // Special case for List Datasource fields if (string.Equals(valueXmlElementMapping.sourceElementName, "Items", StringComparison.InvariantCultureIgnoreCase)) { IFieldConverter converter = IoC.CreateConverter(valueXmlElementMapping.fieldConverter); List <SCField> convertedFields = converter?.ConvertValueElementToFields(filteredConvertedField, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName)); if (convertedFields != null && convertedFields.Any()) { destFields.AddRange(convertedFields); } // Delete existing list items var listItemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateName("ExtendedListItem"); if (lastDescendantItem != null) { var listItems = _destMasterRepository.GetSitecoreChildrenItems(listItemMetadataTemplate.destTemplateId, lastDescendantItem.ID); foreach (SCItem listItem in listItems) { _destMasterRepository.DeleteSitecoreItem(listItem); } } List <SCItem> convertedItems = converter?.ConvertValueElementToItems(filteredConvertedField, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName), listItemMetadataTemplate, lastDescendantItem ?? destItem); if (convertedItems != null && convertedItems.Any()) { destItems.AddRange(convertedItems); } // Reporting if (convertedFields?.Count > 0 && convertedItems?.Count > 0) { _conversionReporter.AddUnmappedValueElementSourceField(filteredConvertedField, itemId, valueXmlElementMapping.sourceElementName, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName)); } } } var filteredValueElements = convertedField.destFields.Where(f => valueElements.Contains(f.sourceElementName.ToLower(), StringComparer.InvariantCultureIgnoreCase) && (f.destFieldId != null && f.destFieldId != Guid.Empty)); foreach (var valueXmlElementMapping in filteredValueElements) { IFieldConverter converter = IoC.CreateConverter(valueXmlElementMapping.fieldConverter); SCField destField = converter?.ConvertValueElement(filteredConvertedField, (Guid)valueXmlElementMapping.destFieldId, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, valueXmlElementMapping.sourceElementName), destItems); if (destField != null && destField.FieldId != Guid.Empty) { destFields.Add(destField); } } // Reporting var unmappedValueElementSourceFields = valueElements.Where(v => !convertedField.destFields.Select(f => f.sourceElementName) .Contains(v, StringComparer.InvariantCultureIgnoreCase)); foreach (var unmappedValueElementSourceField in unmappedValueElementSourceFields) { _conversionReporter.AddUnmappedValueElementSourceField(filteredConvertedField, itemId, unmappedValueElementSourceField, XmlHelper.GetXmlElementValue(filteredConvertedField.Value, unmappedValueElementSourceField)); } } // Process fields that have a single dest field else if (convertedField.destFieldId != null && convertedField.destFieldId != Guid.Empty) { IFieldConverter converter = IoC.CreateConverter(convertedField.fieldConverter); SCField destField = converter?.ConvertField(filteredConvertedField, (Guid)convertedField.destFieldId); if (destField != null && destField.FieldId != Guid.Empty) { destFields.Add(destField); } } } } } if (_itemMetadataTemplate.fields.newFields != null) { // Create new fields foreach (var newField in _itemMetadataTemplate.fields.newFields) { destFields.AddRange(_fieldFactory.CreateFields(newField, itemId, langVersions, languages)); } } destItem.Fields = destFields; destItems.Add(destItem); // Merge multi-language List items with same value destItems = MergeListItems(destItems); // Reporting var unmappedSourceFields = sourceFields?.Where(f => (_itemMetadataTemplate.fields.existingFields == null || !_itemMetadataTemplate.fields.existingFields.Select(mf => mf.fieldId).Contains(f.FieldId)) && (_itemMetadataTemplate.fields.convertedFields == null || !_itemMetadataTemplate.fields.convertedFields.Select(mf => mf.sourceFieldId).Contains(f.FieldId))); foreach (SCField unmappedSourceField in unmappedSourceFields) { _conversionReporter.AddUnmappedItemField(unmappedSourceField, itemId); } return(destItems); }
public List <Guid> ConvertForms() { try { var sourceFormTemplateId = _metadataProvider.GetItemMetadataByTemplateName(FormTemplateName)?.sourceTemplateId; if (sourceFormTemplateId == null) { return(null); } var destPageTemplateId = _metadataProvider.GetItemMetadataByTemplateName(PageTemplateName)?.destTemplateId; if (destPageTemplateId == null) { return(null); } var sourceSectionTemplateId = _metadataProvider.GetItemMetadataByTemplateName(SectionTemplateName)?.sourceTemplateId; if (sourceSectionTemplateId == null) { return(null); } var sourceFieldTemplateId = _metadataProvider.GetItemMetadataByTemplateName(InputTemplateName)?.sourceTemplateId; if (sourceFieldTemplateId == null) { return(null); } var destButtonTemplateId = _metadataProvider.GetItemMetadataByTemplateName(ButtonTemplateName)?.destTemplateId; if (destButtonTemplateId == null) { return(null); } var forms = _sourceMasterRepository.GetSitecoreItems((Guid)sourceFormTemplateId); // Filter forms to select only included forms in appSettings "includeOnlyFormIds" parameter if (_appSettings.includeOnlyFormIds != null && _appSettings.includeOnlyFormIds.Any()) { forms = forms.Where(form => _appSettings.includeOnlyFormIds.Contains(form.ID)).ToList(); } // Filter forms to exclude forms listed in appSettings "excludeFormIds" parameter if (_appSettings.excludeFormIds != null && _appSettings.excludeFormIds.Any()) { forms = forms.Where(form => !_appSettings.excludeFormIds.Contains(form.ID)).ToList(); } // Filter sample forms out if (_appSettings.excludeSampleWffmForms) { forms = forms.Where(form => form.ParentID != _appSettings.itemReferences["sourceSampleFormsFolderId"]).ToList(); } if (_appSettings.enableOnlyAnalysisByDefault) { Console.WriteLine($" Found {forms.Count} forms to analyze."); Console.WriteLine(); Console.WriteLine(" Starting forms analysis..."); Console.WriteLine(); } else { Console.WriteLine($" Found {forms.Count} forms to convert."); Console.WriteLine(); Console.WriteLine(" Starting forms conversion..."); Console.WriteLine(); } var formCounter = 0; // Start progress bar var formAction = _appSettings.enableOnlyAnalysisByDefault ? "analyzed" : "converted"; ProgressBar.DrawTextProgressBar(formCounter, forms.Count, $"forms {formAction}"); foreach (var form in forms) { try { // Set current form being processed in a global variable for reporting _conversionReporter.CurrentFormId = form.ID.ToString("B").ToUpper(); _conversionReporter.CurrentFormName = form.Name; // Convert and Migrate Form items ConvertAndWriteItem(form, _appSettings.itemReferences["destFormFolderId"]); // Create Page item var pageId = Guid.Empty; SCItem pageItem = null; if (!_destMasterRepository.ItemHasChildrenOfTemplate((Guid)destPageTemplateId, form)) { // Create Page items for each form (only once) pageId = WriteNewItem((Guid)destPageTemplateId, form, "Page"); } else { // Get Page Item Id pageItem = _destMasterRepository.GetSitecoreChildrenItems((Guid)destPageTemplateId, form.ID).FirstOrDefault(item => string.Equals(item.Name, "Page", StringComparison.InvariantCultureIgnoreCase)); pageId = pageItem?.ID ?? form.ID; } if (pageItem == null) { pageItem = _destMasterRepository.GetSitecoreItem(pageId); } // Convert and Migrate Section items var sections = _sourceMasterRepository.GetSitecoreChildrenItems((Guid)sourceSectionTemplateId, form.ID); foreach (var section in sections) { ConvertAndWriteItem(section, pageId); _sectionAppearanceConverter.ConvertTitle(section); _sectionAppearanceConverter.ConvertInformation(section); } // Convert and Migrate Form Field items List <SCItem> formFields = new List <SCItem>(); formFields.AddRange(_sourceMasterRepository.GetSitecoreChildrenItems((Guid)sourceFieldTemplateId, form.ID)); foreach (var section in sections) { formFields.AddRange(_sourceMasterRepository.GetSitecoreChildrenItems((Guid)sourceFieldTemplateId, section.ID)); } foreach (var formField in formFields) { var parentItem = _sourceMasterRepository.GetSitecoreItem(formField.ParentID); var destParentId = parentItem.TemplateID == sourceFormTemplateId ? pageId : parentItem.ID; ConvertAndWriteItem(formField, destParentId); } // Convert Submit form section fields _submitConverter.Convert(form, pageItem); // Convert Form Appearance fields _formAppearanceConverter.ConvertTitle(form, pageItem); _formAppearanceConverter.ConvertIntroduction(form, pageItem); _formAppearanceConverter.ConvertFooter(form, pageItem); formCounter++; // Update progress bar ProgressBar.DrawTextProgressBar(formCounter, forms.Count, $"forms {formAction}"); } catch (Exception ex) { _logger.Log(new LogEntry(LoggingEventType.Error, string.Format("Error processing form ItemID = {0}", form.ID), ex)); throw; } } if (_appSettings.enableOnlyAnalysisByDefault) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine(" Finished forms analysis."); Console.WriteLine(); } else { Console.WriteLine(); Console.WriteLine(); Console.WriteLine(" Finished forms conversion."); Console.WriteLine(); } // Write analysis results _conversionReporter.GenerateOutput(); return(forms.Select(form => form.ID).ToList()); } catch (Exception ex) { _logger.Log(new LogEntry(LoggingEventType.Error, "Error while converting forms.", ex)); throw; } }