Beispiel #1
0
        private bool IsPhysicalType(string nameSpace, string localName, string propertyName)
        {
            bool isPhysicalType = false;
            XmlSchemaAttribute schemaAttribute;

            SchemaManager.FindAttribute(nameSpace, localName, propertyName, out schemaAttribute);
            string typeName = "";

            if (schemaAttribute != null)
            {
                XmlSchemaSimpleType simpleType = schemaAttribute.AttributeSchemaType;
                isPhysicalType = SchemaManager.IsPhysicalType(simpleType);
            }
            return(isPhysicalType);
        }
        public static void Save(IAtmlObject atmlObject, Capability capability)
        {
            string id  = atmlObject.GetAtmlId();
            var    dao = new EquipmentDAO();

            if (capability.SignalDescription != null)
            {
                List <Signal> signals = SignalManager.ExtractSignalsFromExtension(capability.SignalDescription);
                foreach (Signal signal in signals)
                {
                    string   signalName      = signal.name;
                    string   signalNameSpace = signal.GetSignalNameSpace();
                    string   capabilityName  = capability.name;
                    object[] items           = signal.Items;

                    foreach (object item in items)
                    {
                        var element = item as XmlElement;
                        if (element != null)
                        {
                            XmlSchemaComplexType complexType;
                            string    elementName = element.Name;
                            string    localName   = element.LocalName;
                            XmlSchema schema      = SchemaManager.GetSchema(element.NamespaceURI);
                            SchemaManager.GetComplexType(element.NamespaceURI, localName, out complexType);
                            var xmlSchemaObjects =
                                new Dictionary <string, XmlSchemaObject>();
                            SchemaManager.ExtractAttributes(complexType, xmlSchemaObjects);


                            foreach (XmlAttribute attribute in element.Attributes)
                            {
                                string propertyName = attribute.Name;
                                string value        = attribute.Value;
                                try
                                {
                                    XmlSchemaAttribute schemaAttribute;
                                    SchemaManager.FindAttribute(element.NamespaceURI, localName, propertyName,
                                                                out schemaAttribute);
                                    bool   isPhysicalType = false;
                                    string typeName       = "";
                                    if (schemaAttribute != null)
                                    {
                                        XmlSchemaSimpleType simpleType = schemaAttribute.AttributeSchemaType;
                                        XmlQualifiedName    qn         = schemaAttribute.SchemaTypeName;
                                        isPhysicalType = SchemaManager.IsPhysicalType(simpleType);
                                        if (!isPhysicalType)
                                        {
                                            isPhysicalType = SchemaManager.IsPhysicalType(qn);
                                        }
                                    }

                                    Physical physical;

                                    try
                                    {
                                        physical = new Physical(value);
                                    }
                                    catch (Exception)
                                    {
                                        continue;
                                    }

                                    RangingInformation range = physical.GetMergedRange();
                                    //TODO: need SignalType, SignalNamespace
                                    if (range != null)
                                    {
                                        InstrumentCapabilitiesBean bean =
                                            dao.GetSignalCapabilityAttribute(Guid.Parse(id),
                                                                             capabilityName,
                                                                             signalName,
                                                                             propertyName);
                                        if (bean == null)
                                        {
                                            bean           = new InstrumentCapabilitiesBean();
                                            bean.DataState = BASEBean.eDataState.DS_ADD;
                                        }
                                        else
                                        {
                                            bean.DataState = atmlObject.IsDeleted()
                                                                 ? BASEBean.eDataState.DS_DELETE
                                                                 : BASEBean.eDataState.DS_EDIT;
                                        }
                                        //bean.signalType =;
                                        bean.capabilityName     = capabilityName;
                                        bean.signalNamespace    = signalNameSpace;
                                        bean.IncludeKeyOnInsert = true;
                                        bean.instrumentUuid     = id;
                                        bean.attribute          = propertyName;
                                        bean.signalName         = signalName;
                                        if (range.FromQuantity != null)
                                        {
                                            bean.lowValue = range.FromQuantity.NominalValue;
                                            bean.lowUnit  = range.FromQuantity.Unit.BaseUnitString;
                                        }
                                        if (range.ToQuantity != null)
                                        {
                                            bean.highValue = range.ToQuantity.NominalValue;
                                            bean.highUnit  = range.ToQuantity.Unit.BaseUnitString;
                                        }
                                        bean.save();
                                    }
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            string       signalFunctionName = item.GetType().Name;
                            PropertyInfo piName             = item.GetType().GetProperty("name");
                            if (piName != null)
                            {
                                object value = piName.GetValue(item, null);
                                //if (value != null)
                                //   propertyName = value as string;
                            }
                            foreach (
                                PropertyInfo pi in
                                item.GetType()
                                .GetProperties(BindingFlags.Public | BindingFlags.Instance |
                                               BindingFlags.DeclaredOnly))
                            {
                                string propertyName = pi.Name;
                                object value        = pi.GetValue(item, null);
                                if (value != null)
                                {
                                    try
                                    {
                                        //TODO: need SignalType, SignalNamespace
                                        if (
                                            !SchemaManager.IsPhysicalType("urn:IEEE-1641:2010:STDBSC",
                                                                          signalFunctionName, propertyName))
                                        {
                                            continue;
                                        }
                                        var physicalValue = new Physical(value.ToString());
                                        Console.WriteLine(physicalValue.ToString());
                                        RangingInformation         range = physicalValue.GetMergedRange();
                                        InstrumentCapabilitiesBean bean  =
                                            dao.GetSignalCapabilityAttribute(Guid.Parse(id),
                                                                             capabilityName,
                                                                             signalName,
                                                                             pi.Name);
                                        if (bean == null)
                                        {
                                            bean           = new InstrumentCapabilitiesBean();
                                            bean.DataState = BASEBean.eDataState.DS_ADD;
                                        }
                                        else
                                        {
                                            bean.DataState = atmlObject.IsDeleted()
                                                                 ? BASEBean.eDataState.DS_DELETE
                                                                 : BASEBean.eDataState.DS_EDIT;
                                        }
                                        //bean.signalType =;
                                        bean.capabilityName     = capabilityName;
                                        bean.signalNamespace    = signalNameSpace;
                                        bean.IncludeKeyOnInsert = true;
                                        bean.instrumentUuid     = id;
                                        bean.attribute          = pi.Name;
                                        bean.signalName         = signalName;
                                        if (range != null)
                                        {
                                            if (range.FromQuantity != null)
                                            {
                                                bean.lowValue = range.FromQuantity.NominalValue;
                                                bean.lowUnit  = range.FromQuantity.Unit.BaseUnitString;
                                            }
                                            if (range.ToQuantity != null)
                                            {
                                                bean.highValue = range.ToQuantity.NominalValue;
                                                bean.highUnit  = range.ToQuantity.Unit.BaseUnitString;
                                            }
                                        }
                                        bean.save();
                                    }
                                    catch (Exception err)
                                    {
                                        LogManager.Error(err,
                                                         "Error Saving Capability [{0}] - Item:{1}, Value:{2}",
                                                         capability.name,
                                                         item.GetType().Name,
                                                         value
                                                         );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        protected override bool Validate()
        {
            string controlValue = ControlToValidate.Text.Trim();
            bool   valid        = true;

            if (IsEnabled && ControlToValidate.Enabled)
            {
                String minInclusive = null;
                String minExclusive = null;
                String maxInclusive = null;
                String maxExclusive = null;
                String length       = null;
                String minLength    = null;
                String maxLength    = null;
                var    errorMessage = new StringBuilder();

                XmlSchemaAttribute attribute = null;
                SchemaManager.FindAttribute(_targetNamespace, _typeName, _attributeName, out attribute);

                if (attribute == null)
                {
                    LogManager.Error("XSDSchemaValidator - Failed to locate attribute: {0} in {1}", _attributeName, _targetNamespace);
                }
                else
                {
                    XmlSchemaUse use = attribute.Use;

                    XmlSchemaSimpleType attributeSchemaType = attribute.AttributeSchemaType;
                    XmlQualifiedName    qname = attributeSchemaType.BaseXmlSchemaType.QualifiedName;
                    String typeName           = qname.Name;
                    ErrorProvider.SetError(_controlToValidate, "");


                    if (attributeSchemaType.Content is XmlSchemaSimpleTypeRestriction)
                    {
                        var restr = (XmlSchemaSimpleTypeRestriction)attributeSchemaType.Content;
                        foreach (object facet in restr.Facets)
                        {
                            if (facet is XmlSchemaMinInclusiveFacet)
                            {
                                var lf = facet as XmlSchemaMinInclusiveFacet;
                                if (!String.IsNullOrEmpty(controlValue))
                                {
                                    int    iResult;
                                    double dResult;
                                    if ((typeName.Contains("int") && int.TryParse(controlValue, out iResult) &&
                                         iResult < int.Parse(lf.Value))
                                        ||
                                        (typeName.Contains("dou") && double.TryParse(controlValue, out dResult) &&
                                         dResult < double.Parse(lf.Value)))
                                    {
                                        errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
                                        .Append(String.Format("The value must not be less than {0}", lf.Value));
                                    }
                                }
                            }
                            else if (facet is XmlSchemaMinExclusiveFacet)
                            {
                                minExclusive = ((XmlSchemaMinExclusiveFacet)facet).Value;
                            }
                            else if (facet is XmlSchemaMaxInclusiveFacet)
                            {
                                maxInclusive = ((XmlSchemaMaxInclusiveFacet)facet).Value;
                            }
                            else if (facet is XmlSchemaMaxExclusiveFacet)
                            {
                                maxExclusive = ((XmlSchemaMaxExclusiveFacet)facet).Value;
                            }
                            else if (facet is XmlSchemaLengthFacet)
                            {
                                var lf = facet as XmlSchemaLengthFacet;
                                if (!String.IsNullOrEmpty(controlValue) && controlValue.Length > int.Parse(lf.Value))
                                {
                                    errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
                                    .Append(String.Format("The value's length must not excede {0} characters",
                                                          lf.Value));
                                }
                            }
                            else if (facet is XmlSchemaMinLengthFacet)
                            {
                                var lf = facet as XmlSchemaMinLengthFacet;
                                if (!String.IsNullOrEmpty(controlValue) && controlValue.Length < int.Parse(lf.Value))
                                {
                                    errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
                                    .Append(String.Format("The value's length must not be less than {0} characters",
                                                          lf.Value));
                                }
                            }
                            else if (facet is XmlSchemaMaxLengthFacet)
                            {
                                var lf = facet as XmlSchemaMaxLengthFacet;
                                if (!String.IsNullOrEmpty(controlValue) && controlValue.Length > int.Parse(lf.Value))
                                {
                                    errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
                                    .Append(
                                        String.Format("The value's length must not be greater than {0} characters",
                                                      lf.Value));
                                }
                            }
                            else if (facet is XmlSchemaPatternFacet)
                            {
                                var spf = facet as XmlSchemaPatternFacet;
                                //Console.WriteLine(spf.Id + " : " + spf.Value);
                                if (!Regex.IsMatch(controlValue, spf.Value))
                                {
                                    if (_icon != null)
                                    {
                                        ErrorProvider.Icon = _icon;
                                    }
                                    errorMessage.Append((errorMessage.Length > 0) ? "\r\n" : "")
                                    .Append(String.Format(
                                                "The \"{0}\" value must match the regular expression: {1}",
                                                _attributeName, spf.Value));
                                    valid = false;
                                }
                            }
                            else if (facet is XmlSchemaEnumerationFacet)
                            {
                            }
                            else if (facet is XmlSchemaTotalDigitsFacet)
                            {
                            }
                            else if (facet is XmlSchemaWhiteSpaceFacet)
                            {
                            }
                        }

                        ErrorProvider.SetError(_controlToValidate, errorMessage.ToString());
                    }
                }
            }
            if (!valid)
            {
                if (ControlToValidate is ATMLControl)
                {
                    ((ATMLControl)ControlToValidate).HasErrors = true;
                }
            }

            return(valid);
        }