PageRenderTime 57ms CodeModel.GetById 51ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/Bundles/Raven.Bundles.UniqueConstraints/UniqueConstraintsDeleteTrigger.cs

https://github.com/kairogyn/ravendb
C# | 51 lines | 40 code | 11 blank | 0 comment | 6 complexity | d3371c003d5466e975f2d943a109cc9b MD5 | raw file
 1using System.Collections.Generic;
 2using System.Linq;
 3using Raven.Imports.Newtonsoft.Json.Linq;
 4
 5namespace Raven.Bundles.UniqueConstraints
 6{
 7	using Abstractions.Data;
 8	using Database.Plugins;
 9	using Json.Linq;
10	using Raven.Database.Extensions;
11	using System;
12
13	public class UniqueConstraintsDeleteTrigger : AbstractDeleteTrigger
14	{
15		public override void OnDelete(string key, TransactionInformation transactionInformation)
16		{
17			if (key.StartsWith("Raven"))
18				return;
19
20			var doc = Database.Get(key, transactionInformation);
21
22			if (doc == null)
23				return;
24
25			var metadata = doc.Metadata;
26
27			var entityName = metadata.Value<string>(Constants.RavenEntityName) + "/";
28
29			var uniqueConstraits = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);
30
31			if (uniqueConstraits == null)
32				return;
33
34			foreach (var property in uniqueConstraits)
35			{
36			    var constraint = Util.GetConstraint(property);
37                var prefix = "UniqueConstraints/" + entityName + constraint.PropName+ "/"; // UniqueConstraints/EntityNamePropertyName/
38                var prop = doc.DataAsJson[constraint.PropName];
39
40			    string[] uniqueValues;
41                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
42                    continue;
43
44				foreach (var uniqueValue in uniqueValues)
45				{
46					Database.Delete(prefix + Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive), null, transactionInformation);
47				}
48			}
49		}
50	}
51}