/// <summary>
        /// function to add aggregate flag
        /// </summary>
        /// <param name="AggregateFlagName"></param>
        /// <param name="AggregateDescription"></param>
        /// <param name="AggregateKeyword"></param>
        /// <param name="IsPublic"></param>
        /// <param name="FlagId"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool AddAggregateFlag(String aggregateFlagName, String aggregateDescription, String aggregateKeyword, bool isPublic, int[] flagId, String userId)
        {
            if (AggregateFlagExist(aggregateFlagName,userId))
            {
                return false;
            }
            else
            {

                _aggregateFlag = new AggregateFlag();

                _aggregateFlag.AggregateFlagName = aggregateFlagName;
                _aggregateFlag.AggregateFlagDescription = aggregateDescription;
                _aggregateFlag.Keyword = aggregateKeyword;
                _aggregateFlag.IsPublic = isPublic;
                _aggregateFlag.UserId = userId;
                _aggregateFlag.CreatedBy = userId;

                _aggregateFlag.CreatedDate = DateTime.Now;
                _aggregateFlag.ModifiedBy = userId;
                _aggregateFlag.ModifiedDate = DateTime.Now;

                _sDACEntities.AddToAggregateFlags(_aggregateFlag);
                _sDACEntities.SaveChanges();

                int AggregateFlagId=GetAggregateFlagById(aggregateFlagName);

                for (int i = 0; i < flagId.Count(); i++)
                {

                        int Flag = flagId[i];
                        if (ExistInAggregateFlag(AggregateFlagId, Flag))
                        {

                        }
                        else
                        {
                            try
                            {

                                _flagAggregateFlag = new FlagAggregateFlag();
                                _flagAggregateFlag.AggregateFlagId = AggregateFlagId;
                                _flagAggregateFlag.FlagId = Flag;
                                _flagAggregateFlag.CreatedBy = userId;
                                _flagAggregateFlag.CreatedDate = DateTime.Now;
                                _flagAggregateFlag.ModifiedBy = userId;
                                _flagAggregateFlag.ModifiedDate = DateTime.Now;
                                _sDACEntities.AddToFlagAggregateFlags(_flagAggregateFlag);
                                _sDACEntities.SaveChanges();
                                _flagAggregateFlag = null;
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                }

                return true;

            }
        }
        /// <summary>
        /// Function to edit the aggregate flag
        /// </summary>
        /// <param name="AggregateFlagId"></param>
        /// <param name="AggregateFlagName"></param>
        /// <param name="AggregateDescription"></param>
        /// <param name="AggregateKeyword"></param>
        /// <param name="IsPublic"></param>
        /// <param name="FlagId"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool EditAddAggregateFlag(int aggregateFlagId, String aggregateFlagName, String aggregateDescription, String aggregateKeyword, bool isPublic, int[] flagId, String userId)
        {
            try
            {
                if (AggregateFlagHasSameName(aggregateFlagId, aggregateFlagName))
                {
                }
                else
                    if (AggregateFlagExist(aggregateFlagName, userId))
                    {
                        return false;
                    }

                _aggregateFlag = new AggregateFlag();
                _aggregateFlag = _sDACEntities.AggregateFlags.First(i => i.AggregateFlagId == aggregateFlagId);
                _aggregateFlag.AggregateFlagName = aggregateFlagName;
                _aggregateFlag.AggregateFlagDescription = aggregateDescription;
                _aggregateFlag.Keyword = aggregateKeyword;
                _aggregateFlag.IsPublic = isPublic;
                _aggregateFlag.ModifiedBy =userId;
                _aggregateFlag.ModifiedDate = DateTime.Now;
                _sDACEntities.SaveChanges();

                // delete all the flags associated with aggregate flag
                DeleteFromFlagAggregateFlagByAggregateFlagId(aggregateFlagId);

                for (int i = 0; i < flagId.Count(); i++)
                {
                    int Flag = flagId[i];
                    if (ExistInAggregateFlag(aggregateFlagId, Flag))
                    {

                    }
                    else
                    {
                        try
                        {
                            _flagAggregateFlag = new FlagAggregateFlag();
                            _flagAggregateFlag.AggregateFlagId = aggregateFlagId;
                            _flagAggregateFlag.FlagId = Flag;
                            _flagAggregateFlag.CreatedBy = userId;
                            _flagAggregateFlag.CreatedDate = DateTime.Now;
                            _flagAggregateFlag.ModifiedBy = userId;
                            _flagAggregateFlag.ModifiedDate = DateTime.Now;
                            _sDACEntities.AddToFlagAggregateFlags(_flagAggregateFlag);
                            _sDACEntities.SaveChanges();
                            _flagAggregateFlag = null;
                        }
                        catch (Exception Ex)
                        {
                        }
                    }

                }

                return true;
            }
            catch (Exception Ex)
            {
                return false;
            }
        }
        /// <summary>
        /// Function to delete the aggregate flag
        /// </summary>
        /// <param name="AggregateFlagId"></param>
        /// <returns></returns>
        public bool DeleteAggregateFlag(int aggregateFlagId)
        {
            _flagAggregateFlag = new FlagAggregateFlag();
            _aggregateFlag = new AggregateFlag();
            try
            {

                var flag = _sDACEntities.FlagAggregateFlags.Where(c => c.AggregateFlagId == aggregateFlagId);
                if (flag.Count() > 0)
                {
                    foreach (var c  in flag)
                    {
                        _sDACEntities.DeleteObject(c);
                    }
                }

                _sDACEntities.SaveChanges();

                _aggregateFlag = _sDACEntities.AggregateFlags.First(i => i.AggregateFlagId == aggregateFlagId);
                _aggregateFlag.IsDelete = true;
                _sDACEntities.SaveChanges();

                return true;
            }
            catch (Exception Ex)
            {
                return false;
            }
        }
        /// <summary>
        /// function to make aggregate flag favorite 
        /// </summary>
        /// <param name="AgggregateFlagId"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool AggregatetFavoriteFlag(int agggregateFlagId, String userId)
        {
            try
            {

                if (AggregatetAddedByUser(userId, agggregateFlagId))
                {
                    // flag is added by user changes in Flag table only
                    _aggregateFlag = new AggregateFlag();
                    _aggregateFlag = _sDACEntities.AggregateFlags.First(i => i.AggregateFlagId == agggregateFlagId);

                    if (IsAggregateIsFav(agggregateFlagId))
                    {
                        // make to unfavorite
                        _aggregateFlag.IsFavorite = false;
                    }
                    else
                    {
                        // make to favorite
                        _aggregateFlag.IsFavorite = true;
                    }
                    _sDACEntities.SaveChanges();
                    return true;
                }
                else
                {
                    if (IsAggregateInPublicFavorite(agggregateFlagId, userId))
                    {
                        // already exist i.e it is favorite delete it

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

                            _sDACEntities.PublicFavorites.AddObject(_publicFavorite);
                            _sDACEntities.SaveChanges();
                            return true;
                        }
                        catch (Exception Ex)
                        {
                            throw Ex;
                        }

                    }

                }

            }
            catch (Exception Ex)
            {
                throw Ex;
               // return false;
            }
        }
 /// <summary>
 /// Create a new AggregateFlag object.
 /// </summary>
 /// <param name="aggregateFlagId">Initial value of the AggregateFlagId property.</param>
 /// <param name="aggregateFlagName">Initial value of the AggregateFlagName property.</param>
 /// <param name="aggregateFlagDescription">Initial value of the AggregateFlagDescription property.</param>
 /// <param name="isPublic">Initial value of the IsPublic property.</param>
 /// <param name="isFavorite">Initial value of the IsFavorite property.</param>
 /// <param name="userId">Initial value of the UserId 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 AggregateFlag CreateAggregateFlag(global::System.Int32 aggregateFlagId, global::System.String aggregateFlagName, global::System.String aggregateFlagDescription, global::System.Boolean isPublic, global::System.Boolean isFavorite, global::System.String userId, global::System.String createdBy, global::System.DateTime createdDate, global::System.String modifiedBy, global::System.DateTime modifiedDate)
 {
     AggregateFlag aggregateFlag = new AggregateFlag();
     aggregateFlag.AggregateFlagId = aggregateFlagId;
     aggregateFlag.AggregateFlagName = aggregateFlagName;
     aggregateFlag.AggregateFlagDescription = aggregateFlagDescription;
     aggregateFlag.IsPublic = isPublic;
     aggregateFlag.IsFavorite = isFavorite;
     aggregateFlag.UserId = userId;
     aggregateFlag.CreatedBy = createdBy;
     aggregateFlag.CreatedDate = createdDate;
     aggregateFlag.ModifiedBy = modifiedBy;
     aggregateFlag.ModifiedDate = modifiedDate;
     return aggregateFlag;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the AggregateFlags EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToAggregateFlags(AggregateFlag aggregateFlag)
 {
     base.AddObject("AggregateFlags", aggregateFlag);
 }