/// <summary>
        ///
        /// </summary>
        public static ADOPersistenceHelper CreatePersistenceHelper(PlatformSettings settings)
        {
            ADOPersistenceHelper helper = new ADOPersistenceHelper();

            if (helper.Initialize(settings.GetMappedFolder("PersistencePath")) == false)
            {
                return(null);
            }

            helper.SetupTypeMapping(typeof(Platform), "Platforms", null);
            helper.SetupTypeMapping(typeof(PlatformComponent), "PlatformComponents", null);


            return(helper);
        }
        /// <summary>
        ///
        /// </summary>
        public void Initialize(Platform platform)
        {
            SystemMonitor.CheckWarning(NewsSourcesUnsafe.Count == 0, "Manager already has assigned sources.");

            _platform = platform;

            _persistenceHelper = Platform.CreatePersistenceHelper(platform.Settings);

            _persistenceHelper.SetupTypeMapping(typeof(RssNewsItem), "RssNewsItems", null);
            _persistenceHelper.SetupTypeMapping(typeof(NewsSource), "NewsSources", null);
            _persistenceHelper.SetupTypeMapping(typeof(RssNewsSource), "NewsSources", null);

            _persistenceHelper.SetupTypeMapping(typeof(ForexNewsItem), "ForexNewsItems", null);
            _persistenceHelper.SetupTypeMapping(typeof(ForexNewsSource), "ForexNewsSources", null);


            // Make sure this load is before the accept events, so that they do not cause adding failure.
            List <NewsSource> sources = _persistenceHelper.SelectDynamicType <NewsSource>(null, "Type", null);

            base.SourceAddedEvent   += new NewsSourceUpdateDelegate(PlatformNewsManager_SourceAddedEvent);
            base.SourceRemovedEvent += new NewsSourceUpdateDelegate(PlatformNewsManager_SourceRemovedEvent);

            foreach (NewsSource source in sources)
            {
                if (source.Enabled)
                {
                    List <RssNewsItem> items =
                        _persistenceHelper.Select <RssNewsItem>(new MatchExpression("NewsSourceId", source.Id), null);

                    foreach (RssNewsItem item in items)
                    {
                        item.Source = source;
                    }

                    // Handle the relation to persistence.
                    source.AddItems(items.ToArray());
                }

                base.AddSource(source);
            }


            GeneralHelper.FireAndForget(UpdateFeeds);
        }
        /// <summary>
        /// 
        /// </summary>
        public void Initialize(Platform platform)
        {
            SystemMonitor.CheckWarning(NewsSourcesUnsafe.Count == 0, "Manager already has assigned sources.");

            _platform = platform;

            _persistenceHelper = Platform.CreatePersistenceHelper(platform.Settings);

            _persistenceHelper.SetupTypeMapping(typeof(RssNewsItem), "RssNewsItems", null);
            _persistenceHelper.SetupTypeMapping(typeof(NewsSource), "NewsSources", null);
            _persistenceHelper.SetupTypeMapping(typeof(RssNewsSource), "NewsSources", null);

            _persistenceHelper.SetupTypeMapping(typeof(ForexNewsItem), "ForexNewsItems", null);
            _persistenceHelper.SetupTypeMapping(typeof(ForexNewsSource), "ForexNewsSources", null);

            // Make sure this load is before the accept events, so that they do not cause adding failure.
            List<NewsSource> sources = _persistenceHelper.SelectDynamicType<NewsSource>(null, "Type", null);

            base.SourceAddedEvent += new NewsSourceUpdateDelegate(PlatformNewsManager_SourceAddedEvent);
            base.SourceRemovedEvent += new NewsSourceUpdateDelegate(PlatformNewsManager_SourceRemovedEvent);

            foreach (NewsSource source in sources)
            {
                if (source.Enabled)
                {
                    List<RssNewsItem> items =
                        _persistenceHelper.Select<RssNewsItem>(new MatchExpression("NewsSourceId", source.Id), null);

                    foreach (RssNewsItem item in items)
                    {
                        item.Source = source;
                    }

                    // Handle the relation to persistence.
                    source.AddItems(items.ToArray());
                }

                base.AddSource(source);
            }

            GeneralHelper.FireAndForget(UpdateFeeds);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        public bool Initialize(PlatformSettings settings)
        {
            lock (this)
            {
                _persistenceHelper = Platform.CreatePersistenceHelper(settings);
                _persistenceHelper.SetupTypeMapping(typeof(DataStoreEntry), "DataStoreEntries", null);
                _dataStoreFilesFolder = settings.GetMappedFolder("DataStoreFolder");
            }

            GeneralHelper.FireAndForget(
                delegate()
            {
                try
                {    // Download online dataDelivery source entries.
                    WebRequest request = WebRequest.Create(settings.DataStoreOnlineSourcesXml);
                    request.Timeout    = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;

                    TextReader reader = new StreamReader(request.GetResponse().GetResponseStream());

                    // If you get an error here in DEBUG MODE, just ignore it, it is a bug in VS 2008.
                    XmlSerializer serializer = new XmlSerializer(typeof(OnlineEntrySource[]));

                    OnlineEntrySource[] sources = (OnlineEntrySource[])serializer.Deserialize(reader);

                    lock (_onlineEntrySources)
                    {    // _onlineEntrySources contains serialized existing sources.
                        _onlineEntrySources.AddRange(sources);
                        sources = _onlineEntrySources.ToArray();
                    }

                    foreach (OnlineEntrySource source in sources)
                    {
                        if (OnlineEntrySourceAddedEvent != null)
                        {
                            OnlineEntrySourceAddedEvent(this, source);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SystemMonitor.OperationError("Failed to obtain online data souces [" + ex.Message + "].", TracerItem.PriorityEnum.Low);
                }
            }
                );

            if (Directory.Exists(_dataStoreFilesFolder) == false)
            {
                if (Directory.CreateDirectory(_dataStoreFilesFolder) == null)
                {
                    SystemMonitor.OperationError("Failed to create Data Store folder [" + settings.GetMappedFolder("DataStoreFolder") + "]");
                    return(false);
                }
            }

            List <DataStoreEntry> entries = _persistenceHelper.Select <DataStoreEntry>(null, null);

            foreach (DataStoreEntry entry in entries)
            {
                DoAddEntry(entry);
            }

            ChangeOperationalState(OperationalStateEnum.Operational);

            return(true);
        }
        /// <summary>
        /// 
        /// </summary>
        public bool Initialize(PlatformSettings settings)
        {
            lock (this)
            {
                _persistenceHelper = Platform.CreatePersistenceHelper(settings);
                _persistenceHelper.SetupTypeMapping(typeof(DataStoreEntry), "DataStoreEntries", null);
                _dataStoreFilesFolder = settings.GetMappedFolder("DataStoreFolder");
            }

            GeneralHelper.FireAndForget(
                delegate()
                {
                    try
                    {// Download online dataDelivery source entries.
                        WebRequest request = WebRequest.Create(settings.DataStoreOnlineSourcesXml);
                        request.Timeout = (int)TimeSpan.FromSeconds(15).TotalMilliseconds;

                        TextReader reader = new StreamReader(request.GetResponse().GetResponseStream());

                        // If you get an error here in DEBUG MODE, just ignore it, it is a bug in VS 2008.
                        XmlSerializer serializer = new XmlSerializer(typeof(OnlineEntrySource[]));

                        OnlineEntrySource[] sources = (OnlineEntrySource[])serializer.Deserialize(reader);

                        lock (_onlineEntrySources)
                        {// _onlineEntrySources contains serialized existing sources.
                            _onlineEntrySources.AddRange(sources);
                            sources = _onlineEntrySources.ToArray();
                        }

                        foreach (OnlineEntrySource source in sources)
                        {
                            if (OnlineEntrySourceAddedEvent != null)
                            {
                                OnlineEntrySourceAddedEvent(this, source);
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        SystemMonitor.OperationError("Failed to obtain online data souces [" + ex.Message + "].", TracerItem.PriorityEnum.Low);
                    }
                }
            );

            if (Directory.Exists(_dataStoreFilesFolder) == false)
            {
                if (Directory.CreateDirectory(_dataStoreFilesFolder) == null)
                {
                    SystemMonitor.OperationError("Failed to create Data Store folder [" + settings.GetMappedFolder("DataStoreFolder") + "]");
                    return false;
                }
            }

            List<DataStoreEntry> entries = _persistenceHelper.Select<DataStoreEntry>(null, null);
            foreach (DataStoreEntry entry in entries)
            {
                DoAddEntry(entry);
            }

            ChangeOperationalState(OperationalStateEnum.Operational);

            return true;
        }
        /// <summary>
        /// The initialization of a platform consumes 2 main dataDelivery sources.
        /// The settings is used for primary, startup information (like where
        /// is the persistence DB file etc.) and the information inside the
        /// persistence is than on used to create components etc.
        /// </summary>
        public bool Initialize(PlatformSettings platformSettings)
        {
            TracerHelper.TraceEntry();

            SystemMonitor.CheckThrow(_settings == null, "Platform already initialized.");

            lock (this)
            {
                _settings = platformSettings;
                if (_persistenceHelper == null)
                {
                    _persistenceHelper = CreatePersistenceHelper(platformSettings);
                }
            }

            if (_persistenceHelper == null)
            {
                return(false);
            }

            LoadModules(_settings);

            if (_persistenceHelper.Count <Platform>(new MatchExpression("Name", this.Name)) == 0)
            {// This is a new platform.
                lock (this)
                {
                    _guid = Guid.NewGuid();
                }

                if (_persistenceHelper.Insert <Platform>(this, null) == false)
                {
                    SystemMonitor.Error("Failed to persist new platform [" + this.Name + "]");
                    return(false);
                }
            }
            else
            {// This is existing.
                // Now try to load self from persistance storage.
                bool selectionResult = _persistenceHelper.SelectScalar <Platform>(this, new MatchExpression("Name", this.Name));

                if (selectionResult == false)
                {// Failed to load self from DB.
                    return(false);
                }
            }

            lock (this)
            {
                if (_serializationData.ContainsValue("diagnosticsMode"))
                {
                    _settings.DiagnosticsMode = _serializationData.GetBoolean("diagnosticsMode");
                }

                if (_serializationData.ContainsValue("uiSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _uiSerializationInfo = _serializationData.GetValue <SerializationInfoEx>("uiSerializationInfo");
                }

                if (_serializationData.ContainsValue("componentSpecificSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _componentSpecificSerializationInfo = _serializationData.GetValue <SerializationInfoEx>("componentSpecificSerializationInfo");
                }
            }

            //_server = new Arbiter.TransportIntegrationServer(_platformUri);
            //Arbiter.AddClient(_server);

            GeneralHelper.FireAndForget(delegate()
            {// LoadFromFile components.
             // Registering of components is better done outside the lock,
             // since components may launch requests to platform at initializations.

                _isLoading = true;

                // Components are stored in the PlatformComponents database, they are being serialized and the entire object is
                // persisted in the DB, as well as the type information for it and a reference to the platform instance it belongs to.
                List <long> failedSerializationsIds = new List <long>();
                List <PlatformComponent> components = PersistenceHelper.SelectSerializedType <PlatformComponent>(
                    new MatchExpression("PlatformId", this.Id), "Data", null, failedSerializationsIds);

                SortedList <int, List <PlatformComponent> > componentsByLevel = GetComponentsByLevel(components);

                GatherMandatoryComponents(componentsByLevel);

                foreach (int level in componentsByLevel.Keys)
                {// Register lower level components first.
                    foreach (PlatformComponent component in componentsByLevel[level])
                    {
                        if (DoRegisterComponent(component, true) == false &&
                            component.Id.HasValue && ComponentDeserializationFailedEvent != null)
                        {
                            ComponentDeserializationFailedEvent(component.Id.Value, component.GetType().Name);
                        }
                    }
                }

                // Handle failed deserializations.
                foreach (int id in failedSerializationsIds)
                {
                    string typeName = "Unknown";

                    try
                    {// Extract the type of this entry.
                        List <object[]> result = _persistenceHelper.SelectColumns <PlatformComponent>(
                            new MatchExpression("Id", id), new string[] { "Type" }, 1);

                        Type type = Type.GetType(result[0][0].ToString());

                        if (type != null)
                        {
                            typeName = type.Name;
                        }
                    }
                    catch (Exception ex)
                    {
                        SystemMonitor.Error("Failed to extract type information [" + ex.Message + "].");
                    }

                    if (ComponentDeserializationFailedEvent != null)
                    {
                        ComponentDeserializationFailedEvent(id, typeName);
                    }
                }

                _isLoading = false;
            });

            return(true);
        }
        /// <summary>
        /// The initialization of a platform consumes 2 main dataDelivery sources.
        /// The settings is used for primary, startup information (like where 
        /// is the persistence DB file etc.) and the information inside the 
        /// persistence is than on used to create components etc.
        /// </summary>
        public bool Initialize(PlatformSettings platformSettings)
        {
            TracerHelper.TraceEntry();

            SystemMonitor.CheckThrow(_settings == null, "Platform already initialized.");

            lock (this)
            {
                _settings = platformSettings;
                if (_persistenceHelper == null)
                {
                    _persistenceHelper = CreatePersistenceHelper(platformSettings);
                }
            }

            if (_persistenceHelper == null)
            {
                return false;
            }

            LoadModules(_settings);

            if (_persistenceHelper.Count<Platform>(new MatchExpression("Name", this.Name)) == 0)
            {// This is a new platform.
                lock (this)
                {
                    _guid = Guid.NewGuid();
                }

                if (_persistenceHelper.Insert<Platform>(this, null) == false)
                {
                    SystemMonitor.Error("Failed to persist new platform [" + this.Name + "]");
                    return false;
                }
            }
            else
            {// This is existing.
                // Now try to load self from persistance storage.
                bool selectionResult = _persistenceHelper.SelectScalar<Platform>(this, new MatchExpression("Name", this.Name));

                if (selectionResult == false)
                {// Failed to load self from DB.
                    return false;
                }
            }

            lock (this)
            {
                if (_serializationData.ContainsValue("diagnosticsMode"))
                {
                    _settings.DiagnosticsMode = _serializationData.GetBoolean("diagnosticsMode");
                }

                if (_serializationData.ContainsValue("uiSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _uiSerializationInfo = _serializationData.GetValue<SerializationInfoEx>("uiSerializationInfo");
                }

                if (_serializationData.ContainsValue("componentSpecificSerializationInfo"))
                {
                    // The main serialization dataDelivery stores the UI orderInfo.
                    _componentSpecificSerializationInfo = _serializationData.GetValue<SerializationInfoEx>("componentSpecificSerializationInfo");
                }
            }

            //_server = new Arbiter.TransportIntegrationServer(_platformUri);
            //Arbiter.AddClient(_server);

            GeneralHelper.FireAndForget(delegate()
            {// LoadFromFile components.
                // Registering of components is better done outside the lock,
                // since components may launch requests to platform at initializations.

                _isLoading = true;

                // Components are stored in the PlatformComponents database, they are being serialized and the entire object is
                // persisted in the DB, as well as the type information for it and a reference to the platform instance it belongs to.
                List<long> failedSerializationsIds = new List<long>();
                List<PlatformComponent> components = PersistenceHelper.SelectSerializedType<PlatformComponent>(
                    new MatchExpression("PlatformId", this.Id), "Data", null, failedSerializationsIds);

                SortedList<int, List<PlatformComponent>> componentsByLevel = GetComponentsByLevel(components);

                GatherMandatoryComponents(componentsByLevel);

                foreach (int level in componentsByLevel.Keys)
                {// Register lower level components first.
                    foreach (PlatformComponent component in componentsByLevel[level])
                    {
                        if (DoRegisterComponent(component, true) == false
                            && component.Id.HasValue && ComponentDeserializationFailedEvent != null)
                        {
                            ComponentDeserializationFailedEvent(component.Id.Value, component.GetType().Name);
                        }
                    }
                }

                // Handle failed deserializations.
                foreach (int id in failedSerializationsIds)
                {
                    string typeName = "Unknown";

                    try
                    {// Extract the type of this entry.
                        List<object[]> result = _persistenceHelper.SelectColumns<PlatformComponent>(
                            new MatchExpression("Id", id), new string[] { "Type" }, 1 );

                        Type type = Type.GetType(result[0][0].ToString());

                        if (type != null)
                        {
                            typeName = type.Name;
                        }
                    }
                    catch(Exception ex)
                    {
                        SystemMonitor.Error("Failed to extract type information [" + ex.Message + "].");
                    }

                    if (ComponentDeserializationFailedEvent != null)
                    {
                        ComponentDeserializationFailedEvent(id, typeName);
                    }
                }

                _isLoading = false;
            });

            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        public static ADOPersistenceHelper CreatePersistenceHelper(PlatformSettings settings)
        {
            ADOPersistenceHelper helper = new ADOPersistenceHelper();
            if (helper.Initialize(settings.GetMappedFolder("PersistencePath")) == false)
            {
                return null;
            }

            helper.SetupTypeMapping(typeof(Platform), "Platforms", null);
            helper.SetupTypeMapping(typeof(PlatformComponent), "PlatformComponents", null);

            return helper;
        }