public static Model.UserComment FromDataModel(Core.Data.UserComment source, bool isVerboseMode) { var userComment = new Model.UserComment { ID = source.ID, Comment = source.Comment, Rating = source.Rating, RelatedURL = source.RelatedURL, DateCreated = source.DateCreated, ChargePointID = source.ChargePointID, User = User.BasicFromDataModel(source.User) }; if (isVerboseMode && source.UserCommentType != null) { userComment.CommentType = UserCommentType.FromDataModel(source.UserCommentType); userComment.CommentTypeID = source.UserCommentTypeID; } else { userComment.CommentTypeID = source.UserCommentTypeID; } if (isVerboseMode && source.CheckinStatusType != null) { userComment.CheckinStatusType = CheckinStatusType.FromDataModel(source.CheckinStatusType); userComment.CheckinStatusTypeID = source.CheckinStatusTypeID; } else { userComment.CheckinStatusTypeID = source.CheckinStatusTypeID; } if (userComment.User != null) { userComment.UserName = userComment.User.Username; } else { userComment.UserName = source.UserName; } return(userComment); }
public static Model.UserComment FromDataModel(Core.Data.UserComment source, bool isVerboseMode, Model.CoreReferenceData refData = null) { var userComment = new Model.UserComment { ID = source.Id, Comment = source.Comment, Rating = source.Rating, RelatedURL = source.RelatedUrl, DateCreated = source.DateCreated, ChargePointID = source.ChargePointId, User = User.BasicFromDataModel(source.User), IsActionedByEditor = source.IsActionedByEditor }; if (isVerboseMode && refData != null) { userComment.CommentType = refData?.UserCommentTypes.FirstOrDefault(i => i.ID == source.UserCommentTypeId) ?? UserCommentType.FromDataModel(source.UserCommentType); userComment.CommentTypeID = source.UserCommentTypeId; } else { userComment.CommentTypeID = source.UserCommentTypeId; } if (isVerboseMode && (refData != null || source.CheckinStatusType != null) && source.CheckinStatusTypeId != null) { userComment.CheckinStatusType = refData?.CheckinStatusTypes.FirstOrDefault(i => i.ID == source.CheckinStatusTypeId) ?? CheckinStatusType.FromDataModel(source.CheckinStatusType); userComment.CheckinStatusTypeID = source.CheckinStatusTypeId; } else { userComment.CheckinStatusTypeID = source.CheckinStatusTypeId; } if (userComment.User != null) { userComment.UserName = userComment.User.Username; } else { userComment.UserName = source.UserName; } return(userComment); }
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 } }
/// <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 } }
/// <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; } } 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 } }