public List <SCItem> CreateDescendantItems(MetadataTemplate metadataTemplate, SCItem parentItem)
        {
            var destItems = new List <SCItem>();

            if (metadataTemplate.descendantItems != null)
            {
                foreach (var descendantItem in metadataTemplate.descendantItems)
                {
                    if (descendantItem.isParentChild)
                    {
                        var destDescItem = CreateDescendantItem(descendantItem, parentItem);
                        if (destDescItem != null)
                        {
                            destItems.Add(destDescItem);
                        }
                    }
                    else
                    {
                        var destParentItem = destItems.FirstOrDefault(d =>
                                                                      string.Equals(d.Name, descendantItem.parentItemName, StringComparison.InvariantCultureIgnoreCase));
                        if (destParentItem != null)
                        {
                            var destDescItem = CreateDescendantItem(descendantItem, destParentItem);
                            if (destDescItem != null)
                            {
                                destItems.Add(destDescItem);
                            }
                        }
                    }
                }
            }

            return(destItems);
        }
        public virtual void WriteDescendentItems(MetadataTemplate metadataTemplate, SCItem parentItem)
        {
            var descendantItems = _itemFactory.CreateDescendantItems(metadataTemplate, parentItem);

            foreach (SCItem descendantItem in descendantItems)
            {
                _destMasterRepository.AddOrUpdateSitecoreItem(descendantItem);
            }
        }
        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);
            }
        }
Beispiel #4
0
        public void GenerateDomainProject(DomainDataService dataService, string outputFolder)
        {
            outputFolder = Path.Combine(outputFolder, $"{dataService.Solution.NamespacePrefix}.{dataService.Name}.Domain");
            var constantsFolder = Path.Combine(outputFolder, "Constants");
            var modelsFolder    = Path.Combine(outputFolder, "Models");
            var metadataFolder  = Path.Combine(outputFolder, "Metadata");

            ReplaceDirectories(constantsFolder, modelsFolder, metadataFolder);

            var session = new Dictionary <string, object>();

            session.Add("DomainDataService", dataService);

            DomainProjectTemplate projectTemplate = new DomainProjectTemplate();

            projectTemplate.Session = session;
            projectTemplate.Initialize();
            string content = projectTemplate.TransformText();

            File.WriteAllText(Path.Combine(outputFolder, $"{dataService.Solution.NamespacePrefix}.{dataService.Name}.Domain.csproj"), content);

            DomainConstantsTemplate constantsTemplate = new DomainConstantsTemplate();

            constantsTemplate.Session = session;
            constantsTemplate.Initialize();
            content = constantsTemplate.TransformText();
            File.WriteAllText(Path.Combine(constantsFolder, $"{dataService.Name}Constants.cs"), content);

            if (!(dataService.Schemas is null))
            {
                foreach (var domainObject in dataService.Schemas.SelectMany(s => s.Objects))
                {
                    session["CurrentObject"] = domainObject;

                    ModelTemplate modelTemplate = new ModelTemplate();
                    modelTemplate.Session = session;
                    modelTemplate.Initialize();
                    content = modelTemplate.TransformText();
                    File.WriteAllText(Path.Combine(modelsFolder, $"{domainObject.ObjectName}.cs"), content);

                    MetadataTemplate metadataTemplate = new MetadataTemplate();
                    metadataTemplate.Session = session;
                    metadataTemplate.Initialize();
                    content = metadataTemplate.TransformText();
                    File.WriteAllText(Path.Combine(metadataFolder, $"{domainObject.ObjectName}Metadata.cs"), content);
                }
            }

            MetadataBundleTemplate bundleTemplate = new MetadataBundleTemplate();

            bundleTemplate.Session = session;
            bundleTemplate.Initialize();
            content = bundleTemplate.TransformText();
            File.WriteAllText(Path.Combine(metadataFolder, $"{dataService.Name}MetadataBundle.cs"), content);
        }
        public SCItem Create(Guid destTemplateId, SCItem parentItem, string itemName, MetadataTemplate metadataTemplate = null)
        {
            if (metadataTemplate != null)
            {
                _itemMetadataTemplate = metadataTemplate;
            }
            else
            {
                _itemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateId(destTemplateId);
            }

            itemName = RemoveInvalidChars(itemName);

            return(CreateItem(parentItem, itemName));
        }
Beispiel #6
0
        private MetadataTemplate GetItemMetadataByFilePath(string filePath)
        {
            // Read json file
            var itemMeta = System.IO.File.ReadAllText(filePath);
            // Deserialize Json to Object
            MetadataTemplate metadataTemplate = JsonConvert.DeserializeObject <MetadataTemplate>(itemMeta);

            if (string.IsNullOrEmpty(metadataTemplate.baseTemplateMetadataFileName))
            {
                return(metadataTemplate);
            }
            var baseTemplateMetadataFilePath = _metadataFiles.FirstOrDefault(f => GetFileName(f).Equals(metadataTemplate.baseTemplateMetadataFileName, StringComparison.InvariantCultureIgnoreCase));
            var fullMetadataTemplate         = MergeBaseMetadataTemplate(metadataTemplate, baseTemplateMetadataFilePath);

            return(fullMetadataTemplate);
        }
Beispiel #7
0
        public void SetDataForEdit(string name, string value, string data)
        {
            this.Text = "Edit Metadata Item";
            bool metadataDefExists = false;

            if (_MetadataTemplate != null && _MetadataTemplate.MetadataDefinitionList != null && _MetadataTemplate.MetadataDefinitionList.Count > 0)
            {
                foreach (MetadataDefinition metadataDefItem in _MetadataTemplate.MetadataDefinitionList)
                {
                    if (metadataDefItem != null)
                    {
                        if (metadataDefItem.Name != null && string.Compare(metadataDefItem.Name, name, false) == 0)
                        {
                            _SelectedMetadataDef = metadataDefItem;
                            metadataDefExists    = true;
                            break;
                        }
                    }
                }
            }

            if (metadataDefExists == false)
            {
                MetadataDefinition metadataDef = new MetadataDefinition();
                metadataDef.CanEditName  = true;
                metadataDef.CanEditValue = true;
                metadataDef.Name         = name;
                metadataDef.ValueList.Add(value);

                if (_MetadataTemplate == null)
                {
                    _MetadataTemplate = new MetadataTemplate();
                }
                if (_MetadataTemplate.MetadataDefinitionList == null)
                {
                    _MetadataTemplate.MetadataDefinitionList = new List <MetadataDefinition>();
                }
                _MetadataTemplate.MetadataDefinitionList.Add(metadataDef);
                _SelectedMetadataDef = metadataDef;
            }

            _SelectedData  = data;
            _SelectedValue = value;
        }
Beispiel #8
0
        private MetadataTemplate MergeBaseMetadataTemplate(MetadataTemplate metadataTemplate, string baseTemplateMetadataFilePath)
        {
            if (string.IsNullOrEmpty(baseTemplateMetadataFilePath))
            {
                return(metadataTemplate);
            }

            MetadataTemplate baseTemplateMeta = GetItemMetadataByFilePath(baseTemplateMetadataFilePath);

            if (baseTemplateMeta == null)
            {
                return(metadataTemplate);
            }

            // Iterate merging if baseTemplate filename is not null
            if (!string.IsNullOrEmpty(baseTemplateMeta.baseTemplateMetadataFileName))
            {
                var filePath = _metadataFiles.FirstOrDefault(f => f.IndexOf(baseTemplateMeta.baseTemplateMetadataFileName, StringComparison.InvariantCultureIgnoreCase) > -1);
                baseTemplateMeta = MergeBaseMetadataTemplate(baseTemplateMeta, filePath);
            }

            // Merge Fields
            metadataTemplate.fields = MergeFields(baseTemplateMeta.fields, metadataTemplate.fields);

            // Merge Descendant Items
            metadataTemplate.descendantItems =
                MergeDescendantItems(baseTemplateMeta.descendantItems, metadataTemplate.descendantItems);

            // Override dataValueType
            if (string.IsNullOrEmpty(metadataTemplate.dataValueType))
            {
                metadataTemplate.dataValueType = baseTemplateMeta.dataValueType;
            }

            // Override dataValueConverter
            if (string.IsNullOrEmpty(metadataTemplate.dataValueConverter))
            {
                metadataTemplate.dataValueConverter = baseTemplateMeta.dataValueConverter;
            }

            return(metadataTemplate);
        }
Beispiel #9
0
        public List <SCItem> Convert(SCItem scItem, Guid destParentId)
        {
            _itemMetadataTemplate = _metadataProvider.GetItemMetadataByTemplateId(scItem.TemplateID);
            if (_itemMetadataTemplate.sourceMappingFieldId != null && _itemMetadataTemplate.sourceMappingFieldId != Guid.Empty)
            {
                var sourceMappingFieldValue = scItem.Fields
                                              .FirstOrDefault(f => f.FieldId == _itemMetadataTemplate.sourceMappingFieldId)?.Value;
                var mappedMetadataTemplate = _metadataProvider.GetItemMetadataBySourceMappingFieldValue(sourceMappingFieldValue);
                if (mappedMetadataTemplate != null)
                {
                    _itemMetadataTemplate = mappedMetadataTemplate;
                }
                else
                {
                    // Add record in conversion analysis
                    _conversionReporter.AddUnmappedFormFieldItem(scItem.ID, sourceMappingFieldValue);
                }
            }

            List <SCItem> destItems = ConvertItemAndFields(scItem, destParentId);

            return(destItems);
        }
Beispiel #10
0
        public override List <SCItem> ConvertValueElementToItems(SCField scField, string elementValue, MetadataTemplate metadataTemplate, SCItem sourceItem)
        {
            List <SCItem> convertedItems = new List <SCItem>();

            var decodedElementValue = Uri.UnescapeDataString(elementValue).Replace("+", " ");

            var queryElements = XmlHelper.GetXmlElementNodeList(decodedElementValue, "query");

            if (queryElements != null && queryElements.Count > 0)
            {
                foreach (XmlNode queryElement in queryElements)
                {
                    if (queryElement.Attributes != null && queryElement.Attributes["t"] != null)
                    {
                        var queryType = queryElement.Attributes["t"].Value;
                        if (string.Equals(queryType, "default", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var value                 = XmlHelper.GetXmlElementValue(queryElement.InnerXml, "value");
                            var displayName           = value;
                            var queryChildrenElements = XmlHelper.GetXmlElementNames(queryElement.InnerXml);
                            foreach (string queryChildrenElementName in queryChildrenElements)
                            {
                                if (!string.Equals(queryChildrenElementName, "value", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    displayName = XmlHelper.GetXmlElementValue(queryElement.InnerXml, queryChildrenElementName);
                                }
                            }

                            if (!string.IsNullOrEmpty(value))
                            {
                                // Set item value
                                metadataTemplate.fields.newFields
                                .First(field => field.destFieldId == new Guid("{3A07C171-9BCA-464D-8670-C5703C6D3F11}")).value = value;
                                // Set display name
                                metadataTemplate.fields.newFields
                                .First(field => field.destFieldId == new Guid("{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}")).value = displayName;
                                SCItem convertedItem = _itemFactory.Create(metadataTemplate.destTemplateId, sourceItem, value, metadataTemplate);
                                convertedItems.Add(convertedItem);
                            }
                        }
                    }
                }
            }

            return(convertedItems);
        }
        public virtual Guid WriteNewItem(Guid destTemplateId, SCItem parentItem, string itemName, MetadataTemplate metadataTemplate = null)
        {
            var destItem = _itemFactory.Create(destTemplateId, parentItem, itemName, metadataTemplate);

            _destMasterRepository.AddOrUpdateSitecoreItem(destItem);

            return(destItem.ID);
        }
Beispiel #12
0
        /// <summary>
        /// Generates Go code for service client.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <returns></returns>
        public override async Task Generate(CodeModel cm)
        {
            var folder = Path.GetFullPath(Settings.Instance.Host.GetValue <string>("output-folder").Result).Replace('\\', '/');
            // check if the namespace contains illegal characters
            var ns = Settings.Instance.Host.GetValue <string>("namespace").Result;

            if (Settings.Instance.Host.GetValue <bool>("namespace-chk").Result)
            {
                NamespaceCheck(ns);
            }

            // if preview-chk:true is specified verify that preview swagger is output under a preview subdirectory.
            // this is a bit of a hack until we have proper support for this in the swagger->sdk bot so it's opt-in.
            if (Settings.Instance.Host.GetValue <bool>("preview-chk").Result)
            {
                PreviewCheck(folder);
            }

            var codeModel = cm as CodeModelGo;

            if (codeModel == null)
            {
                throw new Exception("Code model is not a Go Code Model");
            }

            // unfortunately there is an ordering issue here.  during model generation we might
            // flatten some types (but not all depending on type).  then during client generation
            // the validation codegen needs to know if a type was flattened so it can generate
            // the correct code, so we need to generate models before clients.

            // Models
            var modelsTemplate = new ModelsTemplate
            {
                Model = codeModel
            };

            await Write(modelsTemplate, FormatFileName("models"));

            // Enums - while initially enums should be part of the models, to decrease the size of the models.go file,
            // we separate the enums definitions out of the models.go file
            var enums = codeModel.EnumTypes.Cast <EnumTypeGo>().ToList();

            if (enums.Any())
            {
                var enumsTemplate = new EnumsTemplate
                {
                    Model = codeModel
                };
                await Write(enumsTemplate, FormatFileName("enums"));
            }

            // Service client
            var serviceClientTemplate = new ServiceClientTemplate
            {
                Model = codeModel
            };

            await Write(serviceClientTemplate, FormatFileName("client"));

            // by convention the methods in the method group with an empty
            // name go into the client template so skip them here.
            HashSet <string> ReservedFiles = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
            {
                "models",
                "client",
                "version",
                "interfaces",
                "enums",
            };

            foreach (var methodGroup in codeModel.MethodGroups.Where(mg => !string.IsNullOrEmpty(mg.Name)))
            {
                if (ReservedFiles.Contains(methodGroup.Name.Value))
                {
                    methodGroup.Name += "group";
                }
                ReservedFiles.Add(methodGroup.Name);
                var methodGroupTemplate = new MethodGroupTemplate
                {
                    Model = methodGroup
                };
                await Write(methodGroupTemplate, FormatFileName(methodGroup.Name).ToLowerInvariant());
            }

            // interfaces
            var interfacesTemplate = new InterfacesTemplate {
                Model = codeModel
            };

            await Write(interfacesTemplate, FormatFileName($"{CodeNamerGo.InterfacePackageName(codeModel.Namespace)}/interfaces"));

            // Version
            var versionTemplate = new VersionTemplate {
                Model = codeModel
            };

            await Write(versionTemplate, FormatFileName("version"));

            // go.mod file, opt-in by specifying the gomod-root arg
            var modRoot = Settings.Instance.Host.GetValue <string>("gomod-root").Result;

            if (!string.IsNullOrWhiteSpace(modRoot))
            {
                var i = folder.IndexOf(modRoot);
                if (i == -1)
                {
                    throw new Exception($"didn't find module root '{modRoot}' in output path '{folder}'");
                }
                var goVersion = Settings.Instance.Host.GetValue <string>("go-version").Result;
                if (string.IsNullOrWhiteSpace(goVersion))
                {
                    goVersion = defaultGoVersion;
                }
                // module name is everything to the right of the start of the module root
                var gomodTemplate = new GoModTemplate {
                    Model = new GoMod(folder.Substring(i), goVersion)
                };
                await Write(gomodTemplate, $"{StagingDir()}go.mod");
            }

            // metadata
            var metadataOutputFolder = Settings.Instance.Host.GetValue <string>("metadata-output-folder").Result;

            if (!string.IsNullOrWhiteSpace(metadataOutputFolder))
            {
                var metadataTemplate = new MetadataTemplate
                {
                    Model = new MetadataGo(Settings.Instance.Host.GetValue <string[]>("input-file").Result, folder, ns)
                };
                var tag = Settings.Instance.Host.GetValue <string>("tag").Result;
                await Write(metadataTemplate, $"{metadataOutputFolder}/{tag}.json");
            }
        }
Beispiel #13
0
        public override List <SCItem> ConvertValueElementToItems(SCField scField, string elementValue, MetadataTemplate metadataTemplate, SCItem sourceItem)
        {
            List <SCItem> convertedItems = new List <SCItem>();

            var decodedElementValue = Uri.UnescapeDataString(elementValue).Replace("+", " ");

            XmlNodeList queryElements = null;

            try
            {
                queryElements = XmlHelper.GetXmlElementNodeList(decodedElementValue, "query", true);
            }
            catch (Exception ex)
            {
                _logger.Log(new LogEntry(LoggingEventType.Error,
                                         string.Format("ConvertValueElementToItems - Failed to parse Xml value. ItemID = {0} - FieldID = {1} - ElementName = {2} - ElementValue_Decoded = {3}",
                                                       scField.ItemId, scField.Id, "query", decodedElementValue), ex));
            }
            if (queryElements != null && queryElements.Count > 0)
            {
                foreach (XmlNode queryElement in queryElements)
                {
                    if (queryElement.Attributes != null && queryElement.Attributes["t"] != null)
                    {
                        var queryType = queryElement.Attributes["t"].Value;
                        if (string.Equals(queryType, "default", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var value                 = XmlHelper.GetXmlElementValue(queryElement.InnerXml, "value");
                            var displayName           = value;
                            var displayNames          = new Dictionary <Tuple <string, int>, string>();
                            var queryChildrenElements = XmlHelper.GetXmlElementNames(queryElement.InnerXml);
                            foreach (string queryChildrenElementName in queryChildrenElements)
                            {
                                if (!string.Equals(queryChildrenElementName, "value", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    displayName = XmlHelper.GetXmlElementValue(queryElement.InnerXml, queryChildrenElementName);

                                    displayNames.Add(new Tuple <string, int>(queryChildrenElementName, 1), displayName);
                                }
                            }

                            if (!string.IsNullOrEmpty(value))
                            {
                                // Set item value
                                metadataTemplate.fields.newFields
                                .First(field => field.destFieldId == new Guid("{3A07C171-9BCA-464D-8670-C5703C6D3F11}")).value = value;
                                // Set display name
                                if (displayNames.Any())
                                {
                                    metadataTemplate.fields.newFields
                                    .First(field => field.destFieldId == new Guid("{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}")).values = displayNames;
                                }
                                else
                                {
                                    metadataTemplate.fields.newFields
                                    .First(field => field.destFieldId == new Guid("{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}")).value = displayName;
                                }
                                SCItem convertedItem = _itemFactory.Create(metadataTemplate.destTemplateId, sourceItem, value, metadataTemplate);
                                convertedItems.Add(convertedItem);
                            }
                        }
                    }
                }
            }

            return(convertedItems);
        }
Beispiel #14
0
 public void Init(MetadataTemplate metadataTemplate)
 {
     _MetadataTemplate = metadataTemplate;
 }
 public virtual List <SCItem> ConvertValueElementToItems(SCField scField, string elementValue, MetadataTemplate metadataTemplate, SCItem sourceItem)
 {
     return(new List <SCItem>());
 }