Ejemplo n.º 1
0
        protected BasketRepository CreateBasketRepository(IDataManager manager, CountryRepository countryRepository, IMonitor monitor)
        {
            var basketInfos        = manager.GetAllBaskets();
            var countryBasketInfos = manager.GetAllCountryBaskets().ToDictionary(x => x.Id);
            var regionBasketInfos  = manager.GetAllRegionBaskets().ToDictionary(x => x.Id);

            var baskets = new List <IBasket>();

            foreach (var basketInfo in basketInfos)
            {
                var basketOpt = monitor.DefaultIfFails <IBasket>("Creating a basket of the \"" + basketInfo.Type + "\" type and \"" + basketInfo.Id + "\" ID.", delegate
                {
                    switch (basketInfo.Type)
                    {
                    case "country":
                        {
                            CountryBasketInfo countryBasketInfo;
                            if (countryBasketInfos.TryGetValue(basketInfo.Id, out countryBasketInfo))
                            {
                                var country = countryRepository.GetCountry(countryBasketInfo.IsoCountryCode);
                                return(new CountryBasket(basketInfo.Id, country));
                            }
                            else
                            {
                                throw new ApplicationException("There is no country basket with the \"" + basketInfo.Id + "\" ID.");
                            }
                        }

                    case "region":
                        {
                            RegionBasketInfo regionBasketInfo;
                            if (regionBasketInfos.TryGetValue(basketInfo.Id, out regionBasketInfo))
                            {
                                var result = this.xmlDeserializer.ReadBasket(basketInfo.Id, regionBasketInfo.DefinitionXml, countryRepository);
                                return(result);
                            }
                            else
                            {
                                throw new ApplicationException("There is no region basket with the \"" + basketInfo.Id + "\" ID.");
                            }
                        }

                    default:
                        {
                            throw new ApplicationException("Unexpected basket type \"" + basketInfo.Type + "\".");
                        }
                    }
                });
                if (basketOpt == null)
                {
                    continue;
                }
                baskets.Add(basketOpt);
            }


            var repository = new BasketRepository(baskets);

            return(repository);
        }
Ejemplo n.º 2
0
        public IssuerRepository(IMonitor monitor, IEnumerable <SecurityInfo> securities)
        {
            var grouppedByIssuerId = securities.GroupBy(x => x.IssuerId);

            this.byId = new Dictionary <String, Issuer>();
            foreach (var group in grouppedByIssuerId)
            {
                var issuer = monitor.DefaultIfFails("Creating an issuer (ID: " + group.Key + ").", delegate
                {
                    var id = group.Key;
                    if (String.IsNullOrWhiteSpace(id))
                    {
                        throw new ApplicationException("Issuer ID cannot be empty.");
                    }

                    var grouppedByName = group.GroupBy(x => x.IssuerName);
                    if (grouppedByName.Count() > 1)
                    {
                        throw new ApplicationException("There is more than one issuer name sharing the same ID: \"" + String.Join("\", \"", grouppedByName.Select(x => x.Key).ToArray()) + "\"");
                    }

                    var firstOpt = grouppedByName.FirstOrDefault();
                    if (firstOpt == null)
                    {
                        throw new ApplicationException("There is no issuers associated with this ID.");
                    }

                    var name = firstOpt.Key;
                    if (String.IsNullOrWhiteSpace(name))
                    {
                        throw new ApplicationException("Issuer name cannot be empty.");
                    }

                    return(new Issuer(id, name));
                });

                this.byId.Add(issuer.Id, issuer);
            }
        }
Ejemplo n.º 3
0
        private IEnumerable <ISecurity> CreateSecurities(
            IEnumerable <SecurityInfo> securityInfos,
            CountryRepository countryRepository,
            IMonitor monitor
            )
        {
            var result = new List <ISecurity>();

            foreach (var securityInfo in securityInfos)
            {
                var securityOpt = monitor.DefaultIfFails("Validating security", delegate
                {
                    if (String.IsNullOrEmpty(securityInfo.Name))
                    {
                        throw new ApplicationException("Name is not specified.");
                    }
                    if (String.IsNullOrEmpty(securityInfo.ShortName))
                    {
                        throw new ApplicationException("Short name is not specified.");
                    }

                    ISecurity security;

                    if (String.IsNullOrWhiteSpace(securityInfo.LookThruFund))
                    {
                        Country country = null;
                        if (String.IsNullOrEmpty(securityInfo.IsoCountryCode))
                        {
                            throw new ApplicationException("Country code is not specified.");
                        }
                        try
                        {
                            country = countryRepository.GetCountry(securityInfo.IsoCountryCode);
                        }
                        catch (CountryNotFoundException)
                        {
                            country = new Country(securityInfo.IsoCountryCode, securityInfo.AsecCountryName);
                        }
                        var stock = new CompanySecurity(
                            securityInfo.Id,
                            securityInfo.Ticker,
                            securityInfo.ShortName,
                            securityInfo.Name,
                            country,
                            securityInfo.IssuerId,
                            securityInfo.SecurityType,
                            securityInfo.Currency,
                            securityInfo.Isin,
                            securityInfo.IsoCountryCode
                            );
                        security = stock;
                    }
                    else
                    {
                        var fund = new Fund(
                            securityInfo.Id,
                            securityInfo.Name,
                            securityInfo.ShortName,
                            securityInfo.Ticker,
                            securityInfo.IssuerId,
                            securityInfo.SecurityType,
                            securityInfo.Currency,
                            securityInfo.Isin,
                            securityInfo.IsoCountryCode
                            );
                        security = fund;
                    }
                    return(security);
                });



                if (securityOpt == null)
                {
                    continue;
                }
                result.Add(securityOpt);
            }
            return(result);
        }