public void PutCustomForStaff(JArray homeLinks, Temp tempObj)
 {
     try
     {
         for (int index = 0; index < homeLinks.Count(); index++)
         {
             String relation = _authenticateUser.GetStringWithoutQuote(homeLinks[index]["rel"].ToString());
             if (relation.Equals("custom"))
             {
                 string link = _authenticateUser.GetStringWithoutQuote(homeLinks[index]["href"].ToString());
                 string result = _slcApi.FlagObjectToJson(tempObj);
                 RestApiHelper.CallApiWithParameterForCustomPUT(link, this.AccessToken, result);
             }
         }
     }
     catch (Exception ex)
     {
     }
 }
        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;
            }
        }
 /// <summary>
 /// This function is used to to convert the FlagCustom object to Json object.
 /// </summary>
 /// <param name="flagCustom"></param>
 /// <returns></returns>
 public String FlagObjectToJson(Temp obj)
 {
     return new JavaScriptSerializer().Serialize(obj);
 }
        public void AddAggregateFlagIntoEducationOrganization(User _user, String EducationOrganizationId, AggregateCls[] _aggregateFlagCls)
        {
            try
            {
                AdminCls _adminCls = GetFlagListForAdminUser(EducationOrganizationId, _user.ExternalId);
                Temp _flagListPrivateAdminCustom = null;
                if (_adminCls == null)
                {
                    // no data present in organization
                    Temp[] _temp = new Temp[1];
                    _temp[0] = new Temp();
                    _temp[0].AggregateFlagList = _aggregateFlagCls;
                    _temp[0].UserId = _user.ExternalId;
                    _temp[0].IsAdmin = _user.IsAdminUser;

                    _adminCls = new AdminCls();
                    _adminCls.AdminList = _temp;

                    PutCustomForAdmin(EducationOrganizationId, _adminCls);

                }
                else
                {
                    // data is present need to add new

                    Temp[] _temp = _adminCls.AdminList;
                    bool UserExist = false;
                    int Index = 0;
                    int UserIndex = 0;
                    for (int i = 0; i < _temp.Count(); i++)
                    {
                        if (_temp[i].UserId == _user.ExternalId)
                        {
                            // user record exist
                            UserExist = true;
                            Index = i;
                            break;
                        }
                    }

                    if (UserExist)
                    {

                        Temp _flagForEdit = _temp[Index];

                        AggregateCls[] _flagClsForEdit = _flagForEdit.AggregateFlagList;

                        if (_flagClsForEdit != null)
                        {

                            AggregateCls[] _flagClsNew = new AggregateCls[_flagClsForEdit.Count() + 1];

                            for (int i = 0; i < _flagClsForEdit.Count(); i++)
                            {
                                _flagClsNew[i] = new AggregateCls();
                                _flagClsNew[i] = _flagClsForEdit[i];
                            }

                            _flagClsNew[_flagClsForEdit.Count()] = new AggregateCls();
                            _flagClsNew[_flagClsForEdit.Count()] = _aggregateFlagCls[0];

                            _flagForEdit.AggregateFlagList = _flagClsNew;
                        }
                        else
                        {
                            _flagForEdit.AggregateFlagList = _aggregateFlagCls;
                        }
                        _temp[Index] = _flagForEdit;
                        _adminCls.AdminList = _temp;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);

                    }
                    else
                    {
                        // new admin user
                        Temp[] _tempForNew = new Temp[_temp.Count() + 1];
                        for (int i = 0; i < _temp.Count(); i++)
                        {
                            _tempForNew[i] = new Temp();
                            _tempForNew[i] = _temp[i];
                        }

                        Temp _tempAdmin = new Temp();
                        _tempAdmin.IsAdmin = _user.IsAdminUser;
                        _tempAdmin.UserId = _user.ExternalId;
                        _tempAdmin.AggregateFlagList = _aggregateFlagCls;

                        _tempForNew[_tempForNew.Count() - 1] = new Temp();
                        _tempForNew[_tempForNew.Count() - 1] = _tempAdmin;

                        _adminCls.AdminList = _tempForNew;

                        PutCustomForAdmin(EducationOrganizationId, _adminCls);
                    }
                }
            }
            catch (Exception Ex)
            {

            }
        }
        public void DeleteFlagsIntoCustom(String Link, User _user, FlagCls[] _flagCls)
        {
            FlagCls[] _forNew = _flagCls;
            AggregateCls[] _forAggregateCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.FlagList = _flagCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forAggregateCls = _list.AggregateFlagList;

                        if (_list.FlagList.Count() == 1)
                        {
                            // only one need to delete
                            _temp.FlagList = null;
                        }
                        else
                        {
                            FlagCls[] _flagClsNew = new FlagCls[_list.FlagList.Count() - 1];
                            int Count = 0;
                            for (int i = 0; i < _list.FlagList.Count(); i++)
                            {
                                if (_list.FlagList[i].FlagId == _flagCls[0].FlagId)
                                    continue;
                                _flagClsNew[Count] = new FlagCls();
                                _flagClsNew[Count] = _list.FlagList[i];
                                Count++;
                            }

                            _temp.FlagList = _flagClsNew;
                            _temp.AggregateFlagList = _list.AggregateFlagList;
                            _temp.UserId = _user.ExternalId;
                            _temp.IsAdmin = _user.IsAdminUser;
                        }
                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {

            }
        }
        public void UpdateFlagsIntoCustom(String Link, User _user, FlagCls[] _flagCls)
        {
            FlagCls[] _forNew = _flagCls;
            AggregateCls[] _forAggregateCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.FlagList = _flagCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forAggregateCls = _list.AggregateFlagList;

                        FlagCls[] _flagClsNew = new FlagCls[_list.FlagList.Count()];
                        for (int i = 0; i < _list.FlagList.Count(); i++)
                        {
                            _flagClsNew[i] = new FlagCls();
                            if (_list.FlagList[i].FlagId == _flagCls[0].FlagId)
                            {
                                _flagClsNew[i] = _flagCls[0];
                            }
                            else
                                _flagClsNew[i] = _list.FlagList[i];
                        }

                        _temp.FlagList = _flagClsNew;
                        _temp.AggregateFlagList = _list.AggregateFlagList;

                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {

            }
        }
        public void AddAggregateFlagsIntoCustom(String Link, User _user, AggregateCls[] _aggregateCls)
        {
            AggregateCls[] _forNew = _aggregateCls;
            FlagCls[] _forFlagCls = null;
            try
            {
                Temp _editedFlagByUser = new Temp();
                _editedFlagByUser.AggregateFlagList = _aggregateCls;

                JObject UserCustomResponse = JObject.Parse(RestApiHelper.CallApiWithParameter(Link, this.AccessToken));
                if (UserCustomResponse != null)
                {
                    Temp _list = FlagObjectDeserialize(UserCustomResponse.ToString());
                    if (_list != null)
                    {
                        Temp _temp = new Temp();
                        _forFlagCls = _list.FlagList;
                        AggregateCls[] _flagClsNew = new AggregateCls[_list.AggregateFlagList.Count() + 1];

                        for (int i = 0; i < _list.AggregateFlagList.Count(); i++)
                        {
                            _flagClsNew[i] = new AggregateCls();
                            _flagClsNew[i] = _list.AggregateFlagList[i];
                        }
                        _flagClsNew[_list.AggregateFlagList.Count()] = new AggregateCls();
                        _flagClsNew[_list.AggregateFlagList.Count()] = _aggregateCls[0];

                        _temp.AggregateFlagList = _flagClsNew;
                        _temp.FlagList = _list.FlagList;
                        _temp.UserId = _user.ExternalId;
                        _temp.IsAdmin = _user.IsAdminUser;

                        String Result = FlagObjectToJson(_temp);

                        RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
                    }
                }

            }
            catch (Exception Ex)
            {
                // process to add first
                Temp _temp = new Temp();
                _temp.AggregateFlagList = _forNew;
                _temp.FlagList = _forFlagCls;
                _temp.UserId = _user.ExternalId;
                _temp.IsAdmin = _user.IsAdminUser;
                String Result = FlagObjectToJson(_temp);
                RestApiHelper.CallApiWithParameterForCustomPUT(Link, this.AccessToken, Result);
            }
        }
        public Temp[] GetFlagListOfStaffForAdminUser(User _user, String EducationOrganizationId, ListItem[] _schoolList)
        {
            Temp[] _temp = null;
            try
            {
                for (int i = 0; i < _schoolList.Count(); i++)
                {
                    Temp[] _tempStaff = null;
                    JArray _staffResponse = JArray.Parse(RestApiHelper.CallApi("schools/" + _schoolList[i].Value + "/teacherSchoolAssociations/teachers", this._accessToken));
                    if (_staffResponse != null)
                    {
                        String CustomLink = "";
                        _tempStaff = new Temp[_staffResponse.Count()];
                        for (int Index = 0; Index < _staffResponse.Count(); Index++)
                        {
                            JArray Links = JArray.Parse(_staffResponse[Index]["links"].ToString());
                            for (int j = 0; j < Links.Count; j++)
                            {
                                String Relation = _authenticateUser.GetStringWithoutQuote(Links[j]["rel"].ToString());
                                if (Relation.Equals("custom"))
                                {
                                    CustomLink = _authenticateUser.GetStringWithoutQuote(Links[j]["href"].ToString());
                                    Temp _response = GetCustomForStaff(CustomLink);
                                    _tempStaff[Index] = new Temp();
                                    _tempStaff[Index] = _response;
                                    break;
                                }
                            }

                            if (_temp == null)
                            {
                                _temp = new Temp[_tempStaff.Count()];
                                for (int j = 0; j < _tempStaff.Count(); j++)
                                {
                                    _temp[j] = new Temp();
                                    _temp[j] = _tempStaff[j];
                                }
                            }
                            else
                            {
                                // store _temp array into another
                                Temp[] _forTemp = new Temp[_temp.Count()];
                                for (int j = 0; j < _temp.Count(); j++)
                                {
                                    _forTemp[j] = new Temp();
                                    _forTemp[j] = _temp[j];
                                }

                                // get newly added staff temp
                                _temp = new Temp[_tempStaff.Count() + _temp.Count()];
                                for (int j = 0; j < _tempStaff.Count(); j++)
                                {
                                    _temp[j] = new Temp();
                                    _temp[j] = _tempStaff[j];
                                }

                                // get previous staff
                                int k = 0;
                                for (int j = _tempStaff.Count(); j < _tempStaff.Count() + _forTemp.Count(); j++)
                                {
                                    _temp[j] = new Temp();
                                    _temp[j] = _forTemp[k];
                                    k = k + 1;
                                }
                            }

                        }

                    }
                }

                // check for the blank
                int totalBlank = 0;
                for (int i = 0; i < _temp.Count(); i++)
                {
                    if (_temp[i] == null)
                        totalBlank = totalBlank + 1;
                }

                Temp[] _returnTemp = new Temp[_temp.Count() - totalBlank];
                int p = 0;
                for (int i = 0; i < _temp.Count(); i++)
                {
                    if (_temp[i] != null)
                    {
                        _returnTemp[p] = new Temp();
                        _returnTemp[p] = _temp[i];
                        p++;

                    }
                }

                return _returnTemp;
            }
            catch (InRowChangingEventException Ex)
            {
                return null;
            }
        }
        public Temp[] GetFlagListByEducationOrganization(User _user, String EducationOrganizationId)
        {
            try
            {
                AdminCls _adminCls = null;
                Temp[] _temp = null;
                JObject EducationOrganizationResponse = JObject.Parse(RestApiHelper.CallApi("educationOrganizations/" + EducationOrganizationId + "/custom", this._accessToken));
                if (EducationOrganizationResponse == null)
                {
                    // no data in custom
                }
                else
                {
                    _adminCls = AdminObjectDeserialize(EducationOrganizationResponse.ToString());
                    _temp = _adminCls.AdminList;
                    if (_user != null)
                    {
                        Temp[] _tempForNew = new Temp[1];

                        for (int i = 0; i < _temp.Count(); i++)
                        {
                            if (_temp[i].UserId == _user.ExternalId)
                            {
                                _tempForNew[0] = new Temp();
                                _tempForNew[0] = _temp[i];

                            }
                        }
                        return _tempForNew;
                    }

                }
                return _temp;
            }
            catch (Exception Ex)
            {
                return null;
            }
        }