Exemple #1
0
        public EntityKey DeserialiseKey(XmlNode keyNode, Entity parentEntity)
        {
            var key = new EntityKeyImpl();

            key.Parent = parentEntity;

            if (keyNode.InnerXml == "")
            {
                return(key);
            }

            NodeProcessor keyProc         = new NodeProcessor(keyNode);
            bool          propertiesExist = false;
            bool          componentExists = false;

            var nodes = keyNode.SelectNodes("Properties/Property");

            if (nodes != null)
            {
                propertiesExist = true;
                foreach (XmlNode node in nodes)
                {
                    key.AddProperty(node.InnerText);
                }
            }

            if (keyProc.Exists("Component"))
            {
                componentExists = true;
                string    componentName = keyProc.SubNode("Component").Attributes.GetString("name");
                Component component     = parentEntity.Components.FirstOrDefault(c => c.Name == componentName);

                if (component == null)
                {
                    throw new DeserialisationException(string.Format("Could not find component named {0} on Entity {1}", componentName, parentEntity.Name));
                }

                key.Component = component;
            }

            if (keyProc.Attributes.Exists("keytype"))
            {
                key.KeyType = keyProc.Attributes.GetEnum <EntityKeyType>("keytype");
            }
            else if (propertiesExist && componentExists)
            {
                throw new DeserialisationException(string.Format("Both a Component and a set of Properties were listed as the Key for entity {0}, but no keytype attribute was found on the Key node in the Entity Model XML.", parentEntity.Name));
            }

            ProcessScriptBase(key, keyNode);

            return(key);
        }
        public void ApplyConstraints(MappingSet set, IEnumerable<string> nhvFiles, ParseResults parseResults)
        {
            foreach (var nhv in nhvFiles)
            {
                string text = _fileController.ReadAllText(nhv);
                if (nhibernateFileVerifier.IsValidValidationMappingFile(new StringReader(text)) == false)
                    continue;

                // Load the NHV file
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(text);

                var ns = new XmlNamespaceManager(doc.NameTable);
                ns.AddNamespace("nhv", NHIBERNATE_VALIDATOR_NAMESPACE);
                var className = doc.SelectSingleNode("/nhv:nhv-mapping/nhv:class", ns).GetAttributeValueIfExists("name");
                var shortClassName = GetShortClassName(className);
                var possibleEntity = set.EntitySet.Entities.FirstOrDefault(e => e.Name == shortClassName);

                if (possibleEntity == null)
                {
                    log.WarnFormat("Could not find Entity named {1}", className);
                    return;
                }
                var propertyNodes = doc.SelectNodes("/nhv:nhv-mapping/nhv:class/nhv:property", ns);

                if (propertyNodes == null)
                {
                    log.WarnFormat("Could not find any property nodes in {0}", nhv);
                    return;
                }
                foreach (XmlNode node in propertyNodes)
                {
                    var proc = new NodeProcessor(node, ns);
                    var propertyName = proc.Attributes.GetString("name");
                    var property = possibleEntity.Properties.FirstOrDefault(p => p.Name == propertyName);

                    if (property == null)
                    {
                        log.WarnFormat("Could not find property {0} in Entity {1}", propertyName, possibleEntity.Name);
                        continue;
                    }
                    if (proc.Exists("nhv:length"))
                    {
                        if (proc.SubNode("nhv:length").Attributes.Exists("max"))
                            property.ValidationOptions.MaximumLength = proc.SubNode("nhv:length").Attributes.GetInt("max");

                        if (proc.SubNode("nhv:length").Attributes.Exists("min"))
                            property.ValidationOptions.MinimumLength = proc.SubNode("nhv:length").Attributes.GetInt("min");
                    }
                    if (proc.Exists("nhv:digits"))
                    {
                        var subNode = proc.SubNode("nhv:digits");
                        var integerDigits = subNode.Attributes.GetInt("integerDigits");
                        var fractionalDigits = subNode.Attributes.GetNullableInt("fractionalDigits");
                        property.ValidationOptions.IntegerDigits = integerDigits;
                        property.ValidationOptions.FractionalDigits = fractionalDigits;
                    }
                    if (proc.Exists("nhv:not-null"))
                        property.ValidationOptions.Nullable = false;
                    if (proc.Exists("nhv:not-empty"))
                        property.ValidationOptions.NotEmpty = true;
                    if (proc.Exists("nhv:future"))
                        property.ValidationOptions.FutureDate = true;
                    if (proc.Exists("nhv:past"))
                        property.ValidationOptions.PastDate = true;
                    if (proc.Exists("nhv:email"))
                        property.ValidationOptions.Email = true;
                    //if (proc.Exists("nhv:size"))
                    //    property.ValidationOptions.si = false;
                    if (proc.Exists("nhv:min"))
                    {
                        var subNode = proc.SubNode("nhv:min");
                        property.ValidationOptions.MinimumValue = subNode.Attributes.GetInt("value");
                    }
                    //if (proc.Exists("nhv:range"))
                    //    property.ValidationOptions.patt = false;
                    //if (proc.Exists("nhv:pattern"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:asserttrue"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:rule"))
                    //    property.ValidationOptions.xxx = false;
                    if (proc.Exists("nhv:max"))
                    {
                        var subNode = proc.SubNode("nhv:max");
                        property.ValidationOptions.MaximumValue = subNode.Attributes.GetInt("value");
                    }
                    //if (proc.Exists("nhv:decimalmax"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:decimalmin"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:notnull-notempty"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:ipaddress"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:ean"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:creditcardnumber"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:assertfalse"))
                    //    property.ValidationOptions.xxx = false;
                    //if (proc.Exists("nhv:fileexists"))
                    //    property.ValidationOptions.xxx = false;
                    if (proc.Exists("nhv:valid"))
                        property.ValidationOptions.Validate = true;
                    //if (proc.Exists("nhv:iban"))
                    //    property.ValidationOptions..xxx = false;
                }

            }
        }