public CategoryViewModel(Category entity)
 {
     Id = entity.Id;
     Name = entity.Name;
     Description = entity.Description;
     Queries = entity.Queries.Select(q => new PropertyQueryInfoViewModel(q)).ToList();
     OrganisationalUnits = entity.OrganisationalUnits.Select(o => new OrganisationalUnitInfoViewModel(o)).ToList();
     GroupCategory = new GroupCategoryViewModel(entity.GroupCategory, false);
 }
        public Category ToEntity(Category existing = null)
        {
            Category entity = (existing != null ? existing : new Category());

            entity.Id = this.Id;
            entity.Name = this.Name;
            entity.Description = this.Description;
            entity.Queries = this.Queries.Select(q => q.ToEntity(existing != null ? existing.Queries.FirstOrDefault(exQ => exQ.Id == q.Id) : null)).ToList();
            entity.OrganisationalUnits = this.OrganisationalUnits.Select(o => o.ToEntity(existing != null ? existing.OrganisationalUnits.FirstOrDefault(exO => exO.Id == o.Id) : null)).ToList();

            return entity;
        }
        public CategoryWithUnusedViewModel(Category category, List<OrganisationalUnit> allOrganisationalUnits, List<PropertyQueryGroup> allPropertyQueryGroups)
        {
            Category = new CategoryViewModel(category);
            AllOrganisationalUnits = allOrganisationalUnits.Select(o => new OrganisationalUnitInfoViewModel(o)).ToList();
            AllPropertyQueryGroups = allPropertyQueryGroups.Select(p => new PropertyQueryGroupViewModel(p)).ToList();

            for (int i = 0; i < AllOrganisationalUnits.Count; i++)
            {
                Shared.OrganisationalUnitInfoViewModel existing = Category.OrganisationalUnits.Find(cou => cou.OrganisationalUnitId == AllOrganisationalUnits[i].OrganisationalUnitId);
                if (existing != null)
                {
                    AllOrganisationalUnits[i] = new OrganisationalUnitInfoViewModel(existing);
                    AllOrganisationalUnits[i].Use = true;
                }
                //ou.Use = (Category.OrganisationalUnits.Find(cou => cou.OrganisationalUnitId == ou.OrganisationalUnitId) != null);
            }

            for (int i = 0; i < AllPropertyQueryGroups.Count; i++)
            {
                for (int j = 0; j < AllPropertyQueryGroups[i].Queries.Count; j++)
                {
                    Shared.PropertyQueryInfoViewModel existing = Category.Queries.Find(q => q.QueryId == AllPropertyQueryGroups[i].Queries[j].QueryId);
                    if (existing != null)
                    {
                        AllPropertyQueryGroups[i].Queries[j] = new PropertyQueryInfoViewModel(existing, true);
                        AllPropertyQueryGroups[i].AnyQueriesToUse = true;
                    }
                }
            }
            /*
            foreach (PropertyQueryGroupViewModel qg in AllPropertyQueryGroups)
            {
                foreach (PropertyQueryViewModel q in qg.Queries)
                {
                    q.Use = (Category.Queries.Find(cq => cq.WebServiceName == q.WebServiceName && cq.QueryId == q.QueryId) != null);
                    if (q.Use) { qg.AnyQueriesToUse = true; }
                }
            }
            */
        }
        public List<PropertyQueryWithResults> GetWebServicePropertyResults(Category category, List<OrganisationalUnitInfo> organisationalUnits) //List<string> queryIds, List<string> organisationalUnitIds) //List<PropertyQuery> queries, List<OrganisationalUnit> organisationalUnits)
        {
            //Get id's from All KpiQuestions and OrganisationalUnits in parameter
            //and compund to an unique cacheKey
            
            var queryIds = from q in category.Queries
                           select q.QueryId;
            var organisationalUnitIds = from ou in organisationalUnits
                                        select ou.OrganisationalUnitId;
            

            //adding all KPIQuestionId + ouIds 
            var cacheKey = "PropertyResults" + queryIds.Aggregate("", (current, kpi) => current + kpi);
            cacheKey = organisationalUnitIds.Aggregate(cacheKey, (current, ouId) => current + ouId);

            if (_cache.HasValue(cacheKey))
            {
                return (List<PropertyQueryWithResults>)_cache.GetCache(cacheKey);
            }

            var returnValue = _townWebService.GetPropertyResults(category.Queries.ToList(), organisationalUnits); //queryIds.ToList(), organisationalUnitIds.ToList());
            _cache.SetCache(cacheKey, returnValue, _settings.CacheSeconds_PropertyResult);

            return returnValue;
        }
        public bool InsertCategory(Category category)
        {
            try
            {
                _unitOfWork.CategoriesRepository.Insert(category);
                _unitOfWork.Save();
                return true;
            }
            catch { }

            return false;
        }
        public bool UpdateCategory(Category category, List<OrganisationalUnitInfo> earlierOrganisationalUnits = null, List<PropertyQueryInfo> earlierPropertyQueries = null)
        {
            try
            {
                if (earlierOrganisationalUnits != null)
                {
                    //do some checks that related associations are not removed in category (otherwise delete them)
                    List<OrganisationalUnitInfo> ouToRemove = earlierOrganisationalUnits.Where(eou => !category.OrganisationalUnits.Any(ou => ou.Id == eou.Id)).ToList();
                    foreach (OrganisationalUnitInfo ou in ouToRemove)
                    {
                        _unitOfWork.OrganisationalUnitInfoRepository.Delete(ou);
                    }
                }
                if (earlierPropertyQueries != null)
                { 
                    List<PropertyQueryInfo> pqToRemove = earlierPropertyQueries.Where(eq => !category.Queries.Any(q => q.Id == eq.Id)).ToList();
                    foreach (PropertyQueryInfo pq in pqToRemove)
                    {
                        _unitOfWork.PropertyQueryInfoRepository.Delete(pq);
                    }
                }

                _unitOfWork.CategoriesRepository.Update(category);
                _unitOfWork.Save();
                return true;
            }
            catch (Exception e)
            {
                e = e;
            }

            return false;
        }
        public bool DeleteCategory(Category category)
        {
            try
            {
                _unitOfWork.CategoriesRepository.Delete(category);
                _unitOfWork.Save();
                return true;
            }
            catch { }

            return false;
        }
 public IEnumerable<OperationalUnit> GetTownOperators(Municipality municipality, Category category)
 {
     //Method added becouse project would not compile without it!!
     throw new NotImplementedException();
 }
        public HttpResponseMessage NewCategory(HttpRequestMessage request, int groupCategoryId)
        {
            GroupCategory groupCategory = _service.GetGroupCategory(groupCategoryId);
            if (groupCategory != null)
            {
                Category category = new Category()
                {
                    GroupCategory = groupCategory
                };
                List<OrganisationalUnit> allOrganisationalUnits = _service.GetWebServiceOrganisationalUnits();
                List<PropertyQueryGroup> allPropertyQueryGroups = _service.GetWebServicePropertyQueries();
                CategoryWithUnusedViewModel model = new CategoryWithUnusedViewModel(category, allOrganisationalUnits, allPropertyQueryGroups);
                return request.CreateResponse<CategoryWithUnusedViewModel>(HttpStatusCode.OK, model);
            }

            return new HttpResponseMessage(HttpStatusCode.NotFound);
        }
 public List<OperationalUnit> GetTownOperators(Municipality municipality, Category category)
 {
     throw new NotImplementedException();
 }
Example #11
0
 //Methods
 public List<OperationalUnit> GetTownOperators(Municipality municipality, Category category)
 {
     return _townWebService.GetTownOperators(municipality, category);
 }