private void SaveCapabilities(IAtmlObject atmlObject, Capabilities capabilities)
        {
            var dao = new EquipmentDAO();

            dao.ClearEquipmentCapabilities(Guid.Parse(atmlObject.GetAtmlId()));
            if (!atmlObject.IsDeleted() && capabilities != null && capabilities.Items != null)
            {
                foreach (object item in capabilities.Items)
                {
                    var capability = item as Capability;
                    if (capability != null)
                    {
                        Save(atmlObject, capability);
                    }
                }
            }
        }
        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
                                                         );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public bool Save(IAtmlObject atmlObject)
        {
            bool results          = false;
            bool localTransaction = false;
            bool isDeleted        = atmlObject.IsDeleted();
            var  dao = new DocumentDAO();

            try
            {
                if (!dao.IsInTransaction)
                {
                    dao.StartTransaction();
                    localTransaction = true;
                }
                string uuid = atmlObject.GetAtmlId();
                if (!string.IsNullOrWhiteSpace(uuid))
                {
                    byte[] content  = Serialize(atmlObject);
                    var    document = new Document();
                    if (dao.hasDocument(uuid))
                    {
                        document           = new Document(dao.openDatabaseDocument(uuid));
                        document.DataState = atmlObject.IsDeleted()
                                                 ? BASEBean.eDataState.DS_DELETE
                                                 : BASEBean.eDataState.DS_EDIT;
                    }
                    else
                    {
                        document.DataState    = BASEBean.eDataState.DS_ADD;
                        document.ContentType  = atmlObject.GetAtmlFileContext();
                        document.DocumentType = atmlObject.GetAtmlDocumentType();
                        document.uuid         = uuid;
                    }
                    string originalFileName = document.name;
                    document.name            = atmlObject.GetAtmlFileName();
                    document.Description     = atmlObject.GetAtmlFileDescription();
                    document.DocumentContent = content;
                    Save(document);

                    //-----------------------------------------------------------------------------//
                    //--- Save Asset Numbers - these include all model numbers and part numbers ---//
                    //-----------------------------------------------------------------------------//
                    ItemDescriptionIdentification identification = DetermineIdentification(atmlObject, dao, uuid);
                    if (identification != null && !isDeleted)
                    //When Deleting - let the cascade delete handle the related records.
                    {
                        SaveAssets(atmlObject, identification, dao, uuid);
                    }

                    //------------------------------------//
                    //--- Save Capabilities Separately ---//
                    //------------------------------------//
                    PropertyInfo piCapabilities = atmlObject.GetType().GetProperty("Capabilities");
                    if (piCapabilities != null)
                    {
                        var capabilities = piCapabilities.GetValue(atmlObject, null) as Capabilities;
                        SaveCapabilities(atmlObject, capabilities);
                    }

                    if (localTransaction)
                    {
                        dao.CommitTransaction();
                    }
                    results = true;
                    if (!string.IsNullOrEmpty(originalFileName) &&
                        !originalFileName.Equals(atmlObject.GetAtmlFileName()))
                    {
                        OnAtmlObjectNameChanged(originalFileName, atmlObject.GetAtmlFileName(), uuid,
                                                atmlObject.GetAtmlFileType());
                    }

                    //-------------------------------------------------------------------------------------//
                    //--- We only want to save a file to the project directory if a project is open and ---//
                    //--- the file name contains the project name. This will only occur for the Test    ---//
                    //--- Configuration and the Test Description documents.                             ---//
                    //-------------------------------------------------------------------------------------//
                    if (ATMLContext.HasProjectOpen && document.name.Contains(ATMLContext.CurrentProjectName))
                    {
                        FileManager.WriteFile(Path.Combine(ATMLContext.ProjectAtmlPath, document.name),
                                              document.DocumentContent);
                    }
                }
            }
            catch (Exception e)
            {
                LogManager.Error(e);
                if (localTransaction)
                {
                    dao.RollbackTransaction();
                }
            }
            return(results);
        }