public List <object> FindPositions(HttpRequestBase Request, string zipcode, string attributes, string things, string gender, string programid)
        {
            bool demomode = Request["demo"] != null && Request["demo"] == "1";

            LeadCounterClient  leadcounterclient = new LeadCounterClient();
            List <LeadCounter> leadcounters      = null;

            LeadCapClient  leadcapclient = new LeadCapClient();
            List <LeadCap> leadcaps      = null;

            List <object> results = new List <object>();

            bool validprogramcategoryid = false;

            if (programid != "" && !programid.StartsWith("c"))
            {
                validprogramcategoryid = true;
            }

            //ZipCodeClient zipclient = new ZipCodeClient();
            //ZipCode zipobject = zipclient.GetByRowKey(zipcode);
            string  zipjson = BlobJsonResourceManager.Instance.GetJsonResource("skillcow", "zipcodes", zipcode);
            JObject jzip    = null;

            if (zipjson != "")
            {
                jzip = JObject.Parse(zipjson);
            }

            GeoIndexNationalClient      nclient             = new GeoIndexNationalClient();
            GeoIndexAddStateClient      addstateclient      = new GeoIndexAddStateClient();
            GeoIndexAddZipClient        addzipclient        = new GeoIndexAddZipClient();
            GeoIndexSubtractStateClient subtractstateclient = new GeoIndexSubtractStateClient();
            GeoIndexSubtractZipClient   subtractzipclient   = new GeoIndexSubtractZipClient();

            List <IGeoIndex> allresults   = new List <IGeoIndex>();
            List <IGeoIndex> subtractions = new List <IGeoIndex>();

            //By program id
            if (validprogramcategoryid)
            {
                allresults.AddRange(nclient.GetAllByPartition(programid));
                if (jzip != null)
                {
                    //allresults.AddRange(addstateclient.GetAllByPartition(zipobject.StateCode + "-" + programid));
                    allresults.AddRange(addstateclient.GetAllByPartition(jzip["statecode"].ToString() + "-" + programid));
                    allresults.AddRange(addzipclient.GetAllByPartition(zipcode + "-" + programid));
                }

                if (jzip != null)
                {
                    //subtractions.AddRange(subtractstateclient.GetAllByPartition(zipobject.StateCode + "-" + programid));
                    subtractions.AddRange(subtractstateclient.GetAllByPartition(jzip["statecode"].ToString() + "-" + programid));
                    subtractions.AddRange(subtractzipclient.GetAllByPartition(zipcode + "-" + programid));
                }
            }
            else
            {
                allresults.AddRange(nclient.GetAllByPartition("attr"));
                if (jzip != null)
                {
                    //allresults.AddRange(addstateclient.GetAllByPartition(zipobject.StateCode));
                    allresults.AddRange(addstateclient.GetAllByPartition(jzip["statecode"].ToString()));
                    allresults.AddRange(addzipclient.GetAllByPartition(zipcode));
                }

                if (jzip != null)
                {
                    //subtractions.AddRange(subtractstateclient.GetAllByPartition(zipobject.StateCode));
                    subtractions.AddRange(subtractstateclient.GetAllByPartition(jzip["statecode"].ToString()));
                    subtractions.AddRange(subtractzipclient.GetAllByPartition(zipcode));
                }
            }

            IEnumerable <IGeoIndex> difference = allresults.Except(subtractions, new CompareGeoIndexResults());

            DirectEmployerClientClient              clientclient  = new DirectEmployerClientClient();
            DirectEmployerClientCampusClient        campusclient  = new DirectEmployerClientCampusClient();
            DirectEmployerClientCampusProgramClient programclient = new DirectEmployerClientCampusProgramClient();

            JObject oattributes = null;
            JObject othings     = null;

            long attributemask       = 0;
            long importantthingsmask = 0;

            if (attributes != null && attributes != "")
            {
                oattributes = JObject.Parse(attributes);
                AttributeMaskCalculator amc = new AttributeMaskCalculator();
                attributemask = amc.GetMask(oattributes);
            }
            if (things != null && things != "")
            {
                othings = JObject.Parse(things);
                ImportantThingsMaskCalculator itmc = new ImportantThingsMaskCalculator();
                importantthingsmask = itmc.GetMask(othings);
            }

            foreach (IGeoIndex geoindex in allresults)
            {
                if (geoindex.AttributeMask != null && (geoindex.AttributeMask & attributemask) == attributemask)
                {
                    DirectEmployerClientCampusProgram job = programclient.GetByRowKey(geoindex.RowKey);

                    if (job.Status == "Live" || (demomode == true && job.Status == "Demo"))
                    {
                        bool filtered = false;
                        //Apply other filters
                        if (gender != "" && job.Gender != null && job.Gender != "")
                        {
                            if (job.Gender != gender)
                            {
                                filtered = true;
                            }
                        }

                        if (!filtered)
                        {
                            DirectEmployerClientCampus campus   = campusclient.GetByRowKey(geoindex.CampusRowKey);
                            DirectEmployerClient       employer = clientclient.GetByRowKey(geoindex.ClientRowKey);

                            bool acceptingleads = false;

                            if (employer.Status == "Live" || (demomode == true && employer.Status == "Demo"))
                            {
                                if (campus.Status == "Live" || (demomode == true && campus.Status == "Demo"))
                                {
                                    //List<DirectEmployerClientCampusProgram> campusprograms = new List<DirectEmployerClientCampusProgram>(programclient.GetAllByClientId(employer.RowKey).Where(x => x.CampusRowKey == campus.RowKey));
                                    if (leadcounters == null)
                                    {
                                        leadcounters = new List <LeadCounter>(leadcounterclient.GetAll());
                                        leadcaps     = new List <LeadCap>(leadcapclient.GetAll());
                                    }

                                    LeadCap     schoolcap      = leadcaps.Find(x => x.RowKey == employer.RowKey);
                                    LeadCap     programcap     = leadcaps.Find(x => x.RowKey == job.RowKey);
                                    LeadCounter schoolcounter  = leadcounters.Find(x => x.RowKey == employer.RowKey);
                                    LeadCounter programcounter = leadcounters.Find(x => x.RowKey == job.RowKey);

                                    acceptingleads = AcceptingLeads(schoolcap, programcap, schoolcounter, programcounter);
                                    if (acceptingleads)
                                    {
                                        results.Add(new
                                        {
                                            clienttype   = employer.ClientType,
                                            clientid     = employer.ClientId,
                                            clientsetid  = employer.ClientId,
                                            logoclientid = employer.ClientId,
                                            program      = programid,
                                            formid       = employer.FormId,
                                            distance     = 0,
                                            campustype   = campus.CampusType,
                                            campuskey    = campus.CampusId,
                                            programkey   = geoindex.RowKey,
                                            clientrowkey = employer.RowKey,

                                            jobkey      = job.RowKey,
                                            jobtitle    = job.JobTitle,
                                            company     = employer.Name.ToJSONSafeString(),
                                            companylogo = "https://chaindate.blob.core.windows.net/resources/employerlogos/" + employer.ClientType + employer.RowKey,
                                            city        = campus.City,
                                            state       = campus.State,
                                            date        = job.PostedDateTime.ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT",
                                            snippet     = job.Description,

                                            attributescore       = oattributes != null ? job.GetAttributeScore(oattributes) : 0,
                                            importantthingsscore = othings != null ? job.GetImportantThingsScore(othings) : 0
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(results);
        }
        public List <object> FindSchools(HttpRequestBase Request, string zipcode, string programcategoryid, string edulevel, ref List <string> directclientnames)
        {
            bool demomode = Request["demo"] != null && Request["demo"] == "1";

            LeadCounterClient  leadcounterclient = new LeadCounterClient();
            List <LeadCounter> leadcounters      = null;

            LeadCapClient  leadcapclient = new LeadCapClient();
            List <LeadCap> leadcaps      = null;

            List <object> results = new List <object>();

            if (programcategoryid.StartsWith("c"))
            {
                return(results);
            }


            ZipCodeClient zipclient = new ZipCodeClient();
            ZipCode       zipobject = zipclient.GetByRowKey(zipcode);

            GeoIndexNationalClient      nclient             = new GeoIndexNationalClient();
            GeoIndexAddStateClient      addstateclient      = new GeoIndexAddStateClient();
            GeoIndexAddZipClient        addzipclient        = new GeoIndexAddZipClient();
            GeoIndexSubtractStateClient subtractstateclient = new GeoIndexSubtractStateClient();
            GeoIndexSubtractZipClient   subtractzipclient   = new GeoIndexSubtractZipClient();

            List <IGeoIndex> allresults = new List <IGeoIndex>();

            allresults.AddRange(nclient.GetAllByPartition(programcategoryid));

            if (zipobject != null)
            {
                allresults.AddRange(addstateclient.GetAllByPartition(zipobject.StateCode + "-" + programcategoryid));
                allresults.AddRange(addzipclient.GetAllByPartition(zipcode + "-" + programcategoryid));
            }

            List <IGeoIndex> subtractions = new List <IGeoIndex>();

            if (zipobject != null)
            {
                subtractions.AddRange(subtractstateclient.GetAllByPartition(zipobject.StateCode + "-" + programcategoryid));
                subtractions.AddRange(subtractzipclient.GetAllByPartition(zipcode + "-" + programcategoryid));
            }

            IEnumerable <IGeoIndex> difference = allresults.Except(subtractions, new CompareGeoIndexResults());

            IEnumerable <IGrouping <string, IGeoIndex> > bycampus = difference.GroupBy(x => x.CampusRowKey);

            DirectSchoolClientClient              clientclient  = new DirectSchoolClientClient();
            DirectSchoolClientCampusClient        campusclient  = new DirectSchoolClientCampusClient();
            DirectSchoolClientCampusProgramClient programclient = new DirectSchoolClientCampusProgramClient();

            foreach (IGrouping <string, IGeoIndex> campusgroup in bycampus)
            {
                DirectSchoolClientCampus campus = campusclient.GetByRowKey(campusgroup.Key);
                DirectSchoolClient       school = clientclient.GetByRowKey(campus.ClientRowKey);
                bool acceptingleads             = false;

                if (school.Status == "Live" || (demomode == true && school.Status == "Demo"))
                {
                    if (campus.Status == "Live" || (demomode == true && campus.Status == "Demo"))
                    {
                        List <DirectSchoolClientCampusProgram> campusprograms = new List <DirectSchoolClientCampusProgram>(programclient.GetAllBySchoolId(school.RowKey).Where(x => x.CampusRowKey == campus.RowKey));

                        string        programkey    = "";
                        bool          gotprogramkey = false;
                        List <object> programitems  = new List <object>();
                        foreach (IGeoIndex programresult in campusgroup)
                        {
                            DirectSchoolClientCampusProgram program = campusprograms.SingleOrDefault(x => x.RowKey == programresult.RowKey);

                            if (program.Status == "Live" || (demomode == true && program.Status == "Demo"))
                            {
                                if (leadcounters == null)
                                {
                                    leadcounters = new List <LeadCounter>(leadcounterclient.GetAll());
                                    leadcaps     = new List <LeadCap>(leadcapclient.GetAll());
                                }

                                LeadCap     schoolcap      = leadcaps.Find(x => x.RowKey == school.RowKey);
                                LeadCap     programcap     = leadcaps.Find(x => x.RowKey == program.RowKey);
                                LeadCounter schoolcounter  = leadcounters.Find(x => x.RowKey == school.RowKey);
                                LeadCounter programcounter = leadcounters.Find(x => x.RowKey == program.RowKey);

                                //see if counters need to be reset
                                schoolcounter.ResetCountersIfNeeded(leadcounterclient);
                                programcounter.ResetCountersIfNeeded(leadcounterclient);

                                acceptingleads = AcceptingLeads(schoolcap, programcap, schoolcounter, programcounter);
                                if (acceptingleads)
                                {
                                    programitems.Add(new { id = program.ProgramId + "." + program.RowKey, type = program.ProgramType, typename = program.ResolveProgramTypeName(), isprimary = 1, name = program.Name.ToJSONSafeString() });
                                    if (!gotprogramkey)
                                    {
                                        if (",hs,ged,crt,sc,as,inhs,none,".Contains("," + edulevel.ToLower() + ","))
                                        {
                                            string testprogramkey = program.ProgramType;
                                            //Exclude MS
                                            if (!(",ms,ma,maed,mba,me,mfa,ms,mst,".Contains("," + testprogramkey.ToLower() + ",")))
                                            {
                                                programkey    = HttpUtility.UrlDecode(program.ProgramId);
                                                gotprogramkey = true;
                                            }
                                        }
                                        else
                                        {
                                            programkey    = HttpUtility.UrlDecode(program.ProgramId);
                                            gotprogramkey = true;
                                        }
                                    }
                                }
                            }
                        }

                        if (acceptingleads)
                        {
                            if (!directclientnames.Contains(school.Name))
                            {
                                directclientnames.Add(school.Name);
                            }

                            results.Add(new
                            {
                                clienttype         = "direct",
                                formname           = school.Name.ToJSONSafeString(),
                                clientfrontendname = school.Name.ToJSONSafeString(),
                                formdescription    = school.Description.ToJSONSafeString(),
                                city         = campus.City,
                                state        = campus.State,
                                image        = "https://chaindate.blob.core.windows.net/resources/schoollogos/" + school.RowKey,
                                clientid     = school.ClientId,
                                clientsetid  = school.ClientId,
                                logoclientid = school.ClientId,
                                program      = programcategoryid,
                                formid       = school.FormId,
                                distance     = 0,
                                campustype   = campus.CampusType,
                                campuskey    = campus.CampusId,
                                programs     = programitems.ToArray(),
                                programkey   = programkey,
                                clientrowkey = school.RowKey
                            });
                        }
                    }
                }
            }

            return(results);
        }