Example #1
0
        private static string GenerateCSharpClasses(ModelMetadata modelMetadata)
        {
            JsonMetadataParser modelGenerator = new JsonMetadataParser();
            string             classes        = modelGenerator.CreateModelFromMetadata(modelMetadata);

            return(classes);
        }
Example #2
0
        public void ConvertServiceModel()
        {
            Dictionary <string, Dictionary <string, string> > textDictionary = new Dictionary <string, Dictionary <string, string> >();

            Mock <IRepository> moqRepository = new Mock <IRepository>();

            moqRepository
            .Setup(r => r.GetServiceTexts(It.IsAny <string>(), It.IsAny <string>())).Returns(textDictionary);

            var       seresParser = new SeresXsdParser(moqRepository.Object);
            XDocument mainXsd     = XDocument.Load("Common/xsd/ServiceModel.xsd");

            ServiceMetadata serviceMetadata = seresParser.ParseXsdToServiceMetadata("123", "app", mainXsd, null);

            string metadataAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(serviceMetadata);

            File.WriteAllText("service-model.json", metadataAsJson);

            JsonMetadataParser metadataParser = new JsonMetadataParser();
            string             classMeta      = metadataParser.CreateModelFromMetadata(serviceMetadata);

            File.WriteAllText("service-model.cs", classMeta);

            File.WriteAllText("servcie-model-texts.txt", Newtonsoft.Json.JsonConvert.SerializeObject(textDictionary));
        }
Example #3
0
        public void ConvertNæringsoppgave()
        {
            string xsdFileName = "Common/xsd/melding-2-12186.xsd";
            string outName     = "melding-2-12186-output";

            Dictionary <string, Dictionary <string, string> > textDictionary = new Dictionary <string, Dictionary <string, string> >();

            Mock <IRepository> moqRepository = new Mock <IRepository>();

            moqRepository
            .Setup(r => r.GetServiceTexts(It.IsAny <string>(), It.IsAny <string>())).Returns(textDictionary);

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreWhitespace = true;

            var       doc     = XmlReader.Create(xsdFileName, settings);
            XDocument mainXsd = XDocument.Load(doc);

            var             seresParser     = new SeresXsdParser(moqRepository.Object);
            ServiceMetadata serviceMetadata = seresParser.ParseXsdToServiceMetadata("123", "app", mainXsd, null);

            string metadataAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(serviceMetadata);

            File.WriteAllText(outName + ".json", metadataAsJson);

            JsonMetadataParser metadataParser = new JsonMetadataParser();
            string             classMeta      = metadataParser.CreateModelFromMetadata(serviceMetadata);

            File.WriteAllText(outName + ".cs", classMeta);

            File.WriteAllText(outName + "-texts.json", Newtonsoft.Json.JsonConvert.SerializeObject(textDictionary));
        }
Example #4
0
        public void CreateModelFromMetadata_InputModelSpecifiedModelName_GenerateDataAnnotationForRoomElement()
        {
            // Arrange
            ModelMetadata testData = LoadTestData("Designer.Tests._TestData.Model.Metadata.RA-0678_M.metadata.json");

            JsonMetadataParser target = new JsonMetadataParser();

            // Act
            string modelClass = target.CreateModelFromMetadata(testData);

            // Assert
            Assert.NotNull(modelClass);
            Assert.Contains("[XmlRoot(ElementName=\"melding\")]", modelClass);
        }
Example #5
0
        public void CreateModelFromMetadata_InputModelWithRestrictionMinimumAndMaximum_GenerateDataAnnotationWithRangeFromMinToMax()
        {
            // Arrange
            ModelMetadata testData = LoadTestData("Designer.Tests._TestData.Model.Metadata.restriction-total-digits.metadata.json");

            JsonMetadataParser target = new JsonMetadataParser();

            // Act
            string modelClass = target.CreateModelFromMetadata(testData);

            // Assert
            Assert.NotNull(modelClass);
            Assert.Contains("[Range(-7.766279631452242E+18, 7.766279631452242E+18)]", modelClass);
        }
Example #6
0
        public void CreateModelFromMetadata_InputModelWithRestrictionMinLengthAndMaxLength_GenerateDataAnnotationWithMinLengthAndMaxLengthAttributes()
        {
            // Arrange
            ModelMetadata testData = LoadTestData("Designer.Tests._TestData.Model.Metadata.restriction-total-digits.metadata.json");

            JsonMetadataParser target = new JsonMetadataParser();

            // Act
            string modelClass = target.CreateModelFromMetadata(testData);

            // Assert
            Assert.NotNull(modelClass);
            Assert.Contains("[MinLength(1)]", modelClass);
            Assert.Contains("[MaxLength(20)]", modelClass);
        }
Example #7
0
        public void ConvertSkatteetatenBSU()
        {
            Mock <IRepository> moqRepository = new Mock <IRepository>();
            var       seresParser            = new SeresXsdParser(moqRepository.Object);
            XDocument mainXsd = XDocument.Load("Common/xsd/boligsparingForUngdom_v1_1.xsd");

            ServiceMetadata serviceMetadata = seresParser.ParseXsdToServiceMetadata("123", "app", mainXsd, null);

            string metadataAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(serviceMetadata);

            File.WriteAllText("boligsparingForUngdom.json", metadataAsJson);

            JsonMetadataParser metadataParser = new JsonMetadataParser();
            string             classMeta      = metadataParser.CreateModelFromMetadata(serviceMetadata);

            File.WriteAllText("boligsparingForUngdom.cs", classMeta);
        }
Example #8
0
        public void ConvertExistingEdagSchemaToJsonMetadataAndCsharp()
        {
            Mock <IRepository> moqRepository = new Mock <IRepository>();
            var       seresParser            = new SeresXsdParser(moqRepository.Object);
            XDocument mainXsd = XDocument.Load("Common/xsd/schema.xsd");

            ServiceMetadata serviceMetadata = seresParser.ParseXsdToServiceMetadata("123", "app", mainXsd, null);

            string metadataAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(serviceMetadata);

            File.WriteAllText("edag.original.metadata.json", metadataAsJson);

            JsonMetadataParser metadataParser = new JsonMetadataParser();
            string             classMeta      = metadataParser.CreateModelFromMetadata(serviceMetadata);

            File.WriteAllText("edag.original.csharp.cs", classMeta);
        }
Example #9
0
        public void ConvertRecursiveSchemaJsonMetadata()
        {
            Mock <IRepository> moqRepository = new Mock <IRepository>();
            var       seresParser            = new SeresXsdParser(moqRepository.Object);
            XDocument mainXsd = XDocument.Load("Common/xsd/schema-w-recursion.xsd");

            ServiceMetadata serviceMetadata = seresParser.ParseXsdToServiceMetadata("123", "app", mainXsd, null);

            string metadataAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(serviceMetadata);

            File.WriteAllText("metadata-recursive.json", metadataAsJson);

            JsonMetadataParser metadataParser = new JsonMetadataParser();
            string             classMeta      = metadataParser.CreateModelFromMetadata(serviceMetadata);

            File.WriteAllText("classdata-recursive.cs", classMeta);
        }
Example #10
0
        public static Dictionary <string, string> Convert(Altinn2AppData a2, out string?modelName)
        {
            modelName = null;
            var ret = new Dictionary <string, string>();

            if (a2.XSNFiles.Count == 0)
            {
                return(ret);
            }

            // Get xsd from first xsn file (all languages are equal)
            string xsd = a2.XSNFiles.First().Value.XSDDocument;

            if (xsd == null)
            {
                return(ret);
            }

            ret.Add("model.xsd", xsd);
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xsd)))
            {
                var             reader = XmlReader.Create(stream);
                XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(reader);

                JsonSchema schemaJsonSchema = xsdToJsonSchemaConverter.AsJsonSchema();
                ret.Add("model.schema.json", new Manatee.Json.Serialization.JsonSerializer().Serialize(schemaJsonSchema).GetIndentedString(0));

                JsonSchemaToInstanceModelGenerator converter = new JsonSchemaToInstanceModelGenerator(a2.Org, a2.App, schemaJsonSchema);
                ModelMetadata modelMetadata = converter.GetModelMetadata();
                ret.Add("model.metadata.json", JsonConvert.SerializeObject(modelMetadata, Newtonsoft.Json.Formatting.Indented));

                modelName = modelMetadata.Elements["melding"].TypeName;
                // generate c# model
                JsonMetadataParser modelGenerator = new JsonMetadataParser();
                string             classes        = modelGenerator.CreateModelFromMetadata(modelMetadata);
                ret.Add("model.cs", classes);

                // HandleTexts(org, app, converter.GetTexts());
            }

            return(ret);
        }
Example #11
0
        public async Task <IActionResult> UpdateDatamodel(string org, string repository, string modelName)
        {
            SchemaKeywordCatalog.Add <InfoKeyword>();

            try
            {
                modelName = modelName.AsFileName();
            }
            catch
            {
                return(BadRequest("Invalid model name value."));
            }

            string filePath = $"App/models/{modelName}";

            using (Stream resource = Request.Body)
            {
                // Read the request body and deserialize to Json Schema
                using StreamReader streamReader = new StreamReader(resource);
                string content = await streamReader.ReadToEndAsync();

                TextReader textReader = new StringReader(content);
                JsonValue  jsonValue  = await JsonValue.ParseAsync(textReader);

                JsonSchema jsonSchemas = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue);

                // Create the directory if it does not exist
                string appPath   = _repository.GetAppPath(org, repository);
                string directory = appPath + Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                // Serialize and store the Json Schema
                var          serializer = new Manatee.Json.Serialization.JsonSerializer();
                JsonValue    toar       = serializer.Serialize(jsonSchemas);
                byte[]       byteArray  = Encoding.UTF8.GetBytes(toar.ToString());
                MemoryStream jsonstream = new MemoryStream(byteArray);
                await _repository.WriteData(org, repository, $"{filePath}.schema.json", jsonstream);

                // update meta data
                JsonSchemaToInstanceModelGenerator converter = new JsonSchemaToInstanceModelGenerator(org, repository, jsonSchemas);
                ModelMetadata modelMetadata = converter.GetModelMetadata();
                string        root          = modelMetadata.Elements != null && modelMetadata.Elements.Count > 0 ? modelMetadata.Elements.Values.First(e => e.ParentElement == null).TypeName : null;
                _repository.UpdateApplicationWithAppLogicModel(org, repository, modelName, "Altinn.App.Models." + root);
                _repository.UpdateModelMetadata(org, repository, modelMetadata, modelName);

                // Convert to XML Schema and store in repository
                JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd();
                XmlSchema       xmlschema       = jsonSchemaToXsd.CreateXsd(jsonSchemas);
                MemoryStream    xsdStream       = new MemoryStream();
                XmlTextWriter   xwriter         = new XmlTextWriter(xsdStream, new UpperCaseUtf8Encoding());
                xwriter.Formatting = Formatting.Indented;
                xwriter.WriteStartDocument(false);
                xmlschema.Write(xsdStream);
                await _repository.WriteData(org, repository, $"{filePath}.xsd", xsdStream);

                // Generate updated C# model
                JsonMetadataParser modelGenerator = new JsonMetadataParser();
                string             classes        = modelGenerator.CreateModelFromMetadata(modelMetadata);
                byteArray = Encoding.UTF8.GetBytes(classes);
                MemoryStream stream = new MemoryStream(byteArray);
                await _repository.WriteData(org, repository, $"{filePath}.cs", stream);
            }

            return(Ok());
        }
Example #12
0
 private async static Task UpdateCSharpClasses(AltinnAppGitRepository altinnAppGitRepository, ModelMetadata modelMetadata, string schemaName)
 {
     JsonMetadataParser modelGenerator = new JsonMetadataParser();
     string             classes        = modelGenerator.CreateModelFromMetadata(modelMetadata);
     await altinnAppGitRepository.UpdateCSharpClasses(classes, schemaName);
 }