public ActionResult MatchJobs(string zip, string attributes, string things)
        {
            try
            {
                ZipCodeClient zcc     = new ZipCodeClient();
                ZipCode       zipcode = zcc.GetByRowKey(zip);

                if (zipcode == null)
                {
                    throw new Exception("Invalid ZIP");
                }

                JObject oattributes = JObject.Parse(attributes);
                JObject othings     = JObject.Parse(things);

                AttributeMaskCalculator amc = new AttributeMaskCalculator();
                long attributemask          = amc.GetMask(oattributes);

                ImportantThingsMaskCalculator itmc = new ImportantThingsMaskCalculator();
                long importantthingsmask           = itmc.GetMask(othings);

                JobPostingClient jpc = new JobPostingClient();

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

                foreach (JobPosting posting in jpc.GetAllBySource("TEN").Execute().Where(x => x.IsLive != null && x.IsLive == true))
                {
                    if (posting.AttributeMask != null && (posting.AttributeMask & attributemask) == attributemask)
                    {
                        bool targetsusa   = false;
                        bool targetsstate = false;
                        bool targetszip   = false;

                        if (posting.GeoTarget != null)
                        {
                            targetsusa   = posting.GeoTarget.Contains("USA");
                            targetsstate = posting.GeoTarget.Contains(zipcode.StateCode);
                            targetszip   = posting.GeoTarget.Contains(zipcode.RowKey);
                        }
                        else
                        {
                            Console.Write("geo target not set");
                        }

                        if (targetsusa || targetsstate || targetszip)
                        {
                            try
                            {
                                jobs.Add(new
                                {
                                    id                = posting.RowKey,
                                    jobtitle          = posting.JobTitle != null ? posting.JobTitle.ToJSONSafeString() : "",
                                    description       = posting.Description != null ? posting.Description.ToJSONSafeString() : "",
                                    clientid          = posting.ClientId,
                                    clientname        = posting.CompanyName != null ? posting.CompanyName.ToJSONSafeString() : "",
                                    formattedlocation = posting.FormattedLocation != null ? posting.FormattedLocation.ToJSONSafeString() : "",
                                    logoimage         = posting.Logo,

                                    formid      = posting.FormId,
                                    campuskey   = posting.PositionLocationId,
                                    campusvalue = posting.PositionLocationName != null ? posting.PositionLocationName.ToJSONSafeString() : "",

                                    //zips = node.E("zips"),
                                    //keywords = node.E("keywords"),
                                    date            = posting.PostedDateTime != null ? posting.PostedDateTime.ToString("dd/MM/yyyy HH:mm") : "",
                                    referencenumber = posting.ReferenceId,

                                    dimattitude      = posting.DimensionAttitude,
                                    dimaction        = posting.DimensionAction,
                                    dimcompensation  = posting.DimensionCompensation,
                                    dimconcentration = posting.DimensionConcentration,
                                    dimendurance     = posting.DimensionEndurance,
                                    diminformation   = posting.DimensionInformation,
                                    dimpatterns      = posting.DimensionPatterns,
                                    dimprocessing    = posting.DimensionProcessing,
                                    dimpresence      = posting.DimensionPresence,

                                    thingadmiration  = posting.ImportantThingsAdmiration,
                                    thingadventure   = posting.ImportantThingsAdventure,
                                    thinganimals     = posting.ImportantThingsAnimals,
                                    thingbeauty      = posting.ImportantThingsBeauty,
                                    thingcompetition = posting.ImportantThingsCompetition,

                                    thingcreativity = posting.ImportantThingsCreativity,
                                    thingduty       = posting.ImportantThingsDuty,
                                    thingeasy       = posting.ImportantThingsEasy,
                                    thinggrowth     = posting.ImportantThingsGrowth,
                                    thinghelping    = posting.ImportantThingsHelping,

                                    thingpeople     = posting.ImportantThingsPeople,
                                    thingpolitics   = posting.ImportantThingsPolitics,
                                    thingsafety     = posting.ImportantThingsSafety,
                                    thingscience    = posting.ImportantThingsScience,
                                    thingtechnology = posting.ImportantThingsTechnology,

                                    attributescore       = posting.GetAttributeScore(oattributes),
                                    importantthingsscore = posting.GetImportantThingsScore(othings)
                                });
                            }
                            catch (Exception ex)
                            {
                                Console.Write(ex.Message);
                            }
                        }
                    }
                }


                Response.ContentType = "application/json";
                string retvalue = (new { result = "ok", jobs = jobs.ToArray() }).ToJSON();
                Response.Write(retvalue);
                Response.End();
            }
            catch (Exception ex)
            {
                Response.ContentType = "application/json";
                Response.Write(DefaultErrorResponse(ex.Message));
                Response.End();
            }

            return(null);
        }
        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);
        }