/// <summary> /// Consumers should prepare a new/updated ChargePoint with as much info populated as possible /// </summary> /// <param name="submission">ChargePoint info for submission, if ID and UUID set will be treated as an update</param> /// <returns>false on error or not enough data supplied</returns> public int PerformPOISubmission(Model.ChargePoint updatedPOI, Model.User user, bool performCacheRefresh = true, bool disablePOISuperseding = false) { try { var poiManager = new POIManager(); bool enableEditQueueLogging = bool.Parse(ConfigurationManager.AppSettings["EnableEditQueue"]); bool isUpdate = false; bool userCanEditWithoutApproval = false; bool isSystemUser = false; int? supersedesID = null;//POI from another data provider which has been superseded by an edit //if user signed in, check if they have required permission to perform an edit/approve (if required) if (user != null) { if (user.ID == (int)StandardUsers.System) isSystemUser = true; //if user is system user, edits/updates are not recorded in edit queue if (isSystemUser) { enableEditQueueLogging = false; } userCanEditWithoutApproval = POIManager.CanUserEditPOI(updatedPOI, user); } var dataModel = new Core.Data.OCMEntities(); //if poi is an update, validate if update can be performed if (updatedPOI.ID > 0 && !String.IsNullOrEmpty(updatedPOI.UUID)) { if (dataModel.ChargePoints.Any(c => c.ID == updatedPOI.ID && c.UUID == updatedPOI.UUID)) { //update is valid poi, check if user has permission to perform an update isUpdate = true; if (userCanEditWithoutApproval) AllowUpdates = true; if (!AllowUpdates && !enableEditQueueLogging) { return -1; //valid update requested but updates not allowed } } else { //update does not correctly identify an existing poi return -1; } } //validate if minimal required data is present if (updatedPOI.AddressInfo.Title == null || (updatedPOI.AddressInfo.Country == null && updatedPOI.AddressInfo.CountryID == null)) { return -1; } //convert to DB version of POI and back so that properties are fully populated updatedPOI = PopulateFullPOI(updatedPOI); Model.ChargePoint oldPOI = null; if (updatedPOI.ID > 0) { //get json snapshot of current cp data to store as 'previous' oldPOI = poiManager.Get(updatedPOI.ID); } //if user cannot edit directly, add to edit queue for approval var editQueueItem = new Core.Data.EditQueueItem { DateSubmitted = DateTime.UtcNow }; if (enableEditQueueLogging) { editQueueItem.EntityID = updatedPOI.ID; editQueueItem.EntityType = dataModel.EntityTypes.FirstOrDefault(t => t.ID == 1); //charging point location entity type id //serialize cp as json //null extra data we don't want to serialize/compare updatedPOI.UserComments = null; updatedPOI.MediaItems = null; string editData = PerformSerialisationToString(updatedPOI, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); editQueueItem.EditData = editData; if (updatedPOI.ID > 0) { //check if poi will change with this edit, if not we discard it completely if (!poiManager.HasDifferences(oldPOI, updatedPOI)) { System.Diagnostics.Debug.WriteLine("POI Update has no changes, discarding change."); return updatedPOI.ID; } else { editQueueItem.PreviousData = PerformSerialisationToString(oldPOI, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); } } if (user != null) editQueueItem.User = dataModel.Users.FirstOrDefault(u => u.ID == user.ID); editQueueItem.IsProcessed = false; editQueueItem = dataModel.EditQueueItems.Add(editQueueItem); //TODO: send notification of new item for approval //save edit queue item dataModel.SaveChanges(); //if previous edit queue item exists by same user for same POI, mark as processed var previousEdits = dataModel.EditQueueItems.Where(e => e.UserID == editQueueItem.UserID && e.EntityID == editQueueItem.EntityID && e.EntityTypeID == editQueueItem.EntityTypeID && e.ID != editQueueItem.ID && e.IsProcessed != true); foreach (var previousEdit in previousEdits) { previousEdit.IsProcessed = true; if (editQueueItem.User != null) { previousEdit.ProcessedByUser = editQueueItem.User; } else { editQueueItem.ProcessedByUser = dataModel.Users.FirstOrDefault(u => u.ID == (int)StandardUsers.System); } previousEdit.DateProcessed = DateTime.UtcNow; } //save updated edit queue items dataModel.SaveChanges(); } //prepare and save changes POI changes/addition if (isUpdate && !AllowUpdates) { //user has submitted an edit but is not an approved editor //SendEditSubmissionNotification(updatedPOI, user); //user is not an editor, item is now pending in edit queue for approval. return updatedPOI.ID; } if (isUpdate && updatedPOI.SubmissionStatusTypeID >= 1000) { //update is a delisting, skip superseding poi System.Diagnostics.Debug.WriteLine("skipping superseding of imported POI due to delisting"); } else { //if poi being updated exists from an imported source we supersede the old POI with the new version, unless we're doing a fresh import from same data provider if (!disablePOISuperseding) { //if update by non-system user will change an imported/externally provided data, supersede old POI with new one (retain ID against new POI) if (isUpdate && !isSystemUser && oldPOI.DataProviderID != (int)StandardDataProviders.OpenChargeMapContrib) { //move old poi to new id, set status of new item to superseded supersedesID = poiManager.SupersedePOI(dataModel, oldPOI, updatedPOI); } } } //user is an editor, go ahead and store the addition/update //set/update cp properties var cpData = poiManager.PopulateChargePoint_SimpleToData(updatedPOI, dataModel); //if item has no submission status and user permitted to edit, set to published if (userCanEditWithoutApproval && cpData.SubmissionStatusTypeID == null) { cpData.SubmissionStatusType = dataModel.SubmissionStatusTypes.First(s => s.ID == (int)StandardSubmissionStatusTypes.Submitted_Published); cpData.SubmissionStatusTypeID = cpData.SubmissionStatusType.ID; //hack due to conflicting state change for SubmissionStatusType } else { //no submission status, set to 'under review' if (cpData.SubmissionStatusType == null) cpData.SubmissionStatusType = dataModel.SubmissionStatusTypes.First(s => s.ID == (int)StandardSubmissionStatusTypes.Submitted_UnderReview); } cpData.DateLastStatusUpdate = DateTime.UtcNow; if (!isUpdate) { //new data objects need added to data model before save if (cpData.AddressInfo != null) dataModel.AddressInfoList.Add(cpData.AddressInfo); dataModel.ChargePoints.Add(cpData); } //finally - save poi update dataModel.SaveChanges(); //get id of update/new poi int newPoiID = cpData.ID; //this is an authorised update, reflect change in edit queue item if (enableEditQueueLogging && user != null && user.ID > 0) { var editUser = dataModel.Users.FirstOrDefault(u => u.ID == user.ID); editQueueItem.User = editUser; if (newPoiID > 0) editQueueItem.EntityID = newPoiID; //if user is authorised to edit, process item automatically without review if (userCanEditWithoutApproval) { editQueueItem.ProcessedByUser = editUser; editQueueItem.DateProcessed = DateTime.UtcNow; editQueueItem.IsProcessed = true; } //save edit queue item changes dataModel.SaveChanges(); } else { //anonymous submission, update edit queue item if (enableEditQueueLogging && user == null) { if (newPoiID > 0) editQueueItem.EntityID = newPoiID; dataModel.SaveChanges(); } } System.Diagnostics.Debug.WriteLine("Added/Updated CP:" + cpData.ID); //if user is not anonymous, log their submission and update their reputation points if (user != null) { AuditLogManager.Log(user, isUpdate ? AuditEventType.UpdatedItem : AuditEventType.CreatedItem, "Modified OCM-" + cpData.ID, null); //add reputation points new UserManager().AddReputationPoints(user, 1); } //preserve new POI Id for caller updatedPOI.ID = cpData.ID; if (performCacheRefresh) { var cacheTask = Task.Run(async () => { if (supersedesID != null) { await CacheManager.RefreshCachedPOI((int)supersedesID); } return await CacheManager.RefreshCachedPOI(updatedPOI.ID); }); cacheTask.Wait(); } return updatedPOI.ID; } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); AuditLogManager.ReportWebException(HttpContext.Current.Server, AuditEventType.SystemErrorWeb); //throw exp; //error performing submission return -1; } }
public void ProcessEditQueueItem(int id, bool publishEdit, int userId) { //prepare poi details int updatePOIId = 0; var queueItem = DataModel.EditQueueItems.FirstOrDefault(e => e.ID == id); if (queueItem != null && queueItem.IsProcessed == false) { if (queueItem.EntityType.ID == (int)StandardEntityTypes.POI) { //check current user is authorized to approve edits for this POIs country bool hasEditPermission = false; var editPOI = DeserializePOIFromJSON(queueItem.EditData); var userProfile = new UserManager().GetUser(userId); if (userProfile != null) { if (UserManager.HasUserPermission(userProfile, editPOI.AddressInfo.CountryID, PermissionLevel.Editor)) { hasEditPermission = true; } } //processing a POI add/edit if (hasEditPermission) { if (publishEdit) { //get diff between previous and edit POIManager poiManager = new POIManager(); Model.ChargePoint poiA = DeserializePOIFromJSON(queueItem.PreviousData); Model.ChargePoint poiB = DeserializePOIFromJSON(queueItem.EditData); bool poiUpdateRequired = false; if (poiA != null) { //this is an edit, load the latest version of the POI as version 'A' poiA = poiManager.Get(poiA.ID); if (poiManager.HasDifferences(poiA, poiB)) { poiUpdateRequired = true; } } //save poi update //if its an edit, load the original details before applying the change if (poiUpdateRequired) { //updates to externally provided POIs require old version to be superseded (archived) first if (poiA != null && poiA.DataProviderID != (int)StandardDataProviders.OpenChargeMapContrib) { poiManager.SupersedePOI(DataModel, poiA, poiB); } } //set/update cp properties from simple model to data model var poiData = poiManager.PopulateChargePoint_SimpleToData(poiB, DataModel); //set status type to published if previously unset if (poiData.SubmissionStatusTypeID == null) { poiData.SubmissionStatusType = DataModel.SubmissionStatusTypes.First(s => s.ID == (int)StandardSubmissionStatusTypes.Submitted_Published); } poiData.DateLastStatusUpdate = DateTime.UtcNow; //publish edit DataModel.SaveChanges(); updatePOIId = poiData.ID; //attribute submitter with reputation points if (queueItem.UserID != null) { new UserManager().AddReputationPoints((int)queueItem.UserID, 1); } } //update edit queue item as processed queueItem.IsProcessed = true; queueItem.ProcessedByUser = DataModel.Users.FirstOrDefault(u => u.ID == userId); queueItem.DateProcessed = DateTime.UtcNow; DataModel.SaveChanges(); //TODO: also award processing editor with reputation points if they are approving someone elses edit and they are not Admin //Refresh POI cache Task cacheRefresh = CacheManager.RefreshCachedPOI(updatePOIId); } } } }