Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity.Design / System / Data / Entity / Design / MetadataItemCollectionFactory.cs
1 //---------------------------------------------------------------------
2 // <copyright file="MetadataItemCollectionFactory.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner        Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 namespace System.Data.Entity.Design
11 {
12     using System.Data.Entity;
13     using System.Data.EntityModel;
14     using System.Xml;
15     using System.Collections.Generic;
16     using System.Data.Common;
17     using System.Data.Metadata.Edm;
18     using System.Data.Mapping;
19     using System.Data.Entity.Design.Common;
20     using Microsoft.Build.Utilities;
21     using System.Data.Entity.Design.SsdlGenerator;
22     using System.Diagnostics;
23     using System.Linq;
24
25     /// <summary>
26     /// Factory for creating ItemCollections. This class is to be used for 
27     /// design time scenarios. The consumers of the methods in this class
28     /// will get an error list instead of an exception if there are errors in schema files. 
29     /// </summary>
30     [CLSCompliant(false)]
31     public static class MetadataItemCollectionFactory
32     {
33         /// <summary>
34         /// Create an EdmItemCollection with the passed in parameters.
35         /// Add any errors caused during the ItemCollection creation
36         /// to the error list passed in.
37         /// </summary>
38         /// <param name="readers"></param>
39         /// <param name="errors"></param>
40         /// <returns></returns>
41         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edm")]
42         public static EdmItemCollection CreateEdmItemCollection(IEnumerable<XmlReader> readers,
43             out IList<EdmSchemaError> errors)
44         {
45             System.Collections.ObjectModel.ReadOnlyCollection<string> filePaths = null;
46             return new EdmItemCollection(readers, filePaths, out errors);
47         }
48
49         /// <summary>
50         /// Create an EdmItemCollection with the passed in parameters.
51         /// Add any errors caused during the ItemCollection creation
52         /// to the error list passed in.
53         /// </summary>
54         /// <param name="readers"></param>
55         /// <param name="targetEntityFrameworkVersion"></param>
56         /// <param name="errors"></param>
57         /// <returns></returns>
58         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Edm")]
59         public static EdmItemCollection CreateEdmItemCollection(IEnumerable<XmlReader> readers,
60             Version targetEntityFrameworkVersion, 
61             out IList<EdmSchemaError> errors)
62         {
63             EDesignUtil.CheckTargetEntityFrameworkVersionArgument(targetEntityFrameworkVersion, "targetEntityFrameworkVersion");
64
65             EdmItemCollection edmItemCollection = CreateEdmItemCollection(readers, out errors);
66             if (!errors.Any(e => e.Severity == EdmSchemaErrorSeverity.Error))
67             {
68                 ValidateActualVersionAgainstTarget(targetEntityFrameworkVersion, EntityFrameworkVersionsUtil.ConvertToVersion(edmItemCollection.EdmVersion), errors);
69             }
70
71             return edmItemCollection;
72         }
73
74         internal static bool ValidateActualVersionAgainstTarget(Version maxExpectedVersion, Version actualVersion, IList<EdmSchemaError> errors)
75         {
76             if (!(actualVersion <= maxExpectedVersion))
77             {
78                 errors.Add(new EdmSchemaError(Strings.TargetVersionSchemaVersionMismatch(maxExpectedVersion, actualVersion), (int)ModelBuilderErrorCode.SchemaVersionHigherThanTargetVersion, EdmSchemaErrorSeverity.Error));
79                 return false;
80             }
81             return true;
82         }
83
84         /// <summary>
85         /// Create an StoreItemCollection with the passed in parameters.
86         /// Add any errors caused during the ItemCollection creation
87         /// to the error list passed in.
88         /// </summary>
89         /// <param name="connection"></param>
90         /// <param name="readers"></param>
91         /// <param name="errors"></param>
92         /// <returns></returns>
93         public static StoreItemCollection CreateStoreItemCollection(IEnumerable<XmlReader> readers,
94             out IList<EdmSchemaError> errors)
95         {
96             return new StoreItemCollection(readers, null, out errors);
97         }
98
99         /// <summary>
100         /// Create an StoreItemCollection with the passed in parameters.
101         /// Add any errors caused during the ItemCollection creation
102         /// to the error list passed in.
103         /// </summary>
104         /// <param name="connection"></param>
105         /// <param name="readers"></param>
106         /// <param name="targetEntityFrameworkVersion"></param>
107         /// <param name="errors"></param>
108         /// <returns></returns>
109         public static StoreItemCollection CreateStoreItemCollection(
110             IEnumerable<XmlReader> readers,
111             Version targetEntityFrameworkVersion,
112             out IList<EdmSchemaError> errors)
113         {
114             EDesignUtil.CheckTargetEntityFrameworkVersionArgument(targetEntityFrameworkVersion, "targetEntityFrameworkVersion");
115             return CreateStoreItemCollection(readers, out errors);
116         }
117
118
119         /// <summary>
120         /// Create a StorageMappingItemCollection with the passed in parameters.
121         /// Add any errors caused during the ItemCollection creation
122         /// to the error list passed in.
123         /// </summary>
124         /// <param name="edmCollection"></param>
125         /// <param name="storeCollection"></param>
126         /// <param name="readers"></param>
127         /// <param name="errors"></param>
128         /// <returns></returns>
129         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "edm")]
130         public static StorageMappingItemCollection CreateStorageMappingItemCollection(EdmItemCollection edmCollection,
131             StoreItemCollection storeCollection, IEnumerable<XmlReader> readers, out IList<EdmSchemaError> errors)
132         {
133             return new StorageMappingItemCollection(edmCollection, storeCollection, readers, null, out errors);
134         }
135
136         /// <summary>
137         /// Create a StorageMappingItemCollection with the passed in parameters.
138         /// Add any errors caused during the ItemCollection creation
139         /// to the error list passed in.
140         /// </summary>
141         /// <param name="edmCollection"></param>
142         /// <param name="storeCollection"></param>
143         /// <param name="readers"></param>
144         /// <param name="targetEntityFrameworkVersion"></param>
145         /// <param name="errors"></param>
146         /// <returns></returns>
147         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "edm")]
148         public static StorageMappingItemCollection CreateStorageMappingItemCollection(
149             EdmItemCollection edmCollection,
150             StoreItemCollection storeCollection, 
151             IEnumerable<XmlReader> readers, 
152             Version targetEntityFrameworkVersion, 
153             out IList<EdmSchemaError> errors)
154         {
155             EDesignUtil.CheckArgumentNull(edmCollection, "edmCollection");
156             EDesignUtil.CheckArgumentNull(storeCollection, "storeCollection");
157             EDesignUtil.CheckArgumentNull(readers, "readers");
158             EDesignUtil.CheckTargetEntityFrameworkVersionArgument(targetEntityFrameworkVersion, "targetEntityFrameworkVersion");
159             if (EntityFrameworkVersionsUtil.ConvertToVersion(edmCollection.EdmVersion) > targetEntityFrameworkVersion)
160             {
161                 throw EDesignUtil.Argument("edmCollection");
162             }
163
164             StorageMappingItemCollection storageMappingItemCollection = CreateStorageMappingItemCollection(edmCollection, storeCollection, readers, out errors);
165             if (!errors.Any(e => e.Severity == EdmSchemaErrorSeverity.Error))
166             {
167                 ValidateActualVersionAgainstTarget(targetEntityFrameworkVersion, EntityFrameworkVersionsUtil.ConvertToVersion(storageMappingItemCollection.MappingVersion), errors);
168             }
169             return storageMappingItemCollection;
170         }
171     }
172 }