Ejemplo n.º 1
0
        //performs an aggregation search based on a single aggregation (can be used to remove aggregation as well
        public static void Perform_Aggregation_Search(string[] aggregationIds, Custom_Tracer Tracer)
        {
            //hooks (eventual expansion) 
            int HOOK_maxFIDCount = 8; 

            #region FIDs Support

            //get fids
            
            //old
            if (HttpContext.Current.Session["FIDKey"] == null)
                HttpContext.Current.Session["FIDKey"] = ""; //init
            string FIDKey = HttpContext.Current.Session["FIDKey"].ToString();
            List<string> FIDs = (List<string>)HttpContext.Current.Cache[FIDKey];
            List<string> temp_FIDs = new List<string>();
            foreach (string FID in FIDs)
            {
                //add the metadata name (converted to dbColumn name format)
                temp_FIDs.Add(FID.Replace(" ", "_"));
            }

            //make sure we have alll the FIDs, if not create blank holders
            if (HOOK_maxFIDCount > temp_FIDs.Count)
            {
                while (HOOK_maxFIDCount > temp_FIDs.Count)
                {
                    temp_FIDs.Add("");
                }
            }
            

            #endregion

            #region MSRKey Support

            int MSRKeyHashSpecial = 0;
            foreach (string aggregationId in aggregationIds)
            {
                byte[] tempAggChars = Encoding.ASCII.GetBytes(aggregationId);
                foreach (byte tempAggChar in tempAggChars)
                {
                    MSRKeyHashSpecial += Convert.ToInt32(tempAggChar);
                }
            }
            //finish processing msrkeyhash and store
            MSRKeyHashSpecial = Convert.ToInt32(MSRKeyHashSpecial * aggregationIds.Length);
            HttpContext.Current.Session["MapSearchResultsKey"] = "MapSearchResults_" + MSRKeyHashSpecial.ToString();
            
            #endregion

            #region search the database for all items in all of the provided aggregationPermissions and merge them into one datatable

            //define MSR support objects
            DataTable searchResults = new DataTable();
            DataTable displaySearchResults = new DataTable();
            
            //create MSR columns
            searchResults.Columns.Add("ItemID", typeof(string));
            searchResults.Columns.Add("Point_Latitude", typeof(string));
            searchResults.Columns.Add("Point_Longitude", typeof(string));
            searchResults.Columns.Add("Start_DateTime", typeof(string));
            searchResults.Columns.Add("End_DateTime", typeof(string));
            #region create MSR filter columns
            //get the db column name for each filter
            foreach (string filterName in FIDs)
            {
                searchResults.Columns.Add(filterName.Replace(" ", "_"), typeof(string));
            }

            //determine if we have used all 8 of the real filters, if not, create gap (8 + x where x= non fid column count to date)
            for (int i = searchResults.Columns.Count; i < (8 + 5); i++)
            {
                searchResults.Columns.Add("empty" + i.ToString(), typeof(string));
                //FIDs.Add("empty" + i.ToString());
                FIDs.Add("");
            }
            #endregion

            //create DSR columns
            displaySearchResults.Columns.Add("ItemID", typeof(string));
            displaySearchResults.Columns.Add("Point_Latitude", typeof(string));
            displaySearchResults.Columns.Add("Point_Longitude", typeof(string));
            
            //check to see if we already have msr in cache, else get a new msr
            string MSRKey = HttpContext.Current.Session["MapSearchResultsKey"].ToString();
            if (HttpContext.Current.Cache[MSRKey] == null)
            {
                #region Create New MSR
                
                //temp objects
                List<DataTable> temp_Tables = new List<DataTable>(); //change to dataset?
                DataTable temp_searchResults = new DataTable();

                //holds bounds calculator fields
                double swx = -1;
                double swy = -1;
                double nex = -1;
                double ney = -1;

                //search for all items in each collection (handles multiple aggregationPermissions)
                foreach (string aggregationId in aggregationIds)
                {
                    temp_Tables.Add(SobekCM_Database.Get_All_Items_By_AggregationID(aggregationId, temp_FIDs, Tracer));
                }

                //merge the tables if there are multiple aggregationPermissions
                foreach (DataTable temp_Table in temp_Tables)
                {
                    temp_searchResults.Merge(temp_Table);
                }

                //go through each item from search and it and its metadata to MSR
                foreach (DataRow searchResult in temp_searchResults.Rows)
                {
                    //define holders
                    string a = String.Empty;
                    string b = String.Empty;
                    string c = String.Empty;
                    string d = String.Empty;
                    string e = String.Empty;

                    //get itemID
                    if(searchResult["ItemID"]!=null)
                        a = searchResult["ItemID"].ToString();

                    //get lat/long
                    if (searchResult["MainLatitude"] != null)
                        b = searchResult["MainLatitude"].ToString();
                    if (searchResult["MainLongitude"] != null)
                        c = searchResult["MainLongitude"].ToString();

                    //fallback to spatial kml
                    if (string.IsNullOrEmpty(b))
                    {
                        string[] temp = searchResult["Spatial_KML"].ToString().Split('|');
                        //is this a point?
                        if (temp.Length == 2)
                        {
                            string[] temp2 = temp[1].Split(',');
                            b = temp2[0]; //lat
                            c = temp2[1]; //lng
                        }
                    }

                    //work with bounds calculator
                    if (!string.IsNullOrEmpty(b))
                    {
                        if ((Convert.ToDouble(b) < swx) || (swx == -1))
                            swx = Convert.ToDouble(b);
                        if ((Convert.ToDouble(b) > nex) || (swy == -1))
                            nex = Convert.ToDouble(b);
                        if ((Convert.ToDouble(c) < swy) || (nex == -1))
                            swy = Convert.ToDouble(c);
                        if ((Convert.ToDouble(c) > ney) || (ney == -1))
                            ney = Convert.ToDouble(c);
                    }

                    //handle start datetime
                    if (searchResult["PubDate"] != null)
                        d = searchResult["PubDate"].ToString();
                    else
                        if (searchResult["CreateDate"] != null)
                            d = searchResult["CreateDate"].ToString();

                    //handle end datetime
                    if (searchResult["PubDate"] != null)
                        e = searchResult["PubDate"].ToString();
                    else
                        if (searchResult["CreateDate"] != null)
                            e = searchResult["CreateDate"].ToString();

                    List<string> filterValues = new List<string>();
                    foreach (string filterName in FIDs)
                    {
                        //hande empty filter slots
                        //if (filterName.IndexOf("empty") != -1)
                        if (string.IsNullOrEmpty(filterName))
                            filterValues.Add("");
                        else
                            filterValues.Add(searchResult[filterName.Replace(" ", "_")].ToString());
                    }

                    //add to the DSR (id/lat/lng) only if lat/lng not null
                    if (!string.IsNullOrEmpty(b))
                        displaySearchResults.Rows.Add(a, b, c);

                    //add all to search results (with 8 fids)
                    searchResults.Rows.Add(a, b, c, d, e, filterValues[0], filterValues[1], filterValues[2], filterValues[3], filterValues[4], filterValues[5], filterValues[6], filterValues[7]);

                }

                //add to collection level params to search results
                searchResults.Rows.Add("swBounds", swx.ToString(), swy.ToString(), "", "", "", "", "", "", "", "", "", "");
                searchResults.Rows.Add("neBounds", nex.ToString(), ney.ToString(), "", "", "", "", "", "", "", "", "", "");

                //add to collection level params to display search results
                displaySearchResults.Rows.Add("swBounds", swx.ToString(), swy.ToString());
                displaySearchResults.Rows.Add("neBounds", nex.ToString(), ney.ToString());

                //assign and hold the current search result datatable, from now on we will be using this as the base layer..
                HttpContext.Current.Cache[MSRKey] = searchResults;
                HttpContext.Current.Cache[MSRKey + "_Created"] = DateTime.Now;

                #endregion
            }
            else
            {
                if (HttpContext.Current.Cache[MSRKey + "_Created"] != null)
                {
                    //determine if MSR is more than 30 mins old
                    if ((DateTime.Now.Subtract(Convert.ToDateTime(HttpContext.Current.Cache[MSRKey + "_Created"].ToString())).TotalMinutes) > 30)
                    {
                        #region Create new MSR

                        //temp objects
                        List<DataTable> temp_Tables = new List<DataTable>(); //change to dataset?
                        DataTable temp_searchResults = new DataTable();

                        //holds bounds calculator fields
                        double swx = -1;
                        double swy = -1;
                        double nex = -1;
                        double ney = -1;

                        //search for all items in each collection (handles multiple aggregationPermissions)
                        foreach (string aggregationId in aggregationIds)
                        {
                            temp_Tables.Add(SobekCM_Database.Get_All_Items_By_AggregationID(aggregationId, temp_FIDs, Tracer));
                        }

                        //merge the tables if there are multiple aggregationPermissions
                        foreach (DataTable temp_Table in temp_Tables)
                        {
                            temp_searchResults.Merge(temp_Table);
                        }

                        //go through each item from search and it and its metadata to MSR
                        foreach (DataRow searchResult in temp_searchResults.Rows)
                        {
                            //get itemID
                            string a = searchResult["ItemID"].ToString();

                            //get lat/long
                            string b = searchResult["MainLatitude"].ToString();
                            string c = searchResult["MainLongitude"].ToString();

                            //fallback to spatial kml
                            if (string.IsNullOrEmpty(b))
                            {
                                string[] temp = searchResult["Spatial_KML"].ToString().Split(',');
                                //is this a point?
                                if (temp.Length == 2)
                                {
                                    b = temp[0].Replace("P|", "").Replace("A|", "");
                                    c = temp[1];
                                }
                            }

                            //work with bounds calculator
                            if (!string.IsNullOrEmpty(b))
                            {
                                if ((Convert.ToDouble(b) < swx) || (swx == -1))
                                    swx = Convert.ToDouble(b);
                                if ((Convert.ToDouble(b) > nex) || (swy == -1))
                                    nex = Convert.ToDouble(b);
                                if ((Convert.ToDouble(c) < swy) || (nex == -1))
                                    swy = Convert.ToDouble(c);
                                if ((Convert.ToDouble(c) > ney) || (ney == -1))
                                    ney = Convert.ToDouble(c);
                            }

                            //handle date time
                            string d = searchResult["PubDate"].ToString();
                            if (string.IsNullOrEmpty(d))
                                d = searchResult["CreateDate"].ToString();
                            //d = Convert.ToDateTime(d);
                            string e = searchResult["PubDate"].ToString();
                            if (string.IsNullOrEmpty(e))
                                e = searchResult["CreateDate"].ToString();

                            List<string> filterValues = new List<string>();
                            foreach (string filterName in FIDs)
                            {
                                //hande empty filter slots
                                //if (filterName.IndexOf("empty") != -1)
                                if (string.IsNullOrEmpty(filterName))
                                    filterValues.Add("");
                                else
                                    filterValues.Add(searchResult[filterName.Replace(" ", "_")].ToString());
                            }

                            //add to the DSR (id/lat/lng) only if lat/lng not null
                            if (!string.IsNullOrEmpty(b))
                                displaySearchResults.Rows.Add(a, b, c);

                            //add all to search results (with 8 fids)
                            searchResults.Rows.Add(a, b, c, d, e, filterValues[0], filterValues[1], filterValues[2], filterValues[3], filterValues[4], filterValues[5], filterValues[6], filterValues[7]);

                        }

                        //add to collection level params to search results
                        searchResults.Rows.Add("swBounds", swx.ToString(), swy.ToString(), "", "", "", "", "", "", "", "", "", "");
                        searchResults.Rows.Add("neBounds", nex.ToString(), ney.ToString(), "", "", "", "", "", "", "", "", "", "");

                        //add to collection level params to display search results
                        displaySearchResults.Rows.Add("swBounds", swx.ToString(), swy.ToString());
                        displaySearchResults.Rows.Add("neBounds", nex.ToString(), ney.ToString());

                        //assign and hold the current search result datatable, from now on we will be using this as the base layer..
                        HttpContext.Current.Cache[MSRKey] = searchResults;
                        HttpContext.Current.Cache[MSRKey + "_Created"] = DateTime.Now;

                        #endregion
                    }
                    else
                    {
                        #region Use Existing MSR

                        //assign cached MSR to active MSR
                        searchResults = HttpContext.Current.Cache[MSRKey] as DataTable;

                        //add to the DSR
                        foreach (DataRow searchResult in searchResults.Rows)
                        {
                            //get itemID
                            string a = searchResult["ItemID"].ToString();

                            //get lat/long
                            string b = searchResult["Point_Latitude"].ToString();
                            string c = searchResult["Point_Longitude"].ToString();

                            //add to the DSR (id/lat/lng) only if lat/lng not null
                            if (!string.IsNullOrEmpty(b))
                                displaySearchResults.Rows.Add(a, b, c);
                        }

                        #endregion
                    }
                }
            }
            
            //call json writer with these results
            object DSR = Create_JSON_Search_Results_Object(displaySearchResults);

            //send json obj to page and tell page to read it (via callback results)
            HttpContext.Current.Items["DSR"] = DSR;

            #endregion

        }