public bool ProcessUserCommentSubmission(HttpContext context, ref Common.Model.UserComment comment) { System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream); //TODO: handle encoding (UTF etc) correctly string responseContent = sr.ReadToEnd().Trim(); string jsonString = responseContent; try { JObject o = JObject.Parse(jsonString); JsonSerializer serializer = new JsonSerializer(); comment = (Common.Model.UserComment)serializer.Deserialize(new JTokenReader(o), typeof(Common.Model.UserComment)); return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp); //submission failed return(false); } }
private static void SendPOICommentSubmissionNotifications(Common.Model.UserComment comment, Model.User user, Core.Data.UserComment dataComment) { try { //prepare notification NotificationManager notification = new NotificationManager(); Hashtable msgParams = new Hashtable(); msgParams.Add("Description", ""); msgParams.Add("ChargePointID", comment.ChargePointID); msgParams.Add("ItemURL", "https://openchargemap.org/site/poi/details/" + comment.ChargePointID); msgParams.Add("UserName", user != null ? user.Username : comment.UserName); msgParams.Add("MessageBody", "Comment (" + dataComment.UserCommentType.Title + ") added to OCM-" + comment.ChargePointID + ": " + dataComment.Comment); //if fault report, attempt to notify operator if (dataComment.UserCommentType.Id == (int)StandardCommentTypes.FaultReport) { //decide if we can send a fault notification to the operator notification.PrepareNotification(NotificationType.FaultReport, msgParams); //notify default system recipients bool operatorNotified = false; if (dataComment.ChargePoint.Operator != null) { if (!String.IsNullOrEmpty(dataComment.ChargePoint.Operator.FaultReportEmail)) { try { notification.SendNotification(dataComment.ChargePoint.Operator.FaultReportEmail, ConfigurationManager.AppSettings["DefaultRecipientEmailAddresses"].ToString()); operatorNotified = true; } catch (Exception) { System.Diagnostics.Debug.WriteLine("Fault report: failed to notify operator"); } } } if (!operatorNotified) { notification.Subject += " (OCM: Could not notify Operator)"; notification.SendNotification(NotificationType.LocationCommentReceived); } } else { //normal comment, notification to OCM only notification.PrepareNotification(NotificationType.LocationCommentReceived, msgParams); //notify default system recipients notification.SendNotification(NotificationType.LocationCommentReceived); } } catch (Exception) { ;; // failed to send notification } }
public async Task <bool> ProcessUserCommentSubmission(HttpContext context, Common.Model.UserComment comment) { System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.Body); //TODO: handle encoding (UTF etc) correctly string responseContent = await sr.ReadToEndAsync(); string jsonString = responseContent.Trim(); try { // patch invalid property name from older client if (jsonString.Contains("UserCommentTypeID")) { jsonString = jsonString.Replace("UserCommentTypeID", "CommentTypeID"); } JObject o = JObject.Parse(jsonString); JsonSerializer serializer = new JsonSerializer(); var parsedComment = (Common.Model.UserComment)serializer.Deserialize(new JTokenReader(o), typeof(Common.Model.UserComment)); comment.ID = parsedComment.ID; comment.ChargePointID = parsedComment.ChargePointID; comment.CheckinStatusTypeID = parsedComment.CheckinStatusTypeID; comment.CommentTypeID = parsedComment.CommentTypeID; comment.UserName = parsedComment.UserName; comment.Rating = parsedComment.Rating; comment.RelatedURL = parsedComment.RelatedURL; comment.Comment = parsedComment.Comment; return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp); //submission failed return(false); } }
public async Task <bool> ProcessUserCommentSubmission(HttpContext context, Common.Model.UserComment comment) { //not implemented return(await Task.FromResult(false)); }
public bool ProcessUserCommentSubmission(HttpContext context, ref Common.Model.UserComment comment) { //not implemented return(false); }
/// <summary> /// Submit a new comment against a given charge equipment id /// </summary> /// <param name="comment"></param> /// <returns>ID of new comment, -1 for invalid cp, -2 for general error saving comment</returns> public async Task <int> PerformSubmission(Common.Model.UserComment comment, Model.User user) { //TODO: move all to UserCommentManager //populate data model comment from simple comment object var dataModel = new Core.Data.OCMEntities(); int cpID = comment.ChargePointID; var dataComment = new Core.Data.UserComment(); var dataChargePoint = dataModel.ChargePoints.FirstOrDefault(c => c.Id == cpID); if (dataChargePoint == null) { return(-1); //invalid charge point specified } dataComment.ChargePointId = dataChargePoint.Id; dataComment.Comment = comment.Comment; int commentTypeID = comment.CommentTypeID ?? 10; //default to General Comment // some clients may post a CommentType object instead of just an ID if (comment.CommentType != null) { commentTypeID = comment.CommentType.ID; } dataComment.UserCommentTypeId = commentTypeID; int?checkinStatusType = comment.CheckinStatusTypeID; dataComment.CheckinStatusTypeId = (byte?)comment.CheckinStatusTypeID; // some clients may post a CheckinStatusType object instead of just an ID if (dataComment.CheckinStatusTypeId == null && comment.CheckinStatusType != null) { dataComment.CheckinStatusTypeId = (byte?)comment.CheckinStatusType.ID; } dataComment.UserName = comment.UserName; dataComment.Rating = comment.Rating; dataComment.RelatedUrl = comment.RelatedURL; dataComment.DateCreated = DateTime.UtcNow; if (user != null && user.ID > 0) { var ocmUser = dataModel.Users.FirstOrDefault(u => u.Id == user.ID); if (ocmUser != null) { dataComment.UserId = ocmUser.Id; dataComment.UserName = ocmUser.Username; } } try { dataChargePoint.DateLastStatusUpdate = DateTime.UtcNow; dataModel.UserComments.Add(dataComment); dataModel.SaveChanges(); if (user != null) { AuditLogManager.Log(user, AuditEventType.CreatedItem, "Added Comment " + dataComment.Id + " to OCM-" + cpID, null); //add reputation points new UserManager().AddReputationPoints(user, 1); } //SendPOICommentSubmissionNotifications(comment, user, dataComment); //TODO: only refresh cache for specific POI await CacheManager.RefreshCachedPOI(dataComment.ChargePoint.Id); return(dataComment.Id); } catch (Exception exp) { return(-2); //error saving } }
/// <summary> /// Submit a new comment against a given charge equipment id /// </summary> /// <param name="comment"></param> /// <returns>ID of new comment, -1 for invalid cp, -2 for general error saving comment</returns> public int PerformSubmission(Common.Model.UserComment comment, Model.User user) { //TODO: move all to UserCommentManager //populate data model comment from simple comment object var dataModel = new Core.Data.OCMEntities(); int cpID = comment.ChargePointID; var dataComment = new Core.Data.UserComment(); dataComment.ChargePoint = dataModel.ChargePoints.FirstOrDefault(c => c.ID == cpID); if (dataComment.ChargePoint == null) { return(-1); //invalid charge point specified } dataComment.Comment = comment.Comment; int commentTypeID = 10; //default to General Comment if (comment.CommentType != null) { commentTypeID = comment.CommentType.ID; } dataComment.UserCommentType = dataModel.UserCommentTypes.FirstOrDefault(t => t.ID == commentTypeID); if (comment.CheckinStatusType != null) { dataComment.CheckinStatusType = dataModel.CheckinStatusTypes.FirstOrDefault(t => t.ID == comment.CheckinStatusType.ID); } dataComment.UserName = comment.UserName; dataComment.Rating = comment.Rating; dataComment.RelatedURL = comment.RelatedURL; dataComment.DateCreated = DateTime.UtcNow; if (user != null && user.ID > 0) { var ocmUser = dataModel.Users.FirstOrDefault(u => u.ID == user.ID); if (ocmUser != null) { dataComment.User = ocmUser; dataComment.UserName = ocmUser.Username; } } /*if (dataComment.User==null) * { * return -3; //rejected, not authenticated * }*/ try { dataComment.ChargePoint.DateLastStatusUpdate = DateTime.UtcNow; dataModel.UserComments.Add(dataComment); dataModel.SaveChanges(); if (user != null) { AuditLogManager.Log(user, AuditEventType.CreatedItem, "Added Comment " + dataComment.ID + " to OCM-" + cpID, null); //add reputation points new UserManager().AddReputationPoints(user, 1); } //SendPOICommentSubmissionNotifications(comment, user, dataComment); //TODO: only refresh cache for specific POI CacheManager.RefreshCachedPOI(dataComment.ChargePoint.ID); return(dataComment.ID); } catch (Exception) { return(-2); //error saving } }