private static void SyncBrokers(HttpClient httpClient, IRepository consoleRepository)
		{
			DatabaseInit._LogHandler.WriteLog("SyncBrokers()", LogSeverity.Information, LogCategory.DataAccess);

			var response = httpClient.GetAsync("rest/api/broker").Result;

			if (response.IsSuccessStatusCode)
			{
				var serviceBrokers = response.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.Broker>>().Result;

				foreach (var serviceBroker in serviceBrokers)
				{
					if (!consoleRepository.Query<Broker>().Any(cb => cb.BrokerSequenceId == serviceBroker.Id))
					{
						consoleRepository.Add(new Broker
						{
							BrokerSequenceId = serviceBroker.Id,
							Name = serviceBroker.Name,
							Code = serviceBroker.Code,
							Psu = serviceBroker.Psu,
							GroupCode = serviceBroker.GrpCd
						});
					}
					else
					{
						var consoleBroker = consoleRepository.Query<Broker>()
						                                     .FirstOrDefault(cb => cb.BrokerSequenceId == serviceBroker.Id);

						if (consoleBroker != null)
						{
							consoleBroker.Code = serviceBroker.Code;
							consoleBroker.GroupCode = serviceBroker.GrpCd;
							consoleBroker.Name = serviceBroker.Name;
							consoleBroker.Psu = serviceBroker.Psu;

							consoleRepository.Attach(consoleBroker);
						}
					}
				}
			}
			else
			{
				DatabaseInit._LogHandler.WriteLog("Get rest/api/broker failed", LogSeverity.Warning, LogCategory.DataAccess);
			}
		}
Example #2
0
        public HomeModule(IRepository repository)
        {
            Get["/"] = p =>
                {
                    var feeds = repository.Query(new GetFeedsQuery()).ToArray();

                    return Negotiate
                        .WithModel(feeds)
                        .WithView("home.cshtml");
                };
        }
        private static void UpdateSipAddresses(IRepository consoleRepository)
        {
            Dictionary<String, String> userSipAddresses = DatabaseInit.GetUserSipAddresses();
            foreach (User u in consoleRepository.Query<User>(u => u.IsActive))
	        {
                String shortName = WebSiteModuleManager.ExtractUserName(u.DomainLogon);
                if (userSipAddresses.ContainsKey(shortName))
                {
                    String sipAddress = userSipAddresses[shortName];
                    if (String.IsNullOrEmpty(u.SipAddress))
                    {
                        if (!String.IsNullOrEmpty(sipAddress))
                            u.SipAddress = sipAddress;
                    }
                    else
                    {
                        if (!u.SipAddress.Equals(sipAddress, StringComparison.InvariantCultureIgnoreCase))
                            u.SipAddress = sipAddress;
                    }
                }
	        }            
        }
 private static void UpdateUserPictures(IRepository consoleRepository)
 {
     Dictionary<String, byte[]> userPictures = DatabaseInit.GetUserPictures();
     foreach (User u in consoleRepository.Query<User>(u => u.IsActive))
     {
         String shortName = WebSiteModuleManager.ExtractUserName(u.DomainLogon);
         if (userPictures.ContainsKey(shortName))
         {
             byte[] userPic = userPictures[shortName];
             if ((userPic != null) && (userPic.Length > 0))
             {
                 if ((u.UserThumbnail == null) || (u.UserThumbnail.Length <= 0))
                 {
                     u.UserThumbnail = userPic;
                 }
                 else
                 {
                     if (!u.UserThumbnail.Equals(userPic))
                         u.UserThumbnail = userPic;
                 }
             }
         }
     }  
 }
        /// <summary>
        /// The create new tags.
        /// </summary>
        /// <param name="tags">
        /// The tags.
        /// </param>
        /// <param name="tagRepository">
        /// The tag repository.
        /// </param>
        /// <returns>
        /// The <see cref="IList"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// </exception>
        private IList<Tag> CreateNewTags(IList<Tag> tags, IRepository<Tag, int> tagRepository)
        {
            if (tags == null)
            {
                throw new ArgumentNullException("tags");
            }

            IList<Tag> result = new List<Tag>(tags.Count);
            for (int i = 0; i < tags.Count; ++i)
            {
                string content = tags[i].Content;
                Tag temp =
                    tagRepository.Query()
                                 .Filter(p => p.Content.Equals(content, StringComparison.Ordinal))
                                 .Get()
                                 .FirstOrDefault();
                if (temp == null)
                {
                    tagRepository.Insert(tags[i]);
                    result.Add(tags[i]);
                }
                else
                {
                    result.Add(temp);
                }
            }

            return result;
        }
 private void GetCategoryData(IRepository _repository)
 {
     Categories = _repository.Query<Category>(x => x == x);
 }
        public HomeViewModel(ILog log, IAccount account, ILocalize localize, IApplication application, IHistory history,
                             INavigationService navigationService, IUser user, IRepository repository,
                             IList<IExerciseType> exerciseTypes, ISettings settings)
        {
            _log = log;
            _localize = localize;
            _application = application;
            Account = account;

            _history = history;
            _history.OnHistoryItemsChanged += _history_OnHistoryItemsChanged;
            _NavigationService = navigationService;
            User = user;
            ExerciseTypes = exerciseTypes;
            _repository = repository;
            _settings = settings;
            _settings.OnSettingsChanged += _settings_OnSettingsChanged;
            _settings.Load();
            _history.Load();

            _repository.Single<User>(1).ContinueWith(t =>
                {
                    var foundUser = t.Result;
                    if (foundUser == null)
                    {
                        //this is first load of the app, set it up
                        _repository.Insert<User>(this.User).ContinueWith(task =>
                            {
                                this.User = this.User;
                                Account.AccessToken = this.User.RunkeeperToken;

                            });
                    }
                    else
                    {
                        User = foundUser;
                        Account.AccessToken = foundUser.RunkeeperToken;
                    }
                });

            if (_exerciseTypes == null || _exerciseTypes.Count == 0 ||
                (_exerciseTypes.Count == 1 && _exerciseTypes[0].Id == 0))
            {
                if (HomeViewModel.cachedTypes != null)
                {
                    this.ExerciseTypes = HomeViewModel.cachedTypes;
                    _log.Info("cache hit");
                }
                else
                {
                    _log.Info("cache miss");
                    this.ExerciseTypes = DefaultTypes;
                    _log.Info("default types set, querying");
                    _repository.Query<ExerciseType>("select * from ExerciseType").ContinueWith(t =>
                        {
                            _log.Info("query complete");
                            var types = t.Result;
                            if (types == null || types.Count == 0)
                            {

                                _log.Info("db does not have Exercise types, loading default items");
                                foreach (var e in from tt in this.ExerciseTypes orderby tt.Id select tt)
                                {
                                    _repository.Insert<ExerciseType>(e);
                                }
                            }
                            else
                            {
                                _log.Info("all excecise types retreived from the db, update local data store");
                                this.ExerciseTypes = (from tt in types select tt).ToArray();
                            }
                            _log.Info("cache extypes to static var");
                            HomeViewModel.cachedTypes = ExerciseTypes;

                        });
                }
            }
        }
		private static void SyncRiskCodes(HttpClient httpClient, IRepository consoleRepository)
		{
			DatabaseInit._LogHandler.WriteLog("SyncRiskCodes()", LogSeverity.Information, LogCategory.DataAccess);

			var response = httpClient.GetAsync("rest/api/riskcode").Result;

			if (response.IsSuccessStatusCode)
			{
				var serviceRisks = response.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.RiskCode>>().Result;

				foreach (var serviceRisk in serviceRisks)
				{
					if (!consoleRepository.Query<RiskCode>()
					                      .Any(crc => crc.Code == serviceRisk.Code))
					{
						consoleRepository.Add(new RiskCode
						{
							Code = serviceRisk.Code,
							Name = serviceRisk.Name
						});
					}
					else
					{
						var consoleRisk = consoleRepository.Query<RiskCode>()
						                                   .FirstOrDefault(crc => crc.Code == serviceRisk.Code);

						if (consoleRisk != null)
						{
							consoleRisk.Code = serviceRisk.Code;
							consoleRisk.Name = serviceRisk.Name;

							consoleRepository.Attach(consoleRisk);
						}
					}
				}
			}
			else
			{
				DatabaseInit._LogHandler.WriteLog("Get rest/api/riskcode failed", LogSeverity.Warning, LogCategory.DataAccess);
			}
		}
		private static void SyncUnderwriters(HttpClient httpClient, IRepository consoleRepository)
		{
			DatabaseInit._LogHandler.WriteLog("SyncUnderwriters()", LogSeverity.Information, LogCategory.DataAccess);

			var response = httpClient.GetAsync("rest/api/underwriter").Result;

			if (response.IsSuccessStatusCode)
			{
				var serviceUnderwriters = response.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.Underwriter>>().Result;

				foreach (var serviceUnderwriter in serviceUnderwriters)
				{
					if (!consoleRepository.Query<Underwriter>().Any(cu => cu.Code == serviceUnderwriter.Code))
					{
						consoleRepository.Add(new Underwriter
						{
							Code = serviceUnderwriter.Code,
							Name = serviceUnderwriter.Name
						});
					}
					else
					{
						var consoleUnderwriter = consoleRepository.Query<Underwriter>()
						                                          .FirstOrDefault(b => b.Code == serviceUnderwriter.Code);

						if (consoleUnderwriter != null)
						{
							consoleUnderwriter.Code = serviceUnderwriter.Code;
							consoleUnderwriter.Name = serviceUnderwriter.Name;

							consoleRepository.Attach(consoleUnderwriter);
						}
					}
				}
			}
			else
			{
				DatabaseInit._LogHandler.WriteLog("Get rest/api/underwriter failed", LogSeverity.Warning, LogCategory.DataAccess);
			}
		}
		private static void SyncOffices(HttpClient httpClient, IRepository consoleRepository)
		{
			DatabaseInit._LogHandler.WriteLog("SyncOffices()", LogSeverity.Information, LogCategory.DataAccess);

			var response = httpClient.GetAsync("rest/api/office").Result;

			if (response.IsSuccessStatusCode)
			{
				var serviceOffices = response.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.Office>>().Result;

				foreach (var serviceOffice in serviceOffices.Where(so => !consoleRepository.Query<Office>()
				                                                                           .Any(co => co.Id == so.Code)))
				{
					consoleRepository.Add(new Office
					{
						Id = serviceOffice.Code,
						Name = serviceOffice.Name,
						Title = serviceOffice.Name
					});
				}
			}
			else
			{
				DatabaseInit._LogHandler.WriteLog("Get rest/api/office failed", LogSeverity.Warning, LogCategory.DataAccess);
			}
		}
		private static void SyncCOBs(HttpClient httpClient, IRepository consoleRepository)
		{
			DatabaseInit._LogHandler.WriteLog("SyncCOBs()", LogSeverity.Information, LogCategory.DataAccess);

			var response = httpClient.GetAsync("rest/api/cob").Result;

			if (response.IsSuccessStatusCode)
			{
				var serviceCOBs = response.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.COB>>().Result;

				foreach (var serviceCOB in serviceCOBs.Where(sc => !consoleRepository.Query<COB>()
				                                                                     .Any(cc => cc.Id == sc.Code)))
				{
					consoleRepository.Add(new COB
					{
						Id = serviceCOB.Code,
						Narrative = serviceCOB.Name
					});
				}
			}
			else
			{
				DatabaseInit._LogHandler.WriteLog("Get rest/api/cob failed", LogSeverity.Warning, LogCategory.DataAccess);
			}
		}
		private static void SyncNonLondonBrokers(HttpClient httpClient, IRepository consoleRepository)
		{
            DatabaseInit._LogHandler.WriteLog("SyncNonLondonBrokers()", LogSeverity.Information, LogCategory.DataAccess);

			var responseOffices = httpClient.GetAsync("rest/api/office").Result;

		    if (responseOffices.IsSuccessStatusCode)
		    {
		        var serviceOffices = responseOffices.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.Office>>().Result;

		        foreach (var serviceOffice in serviceOffices)
		        {
                    DatabaseInit._LogHandler.WriteLog("SyncNonLondonBrokers()_" + serviceOffice.Code, LogSeverity.Information, LogCategory.DataAccess);

                    var responseNonLondonBroker = httpClient.GetAsync("rest/api/NonLondonBroker?office=" + serviceOffice.Code).Result;

                    if (responseNonLondonBroker.IsSuccessStatusCode)
                    {
                        var serviceNonLondonBrokers = responseNonLondonBroker.Content.ReadAsAsync<IEnumerable<Validus.Services.Models.NonLondonBroker>>().Result;

                        foreach (var serviceNonLondonBroker in serviceNonLondonBrokers)
                        {
                            if (serviceNonLondonBroker.Code == "NA") continue;
                            if (!consoleRepository.Query<NonLondonBroker>().Any(cb => cb.Code == serviceNonLondonBroker.Code))
                            {
                                consoleRepository.Add(new NonLondonBroker
                                {
                                    Name = serviceNonLondonBroker.Name,
                                    Code = serviceNonLondonBroker.Code,
                                });
                            }
                            else
                            {
                                var consoleNonLondonBroker = consoleRepository.Query<NonLondonBroker>()
                                                                     .FirstOrDefault(cb => cb.Code == serviceNonLondonBroker.Code);

                                if (consoleNonLondonBroker != null)
                                {
                                    consoleNonLondonBroker.Code = serviceNonLondonBroker.Code;
                                    consoleNonLondonBroker.Name = serviceNonLondonBroker.Name;

                                    consoleRepository.Attach(consoleNonLondonBroker);
                                }
                            }
                        }
                    }
                    else
                    {
                        DatabaseInit._LogHandler.WriteLog("Get rest/api/NonLondonBroker failed_" + serviceOffice.Code, LogSeverity.Warning, LogCategory.DataAccess);
                    } 
		        }

                if (!consoleRepository.Query<NonLondonBroker>().Any(cb => cb.Code == "NA"))
                {
                    consoleRepository.Add(new NonLondonBroker
                    {
                        Name = "Not Applicable",
                        Code = "NA",
                    });
                }


		    }
            else
            {
                DatabaseInit._LogHandler.WriteLog("Get rest/api/NonLondonBroker failed", LogSeverity.Warning, LogCategory.DataAccess);
            } 
		  
                
            
		}
        private void SetupSubstitutions()
        {
            uow = Substitute.For<IUnitOfWork>();

            tokenService = Substitute.For<IRepository<ApiToken>>();
            tokenService.Query().Returns(FakeTokens);

            uow.GetRepository<ApiToken>().Returns(tokenService);
        }