| | | 1 | | using pva.Helpers.Extensions; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using pva.SuperV.Engine.FieldFormatters; |
| | | 4 | | using pva.SuperV.Engine.HistoryStorage; |
| | | 5 | | using pva.SuperV.Engine.Processing; |
| | | 6 | | |
| | | 7 | | namespace pva.SuperV.Engine |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// WIP (Work In Progress) project. It allows adding/changing/removing <see cref="Class"/>, <see cref="FieldDefiniti |
| | | 11 | | /// </summary> |
| | | 12 | | /// <seealso cref="pva.SuperV.Engine.Project" /> |
| | | 13 | | public class WipProject : Project |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// To be loaded instances when the project is converted to a <see cref="RunnableProject"/> through <see cref="P |
| | | 17 | | /// </summary> |
| | 948 | 18 | | public Dictionary<string, Instance> ToLoadInstances { get; } = new(StringComparer.OrdinalIgnoreCase); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="WipProject"/> class. Only used for deserialization. |
| | | 22 | | /// </summary> |
| | 1 | 23 | | public WipProject() |
| | 1 | 24 | | { |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="WipProject"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="projectName">Name of the project.</param> |
| | 239 | 31 | | public WipProject(string projectName) |
| | 239 | 32 | | { |
| | 239 | 33 | | Name = projectName; |
| | 236 | 34 | | Version = GetNextVersion(); |
| | 236 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="WipProject"/> class from a <see cref="RunnableProject"/>. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="runnableProject">The runnable project.</param> |
| | 36 | 41 | | public WipProject(RunnableProject runnableProject) |
| | 36 | 42 | | { |
| | 36 | 43 | | Name = runnableProject.Name; |
| | 36 | 44 | | Description = runnableProject.Description; |
| | 36 | 45 | | Version = GetNextVersion(); |
| | 36 | 46 | | Classes = new(runnableProject.Classes.Count); |
| | 36 | 47 | | runnableProject.Classes |
| | 144 | 48 | | .ForEach((k, v) => Classes.Add(k, v.Clone())); |
| | 36 | 49 | | FieldFormatters = new(runnableProject.FieldFormatters); |
| | 36 | 50 | | HistoryStorageEngineConnectionString = runnableProject.HistoryStorageEngineConnectionString; |
| | 36 | 51 | | HistoryStorageEngine = HistoryStorageEngineFactory.CreateHistoryStorageEngine(runnableProject.HistoryStorage |
| | 36 | 52 | | HistoryRepositories = new(runnableProject.HistoryRepositories); |
| | 36 | 53 | | HistoryRepositories.Values.ForEach(repository |
| | 72 | 54 | | => repository.HistoryStorageEngine = HistoryStorageEngine); |
| | 36 | 55 | | ToLoadInstances = new(runnableProject.Instances, StringComparer.OrdinalIgnoreCase); |
| | 36 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Adds a new class to the project. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="className">Name of the class.</param> |
| | | 62 | | /// <returns>The newly created <see cref="Class"/>.</returns> |
| | | 63 | | /// <exception cref="pva.SuperV.Engine.Exceptions.EntityAlreadyExistException"></exception> |
| | | 64 | | public Class AddClass(string className) |
| | 362 | 65 | | { |
| | 362 | 66 | | if (Classes.ContainsKey(className)) |
| | 1 | 67 | | { |
| | 1 | 68 | | throw new EntityAlreadyExistException("Class", className); |
| | | 69 | | } |
| | | 70 | | |
| | 361 | 71 | | Class clazz = new(className); |
| | 361 | 72 | | Classes.Add(className, clazz); |
| | 361 | 73 | | return clazz; |
| | 361 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Adds a new class with inheritance to the project. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="className">Name of the class.</param> |
| | | 80 | | /// <param name="baseClassName">Name of the base class.</param> |
| | | 81 | | /// <returns>The newly created <see cref="Class"/>.</returns> |
| | | 82 | | /// <exception cref="pva.SuperV.Engine.Exceptions.EntityAlreadyExistException"></exception> |
| | | 83 | | public Class AddClass(string className, string? baseClassName) |
| | 148 | 84 | | { |
| | 148 | 85 | | Class? baseClass = baseClassName is null ? null : GetClass(baseClassName); |
| | 148 | 86 | | if (Classes.ContainsKey(className)) |
| | 0 | 87 | | { |
| | 0 | 88 | | throw new EntityAlreadyExistException("Class", className); |
| | | 89 | | } |
| | | 90 | | |
| | 148 | 91 | | Class clazz = new(className, baseClass); |
| | 148 | 92 | | Classes.Add(className, clazz); |
| | 148 | 93 | | return clazz; |
| | 148 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Updates a class. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="className">Name of class to be updated</param> |
| | | 100 | | /// <param name="baseClassName">Base class name</param> |
| | | 101 | | /// <returns>Updated class.</returns> |
| | | 102 | | /// <exception cref="UnknownEntityException"></exception> |
| | | 103 | | public Class UpdateClass(string className, string? baseClassName) |
| | 1 | 104 | | { |
| | 1 | 105 | | if (Classes.TryGetValue(className, out Class? clazz)) |
| | 1 | 106 | | { |
| | 1 | 107 | | Class? baseClass = null; |
| | 1 | 108 | | if (!String.IsNullOrEmpty(baseClassName) && !Classes.TryGetValue(className, out baseClass)) |
| | 0 | 109 | | { |
| | 0 | 110 | | throw new UnknownEntityException("Class", baseClassName); |
| | | 111 | | } |
| | 1 | 112 | | clazz.BaseClassName = baseClassName; |
| | 1 | 113 | | clazz.BaseClass = baseClass; |
| | 1 | 114 | | return clazz; |
| | | 115 | | } |
| | 0 | 116 | | throw new UnknownEntityException("Class", className); |
| | 1 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Removes a class from project. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="className">Name of the class.</param> |
| | | 123 | | public void RemoveClass(string className) |
| | 3 | 124 | | { |
| | 3 | 125 | | if (Classes.Remove(className, out var clazz)) |
| | 2 | 126 | | { |
| | 2 | 127 | | ToLoadInstances.Values |
| | 1 | 128 | | .Where(instance => instance.Class.Name!.Equals(clazz.Name)) |
| | 2 | 129 | | .ToList() |
| | 3 | 130 | | .ForEach(instance => ToLoadInstances.Remove(instance.Name)); |
| | 2 | 131 | | } |
| | 3 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Adds a field to a class. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <typeparam name="T"></typeparam> |
| | | 138 | | /// <param name="className">Name of the class.</param> |
| | | 139 | | /// <param name="field">The field.</param> |
| | | 140 | | /// <returns></returns> |
| | | 141 | | public IFieldDefinition AddField(string className, IFieldDefinition field) |
| | 2612 | 142 | | { |
| | 2612 | 143 | | Class clazz = GetClass(className); |
| | 2612 | 144 | | return clazz.AddField(field); |
| | 2611 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Adds a field to a class with a specific field formatter. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <typeparam name="T"></typeparam> |
| | | 151 | | /// <param name="className">Name of the class.</param> |
| | | 152 | | /// <param name="field">The field.</param> |
| | | 153 | | /// <param name="formatterName">Name of the formatter.</param> |
| | | 154 | | /// <returns></returns> |
| | | 155 | | public IFieldDefinition AddField(string className, IFieldDefinition field, string? formatterName) |
| | 292 | 156 | | { |
| | 292 | 157 | | Class clazz = GetClass(className); |
| | 292 | 158 | | FieldFormatter? formatter = GetFormatter(formatterName); |
| | 291 | 159 | | formatter?.ValidateAllowedType(field.Type); |
| | 290 | 160 | | return clazz.AddField(field, formatter); |
| | 290 | 161 | | } |
| | | 162 | | |
| | | 163 | | public IFieldDefinition UpdateField(string className, string fieldName, IFieldDefinition field, string? formatte |
| | 4 | 164 | | { |
| | 4 | 165 | | Class clazz = GetClass(className); |
| | 4 | 166 | | FieldFormatter? formatter = GetFormatter(formatterName); |
| | 4 | 167 | | formatter?.ValidateAllowedType(field.Type); |
| | 4 | 168 | | return clazz.UpdateField(fieldName, field, formatter); |
| | 2 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Removes a field from a class. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="className">Name of the class.</param> |
| | | 175 | | /// <param name="fieldName">Name of the field.</param> |
| | | 176 | | public void RemoveField(string className, string fieldName) |
| | 2 | 177 | | { |
| | 2 | 178 | | Class clazz = GetClass(className); |
| | 2 | 179 | | clazz.RemoveField(fieldName); |
| | 1 | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Adds a field formatter to project to be later used for a specific field formatting (<see cref="AddField{T}(s |
| | | 184 | | /// </summary> |
| | | 185 | | /// <param name="fieldFormatter">The field formatter.</param> |
| | | 186 | | /// <exception cref="pva.SuperV.Engine.Exceptions.EntityAlreadyExistException"></exception> |
| | | 187 | | public void AddFieldFormatter(FieldFormatter fieldFormatter) |
| | 199 | 188 | | { |
| | 199 | 189 | | if (FieldFormatters.ContainsKey(fieldFormatter.Name!)) |
| | 1 | 190 | | { |
| | 1 | 191 | | throw new EntityAlreadyExistException("Field formatter", fieldFormatter.Name); |
| | | 192 | | } |
| | | 193 | | |
| | 198 | 194 | | FieldFormatters.Add(fieldFormatter.Name!, fieldFormatter); |
| | 198 | 195 | | } |
| | | 196 | | |
| | | 197 | | public void UpdateFieldFormatter(string fieldFormatterName, FieldFormatter fieldFormatter) |
| | 2 | 198 | | { |
| | 2 | 199 | | FieldFormatter? existingFormatter = GetFormatter(fieldFormatterName); |
| | 2 | 200 | | if (existingFormatter != null) |
| | 2 | 201 | | { |
| | 2 | 202 | | if (existingFormatter.GetType() == fieldFormatter.GetType()) |
| | 2 | 203 | | { |
| | 2 | 204 | | FieldFormatters[fieldFormatterName] = fieldFormatter; |
| | 2 | 205 | | return; |
| | | 206 | | } |
| | 0 | 207 | | throw new WrongFieldTypeException(fieldFormatterName, existingFormatter.GetType(), fieldFormatter.GetTyp |
| | | 208 | | } |
| | 0 | 209 | | throw new UnknownEntityException("Field formatter", fieldFormatterName); |
| | 2 | 210 | | } |
| | | 211 | | |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Removes a field formatter. |
| | | 215 | | /// </summary> |
| | | 216 | | /// <param name="fieldFormatterName">Name of the field formatter.</param> |
| | | 217 | | public bool RemoveFieldFormatter(string fieldFormatterName) |
| | 5 | 218 | | { |
| | 5 | 219 | | VerifyFieldFormatterNotUsed(fieldFormatterName); |
| | 3 | 220 | | return FieldFormatters.Remove(fieldFormatterName); |
| | 3 | 221 | | } |
| | | 222 | | |
| | | 223 | | private void VerifyFieldFormatterNotUsed(string fieldFormatterName) |
| | 5 | 224 | | { |
| | 5 | 225 | | Classes.Values.ForEach(clazz => |
| | 9 | 226 | | { |
| | 9 | 227 | | List<String> fieldsUsingFormatter = [.. clazz.FieldDefinitions.Values |
| | 48 | 228 | | .Where(field => field.Formatter is not null && field.Formatter.Name.Equals(fieldFormatterName)) |
| | 11 | 229 | | .Select(field => field.Name)]; |
| | 9 | 230 | | if (fieldsUsingFormatter.Count > 0) |
| | 2 | 231 | | { |
| | 2 | 232 | | throw new EntityInUseException("Field formatter", fieldFormatterName, clazz.Name, fieldsUsingFormatt |
| | 5 | 233 | | } |
| | 12 | 234 | | }); |
| | 3 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Adds a field change post processing on a field.. |
| | | 239 | | /// </summary> |
| | | 240 | | /// <param name="className">Name of the class.</param> |
| | | 241 | | /// <param name="fieldName">Name of the field.</param> |
| | | 242 | | /// <param name="fieldValueProcessing">The field value processing.</param> |
| | | 243 | | public void AddFieldChangePostProcessing(string className, string fieldName, IFieldValueProcessing fieldValuePro |
| | 440 | 244 | | { |
| | 440 | 245 | | Class clazz = GetClass(className); |
| | 440 | 246 | | clazz.AddFieldChangePostProcessing(fieldName, fieldValueProcessing); |
| | 440 | 247 | | } |
| | | 248 | | |
| | | 249 | | public void UpdateFieldChangePostProcessing(string className, string fieldName, string processingName, IFieldVal |
| | 1 | 250 | | { |
| | 1 | 251 | | Class clazz = GetClass(className); |
| | 1 | 252 | | clazz.UpdateFieldChangePostProcessing(fieldName, processingName, fieldProcessing); |
| | 1 | 253 | | } |
| | | 254 | | |
| | | 255 | | public void RemoveFieldChangePostProcessing(string className, string fieldName, string processingName) |
| | 1 | 256 | | { |
| | 1 | 257 | | Class clazz = GetClass(className); |
| | 1 | 258 | | clazz.RemoveFieldChangePostProcessing(fieldName, processingName); |
| | 1 | 259 | | } |
| | | 260 | | |
| | | 261 | | public override string GetId() |
| | 987 | 262 | | { |
| | 987 | 263 | | return $"{Name!}-WIP"; |
| | 987 | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Adds a history repository to project./>. |
| | | 268 | | /// </summary> |
| | | 269 | | /// <param name="historyRepository">The history repository.</param> |
| | | 270 | | /// <exception cref="pva.SuperV.Engine.Exceptions.EntityAlreadyExistException"></exception> |
| | | 271 | | public void AddHistoryRepository(HistoryRepository historyRepository) |
| | 155 | 272 | | { |
| | 155 | 273 | | if (HistoryRepositories.ContainsKey(historyRepository.Name)) |
| | 1 | 274 | | { |
| | 1 | 275 | | throw new EntityAlreadyExistException("History repository", historyRepository.Name); |
| | | 276 | | } |
| | | 277 | | |
| | 154 | 278 | | historyRepository.HistoryStorageEngine = HistoryStorageEngine; |
| | 154 | 279 | | HistoryRepositories.Add(historyRepository.Name, historyRepository); |
| | 154 | 280 | | } |
| | | 281 | | |
| | | 282 | | public void UpdateHistoryRepository(string historyRepositoryName, HistoryRepository historyRepository) |
| | 2 | 283 | | { |
| | 2 | 284 | | if (HistoryRepositories.TryGetValue(historyRepositoryName, out HistoryRepository? _)) |
| | 1 | 285 | | { |
| | 1 | 286 | | HistoryRepositories[historyRepositoryName] = historyRepository; |
| | 1 | 287 | | return; |
| | | 288 | | } |
| | 1 | 289 | | throw new UnknownEntityException("History repository", historyRepositoryName); |
| | 1 | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Removes a history repository. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <param name="historyRepositoryName">Name of the history repository.</param> |
| | | 296 | | public void RemoveHistoryRepository(string historyRepositoryName) |
| | 3 | 297 | | { |
| | 3 | 298 | | VerifyHistoryRepositoryNotUsed(historyRepositoryName); |
| | 2 | 299 | | HistoryRepositories.Remove(historyRepositoryName); |
| | 2 | 300 | | } |
| | | 301 | | |
| | | 302 | | private void VerifyHistoryRepositoryNotUsed(string historyRepositoryName) |
| | 3 | 303 | | { |
| | 3 | 304 | | Classes.Values.ForEach(clazz => |
| | 5 | 305 | | { |
| | 5 | 306 | | List<String> fieldsUsingHistoryRepository = [.. clazz.FieldDefinitions.Values |
| | 27 | 307 | | .Where(field => field.ValuePostChangeProcessings |
| | 27 | 308 | | .OfType<IHistorizationProcessing>() |
| | 28 | 309 | | .Any(historyValueProcessing => historyValueProcessing.IsUsingRepository(historyRepositoryName))) |
| | 6 | 310 | | .Select(field => field.Name)]; |
| | 5 | 311 | | if (fieldsUsingHistoryRepository.Count > 0) |
| | 1 | 312 | | { |
| | 1 | 313 | | throw new EntityInUseException("History Repository", historyRepositoryName, clazz.Name, fieldsUsingH |
| | 3 | 314 | | } |
| | 7 | 315 | | }); |
| | 2 | 316 | | } |
| | | 317 | | |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Clones as <see cref="RunnableProject"/>. |
| | | 321 | | /// </summary> |
| | | 322 | | /// <returns><see cref="RunnableProject"/></returns> |
| | | 323 | | public RunnableProject CloneAsRunnable() |
| | 126 | 324 | | => new(this); |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Unloads the project. |
| | | 328 | | /// </summary> |
| | | 329 | | public override void Unload() |
| | 272 | 330 | | { |
| | 274 | 331 | | ToLoadInstances.Values.ForEach(instance => instance.Dispose()); |
| | 272 | 332 | | ToLoadInstances.Clear(); |
| | 272 | 333 | | base.Unload(); |
| | 272 | 334 | | } |
| | | 335 | | } |
| | | 336 | | } |