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