private void SaveSelectedSignalModel()
        {
            HourGlass.Start();
            try
            {
                SignalDAO dao = DataManager.getSignalDAO();
                if (_signalModelLibrary != null)
                {
                    dao.DeleteTSFLibrary( _signalModelLibrary.TSFLibrary.uuid,
                                          _signalModelLibrary.TSFLibrary.targetNamespace );
                    var library = new dbTSFLibrary();
                    library.IncludeKeyOnInsert = true;
                    library.lastUpdate = DateTime.UtcNow;
                    library.id = Guid.Parse( _signalModelLibrary.TSFLibrary.uuid );
                    library.content = _signalModelLibrary.XmlContent;
                    library.targetNamespace = _signalModelLibrary.TSFLibrary.targetNamespace;
                    library.libraryName = _signalModelLibrary.TSFLibrary.name;
                    library.DataState = ( !dao.hasTSFLibrary( _signalModelLibrary.TSFLibrary.uuid ) )
                                            ? BASEBean.eDataState.DS_ADD
                                            : BASEBean.eDataState.DS_EDIT;
                    library.save();

                    foreach (SignalModel sm in _signalModelLibrary.SignalModels.Values)
                    {
                        TSFType tsf = sm.TSF;
                        dbSignal dataSignal = dao.getSignal( sm.Name, library.targetNamespace );
                        if (dataSignal == null)
                        {
                            //Add Signal to the database

                            string baseSignalName = sm.BaseSignalName;
                            dbSignal baseSignal = dao.getSignal( baseSignalName, sm.BaseSignalNameSpace );
                            dataSignal = new dbSignal();
                            dataSignal.ParentSignal = baseSignal;
                            dataSignal.signalName = sm.Name;
                            dataSignal.uuid = Guid.Parse(tsf.uuid);
                            dataSignal.xmlns = library.targetNamespace;
                            foreach (SignalAttribute attribute in sm.Attributes)
                            {
                                var a = new dbSignalAttribute();
                                a.attributeName = attribute.Name;
                                a.defaultValue = attribute.DefaultValue;
                                a.DataState = BASEBean.eDataState.DS_ADD;
                                a.type = attribute.SchemaType;
                                a.fixedValue = attribute.FixedValue;
                                if (dataSignal.Attributes == null )
                                    dataSignal.Attributes = new List<dbSignalAttribute>();
                                dataSignal.Attributes.Add( a );
                            }
                            dataSignal.DataState = BASEBean.eDataState.DS_ADD;
                        }
                        else
                        {
                            dataSignal.xmlns = library.targetNamespace;
                            dataSignal.uuid = Guid.Parse(tsf.uuid);
                            List<dbSignalAttribute> attributes = dataSignal.Attributes;
                            var attrMap = new Dictionary<string, SignalAttribute>();
                            var dbAttrMap = new Dictionary<string, dbSignalAttribute>();
                            foreach (SignalAttribute sa in sm.Attributes)
                                attrMap.Add( sa.Name, sa );

                            foreach (dbSignalAttribute dbAttribute in attributes)
                            {
                                string an = dbAttribute.attributeName;
                                dbAttrMap.Add( an, dbAttribute );
                                if (attrMap.ContainsKey( an ))
                                {
                                    SignalAttribute sa = attrMap[an];
                                    dbAttribute.type = sa.SchemaType;
                                    dbAttribute.defaultValue = sa.DefaultValue;
                                    dbAttribute.fixedValue = sa.FixedValue;
                                    dbAttribute.DataState = BASEBean.eDataState.DS_EDIT;
                                }
                                else
                                {
                                    dbAttribute.DataState = BASEBean.eDataState.DS_DELETE;
                                }
                            }

                            foreach (SignalAttribute sa in sm.Attributes)
                            {
                                if (!dbAttrMap.ContainsKey( sa.Name ))
                                {
                                    var dbAttribute = new dbSignalAttribute();
                                    dbAttribute.type = sa.SchemaType;
                                    dbAttribute.defaultValue = sa.DefaultValue;
                                    dbAttribute.fixedValue = sa.FixedValue;
                                    dbAttribute.DataState = BASEBean.eDataState.DS_ADD;
                                    dataSignal.Attributes.Add( dbAttribute );
                                }
                            }
                            dataSignal.DataState = BASEBean.eDataState.DS_EDIT;
                        }
                        dataSignal.xmlns = _signalModelLibrary.TSFLibrary.targetNamespace;
                        dataSignal.save();
                    }

                    foreach (ListViewItem item in lvSignals.Items)
                    {
                        var model = item.Tag as SignalModel;
                        if (model != null)
                        {
                            var signal = new dbTSFSignal();
                            signal.signalName = model.Name;
                            signal.id = Guid.Parse( model.TSF.uuid );
                            signal.signalContent = model.TSF.Serialize();
                            signal.libraryUuid = _signalModelLibrary.TSFLibrary.uuid;
                            signal.DataState = ( !dao.hasTSFSignal( model.TSF.uuid )
                                                     ? BASEBean.eDataState.DS_ADD
                                                     : BASEBean.eDataState.DS_EDIT );
                            signal.lastUpdate = DateTime.UtcNow;
                            try
                            {
                                signal.save();
                            }
                            catch (Exception e)
                            {
                                if (e.Message.ToLower().Contains( "duplicate" ))
                                {
                                    TestSignalBean otherSignal = dao.getTSFSignal( model.Name,
                                                                                   _signalModelLibrary.TSFLibrary.uuid );
                                    LogManager.Error( "UUID Conflict between document data and the database for Signal {0} in Signal Library {1} ",
                                                      model.Name, library.libraryName );
                                    if (otherSignal != null)
                                    {
                                        LogManager.Error( "\tDocument signal uuid {0}, Database signal uuid {{{1}}} ",
                                                          model.TSF.uuid, otherSignal.id.ToString().ToUpper() );

                                        if (dao.changeTSFSignalId( otherSignal.id, Guid.Parse(model.TSF.uuid) ))
                                        {
                                            LogManager.Info(
                                                "The Database signal uuid has been changed to reflect the Document signal uuid" );
                                            signal.DataState = BASEBean.eDataState.DS_EDIT;
                                            signal.save();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogManager.Error( e, "An error has occured saving the signal model library: {0}", e.Message );
            }
            finally
            {
                HourGlass.Stop();
                if (_signalModelLibrary == null)
                {
                    MessageBox.Show(Resources.A_Signal_Model_must_be_selected_to_save_);
                }
                else
                {
                    LogManager.Trace(Resources.Completed_Saving_Selected_Signal_Model);
                    MessageBox.Show(Resources.Completed_Saving_Selected_Signal_Model);
                }
            }
        }
 public void saveTSFLibrary(dbTSFLibrary library)
 {
     if (library.id == null)
     {
         library.DataState = BASEBean.eDataState.DS_ADD;
         library.id = Guid.NewGuid();
     }
     library.save();
 }
 public dbTSFLibrary getTSFLibrary(Guid libraryUUID)
 {
     dbTSFLibrary lb = new dbTSFLibrary();
     String sql = "SELECT * FROM "
                 + dbTSFLibrary._TABLE_NAME
                 + " WHERE "
                 + dbTSFLibrary._ID
                 + " = ?";
     OleDbParameter[] dbParams = { CreateParameter(dbTSFLibrary._ID, libraryUUID) };
     OleDbDataReader reader = ExecuteSqlQuery(sql, dbParams);
     if (reader != null)
     {
         while (reader.Read())
         {
             lb.load(reader);
             lb.Signals = getTSFLibrarySignals(libraryUUID);
         }
         reader.Close();
         reader.Dispose();
     }
     return lb;
 }
 public dbTSFLibrary getTSFLibraryByNameSpace(String libraryNameSpace)
 {
     dbTSFLibrary lb = null;
     String sql = "SELECT * FROM "
                 + dbTSFLibrary._TABLE_NAME
                 + " WHERE "
                 + dbTSFLibrary._TARGET_NAMESPACE
                 + " = ?";
     OleDbParameter[] dbParams = { CreateParameter(dbTSFLibrary._ID, libraryNameSpace) };
     OleDbDataReader reader = ExecuteSqlQuery(sql, dbParams);
     if (reader != null)
     {
         bool reading = reader.Read();
         if (reading)
         {
             lb = new dbTSFLibrary();
             while (reading)
             {
                 lb.load(reader);
                 lb.Signals = getTSFLibrarySignals(lb.id);
                 reading = reader.Read();
             }
         }
         reader.Close();
         reader.Dispose();
     }
     return lb;
 }
 public List<dbTSFLibrary> getTSFLibraries()
 {
     List<dbTSFLibrary> list = new List<dbTSFLibrary>();
     String sql = "SELECT * FROM "
                 + dbTSFLibrary._TABLE_NAME;
     OleDbParameter[] dbParams = {};
     OleDbDataReader reader = ExecuteSqlQuery(sql, dbParams);
     if (reader != null)
     {
         while (reader.Read())
         {
             dbTSFLibrary lb = new dbTSFLibrary();
             lb.load(reader);
             list.Add(lb);
         }
         reader.Close();
         reader.Dispose();
     }
     return list;
 }