コード例 #1
0
        public VocabularyValue AddGlobalValue(VocabularyValue value)
        {
            var mappedValue    = Mapper.Map <DbVocabularyValue>(value);
            var newDbValue     = _vocabularyValuesRepository.Insert(mappedValue);
            var newMappedValue = Mapper.Map <VocabularyValue>(newDbValue);

            return(newMappedValue);
        }
コード例 #2
0
        /// <summary>
        /// Return external vocabulary values
        /// </summary>
        /// <returns>Collection of vocabulary values</returns>
        public IEnumerable <VocabularyValue> GetValuesExtended(string search, string elementName)
        {
            try
            {
                var userName = "******";

                var request = elementName == "Country" ?
                              String.Format("http://api.geonames.org/search?username={0}&name_startsWith={1}&featureCode=PCLI", userName, search) :
                              String.Format("http://api.geonames.org/search?username={0}&name_startsWith={1}&style=FULL", userName, search);

                var xml     = XElement.Load(request);
                var results = xml.Descendants("geoname");

                if (elementName != "Country")
                {
                    var allowedLocationTypes = _locationTypeCodes.SelectMany(l => l.Value).ToList();

                    return
                        (results.
                         Where(g => allowedLocationTypes.Contains(g.Element("fcode").Value)).
                         Select(g =>
                    {
                        var continent =
                            _continentCodes.Any(l => !string.IsNullOrEmpty(g.Element("continentCode")?.Value) && l.Value == g.Element("continentCode").Value) ? _continentCodes.Single(l => l.Value == g.Element("continentCode").Value).Key.ToString().Replace("_", " ") : string.Empty;
                        var locationType =
                            _locationTypeCodes.Any(l => !string.IsNullOrEmpty(g.Element("fcode")?.Value) && l.Value.Contains(g.Element("fcode")?.Value)) ? _locationTypeCodes.Single(l => l.Value.Contains(g.Element("fcode").Value)).Key.ToString().Replace("_", " ") : string.Empty;
                        var country = g.Element("countryName").Value;
                        var name = g.Element("name").Value;
                        var item = new VocabularyValue
                        {
                            Value = name,
                            FullValue = $"{name} ({locationType}, {country}, {continent})",
                            Uri = string.Empty,
                            Country = country,
                            ContinentName = continent,
                            LocationType = locationType
                        };

                        return item;
                    }).
                         DistinctBy(g => new { g.Value, g.ContinentName, g.Country, g.LocationType }).
                         ToList());
                }
                else
                {
                    return(results.Select(g => new VocabularyValue {
                        Value = g.Element("name").Value, Uri = string.Empty
                    }).ToList());
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
コード例 #3
0
        public VocabularyValue Save(VocabularyValue value)
        {
            VocabularyValue result;

            if (value.Id == 0)
            {
                result = _extensibleVocabularyService.AddGlobalValue(value);
            }
            else
            {
                _extensibleVocabularyService.UpdateGlobalValue(value);
                result = value;
            }

            return(result);
        }
コード例 #4
0
        public void UpdateGlobalValue(VocabularyValue value)
        {
            var oldValue    = _vocabularyValuesRepository.FindWithDetaching(v => v.Id == value.Id);
            var mappedValue = Mapper.Map <DbVocabularyValue>(value);

            if (oldValue != null)
            {
                var context  = new DAL.DbContexts.ApplicationDbContext();
                var typeName = mappedValue.Type.Replace("Vocabulary", "");
                var type     = Type.GetType("CAFE.DAL.Models.Db" + typeName + ", CAFE.DAL");

                MethodInfo method        = typeof(ContextExtensions).GetMethod("GetTableNameByDbContext");
                MethodInfo genericMethod = method.MakeGenericMethod(type);
                var        tableName     = (string)genericMethod.Invoke(this, new [] { context });

                var updateResult =
                    context.
                    Database.
                    ExecuteSqlCommand($"update {tableName} set Value = '{mappedValue.Value}' where Value = '{oldValue.Value}'");
            }

            _vocabularyValuesRepository.Update(mappedValue);
        }