/// <summary>
        /// Function to toggle from favorite to not favorite and vice versa
        /// </summary>
        /// <param name="FlagId"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool FavoriteFlag(int flagId, String userId)
        {
            try
            {

                if (AddedByUser(userId, flagId))
                {
                    // flag is added by user changes in Flag table only

                    _flag = new Flag();
                    _flag = _sDACEntities.Flags.First(i => i.FlagId == flagId);

                    if (IsFav(flagId))
                    {
                        // make to unfavorite
                        _flag.IsFavorite = false;
                    }
                    else
                    {
                        // make to favorite
                        _flag.IsFavorite = true;
                    }
                    _sDACEntities.SaveChanges();
                    return true;
                }
                else
                {

                    if (IsInPublicFavorite(flagId, userId))
                    {
                        // already exist i.e it is favorite delete it

                        _publicFavorite = (from _publicFav in _sDACEntities.PublicFavorites where (_publicFav.UserId == userId && _publicFav.FlagId == flagId) select _publicFav).First();
                        _sDACEntities.DeleteObject(_publicFavorite);
                        _sDACEntities.SaveChanges();
                        return true;
                    }
                    else
                    {
                        _publicFavorite = new PublicFavorite();
                        _publicFavorite.UserId = userId;
                        _publicFavorite.FlagId = flagId;

                        _sDACEntities.AddToPublicFavorites(_publicFavorite);
                        _sDACEntities.SaveChanges();
                         return true;

                    }

                }

            }
            catch (Exception Ex)
            {
                return false;
            }
        }
        /// <summary>
        /// function to delete the flag by flag id
        /// </summary>
        /// <param name="FlagId"></param>
        /// <returns></returns>
        public bool DeleteFlag(int flagId)
        {
            _flag = new Flag();
            try
            {

                _flag = _sDACEntities.Flags.First(i => i.FlagId == flagId);
                _flag.IsDeleted = true;
                _sDACEntities.SaveChanges();
                 return true;
            }
            catch (Exception Ex)
            {
                return false;
            }
        }
        /// <summary>
        /// function to add the flag
        /// </summary>
        /// <param name="FlagName"></param>
        /// <param name="FlagDescription"></param>
        /// <param name="Keyword"></param>
        /// <param name="DataElementId"></param>
        /// <param name="ConditionId"></param>
        /// <param name="ValueSet1"></param>
        /// <param name="ValueSet2"></param>
        /// <param name="UserId"></param>
        /// <param name="UserName"></param>
        /// <param name="IsPublic"></param>
        /// <returns></returns>
        public bool AddFlag(String flagName, String flagDescription, String keyword, int dataElementId, int conditionId, String valueSet1, String valueSet2, String userId, String userName, bool isPublic)
        {
            _flag = new Flag();
            _flag.FlagName = flagName;
            _flag.FlagDescription =flagDescription;
            _flag.Keyword = keyword;
            _flag.IsPublic = isPublic;
            _flag.DataElementId = dataElementId;
            _flag.ConditionId = conditionId;
            _flag.ValueSet1 = valueSet1;
            _flag.ValueSet2 = valueSet2;
            _flag.UserId =userId;
            _flag.CreatedBy =userName;
            _flag.CreatedDate = DateTime.Now;
            _flag.ModifiedBy = userName;
            _flag.ModifiedDate = DateTime.Now;

            if (isPublic)
            {
                if (FlagIsExistForAdmin(flagName, userId))
                    return false;
            }
            else
                if (FlagIsExist(flagName, userId))
                {
                    return false;
                }

                _sDACEntities.AddToFlags(_flag);
                _sDACEntities.SaveChanges();
                return true;
        }
        /// <summary>
        /// function to update flag
        /// </summary>
        /// <param name="FlagId"></param>
        /// <param name="FlagName"></param>
        /// <param name="FlagDescription"></param>
        /// <param name="Keyword"></param>
        /// <param name="DataElementId"></param>
        /// <param name="ConditionId"></param>
        /// <param name="ValueSet1"></param>
        /// <param name="ValueSet2"></param>
        /// <param name="UserId"></param>
        /// <param name="UserName"></param>
        /// <param name="IsPublic"></param>
        /// <returns></returns>
        public bool UpdateFlag(int flagId, String flagName, String flagDescription, String keyword, int dataElementId, int conditionId, String valueSet1, String valueSet2, String userId, String userName, bool isPublic)
        {
            try
            {
                if (FlagHasSameName(flagId, flagName))
                {
                }
                else
                    if (isPublic)
                    {
                        if (FlagIsExistForAdmin(flagName, userId))
                            return false;
                    }
                    else
                        if (FlagIsExist(flagName, userId))
                    {
                        return false;
                    }
                _flag = new Flag();

                _flag = _sDACEntities.Flags.First(i => i.FlagId == flagId);

                _flag.FlagName = flagName;
                _flag.FlagDescription = flagDescription;
                _flag.Keyword = keyword;
                _flag.IsPublic = isPublic;
                _flag.DataElementId = dataElementId;
                _flag.ConditionId = conditionId;
                _flag.ValueSet1 = valueSet1;
                _flag.ValueSet2 = valueSet2;

                _flag.ModifiedBy = userName;
                _flag.ModifiedDate = DateTime.Now;

                _sDACEntities.SaveChanges();
                return true;
            }
            catch (Exception Ex)
            {
                return false;
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Flags EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToFlags(Flag flag)
 {
     base.AddObject("Flags", flag);
 }
 /// <summary>
 /// Create a new Flag object.
 /// </summary>
 /// <param name="flagId">Initial value of the FlagId property.</param>
 /// <param name="flagName">Initial value of the FlagName property.</param>
 /// <param name="flagDescription">Initial value of the FlagDescription property.</param>
 /// <param name="isPublic">Initial value of the IsPublic property.</param>
 /// <param name="dataElementId">Initial value of the DataElementId property.</param>
 /// <param name="conditionId">Initial value of the ConditionId property.</param>
 /// <param name="valueSet1">Initial value of the ValueSet1 property.</param>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="isFavorite">Initial value of the IsFavorite property.</param>
 /// <param name="createdBy">Initial value of the CreatedBy property.</param>
 /// <param name="createdDate">Initial value of the CreatedDate property.</param>
 /// <param name="modifiedBy">Initial value of the ModifiedBy property.</param>
 /// <param name="modifiedDate">Initial value of the ModifiedDate property.</param>
 public static Flag CreateFlag(global::System.Int32 flagId, global::System.String flagName, global::System.String flagDescription, global::System.Boolean isPublic, global::System.Int32 dataElementId, global::System.Int32 conditionId, global::System.String valueSet1, global::System.String userId, global::System.Boolean isFavorite, global::System.String createdBy, global::System.DateTime createdDate, global::System.String modifiedBy, global::System.DateTime modifiedDate)
 {
     Flag flag = new Flag();
     flag.FlagId = flagId;
     flag.FlagName = flagName;
     flag.FlagDescription = flagDescription;
     flag.IsPublic = isPublic;
     flag.DataElementId = dataElementId;
     flag.ConditionId = conditionId;
     flag.ValueSet1 = valueSet1;
     flag.UserId = userId;
     flag.IsFavorite = isFavorite;
     flag.CreatedBy = createdBy;
     flag.CreatedDate = createdDate;
     flag.ModifiedBy = modifiedBy;
     flag.ModifiedDate = modifiedDate;
     return flag;
 }