EscapeUniqueValue() public static method

public static EscapeUniqueValue ( object value, bool caseInsensitive = false ) : string
value object
caseInsensitive bool
return string
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            var oldDoc = Database.Get(key, transactionInformation);

            if (oldDoc == null)
            {
                return;
            }

            foreach (var property in metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints))
            {
                var constraint = Util.GetConstraint(property);

                var newProp = document[constraint.PropName];

                // Handle Updates in the constraint since it changed
                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";

                var oldProp = oldDoc.DataAsJson[constraint.PropName];

                string[] oldUniqueValues;
                if (!Util.TryGetUniqueValues(oldProp, out oldUniqueValues))
                {
                    continue;
                }

                string[] newUniqueValues;
                if (Util.TryGetUniqueValues(newProp, out newUniqueValues))
                {
                    var join = (from oldValue in oldUniqueValues
                                join newValue in newUniqueValues
                                on oldValue equals newValue
                                select oldValue);

                    if (join.Count() == oldUniqueValues.Count())
                    {
                        continue;
                    }
                }

                foreach (var oldUniqueValue in oldUniqueValues)
                {
                    Database.Delete(prefix + Util.EscapeUniqueValue(oldUniqueValue, constraint.CaseInsensitive), null, transactionInformation);
                }
            }
        }
Example #2
0
		public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, System.Guid etag, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/"))
			{
				return;
			}

			var entityName = metadata.Value<string>(Constants.RavenEntityName) + "/";

			var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

			if (properties == null || properties.Length <= 0) 
				return;

			var constraintMetaObject = new RavenJObject { { Constants.IsConstraintDocument, true } };
			constraintMetaObject.EnsureSnapshot();
			foreach (var property in properties)
			{
				var propName = ((RavenJValue)property).Value.ToString();
				var uniqueValue = document.Value<string>(propName);
				if(uniqueValue == null)
					continue;
				string documentName = "UniqueConstraints/" + entityName + propName + "/" +Util.EscapeUniqueValue(uniqueValue);
				Database.Put(
					documentName,
					null,
					RavenJObject.FromObject(new { RelatedId = key }),
					(RavenJObject)constraintMetaObject.CreateSnapshot(),
					transactionInformation);
			}
		}
Example #3
0
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            foreach (var property in properties)
            {
                var constraint = Util.GetConstraint(property);
                var prop       = document[constraint.PropName];

                string[] uniqueValues;
                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                {
                    continue;
                }

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";

                foreach (var uniqueValue in uniqueValues)
                {
                    var escapedUniqueValue           = Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive);
                    var uniqueConstraintsDocumentKey = prefix + escapedUniqueValue;
                    var uniqueConstraintsDocument    = Database.Documents.Get(uniqueConstraintsDocumentKey, transactionInformation);

                    if (uniqueConstraintsDocument != null)
                    {
                        uniqueConstraintsDocument = DeepCloneDocument(uniqueConstraintsDocument);
                        ConvertUniqueConstraintsDocumentIfNecessary(uniqueConstraintsDocument, escapedUniqueValue); // backward compatibility
                    }
                    else
                    {
                        uniqueConstraintsDocument = new JsonDocument();
                    }

                    AddConstraintToUniqueConstraintsDocument(uniqueConstraintsDocument, escapedUniqueValue, key);
                    uniqueConstraintsDocument.Metadata[Constants.IsConstraintDocument] = true;

                    Database.Documents.Put(
                        uniqueConstraintsDocumentKey,
                        null,
                        uniqueConstraintsDocument.DataAsJson,
                        uniqueConstraintsDocument.Metadata,
                        transactionInformation);
                }
            }
        }
Example #4
0
        public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return(VetoResult.Allowed);
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName);

            if (string.IsNullOrEmpty(entityName))
            {
                return(VetoResult.Allowed);
            }
            entityName += "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return(VetoResult.Allowed);
            }

            var invalidFields = new StringBuilder();

            foreach (var property in properties)
            {
                var propName    = ((RavenJValue)property).Value.ToString();
                var uniqueValue = document.Value <string>(propName);
                if (uniqueValue == null)
                {
                    continue;
                }
                var checkKey = "UniqueConstraints/" + entityName + propName + "/" +
                               Util.EscapeUniqueValue(uniqueValue);
                var checkDoc = Database.Get(checkKey, transactionInformation);
                if (checkDoc == null)
                {
                    continue;
                }

                var checkId = checkDoc.DataAsJson.Value <string>("RelatedId");

                if (checkId != key)
                {
                    invalidFields.Append(property + ", ");
                }
            }

            if (invalidFields.Length > 0)
            {
                invalidFields.Length = invalidFields.Length - 2;
                return(VetoResult.Deny("Ensure unique constraint violated for fields: " + invalidFields));
            }

            return(VetoResult.Allowed);
        }
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            var oldDoc = Database.Get(key, transactionInformation);

            if (oldDoc == null)
            {
                return;
            }

            var oldJson = oldDoc.DataAsJson;

            foreach (var property in metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints))
            {
                var constraint = Util.GetConstraint(property);

                var oldValue = oldJson.Value <string>(constraint.PropName);
                if (oldValue == null || oldValue.Equals(document.Value <string>(constraint.PropName)))
                {
                    continue;
                }

                // Handle Updates in the constraint since it changed
                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";
                var prop   = oldDoc.DataAsJson[constraint.PropName];
                if (prop == null || prop.Type == JTokenType.Null)
                {
                    continue;
                }
                var array = prop as RavenJArray;

                var deleteKeys = array != null?array.Select(p => p.Value <string>()) : new[] { prop.Value <string>() };

                foreach (var deleteKey in deleteKeys)
                {
                    Database.Delete(prefix + Util.EscapeUniqueValue(deleteKey, constraint.CaseInsensitive), null, transactionInformation);
                }
            }
        }
        public override void OnDelete(string key, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven"))
            {
                return;
            }

            var doc = Database.Get(key, transactionInformation);

            if (doc == null)
            {
                return;
            }

            var metadata = doc.Metadata;

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var uniqueConstraits = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (uniqueConstraits == null)
            {
                return;
            }

            foreach (var property in uniqueConstraits)
            {
                var propName = property.Value <string>();                        // the name of the constraint property

                var prefix = "UniqueConstraints/" + entityName + property + "/"; // UniqueConstraints/EntityNamePropertyName/
                var prop   = doc.DataAsJson[propName];
                if (prop == null || prop.Type == JTokenType.Null)
                {
                    continue;
                }
                var array     = prop as RavenJArray;
                var checkKeys = array != null?array.Select(p => p.Value <string>()) : new[] { prop.Value <string>() };

                foreach (var checkKey in checkKeys)
                {
                    Database.Delete(prefix + Util.EscapeUniqueValue(checkKey), null, transactionInformation);
                }
            }
        }
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            foreach (var property in properties)
            {
                var constraint = Util.GetConstraint(property);
                var prop       = document[constraint.PropName];
                if (prop == null || prop.Type == JTokenType.Null)
                {
                    continue;
                }

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";

                var array       = prop as RavenJArray;
                var relatedKeys = array != null?array.Select(p => p.Value <string>()) : new[] { prop.Value <string>() };

                foreach (var relatedKey in relatedKeys)
                {
                    Database.Put(
                        prefix + Util.EscapeUniqueValue(relatedKey, constraint.CaseInsensitive),
                        null,
                        RavenJObject.FromObject(new { RelatedId = key }),
                        new RavenJObject {
                        { Constants.IsConstraintDocument, true }
                    },
                        transactionInformation);
                }
            }
        }
Example #8
0
        public override void OnDelete(string key, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven"))
            {
                return;
            }

            var doc = Database.Get(key, transactionInformation);

            if (doc == null)
            {
                return;
            }

            var metadata = doc.Metadata;

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var uniqueConstraits = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (uniqueConstraits == null)
            {
                return;
            }

            foreach (var property in uniqueConstraits)
            {
                var constraint = Util.GetConstraint(property);
                var prefix     = "UniqueConstraints/" + entityName + constraint.PropName + "/"; // UniqueConstraints/EntityNamePropertyName/
                var prop       = doc.DataAsJson[constraint.PropName];

                string[] uniqueValues;
                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                {
                    continue;
                }

                foreach (var uniqueValue in uniqueValues)
                {
                    Database.Delete(prefix + Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive), null, transactionInformation);
                }
            }
        }
Example #9
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            var oldDoc = Database.Get(key, transactionInformation);

            if (oldDoc == null)
            {
                return;
            }

            var oldJson = oldDoc.DataAsJson;

            foreach (var property in metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints))
            {
                var propName = ((RavenJValue)property).Value.ToString();

                // Handle Updates in the Constraint
                var uniqueValue = oldJson.Value <string>(propName);
                if (uniqueValue == null)
                {
                    continue;
                }
                if (!uniqueValue.Equals(document.Value <string>(propName)))
                {
                    Database.Delete(
                        "UniqueConstraints/" + entityName + propName + "/" + Util.EscapeUniqueValue(uniqueValue),
                        null, transactionInformation);
                }
            }
        }
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            foreach (var property in properties)
            {
                var constraint = Util.GetConstraint(property);
                var prop       = document[constraint.PropName];

                string[] uniqueValues;
                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                {
                    continue;
                }

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";

                foreach (var uniqueValue in uniqueValues)
                {
                    Database.Put(
                        prefix + Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive),
                        null,
                        RavenJObject.FromObject(new { RelatedId = key }),
                        new RavenJObject {
                        { Constants.IsConstraintDocument, true }
                    },
                        transactionInformation);
                }
            }
        }
        public override void OnDelete(string key, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven"))
            {
                return;
            }

            var doc = Database.Get(key, transactionInformation);

            if (doc == null)
            {
                return;
            }

            var metadata = doc.Metadata;

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var uniqueConstraits = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (uniqueConstraits == null)
            {
                return;
            }

            foreach (var property in uniqueConstraits)
            {
                var value = property.Value <string>();
                if (value == null)
                {
                    continue;
                }
                var checkKey = "UniqueConstraints/" + entityName + property + "/" +
                               Util.EscapeUniqueValue(doc.DataAsJson.Value <string>(value));

                Database.Delete(checkKey, null, transactionInformation);
            }
        }
Example #12
0
        public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return(VetoResult.Allowed);
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName);

            if (string.IsNullOrEmpty(entityName))
            {
                return(VetoResult.Allowed);
            }
            entityName += "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return(VetoResult.Allowed);
            }

            var invalidFields = new StringBuilder();

            foreach (var property in properties)
            {
                var constraint = Util.GetConstraint(property);

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";
                var prop   = document[constraint.PropName];

                string[] uniqueValues;
                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                {
                    continue;
                }

                foreach (var uniqueValue in uniqueValues)
                {
                    var escapedUniqueValue = Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive);
                    var checkDocKey        = prefix + escapedUniqueValue;
                    var checkDoc           = Database.Documents.Get(checkDocKey, transactionInformation);

                    if (checkDoc == null)
                    {
                        continue;
                    }

                    var checkId = GetRelatedIdFromUniqueConstraintsDocument(checkDoc, escapedUniqueValue);

                    if (!string.IsNullOrEmpty(checkId) && checkId != key)
                    {
                        invalidFields.Append(constraint.PropName + ", ");
                    }
                }
            }

            if (invalidFields.Length > 0)
            {
                invalidFields.Length = invalidFields.Length - 2;
                return(VetoResult.Deny("Ensure unique constraint violated for fields: " + invalidFields));
            }

            return(VetoResult.Allowed);
        }
        public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return(VetoResult.Allowed);
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName);

            if (string.IsNullOrEmpty(entityName))
            {
                return(VetoResult.Allowed);
            }
            entityName += "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return(VetoResult.Allowed);
            }

            var invalidFields = new StringBuilder();

            foreach (var property in properties)
            {
                var constraint = Util.GetConstraint(property);

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";
                var prop   = document[constraint.PropName];

                if (prop == null || prop.Type == JTokenType.Null)
                {
                    continue;
                }

                var array     = prop as RavenJArray;
                var checkKeys = array != null?array.Select(p => p.Value <string>()) : new[] { prop.Value <string>() };

                foreach (var checkKey in checkKeys)
                {
                    var checkDoc = Database.Get(prefix + Util.EscapeUniqueValue(checkKey, constraint.CaseInsensitive), transactionInformation);

                    if (checkDoc == null)
                    {
                        continue;
                    }

                    var checkId = checkDoc.DataAsJson.Value <string>("RelatedId");

                    if (checkId != key)
                    {
                        invalidFields.Append(constraint.PropName + ", ");
                    }
                }
            }

            if (invalidFields.Length > 0)
            {
                invalidFields.Length = invalidFields.Length - 2;
                return(VetoResult.Deny("Ensure unique constraint violated for fields: " + invalidFields));
            }

            return(VetoResult.Allowed);
        }
        public override void OnPut(string key, RavenJObject jsonReplicationDocument, RavenJObject metadata)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
            {
                return;
            }

            var oldDoc = Database.Documents.Get(key);

            if (oldDoc == null)
            {
                return;
            }

            foreach (var property in metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints))
            {
                var constraint = Util.GetConstraint(property);

                var newProp = jsonReplicationDocument[constraint.PropName];

                // Handle Updates in the constraint since it changed
                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";

                var oldProp = oldDoc.DataAsJson[constraint.PropName];

                string[] oldUniqueValues;
                if (!Util.TryGetUniqueValues(oldProp, out oldUniqueValues))
                {
                    continue;
                }

                string[] newUniqueValues;
                if (Util.TryGetUniqueValues(newProp, out newUniqueValues))
                {
                    var join = (from oldValue in oldUniqueValues
                                join newValue in newUniqueValues
                                on oldValue equals newValue
                                select oldValue);

                    if (join.Count() == oldUniqueValues.Count())
                    {
                        continue;
                    }
                }

                foreach (var oldUniqueValue in oldUniqueValues)
                {
                    var escapedUniqueValue           = Util.EscapeUniqueValue(oldUniqueValue, constraint.CaseInsensitive);
                    var uniqueConstraintsDocumentKey = prefix + escapedUniqueValue;
                    var uniqueConstraintsDocument    = Database.Documents.Get(uniqueConstraintsDocumentKey);

                    if (uniqueConstraintsDocument == null)
                    {
                        continue;
                    }

                    uniqueConstraintsDocument = DeepCloneDocument(uniqueConstraintsDocument);

                    var removed = RemoveConstraintFromUniqueConstraintDocument(uniqueConstraintsDocument, escapedUniqueValue);

                    if (ShouldRemoveUniqueConstraintDocument(uniqueConstraintsDocument))
                    {
                        Database.Documents.Delete(uniqueConstraintsDocumentKey, null);
                    }
                    else if (removed)
                    {
                        Database.Documents.Put(
                            uniqueConstraintsDocumentKey, null,
                            uniqueConstraintsDocument.DataAsJson,
                            uniqueConstraintsDocument.Metadata);
                    }
                }
            }
        }
        public override void OnDelete(string key)
        {
            if (key.StartsWith("Raven"))
            {
                return;
            }

            var doc = Database.Documents.Get(key);

            if (doc == null)
            {
                return;
            }

            var metadata = doc.Metadata;

            var entityName = metadata.Value <string>(Constants.RavenEntityName) + "/";

            var uniqueConstraits = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (uniqueConstraits == null)
            {
                return;
            }

            foreach (var property in uniqueConstraits)
            {
                var constraint = Util.GetConstraint(property);
                var prefix     = "UniqueConstraints/" + entityName + constraint.PropName + "/"; // UniqueConstraints/EntityNamePropertyName/
                var prop       = doc.DataAsJson[constraint.PropName];

                string[] uniqueValues;
                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                {
                    continue;
                }

                foreach (var uniqueValue in uniqueValues)
                {
                    var escapedUniqueValue           = Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive);
                    var uniqueConstraintsDocumentKey = prefix + escapedUniqueValue;
                    var uniqueConstraintsDocument    = Database.Documents.Get(uniqueConstraintsDocumentKey);

                    if (uniqueConstraintsDocument == null)
                    {
                        continue;
                    }

                    var removed = RemoveConstraintFromUniqueConstraintDocument(uniqueConstraintsDocument, escapedUniqueValue);

                    if (ShouldRemoveUniqueConstraintDocument(uniqueConstraintsDocument))
                    {
                        Database.Documents.Delete(uniqueConstraintsDocumentKey, null);
                    }
                    else if (removed)
                    {
                        Database.Documents.Put(
                            uniqueConstraintsDocumentKey,
                            null,
                            uniqueConstraintsDocument.DataAsJson,
                            uniqueConstraintsDocument.Metadata);
                    }
                }
            }
        }