protected void Page_Load(object sender, EventArgs e)
        {
            _inBloomApi = new inBloomApi();
            try
            {
                _inBloomApi.AccessToken = Session[SessionEnum.AccessToken.ToString()].ToString();

                if (Request.QueryString["LogOut"].ToString().Equals("1"))
                {
                    _inBloomApi.LogOut();
                    Session[SessionEnum.AccessToken.ToString()] = null;
                    Session["UserDetail"] = null;
                    Session["School"] = null;
                    Session["Course"] = null;
                    Session["Section"] = null;
                    Session["SectionAndCourseOfferingId"] = null;
                    Session["CourseIdAndCourseOfferingId"] = null;
                    Response.Redirect("https://portal.sandbox.inbloom.org/portal/c/logout");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("https://portal.sandbox.inbloom.org/portal/c/logout");
            }
        }
 public inBloomCustom(String accessToken)
 {
     AccessToken = accessToken;
     _authenticateUser = new AuthenticateUser();
     _slcApi = new inBloomApi(accessToken);
     _sqlHelper = new SqlHelper();
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     try
     {
         _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
     }
     catch (Exception Ex)
     {
         Session[SessionEnum.AccessToken.ToString()] = null;
         Response.Redirect("Search.aspx");
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session[SessionEnum.AccessToken.ToString()] != null)
                {
                    _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
                    _sqlHelper = new SqlHelper();

                    _user = (User)Session["UserDetail"];
                    //Label lbl = (Label)Master.FindControl("lblUser");
                    lblUser.Text = _user.FullName;

                    _user = _inBloomApi.UserDetails();

                    if (_user == null)
                        Response.Redirect("Search.aspx");
                    else
                        if (Session["UserDetail"] == null)
                        {
                            Session.Add("UserDetail", _user);
                        }

                    if (!IsPostBack)
                    {
                        DropDownListSection.Items.Clear();
                        GetFlag();

                        if (Session["FlagId"] == null && Session["FlagType"] == null)
                        {
                        }
                        else
                        {
                            String SelectFlag = Session["FlagId"].ToString() + "_" + Session["FlagType"].ToString();
                            DropDownListSection.SelectedValue = SelectFlag;
                        }
                    }

                    if (Session["Success"] != null)
                    {
                        //lblSuccess.Text = Session["Success"].ToString();
                        hdnFieldMessage.Value = Session["Success"].ToString();
                        Session["Success"] = null;

                    }
                    else
                    {
                        hdnFieldMessage.Value = "";
                    }

                    if (!IsPostBack)
                    {

                        if (_user.IsAdminUser || IsLeader())
                        {
                            GetDistrictList();
                        }
                        else
                        {
                            DropDownListDistrict.Visible = false;
                            LabelDistrict.Visible = false;
                        }

                        //|| IsLeader() need to add
                        if (IsEducator())
                        {
                            dropDownListStaff.Visible = false;
                            LabelStaff.Visible = false;
                        }

                        if (Session["School"] == null)
                        {
                            GetSchoolList();
                        }
                        else
                        {
                            try
                            {
                                DropDownListSchool.Items.Clear();
                                School school = (School)Session["School"];
                                DropDownListSchool.Items.AddRange(school.GetSchoolList());
                                DropDownListSchool.SelectedValue = Session[SessionEnum.SchoolId.ToString()].ToString();
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                        if (Session["Staff"] == null)
                        {
                            if (!IsEducator())
                                GetStaffList();
                        }
                        else
                            if (!IsEducator())
                            {
                                dropDownListStaff.Items.Clear();
                                Staff staff = (Staff)Session["Staff"];
                                dropDownListStaff.Items.AddRange(staff.GetStaffList());
                                dropDownListStaff.SelectedValue = Session[SessionEnum.StaffID.ToString()].ToString();
                            }

                        if (Session["Course"] == null)
                        {
                            GetCourseListNew();
                            //GetCourseList();
                        }
                        else
                        {
                            try
                            {
                                Course course = (Course)Session["Course"];
                                ListItem[] _courseList = course.GetCourseList();
                                //DropDownListCourse.Items.AddRange(course.GetCourseList());
                                for (int i = 0; i < _courseList.Count(); i++)
                                {
                                    if (_courseList[i] != null)
                                        DropDownListCourse.Items.Add(_courseList[i]);
                                }
                                DropDownListCourse.SelectedValue = Session[SessionEnum.CourseId.ToString()].ToString();
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                        if (Session["Section"] == null)
                        {
                            GetSectionListNew();
                            //GetSectionList();
                        }
                        else
                        {
                            try
                            {
                                Section section = (Section)Session["Section"];
                                ListItem[] _listSection = section.GetSectionList();
                                if (_listSection != null)
                                {
                                    for (int Index = 0; Index < _listSection.Length; Index++)
                                    {
                                        if (_listSection[Index] != null)
                                        {
                                            DropDownListSection.Items.Add(new ListItem(_listSection[Index].Text, _listSection[Index].Value.ToString()));
                                        }
                                    }

                                }

                                DropDownListSection.SelectedValue = Session[SessionEnum.SectionId.ToString()].ToString();
                            }
                            catch (Exception Ex)
                            {
                            }
                        }

                        try
                        {
                            Session.Add(SessionEnum.SchoolId.ToString(), DropDownListSchool.SelectedValue);
                            Session.Add(SessionEnum.CourseId.ToString(), DropDownListCourse.SelectedValue);
                            Session.Add(SessionEnum.SectionId.ToString(), DropDownListSection.SelectedValue);
                            Session.Add(SessionEnum.StaffID.ToString(), dropDownListStaff.SelectedValue);
                        }
                        catch (Exception Ex)
                        {
                        }

                    }

                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
            catch (Exception ex)
            {
                throw;
                //Response.Redirect("Search.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
            }
            catch (Exception Ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {
                if (Session["CopyAggregateFlag"] != null)
                {
                    _aggregateFlagId = Convert.ToInt16(Session["CopyAggregateFlag"].ToString());

                    IList<AggregateFlag> _AggregateFlag = _sqlHelper.GetAggregateFlag(_aggregateFlagId);

                    txtFlagName.Text = _AggregateFlag[0].AggregateFlagName;
                    txtDescription.Text = _AggregateFlag[0].AggregateFlagDescription;
                    txtFlag.Text = _AggregateFlag[0].Keyword;

                    _user = (User)Session["UserDetail"];
                    if (_user == null)
                    {
                        Session[SessionEnum.AccessToken.ToString()] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else
                    {
                        if (_user.IsAdminUser)
                        {
                            String IsPublic = _AggregateFlag[0].IsPublic.ToString();
                            if (IsPublic == "True")
                            {
                                radioFlagType.SelectedIndex = 0;
                            }
                            else
                            {
                                radioFlagType.SelectedIndex = 1;
                            }
                        }
                        else
                        {
                            PanelFlagType.Visible = false;
                        }

                        _user = (User)Session["UserDetail"];
                        //gridStudentInfo.DataSource = _sqlHelper.GetFlagList(_user.ExternalId);
                        //gridStudentInfo.DataBind();

                        IList<Flag> _flagForGrid = _sqlHelper.GetFlagList(_user.ExternalId);
                        IList<Flag> _flagListTemp = null;

                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();
                        String[] AdminUserId = _inBloomApi.GetAdminIdByEducationOrganizationId(EducationOrganizationId);
                        if (AdminUserId != null)
                        {
                            for (int i = 0; i < AdminUserId.Count(); i++)
                            {
                                _flagListTemp = _sqlHelper.GetPublicFlagListForAdmin(AdminUserId[i]);
                                if (_flagForGrid == null)
                                {
                                    _flagForGrid = _flagListTemp;
                                }
                                else
                                {
                                    for (int j = 0; j < _flagListTemp.Count; j++)
                                    {
                                        _flagForGrid.Add(_flagListTemp[j]);
                                    }

                                }
                            }
                        }

                        gridStudentInfo.DataSource = _flagForGrid;
                        gridStudentInfo.DataBind();

                        // add to listbox
                        IList<Flag> _flag = _sqlHelper.GetFlagAddedForInAggregate(_aggregateFlagId);
                        for (int i = 0; i < _flag.Count; i++)
                        {
                            int FlagId = _flag[i].FlagId;
                            String FlagName = _flag[i].FlagName;

                            lstcategory.Items.Add(new ListItem(FlagName, FlagId.ToString()));
                        }

                        foreach (GridViewRow row in gridStudentInfo.Rows)
                        {
                            int FlagId = Convert.ToInt16(row.Cells[4].Text);
                            for (int i = 0; i < _flag.Count; i++)
                            {
                                if (_flag[i].FlagId == FlagId)
                                {
                                    row.Visible = false;
                                    break;
                                }
                            }
                        }
                    }

                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _inBloomApi = new inBloomApi();
            _sqlHelper = new SqlHelper();
            _user = (User) Session["UserDetail"];

            try
            {
                _inBloomApi.AccessToken = Session[SessionEnum.AccessToken.ToString()].ToString();
            }
            catch (Exception ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Session[SessionEnum.SchoolId.ToString()] == null)
                        {
                        }
                        else
                        {
                            LoadFieldNames();

                            _schoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                            _courseId = Session[SessionEnum.CourseId.ToString()].ToString();
                            _sectionId = Session[SessionEnum.SectionId.ToString()].ToString();

                            _user = (User) Session["UserDetail"];

                            if (Session["FlagId"] == null || Session["FlagType"] == null)
                            {
                                // show result for school and courses only.
                                Response.Redirect("Search.aspx");
                            }
                            else
                            {
                                _flagId = Convert.ToInt16(Session["FlagId"].ToString());
                                _flagType = Session["FlagType"].ToString();

                                if (_flagType.Equals("Flag"))
                                {
                                    // get all the values for flag
                                    IList<Flag> _flag = _sqlHelper.GetFlag(_flagId);

                                    lblFlagName.Text = _flag[0].FlagName;
                                    lblFlagDescription.Text = _flag[0].FlagDescription;

                                    if (_flag.Count > 0)
                                    {
                                        int DataElementId = _flag[0].DataElementId;
                                        int ConditionId = _flag[0].ConditionId;
                                        _value1 = _flag[0].ValueSet1;
                                        _value2 = _flag[0].ValueSet2;
                                        _flagUser = _flag[0].UserId;

                                        _dataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                                        _fieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                                        _entityName = _sqlHelper.GetEntityNameByDataElementId(DataElementId);

                                        DataTable dt = _inBloomApi.RunFlag(_fieldName, _dataType, _responseType, _flagUser, _schoolId, _courseId, _sectionId, ConditionId, _value1, _value2, _entityName, _isPreview);

                                        Session.Add("FieldName", _fieldName);
                                        gvResult.DataSource = dt;
                                        gvResult.DataBind();

                                        //gvResult.HeaderRow.Cells[3].Text = _inBloomApi.GetWellFormattedString(FieldName);
                                        for (int i = 0; i < gvResult.HeaderRow.Cells.Count; i++)
                                        {
                                            String HeaderText = gvResult.HeaderRow.Cells[i].Text;
                                            if (HeaderText == "FieldName")
                                            {

                                                gvResult.HeaderRow.Cells[i].Text = _inBloomApi.GetWellFormattedString(char.ToUpper(_entityName[0]) + _entityName.Substring(1)) + "." + _inBloomApi.GetWellFormattedString(_fieldName);

                                                break;
                                            }
                                        }

                                        HideColumnInitial();

                                        Session.Add("ResultGrid", dt);

                                        if (_sqlHelper.IsFav(_flagId) || _sqlHelper.IsInPublicFavorite(_flagId, _user.ExternalId))
                                        {
                                            btnlnkfavorite.Text = "Remove Favorite";
                                            btnlnkfavorite.CssClass = "";
                                        }
                                        else
                                        {
                                            btnlnkfavorite.Text = "Add to Favorite";
                                            btnlnkfavorite.CssClass = "unfavorite_result";
                                        }
                                    }
                                }
                                else if (_flagType.Equals("AggregateFlag"))
                                {
                                    try
                                    {
                                        IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(_flagId);

                                        lblFlagName.Text = _aggregateFlag[0].AggregateFlagName;
                                        lblFlagDescription.Text = _aggregateFlag[0].AggregateFlagDescription;

                                        int[] FlagArrayId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(_flagId);

                                        DataTable dt = _inBloomApi.RunAggregateFlag(FlagArrayId, _schoolId, _courseId, _sectionId);

                                        GridViewAggregateFlag.DataSource = dt;
                                        Session.Add("ResultGrid", dt);
                                        GridViewAggregateFlag.DataBind();
                                        for (int i = 0; i < GridViewAggregateFlag.HeaderRow.Cells.Count; i++)
                                        {
                                            //GridViewAggregateFlag.HeaderRow.Cells[i].Text = _inBloomApi.GetWellFormattedString(GridViewAggregateFlag.HeaderRow.Cells[i].Text);
                                        }

                                        HideColumnInitialForAggregate();

                                        //GridViewAggregateFlag.HeaderRow.Cells[0].Visible = false;

                                        if (_sqlHelper.IsAggregateIsFav(_flagId) || _sqlHelper.IsAggregateInPublicFavorite(_flagId, _user.ExternalId))
                                        {
                                            btnlnkfavorite.Text = "Remove Favorite";
                                            btnlnkfavorite.CssClass = "";
                                            btnlnkfavorite.Attributes.Add("onclick", "showunfavmsg();");

                                        }
                                        else
                                        {
                                            btnlnkfavorite.Text = "Add to Favorite";
                                            btnlnkfavorite.CssClass = "unfavorite_result";
                                            btnlnkfavorite.Attributes.Add("onclick", "showfavmsg();");

                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }

                                }
                            }

                            //LoadStudentGrid();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("Search.aspx");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                if (!IsPostBack)
                {
                    if (Session[SessionEnum.AccessToken.ToString()] == null)
                    {
                        if (Request.QueryString[QueryStringTokenEnum.Code.ToString()] == null)
                        {
                            _authenticateUser.AuthorizeUser();
                        }
                        else
                        {
                            Session.Add(SessionEnum.AccessToken.ToString(), _authenticateUser.GetAccessToken());
                        }
                    }

                    _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

                    Session["ResultPage"] = null;
                    _user = _slcApi.UserDetails();

                    if (_user == null)
                    {
                        Session[SessionEnum.AccessToken.ToString()] = null;
                        Response.Redirect("Search.aspx");
                    }
                    else if (Session["UserDetail"] == null)
                    {
                        Session.Add("UserDetail", _user);
                    }

                    _homeLinks = _slcApi.GetHomeLinks();
                    if (_homeLinks != null)
                    {
                        Session.Add("HomeLinks", _homeLinks);
                    }

                    _homeLinks = (JArray) Session["HomeLinks"];
                    String EducationOrganizationId = _slcApi.GetEducationOrganizationId(_homeLinks);

                    if (EducationOrganizationId != null || EducationOrganizationId != "")
                    {
                        // not state level admin
                        Session.Add("EducationOrganizationId", EducationOrganizationId);
                        //_slcApi.GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                    }

                    LoadFlagGrid();
                }
            }
            catch (Exception Ex)
            {
                //if (Ex.ToString().Contains("The remote server returned an error: (403) Forbidden."))
                //{
                //    Session.Add("ErrorStack", Ex.ToString());
                //    Session[SessionEnum.AccessToken.ToString()] = null;
                //    Response.Redirect("ErrorPage.aspx");

                //}
                //Response.Redirect("Search.aspx");
            }
        }
        public DataView GetFlagForGrid()
        {
            try
            {
                _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

                _user = (User) Session["UserDetail"];

                Temp[] _temp = null;
                Temp[] _tempForStaff = null;
                JArray _homeLinks = (JArray) Session["HomeLinks"];
                String UserCustomLink = _slcApi.GetCustomLink(_homeLinks);

                String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                if (_user.IsAdminUser)
                {
                    // get all the flags from organization by admin user specific
                    _temp = _slcApi.GetFlagListByEducationOrganization(_user, EducationOrganizationId);
                    ListItem[] _schoolList = null;

                    _schoolList = GetSchoolList();

                    _tempForStaff = _slcApi.GetFlagListOfStaffForAdminUser(_user, EducationOrganizationId, _schoolList);

                    if (_tempForStaff.Any())
                    {
                        if (_temp != null)
                        {
                            Temp[] _tempNew = _temp;
                            _temp = new Temp[_tempNew.Count() + _tempForStaff.Count()];

                            for (int i = 0; i < _tempNew.Count(); i++)
                            {
                                _temp[i] = new Temp();
                                _temp[i] = _tempNew[i];
                            }
                            int j = 0;
                            for (int i = _tempNew.Count(); i < _tempNew.Count() + _tempForStaff.Count(); i++)
                            {
                                _temp[i] = new Temp();
                                _temp[i] = _tempForStaff[j];
                                j++;
                            }
                        }
                        else
                            _temp = _tempForStaff;
                    }
                }
                else
                {
                    // get all the flags from custom and organization for normal users i.e public flag
                    _temp = _slcApi.GetFlagListByEducationOrganization(null, EducationOrganizationId);
                }

                Temp[] _newTemp = null;
                Temp _ownTemp = null;

                if (_temp != null)
                {
                    _newTemp = new Temp[_temp.Count() + 1];
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        _newTemp[i] = new Temp();
                        _newTemp[i] = _temp[i];
                    }

                    _ownTemp = _slcApi.GetCustomForStaff(UserCustomLink);
                    _newTemp[_temp.Count()] = new Temp();
                    _newTemp[_temp.Count()] = _ownTemp;
                }
                else
                {
                    // to get the flags from user custom, private for admin users
                    _ownTemp = _slcApi.GetCustomForStaff(UserCustomLink);
                    _newTemp = new Temp[1];
                    _newTemp[0] = new Temp();
                    _newTemp[0] = _ownTemp;
                }

                DataTable dt = new DataTable("blablaTable");
                dt.Columns.Add("FlagId", typeof (int));
                dt.Columns.Add("FlagName", typeof (string));
                dt.Columns.Add("FlagDescription", typeof (string));
                dt.Columns.Add("Keyword", typeof (string));
                dt.Columns.Add("IsPublic", typeof (bool));
                dt.Columns.Add("IsFavorite", typeof (bool));
                dt.Columns.Add("CreatedDate", typeof (DateTime));
                dt.Columns.Add("Type", typeof (string));

                dt.Columns.Add("IsAdmin", typeof (string));
                dt.Columns.Add("UserId", typeof (string));

                for (int i = 0; i < _newTemp.Count(); i++)
                {
                    if (_newTemp[i] != null)
                    {
                        FlagCls[] _flagCls = _newTemp[i].FlagList;
                        AggregateCls[] _aggregateFlagCls = _newTemp[i].AggregateFlagList;

                        if (_flagCls != null)
                        {
                            for (int j = 0; j < _flagCls.Count(); j++)
                            {
                                if (_flagCls[j].IsDeleted == false)
                                    dt.Rows.Add(new Object[] {_flagCls[j].FlagId, _flagCls[j].FlagName, _flagCls[j].FlagDescription, _flagCls[j].FlagKeyword, _flagCls[j].IsPublic, _flagCls[j].IsFavorite, _flagCls[j].CreatedDate, "Flag", _newTemp[i].IsAdmin, _newTemp[i].UserId});
                            }
                        }

                        if (_aggregateFlagCls != null)
                        {
                            for (int j = 0; j < _aggregateFlagCls.Count(); j++)
                            {
                                if (_aggregateFlagCls[j].IsDeleted == false)
                                    dt.Rows.Add(new Object[] {_aggregateFlagCls[j].AggregateFlagId, _aggregateFlagCls[j].AggregateFlagName, _aggregateFlagCls[j].AggregateFlagDescription, _aggregateFlagCls[j].Keyword, _aggregateFlagCls[j].IsPublic, _aggregateFlagCls[j].IsFavorite, _aggregateFlagCls[j].CreatedDate, "AggregateFlag", _newTemp[i].IsAdmin, _newTemp[i].UserId});
                            }
                        }

                    }
                }

                return dt.DefaultView;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        protected void gridViewFlag_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            #region Grid Operation Edit, Delete, Update, Favorite

            _slcApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

            if (e.CommandName == "CmdDelete")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];
                    IList<Flag> _flag = (IList<Flag>) _sqlHelper.GetFlag(FlagID);

                    if (_user.ExternalId == _flag[0].UserId)
                    {

                        _sqlHelper.DeleteFlag(FlagID);

                        JArray _links = (JArray) Session["HomeLinks"];

                        String UserCustomLink = _slcApi.GetCustomLink(_links);
                        String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                        String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                        String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                        bool IsAdminUser = (AdminUser == "True");

                        String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                        bool IsPublic = (IsPublicFag == "True");

                        FlagCls[] _flagCls = new FlagCls[1];
                        _flagCls[0] = new FlagCls();
                        _flagCls[0].FlagId = _flag[0].FlagId;
                        _flagCls[0].FlagName = _flag[0].FlagName;
                        _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                        _flagCls[0].FlagKeyword = _flag[0].Keyword;
                        _flagCls[0].IsPublic = _flag[0].IsPublic;
                        _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                        _flagCls[0].IsDeleted = true;
                        _flagCls[0].ConditionId = _flag[0].ConditionId;
                        _flagCls[0].DataElementId = _flag[0].DataElementId;
                        _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                        _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                        _flagCls[0].UserId = _flag[0].UserId;
                        _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                        _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                        _flagCls[0].ModifiedBy = _user.ExternalId;
                        _flagCls[0].ModifiedDate = DateTime.Now;

                        _slcApi.UpateFlag(FlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _flagCls, _user);

                        LoadFlagGrid();
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdFavorite")
            {
                try
                {
                    _user = (User) Session["UserDetail"];

                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _sqlHelper.FavoriteFlag(FlagID, _user.ExternalId);
                    _user = (User) Session["UserDetail"];

                    JArray _links = (JArray) Session["HomeLinks"];

                    String UserCustomLink = _slcApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = AdminUser == "True";

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = IsPublicFag == "True";

                    IList<Flag> _flag = _sqlHelper.GetFlag(FlagID);

                    FlagCls[] _flagCls = new FlagCls[1];
                    _flagCls[0] = new FlagCls();
                    _flagCls[0].FlagId = _flag[0].FlagId;
                    _flagCls[0].FlagName = _flag[0].FlagName;
                    _flagCls[0].FlagDescription = _flag[0].FlagDescription;
                    _flagCls[0].FlagKeyword = _flag[0].Keyword;
                    _flagCls[0].IsPublic = _flag[0].IsPublic;
                    _flagCls[0].IsFavorite = _flag[0].IsFavorite;
                    //_flagCls[0].IsDeleted =(bool) _flag[0].IsDeleted;
                    _flagCls[0].ConditionId = _flag[0].ConditionId;
                    _flagCls[0].DataElementId = _flag[0].DataElementId;
                    _flagCls[0].ValueSet1 = _flag[0].ValueSet1;
                    _flagCls[0].ValueSet2 = _flag[0].ValueSet2;
                    _flagCls[0].UserId = _flag[0].UserId;
                    _flagCls[0].CreatedBy = _flag[0].CreatedBy;
                    _flagCls[0].CreatedDate = _flag[0].CreatedDate;
                    _flagCls[0].ModifiedBy = _user.ExternalId;
                    _flagCls[0].ModifiedDate = DateTime.Now;

                    _slcApi.UpateFlag(FlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _flagCls, _user);

                    UpdateMasterFlagList();

                    LoadFlagGrid();

                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdCopyFlag")
            {
                try
                {
                    int FlagID = Int32.Parse(e.CommandArgument.ToString());
                    Session.Add("CopyFlag", FlagID);
                    Response.Redirect("CopyFlag.aspx");
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdEditFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int FlagId = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());
                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                    Session.Add("CustomUserId", UserId);
                    Session.Add("CustomIsPublic", IsPublic);
                    Session.Add("CustomFlagType", FlagType);
                    Session.Add("CustomIsAdminUser", IsAdminUser);
                    Session.Add("EditFlag", FlagId);
                    Response.Redirect("EditFlag.aspx");
                }
                catch (Exception Ex)
                {
                }
            }
            else if (e.CommandName == "CmdCopyAggregateFlag")
            {
                try
                {
                    int AggregateFlagID = Int32.Parse(e.CommandArgument.ToString());
                    Session.Add("CopyAggregateFlag", AggregateFlagID);
                    Response.Redirect("CopyAggregateFlag.aspx");
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdEditAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());
                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                    Session.Add("CustomUserId", UserId);
                    Session.Add("CustomIsPublic", IsPublic);
                    Session.Add("CustomFlagType", FlagType);
                    Session.Add("CustomIsAdminUser", IsAdminUser);
                    Session.Add("EditAggregateFlag", AggregateFlagID);

                    Response.Redirect("EditAggregateFlag.aspx");
                }
                catch (Exception Ex)
                {
                }
            }
            else if (e.CommandName == "CmdFavoriteAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];
                    _sqlHelper.AggregatetFavoriteFlag(AggregateFlagID, _user.ExternalId);

                    JArray _links = (JArray) Session["HomeLinks"];

                    String UserCustomLink = _slcApi.GetCustomLink(_links);
                    String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                    String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                    String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                    bool IsAdminUser = (AdminUser == "True");

                    String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                    bool IsPublic = (IsPublicFag == "True");

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagID);

                    _aggregateCls[0] = new AggregateCls();
                    _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                    _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                    _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                    _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;

                    _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                    _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                    _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                    _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                    _aggregateCls[0].ModifiedBy = _user.ExternalId;
                    _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                    _aggregateCls[0].ModifiedDate = DateTime.Now;

                    int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(AggregateFlagID);
                    FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];
                    for (int j = 0; j < flagId.Count(); j++)
                    {
                        _flagForAggregateList[j] = new FlagForAggregate();

                        _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                        _flagForAggregateList[j].FlagId = flagId[j];
                        _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                        _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                        _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                        _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;

                    }

                    _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                    _slcApi.UpateAggregateFlag(AggregateFlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                    UpdateMasterFlagList();

                    LoadFlagGrid();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else if (e.CommandName == "CmdDeleteAggregateFlag")
            {
                try
                {
                    int RowIndex = Int32.Parse(e.CommandArgument.ToString());
                    int AggregateFlagID = Convert.ToInt16(gridViewFlag.DataKeys[RowIndex].Value.ToString());

                    _user = (User) Session["UserDetail"];

                    AggregateCls[] _aggregateCls = new AggregateCls[1];

                    IList<AggregateFlag> _aggregateFlag = _sqlHelper.GetAggregateFlag(AggregateFlagID);

                    if (_user.ExternalId == _aggregateFlag[0].UserId)
                    {
                        if (_sqlHelper.DeleteAggregateFlag(AggregateFlagID))
                        {
                            // deleted successfully

                            _user = (User) Session["UserDetail"];

                            JArray _links = (JArray) Session["HomeLinks"];

                            String UserCustomLink = _slcApi.GetCustomLink(_links);
                            String EducationOrganizationId = Session["EducationOrganizationId"].ToString();

                            String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                            String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                            bool IsAdminUser = (AdminUser == "True");

                            String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                            bool IsPublic = (IsPublicFag == "True");

                            _aggregateCls[0] = new AggregateCls();
                            _aggregateCls[0].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                            _aggregateCls[0].AggregateFlagDescription = _aggregateFlag[0].AggregateFlagDescription;
                            _aggregateCls[0].AggregateFlagName = _aggregateFlag[0].AggregateFlagName;
                            _aggregateCls[0].Keyword = _aggregateFlag[0].Keyword;
                            _aggregateCls[0].IsDeleted = true;
                            _aggregateCls[0].IsPublic = _aggregateFlag[0].IsPublic;
                            _aggregateCls[0].IsFavorite = _aggregateFlag[0].IsFavorite;
                            _aggregateCls[0].UserId = _aggregateFlag[0].UserId;
                            _aggregateCls[0].CreatedBy = _aggregateFlag[0].CreatedBy;
                            _aggregateCls[0].ModifiedBy = _user.ExternalId;
                            _aggregateCls[0].CreatedDate = _aggregateFlag[0].CreatedDate;
                            _aggregateCls[0].ModifiedDate = DateTime.Now;

                            int[] flagId = _sqlHelper.GetAllFlagIdOfAggregateFlagByAggregateFlagId(AggregateFlagID);
                            FlagForAggregate[] _flagForAggregateList = new FlagForAggregate[flagId.Count()];

                            for (int j = 0; j < flagId.Count(); j++)
                            {
                                _flagForAggregateList[j] = new FlagForAggregate();

                                _flagForAggregateList[j].AggregateFlagId = _aggregateFlag[0].AggregateFlagId;
                                _flagForAggregateList[j].FlagId = flagId[j];
                                _flagForAggregateList[j].CreatedBy = _aggregateFlag[0].CreatedBy;
                                _flagForAggregateList[j].CreatedDate = _aggregateFlag[0].CreatedDate;
                                _flagForAggregateList[j].ModifiedBy = _aggregateFlag[0].ModifiedBy;
                                _flagForAggregateList[j].ModifiedDate = _aggregateFlag[0].ModifiedDate;
                            }

                            _aggregateCls[0].FlagForAggregate = _flagForAggregateList;

                            _slcApi.UpateAggregateFlag(AggregateFlagID, UserCustomLink, EducationOrganizationId, IsAdminUser, IsPublic, UserId, _aggregateCls, _user);

                            LoadFlagGrid();
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            else if (e.CommandName == "CmdRunFlag")
            {
                //Session.Add("ResultPage", true);
                int RowIndex = Convert.ToInt16(e.CommandArgument);
                int FlagId = Convert.ToInt16(gridViewFlag.Rows[RowIndex].Cells[8].Text);
                String FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;
                Session.Add("FlagId", FlagId);
                Session.Add("FlagType", FlagType);

                String UserId = gridViewFlag.Rows[RowIndex].Cells[10].Text;
                String AdminUser = gridViewFlag.Rows[RowIndex].Cells[9].Text;

                bool IsAdminUser = (AdminUser == "True");

                String IsPublicFag = gridViewFlag.Rows[RowIndex].Cells[4].Text;
                bool IsPublic = (IsPublicFag == "True");

                FlagType = gridViewFlag.Rows[RowIndex].Cells[7].Text;

                Session.Add("CustomUserId", UserId);
                Session.Add("CustomIsPublic", IsPublic);
                Session.Add("CustomFlagType", FlagType);
                Session.Add("CustomIsAdminUser", IsAdminUser);
                Session.Add("EditFlag", FlagId);

                Response.Redirect("Result.aspx");
            }

            #endregion
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());

            if (!IsPostBack)
            {
                _sqlHelper = new SqlHelper();
                LoadDataDomainList();
                LoadGridWithAttribute();
                lstCondition.DataSource = _sDACEntities.Conditions;
                lstCondition.DataBind();

                _user = (User)Session["UserDetail"];

                if (!_user.IsAdminUser)
                    PanelFlagType.Visible = false;

                if (Session["EditFlag"] != null)
                {
                    try
                    {
                        int FlagId = Convert.ToInt16(Session["EditFlag"]);
                        Session.Add("FlagIdForEditPage", FlagId);
                        _flagIdForPage = FlagId;

                        IList<Flag> _Flag = _sqlHelper.GetFlag(FlagId);

                        txtFlagName.Text = _Flag[0].FlagName;
                        txtDescription.Text = _Flag[0].FlagDescription;
                        txtFlag.Text = _Flag[0].Keyword;

                        String IsPublic = _Flag[0].IsPublic.ToString();

                        if (IsPublic == "True")
                        {
                            radioFlagType.SelectedIndex = 0;
                        }
                        else
                        {
                            radioFlagType.SelectedIndex = 1;
                        }

                        lstCondition.SelectedValue = _Flag[0].ConditionId.ToString();

                        if (_Flag[0].ConditionId.ToString() != null && _Flag[0].ConditionId.ToString() != "")
                        {
                            condition.Style.Add("display", "block");
                            condition.Text = lstCondition.SelectedItem.Text;
                        }

                        txtSetVal.Text = _Flag[0].ValueSet1;
                        txtSetVal2.Text = _Flag[0].ValueSet2;

                        if (_Flag[0].ValueSet1 != null && _Flag[0].ValueSet1 != "")
                        {
                            val1.Style.Add("display", "block");
                            val1.Text = _Flag[0].ValueSet1;
                        }

                        if (_Flag[0].ValueSet2 != null && _Flag[0].ValueSet2 != "")
                        {
                            val2.Style.Add("display", "block");
                            val2.Text = " & " + _Flag[0].ValueSet2;
                        }

                        _conditionId = _Flag[0].ConditionId;
                        if (condition.Text == "Is between" || condition.Text == "Is not between")
                        {
                            txtSetVal2.Style.Add("display", "block");
                        }

                        hdnDataElementId.Value = _Flag[0].DataElementId.ToString();

                        int DataElementId = _Flag[0].DataElementId;
                        hdnDataType.Value = _sqlHelper.GetDataTypeByDataElementId(DataElementId);

                        String EntityName = _sqlHelper.GetDataDomainNameByDataElementId(DataElementId);

                        // LoadEntityList();
                        LoadDataDomainList();
                        DropDownListEntity.SelectedItem.Text = EntityName;
                        LoadGridWithAttribute();

                        SelectRow(_Flag[0].DataElementId);

                        // process to call run flag

                        String Entity = _sqlHelper.GetEntityNameByDataElementId(DataElementId);
                        String FieldName = _sqlHelper.GetFieldNameByDataElementId(DataElementId);
                        String DataType = _sqlHelper.GetDataTypeByDataElementId(DataElementId);
                        bool ResponseType = false;
                        _user = (User)Session["UserDetail"];
                        String UserId = _user.ExternalId;
                        String SchoolId = Session[SessionEnum.SchoolId.ToString()].ToString();
                        String CourseId = Session[SessionEnum.CourseId.ToString()].ToString();
                        String SectionId = Session[SessionEnum.SectionId.ToString()].ToString();
                        String Value1 = txtSetVal.Text;
                        String Value2 = txtSetVal2.Text;

                       // String ExternalField = "";
                        bool IsPreview = true;

                        DataTable dt = _inBloomApi.RunFlag(FieldName, DataType, ResponseType, UserId, SchoolId, CourseId, SectionId, _conditionId, Value1, Value2, Entity, IsPreview);

                        gridDisplayResult.DataSource = dt;
                        gridDisplayResult.DataBind();
                        gridDisplayResult.HeaderRow.Cells[1].Text =_inBloomApi.GetWellFormattedString(char.ToUpper(Entity[0]) + Entity.Substring(1))+"."+_inBloomApi.GetWellFormattedString(FieldName);
                        entity.Text = _inBloomApi.GetWellFormattedString(char.ToUpper(Entity[0]) + Entity.Substring(1)) + "." + _inBloomApi.GetWellFormattedString(FieldName); ;

                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    Response.Redirect("Search.aspx");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
            }
            catch (Exception Ex)
            {
                Session[SessionEnum.AccessToken.ToString()] = null;
                Response.Redirect("Search.aspx");

            }

            if (!IsPostBack)
            {
                try
                {

                    LoadDataDomainList();
                    LoadGridWithAttribute();
                    lstCondition.DataSource = _sDACEntities.Conditions;
                    lstCondition.DataBind();

                    DataTable dt = new DataTable();
                    dt.Columns.Add("student_Name");
                    dt.Columns.Add("GPA");

                    for (int i = 0; i < 5; i++)
                    {
                        dt.Rows.Add((new object[] { "", "" }));
                    }

                    gridDisplayResult.DataSource = dt;
                    gridDisplayResult.DataBind();

                    _user = (User)Session["UserDetail"];

                    if (_user.IsAdminUser)
                    {
                    }
                    else
                    {
                        PanelFlagType.Visible = false;
                    }

                }
                catch (Exception Ex)
                {
                }

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _sqlHelper = new SqlHelper();

            try
            {
                _inBloomApi = new inBloomApi(Session[SessionEnum.AccessToken.ToString()].ToString());
            }
            catch (Exception Ex)
            {
                Response.Redirect("Search.aspx");
            }

            if (!IsPostBack)
            {

                 _user = (User)Session["UserDetail"];

                 if (_user == null)
                 {
                     Session[SessionEnum.AccessToken.ToString()] = null;
                     Response.Redirect("Search.aspx");
                 }
                 else
                 {
                     //gridStudentInfo.DataSource = _sDACEntities.Flags;
                     //gridStudentInfo.DataSource = _sqlHelper.GetFlagList(_user.ExternalId);
                     //gridStudentInfo.DataBind();

                     IList<Flag> _flag = _sqlHelper.GetFlagList(_user.ExternalId);

                     IList<Flag> _flagListTemp = null;

                     String EducationOrganizationId = Session["EducationOrganizationId"].ToString();
                     String[] AdminUserId = _inBloomApi.GetAdminIdByEducationOrganizationId(EducationOrganizationId);
                     if (AdminUserId != null)
                     {
                         for (int i = 0; i < AdminUserId.Count(); i++)
                         {
                             _flagListTemp = _sqlHelper.GetPublicFlagListForAdmin(AdminUserId[i]);
                             if (_flag == null)
                             {
                                 _flag = _flagListTemp;
                             }
                             else
                             {
                                 for (int j = 0; j < _flagListTemp.Count; j++)
                                 {
                                     _flag.Add(_flagListTemp[j]);
                                 }

                             }
                         }
                     }

                     gridStudentInfo.DataSource = _flag;

                     gridStudentInfo.DataBind();

                     if (_user.IsAdminUser)
                     {
                     }
                     else
                     {
                         PanelFlagType.Visible = false;
                     }

                 }

            }

            String script = "function abc() {" +

                         "var count = 0;" +
                         "$('#lstcategory option').each(function (e) {" +
                             "count = count + 1;" +
                         "});" +

                         "$('#gridStudentInfo tr input:checkbox').each(function () {" +

                             "if (this.checked) {" +
                                 "count = count + 1;" +
                             "}" +

                         "});" +

                         "if (count > 5) {" +

                             "DisplayErrorPopup('You can select only 5 flags.');" +
                             "return false;" +
                         "}" +

                         "return true;" +

                     "}";

            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), Guid.NewGuid().ToString(), script, true);
        }