Exemplo n.º 1
0
        public string GetSubscriptionMatchHTMLSummary(SubscriptionMatchGroup subscriptionResults)
        {
            int    maxItemsShown = 20;
            var    summaryHTML   = "";
            string siteUrl       = "https://openchargemap.org/site";
            string poiLinkText   = "View POI Details";

            if (subscriptionResults.SubscriptionMatches == null || subscriptionResults.SubscriptionMatches.Count == 0)
            {
                return("<p>No Subscription Matches</p>");
            }
            summaryHTML += "<p>This subscription matches the following items which have been updated since " + subscriptionResults.DateFrom.ToString() + ": <ul>";
            foreach (var match in subscriptionResults.SubscriptionMatches)
            {
                //output a summary
                summaryHTML += "<li><a href='#" + match.Category.ToString() + "'>" + match.Description + " (" + match.ItemList.Count + ")</a></li>";
            }

            summaryHTML += "</ul>";

            foreach (var match in subscriptionResults.SubscriptionMatches)
            {
                if (match.ItemList != null && match.ItemList.Count > 0)
                {
                    summaryHTML += "<h3 id='" + match.Category.ToString() + "'>" + match.Description + " (" + match.ItemList.Count + ")</h3>";

                    if (match.Category == SubscriptionMatchCategory.ChargingRequestEmergency)
                    {
                        //emergency charging
                        foreach (var i in match.ItemList)
                        {
                            var r = (Core.Data.UserChargingRequest)i.Item;
                            summaryHTML += "<div class='result'><blockquote>" + r.Comment + "</blockquote><p>" + (r.User != null ? r.User.Username : "******") + " - " + r.DateCreated.ToShortDateString() + "</p></div>";
                        }
                    }

                    if (match.Category == SubscriptionMatchCategory.ChargingRequestGeneral)
                    {
                        //general charging
                        foreach (var i in match.ItemList)
                        {
                            var r = (Core.Data.UserChargingRequest)i.Item;
                            summaryHTML += "<div class='result'><blockquote>" + r.Comment + "</blockquote><p>" + (r.User != null ? r.User.Username : "******") + " - " + r.DateCreated.ToShortDateString() + "</p></div>";
                        }
                    }

                    if (match.Category == SubscriptionMatchCategory.EditedPOI)
                    {
                        //edits
                        summaryHTML += "<a href='" + siteUrl + "/editqueue'>View Edit Queue</a> <ul>";
                        foreach (var i in match.ItemList)
                        {
                            var item = (Core.Data.EditQueueItem)i.Item;
                            var poi  = (ChargePoint)i.POI;
                            summaryHTML += "<li>OCM-" + poi.ID + ": " + poi.AddressInfo.Title + " : " + poi.AddressInfo.ToString().Replace(",", ", ") + " <a href='" + siteUrl + "/poi/details/" + poi.ID + "'>" + poiLinkText + "</a></li>";
                        }
                        summaryHTML += "</ul>";
                    }

                    if (match.Category == SubscriptionMatchCategory.NewPOI)
                    {
                        //POI list

                        summaryHTML += "<ul>";
                        foreach (var p in match.ItemList.Take(maxItemsShown))
                        {
                            var poi = (ChargePoint)p.POI;
                            summaryHTML += "<li>OCM-" + poi.ID + ": " + poi.AddressInfo.Title + " : " + poi.AddressInfo.ToString().Replace(",", ", ") + " <a href='" + siteUrl + "/poi/details/" + poi.ID + "'>" + poiLinkText + "</a></li>";
                        }
                        summaryHTML += "</ul>";

                        if (match.ItemList.Count > maxItemsShown)
                        {
                            summaryHTML += "<p>In addition there were " + (match.ItemList.Count - maxItemsShown) + " more items added.</p>";
                        }
                    }

                    if (match.Category == SubscriptionMatchCategory.UpdatedPOI)
                    {
                        //updates
                        summaryHTML += "<ul>";
                        foreach (var i in match.ItemList.Take(maxItemsShown))
                        {
                            var item = (Core.Data.EditQueueItem)i.Item;
                            var poi  = (ChargePoint)i.POI;
                            summaryHTML += "<li>OCM-" + poi.ID + ": " + poi.AddressInfo.Title + " : " + poi.AddressInfo.ToString().Replace(",", ", ") + " <a href='" + siteUrl + "/poi/details/" + poi.ID + "'>" + poiLinkText + "</a></li>";
                        }
                        summaryHTML += "</ul>";

                        if (match.ItemList.Count > maxItemsShown)
                        {
                            summaryHTML += "<p>In addition there were " + (match.ItemList.Count - maxItemsShown) + " more items updated.</p>";
                        }
                    }

                    if (match.Category == SubscriptionMatchCategory.NewComment)
                    {
                        //comment list
                        summaryHTML += "<div class='results-list'>";

                        foreach (var i in match.ItemList)
                        {
                            var comment = (UserComment)i.Item;
                            var poi     = (ChargePoint)i.POI;
                            summaryHTML += "<div class='result'>" + FormatPOIHeading(poi) + "<div style='padding:0.2em;'>" + (!String.IsNullOrEmpty(comment.UserName) ? comment.UserName : "******") + " - " + comment.DateCreated.ToShortDateString() + "<br/>";
                            if (!String.IsNullOrEmpty(comment.Comment))
                            {
                                summaryHTML += "<blockquote>";
                                summaryHTML += comment.Comment;
                                summaryHTML += "</blockquote>";
                            }
                            if (comment.Rating != null)
                            {
                                summaryHTML += "" + comment.Rating + " out of 5";
                            }
                            if (comment.CommentType != null)
                            {
                                summaryHTML += " <span class='label label-info'>" + comment.CommentType.Title + "</span>";
                            }
                            if (comment.CheckinStatusType != null)
                            {
                                summaryHTML += " <span class='label " + (comment.CheckinStatusType.IsPositive == true ? "label-success" : "label-danger") + "'>" + comment.CheckinStatusType.Title + "</span>";
                            }

                            summaryHTML += "</div></div>";
                        }
                        summaryHTML += "</div>";
                    }

                    if (match.Category == SubscriptionMatchCategory.NewMediaUpload)
                    {
                        //media list
                        summaryHTML += "<div class='results-list'>";

                        foreach (var i in match.ItemList)
                        {
                            var item = (MediaItem)i.Item;
                            var poi  = (ChargePoint)i.POI;
                            summaryHTML += "<div class='result'>" + FormatPOIHeading(poi) + "<div style='padding:0.2em;'>" + (item.User != null ? item.User.Username : "******") + " - " + item.DateCreated.ToShortDateString() + "<br/><blockquote><img src='" + item.ItemThumbnailURL + "'/>" + item.Comment + "</blockquote></div></div>";
                        }
                        summaryHTML += "</div>";
                    }
                }
            }

            return(summaryHTML);
        }
Exemplo n.º 2
0
        public SubscriptionMatchGroup GetSubscriptionMatches(OCM.Core.Data.OCMEntities dataModel, POIManager poiManager, OCM.Core.Data.UserSubscription subscription, DateTime?dateFrom = null)
        {
            SubscriptionMatchGroup subscriptionMatchGroup = new SubscriptionMatchGroup();

            var checkFromDate = DateTime.UtcNow.AddMinutes(-CHECK_INTERVAL_MINS); //check from last 5 mins etc

            if (subscription.DateLastNotified != null)
            {
                checkFromDate = subscription.DateLastNotified.Value;                                        //check from date last notified
            }
            else
            {
                checkFromDate = subscription.DateCreated;
            }

            if (dateFrom != null)
            {
                checkFromDate = dateFrom.Value;
            }

            subscriptionMatchGroup.DateFrom = checkFromDate;

            System.Data.Entity.Spatial.DbGeography searchPos = null;

            UserSubscriptionFilter filter = null;

            if (subscription.FilterSettings != null)
            {
                try
                {
                    filter = JsonConvert.DeserializeObject <UserSubscriptionFilter>(subscription.FilterSettings);
                }
                catch (Exception)
                {
                    //failed to parse subscription filter
                }
            }

            if (subscription.Latitude != null && subscription.Longitude != null)
            {
                searchPos = System.Data.Entity.Spatial.DbGeography.PointFromText("POINT(" + subscription.Longitude + " " + subscription.Latitude + ")", 4326);
            }

            if (subscription.NotifyEmergencyChargingRequests)
            {
                var emergencyCharging = dataModel.UserChargingRequests.Where(c => c.DateCreated >= checkFromDate && c.IsActive == true && c.IsEmergency == true);
                var subscriptionMatch = new SubscriptionMatch {
                    Category = SubscriptionMatchCategory.ChargingRequestEmergency, Description = "New Emergency Charging Requests"
                };

                foreach (var chargingRequest in emergencyCharging)
                {
                    //filter on location
                    if (searchPos != null)
                    {
                        if (GeoManager.CalcDistance(chargingRequest.Latitude, chargingRequest.Longitude, (double)searchPos.Latitude, (double)searchPos.Longitude, DistanceUnit.KM) < subscription.DistanceKM)
                        {
                            subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                Item = chargingRequest
                            });
                        }
                    }
                    else
                    {
                        subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                            Item = chargingRequest
                        });
                    }
                }
                if (subscriptionMatch.ItemList.Any())
                {
                    subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                }
            }

            if (subscription.NotifyGeneralChargingRequests)
            {
                //TODO: subscription not filtered on lat/long will return global charging requests
                var generalCharging   = dataModel.UserChargingRequests.Where(c => c.DateCreated >= checkFromDate && c.IsActive == true && c.IsEmergency == false);
                var subscriptionMatch = new SubscriptionMatch {
                    Category = SubscriptionMatchCategory.ChargingRequestGeneral, Description = "New Charging Requests"
                };
                //filter on location
                foreach (var gc in generalCharging)
                {
                    if (searchPos != null)
                    {
                        if (GeoManager.CalcDistance(gc.Latitude, gc.Longitude, (double)searchPos.Latitude, (double)searchPos.Longitude, DistanceUnit.KM) < subscription.DistanceKM)
                        {
                            subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                Item = gc
                            });
                        }
                    }
                    else
                    {
                        subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                            Item = gc
                        });
                    }
                }

                if (subscriptionMatch.ItemList.Any())
                {
                    subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                }
            }

            //check if any POI Edits (pending approval) match this subscription
            if (subscription.NotifyPOIEdits)
            {
                var poiEdits = dataModel.EditQueueItems.Where(c => c.DateSubmitted >= checkFromDate && c.PreviousData != null && c.IsProcessed == false);
                if (poiEdits.Any())
                {
                    var subscriptionMatch = new SubscriptionMatch {
                        Category = SubscriptionMatchCategory.EditedPOI, Description = "Proposed POI Edits"
                    };
                    foreach (var p in poiEdits)
                    {
                        try
                        {
                            var updatedPOI = JsonConvert.DeserializeObject <ChargePoint>(p.EditData);
                            if (IsPOISubscriptionFilterMatch(updatedPOI, filter, subscription))
                            {
                                if (searchPos != null)
                                {
                                    if (GeoManager.CalcDistance(updatedPOI.AddressInfo.Latitude, updatedPOI.AddressInfo.Longitude, (double)searchPos.Latitude, (double)searchPos.Longitude, DistanceUnit.KM) < subscription.DistanceKM)
                                    {
                                        subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                            Item = p, POI = updatedPOI
                                        });
                                    }
                                }
                                else
                                {
                                    subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                        Item = p, POI = updatedPOI
                                    });
                                }
                            }
                        }
                        catch (Exception)
                        {
                            ;;
                        }
                    }
                    if (subscriptionMatch.ItemList.Any())
                    {
                        subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                    }
                }
            }

            //check if any new POIs
            if (subscription.NotifyPOIAdditions)
            {
                /* var newPOIs = dataModel.ChargePoints.Where(c => c.DateCreated >= checkFromDate && c.SubmissionStatusType.IsLive == true &&
                 *    (searchPos == null ||
                 *       (searchPos != null &&
                 *           c.AddressInfo.SpatialPosition.Distance(searchPos) / 1000 < subscription.DistanceKM
                 *       ))
                 *     ).ToList();*/

                var filterParams = new APIRequestParams {
                    CreatedFromDate = checkFromDate, AllowMirrorDB = true
                };
                if (searchPos != null)
                {
                    filterParams.DistanceUnit = DistanceUnit.KM;
                    filterParams.Distance     = subscription.DistanceKM;
                    filterParams.Latitude     = searchPos.Latitude;
                    filterParams.Longitude    = searchPos.Longitude;
                }
                if (subscription.CountryID != null)
                {
                    filterParams.CountryIDs = new int[] { (int)subscription.CountryID };
                }
                var poiCollection = poiManager.GetChargePoints(filterParams);

                if (poiCollection.Any())
                {
                    var subscriptionMatch = new SubscriptionMatch {
                        Category = SubscriptionMatchCategory.NewPOI, Description = "New POIs Added"
                    };
                    foreach (var p in poiCollection)
                    {
                        //var poi = OCM.API.Common.Model.Extensions.ChargePoint.FromDataModel(p);
                        if (IsPOISubscriptionFilterMatch(p, filter, subscription))
                        {
                            subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                POI = p
                            });
                        }
                    }
                    if (subscriptionMatch.ItemList.Any())
                    {
                        subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                    }
                }
            }

            //check if any POI Updates match this subscription
            if (subscription.NotifyPOIUpdates)
            {
                var poiUpdates = dataModel.EditQueueItems.Where(c => c.DateProcessed >= checkFromDate && c.IsProcessed == true && c.PreviousData != null);
                if (poiUpdates.Any())
                {
                    var subscriptionMatch = new SubscriptionMatch {
                        Category = SubscriptionMatchCategory.UpdatedPOI, Description = "POIs Updated"
                    };
                    foreach (var p in poiUpdates)
                    {
                        try
                        {
                            var updatedPOI = JsonConvert.DeserializeObject <ChargePoint>(p.EditData);
                            if (IsPOISubscriptionFilterMatch(updatedPOI, filter, subscription))
                            {
                                if (searchPos != null)
                                {
                                    if (GeoManager.CalcDistance(updatedPOI.AddressInfo.Latitude, updatedPOI.AddressInfo.Longitude, (double)searchPos.Latitude, (double)searchPos.Longitude, DistanceUnit.KM) < subscription.DistanceKM)
                                    {
                                        subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                            Item = p, POI = updatedPOI
                                        });
                                    }
                                }
                                else
                                {
                                    subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                        Item = p, POI = updatedPOI
                                    });
                                }
                            }
                        }
                        catch (Exception)
                        {
                            ;;
                        }
                    }
                    if (subscriptionMatch.ItemList.Any())
                    {
                        subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                    }
                }
            }

            //check if any new comments match this subscription
            if (subscription.NotifyComments)
            {
                var newComments = dataModel.UserComments.Where(c => c.DateCreated >= checkFromDate &&
                                                               (searchPos == null ||
                                                                (searchPos != null &&
                                                                 c.ChargePoint.AddressInfo.SpatialPosition.Distance(searchPos) / 1000 < subscription.DistanceKM
                                                                ))
                                                               );
                if (newComments.Any())
                {
                    var subscriptionMatch = new SubscriptionMatch {
                        Category = SubscriptionMatchCategory.NewComment, Description = "New Comments Added"
                    };
                    foreach (var c in newComments)
                    {
                        var poi = OCM.API.Common.Model.Extensions.ChargePoint.FromDataModel(c.ChargePoint);
                        if (IsPOISubscriptionFilterMatch(poi, filter, subscription))
                        {
                            subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                Item = OCM.API.Common.Model.Extensions.UserComment.FromDataModel(c, true), POI = poi
                            });
                        }
                    }
                    if (subscriptionMatch.ItemList.Any())
                    {
                        subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                    }
                }
            }

            //check if any new Media uploads match this subscription
            if (subscription.NotifyMedia)
            {
                var newMedia = dataModel.MediaItems.Where(c => c.DateCreated >= checkFromDate &&
                                                          (searchPos == null ||
                                                           (searchPos != null &&
                                                            c.ChargePoint.AddressInfo.SpatialPosition.Distance(searchPos) / 1000 < subscription.DistanceKM
                                                           ))
                                                          );
                if (newMedia.Any())
                {
                    var subscriptionMatch = new SubscriptionMatch {
                        Category = SubscriptionMatchCategory.NewMediaUpload, Description = "New Photos Added"
                    };
                    foreach (var c in newMedia)
                    {
                        var poi = OCM.API.Common.Model.Extensions.ChargePoint.FromDataModel(c.ChargePoint);
                        if (IsPOISubscriptionFilterMatch(poi, filter, subscription))
                        {
                            subscriptionMatch.ItemList.Add(new SubscriptionMatchItem {
                                Item = OCM.API.Common.Model.Extensions.MediaItem.FromDataModel(c), POI = poi
                            });
                        }
                    }
                    if (subscriptionMatch.ItemList.Any())
                    {
                        subscriptionMatchGroup.SubscriptionMatches.Add(subscriptionMatch);
                    }
                }
            }

            return(subscriptionMatchGroup);
        }