//	public static List<CodeItem> SearchAsCodeItem( string keyword, int startingPageNbr, int pageSize, ref int totalRows )
        //	{
        //		List<ThisEntity> list = Search( keyword, startingPageNbr, pageSize, ref totalRows );
        //		List<CodeItem> codes = new List<CodeItem>();
        //		foreach (ThisEntity item in list)
        //		{
        //			codes.Add(new CodeItem() {
        //				Id = item.Id,
        //				Name = item.Name,
        //				Description = item.Description
        //			});
        //		}
        //		return codes;
        //}
        public static List <object> Autocomplete(string keyword, int maxTerms = 25)
        {
            string where = "";
            int totalRows = 0;

            //SetKeywordFilter( keyword, true, ref where );
            string keywords = ServiceHelper.HandleApostrophes(keyword);

            if (keywords.IndexOf("%") == -1)
            {
                keywords = "%" + keywords.Trim() + "%";
            }
            where = string.Format(" (base.name like '{0}') ", keywords);


            if (UtilityManager.GetAppKeyValue("usingElasticAssessmentSearch", false))
            {
                return(ElasticServices.AssessmentAutoComplete(keyword, maxTerms, ref totalRows));
            }
            else
            {
                SetKeywordFilter(keyword, true, ref where);
                return(EntityMgr.Autocomplete(where, 1, maxTerms, ref totalRows));
            }
        }
        public static List <ThisEntity> DoSearch(MainSearchInput data, ref int totalRows)
        {
            string where = "";
            List <string> competencies = new List <string>();
            int           userId       = 0;
            AppUser       user         = AccountServices.GetCurrentUser();

            if (user != null && user.Id > 0)
            {
                userId = user.Id;
            }

            //only target full entities
            where = " ( base.EntityStateId = 3 ) ";

            SetKeywordFilter(data.Keywords, false, ref where);

            SearchServices.SetSubjectsFilter(data, CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE, ref where);

            SearchServices.HandleCustomFilters(data, 60, ref where);

            SetPropertiesFilter(data, ref where);
            SearchServices.SetRolesFilter(data, ref where);
            SearchServices.SetBoundariesFilter(data, ref where);
            //CIP
            SetFrameworksFilter(data, ref where);
            //Competencies
            SetCompetenciesFilter(data, ref where, ref competencies);

            LoggingHelper.DoTrace(5, "AssessmentServices.Search(). Filter: " + where);
            return(EntityMgr.Search(where, data.SortOrder, data.StartPage, data.PageSize, ref totalRows));
        }
        public static ThisEntity GetByCtid(string ctid)
        {
            ThisEntity entity = new ThisEntity();

            if (string.IsNullOrWhiteSpace(ctid))
            {
                return(entity);
            }

            return(EntityMgr.GetByCtid(ctid));
        }
        public static ThisEntity GetDetailByCtid(string ctid, bool skippingCache = false)
        {
            ThisEntity entity = new ThisEntity();

            if (string.IsNullOrWhiteSpace(ctid))
            {
                return(entity);
            }
            var assessment = EntityMgr.GetByCtid(ctid);

            return(GetDetail(assessment.Id, skippingCache));
        }
        public bool Import(ThisEntity entity, ref SaveStatus status)
        {
            //do a get, and add to cache before updating
            if (entity.Id > 0)
            {
                //no need to get and cache if called from batch import - maybe during day, but likelihood of issues is small?
                if (UtilityManager.GetAppKeyValue("learningOppCacheMinutes", 0) > 0)
                {
                    if (System.DateTime.Now.Hour > 7 && System.DateTime.Now.Hour < 18)
                    {
                        GetDetail(entity.Id);
                    }
                }
            }
            bool          isValid  = new EntityMgr().Save(entity, ref status);
            List <string> messages = new List <string>();

            if (entity.Id > 0)
            {
                if (UtilityManager.GetAppKeyValue("learningOppCacheMinutes", 0) > 0)
                {
                    CacheManager.RemoveItemFromCache("asmt", entity.Id);
                }

                if (UtilityManager.GetAppKeyValue("delayingAllCacheUpdates", false) == false)
                {
                    //update cache
                    ThreadPool.QueueUserWorkItem(UpdateCaches, entity);

                    //new CacheManager().PopulateEntityRelatedCaches( entity.RowId );
                    //update Elastic - this if makes no sense, it is either update elastic immediate or add to pending
                    //if ( UtilityManager.GetAppKeyValue( "updatingElasticIndexImmediately", false ) )
                    //    ElasticHelper.Assessment_UpdateIndex( entity.Id );
                    //else
                    //{
                    //    new SearchPendingReindexManager().Add( 3, entity.Id, 1, ref messages );
                    //    if ( messages.Count > 0 )
                    //        status.AddWarningRange( messages );
                    //}
                }
                else
                {
                    new SearchPendingReindexManager().Add(CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE, entity.Id, 1, ref messages);
                    new SearchPendingReindexManager().Add(CodesManager.ENTITY_TYPE_ORGANIZATION, entity.OwningOrganizationId, 1, ref messages);
                    if (messages.Count > 0)
                    {
                        status.AddWarningRange(messages);
                    }
                }
            }

            return(isValid);
        }
        public bool Import(ThisEntity entity, ref SaveStatus status)
        {
            //do a get, and add to cache before updating
            if (entity.Id > 0)
            {
                //need to force caching here
                var detail = GetDetail(entity.Id);
            }
            bool          isValid  = new EntityMgr().Save(entity, ref status);
            List <string> messages = new List <string>();

            if (entity.Id > 0)
            {
                CacheManager.RemoveItemFromCache("asmt", entity.Id);

                if (UtilityManager.GetAppKeyValue("delayingAllCacheUpdates", false) == false)
                {
                    //update cache
                    new CacheManager().PopulateEntityRelatedCaches(entity.RowId);
                    //update Elastic
                    if (UtilityManager.GetAppKeyValue("usingElasticAssessmentSearch", false))
                    {
                        ElasticServices.Assessment_UpdateIndex(entity.Id);
                    }
                    else
                    {
                        new SearchPendingReindexManager().Add(3, entity.Id, 1, ref messages);
                        if (messages.Count > 0)
                        {
                            status.AddWarningRange(messages);
                        }
                    }
                }
                else
                {
                    new SearchPendingReindexManager().Add(CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE, entity.Id, 1, ref messages);
                    new SearchPendingReindexManager().Add(CodesManager.ENTITY_TYPE_ORGANIZATION, entity.OwningOrganizationId, 1, ref messages);
                    if (messages.Count > 0)
                    {
                        status.AddWarningRange(messages);
                    }
                }
            }

            return(isValid);
        }
        public static ThisEntity GetDetail(int id, bool skippingCache = false)
        {
            int      cacheMinutes = UtilityManager.GetAppKeyValue("learningOppCacheMinutes", 0);
            DateTime maxTime      = DateTime.Now.AddMinutes(cacheMinutes * -1);
            string   key          = "asmt_" + id.ToString();

            if (skippingCache == false &&
                HttpRuntime.Cache[key] != null &&
                cacheMinutes > 0)
            {
                var cache = ( CachedAssessment )HttpRuntime.Cache[key];
                try
                {
                    if (cache.lastUpdated > maxTime)
                    {
                        LoggingHelper.DoTrace(6, string.Format(thisClassName + ".GetDetail === Using cached version of Asmt, Id: {0}, {1}", cache.Item.Id, cache.Item.Name));
                        return(cache.Item);
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.DoTrace(6, thisClassName + ".GetDetail === exception " + ex.Message);
                }
            }
            else
            {
                LoggingHelper.DoTrace(8, thisClassName + string.Format(".GetDetail === Retrieving full version of Asmt, Id: {0}", id));
            }

            DateTime   start  = DateTime.Now;
            ThisEntity entity = EntityMgr.GetDetails(id);

            DateTime end     = DateTime.Now;
            int      elasped = (end - start).Seconds;

            //Cache the output if more than specific seconds,
            //NOTE need to be able to force it for imports
            //&& elasped > 2
            if (key.Length > 0 && cacheMinutes > 0)
            {
                var newCache = new CachedAssessment()
                {
                    Item        = entity,
                    lastUpdated = DateTime.Now
                };
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Cache[key] != null)
                    {
                        HttpRuntime.Cache.Remove(key);
                        HttpRuntime.Cache.Insert(key, newCache);

                        LoggingHelper.DoTrace(6, string.Format("==={0}.GetDetail $$$ Updating cached version of Asmt, Id: {1}, {2}", thisClassName, entity.Id, entity.Name));
                    }
                    else
                    {
                        LoggingHelper.DoTrace(6, string.Format("==={0}.GetDetail ****** Inserting new cached version of Asmt, Id: {1}, {2}", thisClassName, entity.Id, entity.Name));

                        System.Web.HttpRuntime.Cache.Insert(key, newCache, null, DateTime.Now.AddMinutes(cacheMinutes), TimeSpan.Zero);
                    }
                }
            }
            else
            {
                LoggingHelper.DoTrace(7, string.Format("==={0}.GetDetail $$$$$$ skipping caching of Asmt, Id: {1}, {2}, elasped:{3}", thisClassName, entity.Id, entity.Name, elasped));
            }
            return(entity);
        }
        public static ThisEntity GetBasic(int id)
        {
            ThisEntity entity = EntityMgr.GetBasic(id);

            return(entity);
        }