/// <summary> /// Handle input to API /// </summary> /// <param name="context"></param> private void PerformInput(HttpContext context) { OCM.API.InputProviders.IInputProvider inputProvider = null; bool performSubmissionCompletedRedirects = false; if (context.Request["format"] == "json") { inputProvider = new InputProviders.JSONInputProvider(); } else { inputProvider = new InputProviders.HTMLFormInputProvider(); performSubmissionCompletedRedirects = true; } SubmissionManager submissionManager = new SubmissionManager(); //attempt to determine user from api call User user = inputProvider.GetUserFromAPICall(context); if (user != null && user.IsCurrentSessionTokenValid == false) { //session token provided didn't match latest in user details, reject user details. context.Response.StatusCode = 401; } else { //gather input variables if (context.Request["action"] == "cp_submission") { //gather/process data for submission OCM.API.Common.Model.ChargePoint cp = new Common.Model.ChargePoint(); bool processedOK = inputProvider.ProcessEquipmentSubmission(context, ref cp); bool submittedOK = false; if (processedOK == true) { //perform submission int submissionId = submissionManager.PerformPOISubmission(cp, user); if (submissionId > -1) submittedOK = true; } if (processedOK && submittedOK) { if (performSubmissionCompletedRedirects) { if (submittedOK) { context.Response.Redirect("http://openchargemap.org/submissions/complete.aspx", true); } else { context.Response.Redirect("http://openchargemap.org/submissions/error.aspx", true); } } else { context.Response.StatusCode = 202; //context.Response.AddHeader("Access-Control-Allow-Origin", "*"); } } else { if (performSubmissionCompletedRedirects) { context.Response.Redirect("http://openchargemap.org/submissions/error.aspx", true); } else { context.Response.StatusCode = 500; //context.Response.AddHeader("Access-Control-Allow-Origin", "*"); } } } if (context.Request["action"] == "comment_submission") { UserComment comment = new UserComment(); bool processedOK = inputProvider.ProcessUserCommentSubmission(context, ref comment); if (processedOK == true) { //perform submission int result = submissionManager.PerformSubmission(comment, user); if (result >= 0) { context.Response.Write("OK:" + result); } else { context.Response.Write("Error:" + result); } } else { context.Response.Write("Error: Validation Failed"); } } if (context.Request["action"] == "contactus_submission") { ContactSubmission contactSubmission = new ContactSubmission(); bool processedOK = inputProvider.ProcessContactUsSubmission(context, ref contactSubmission); bool resultOK = submissionManager.SendContactUsMessage(contactSubmission.Name, contactSubmission.Email, contactSubmission.Comment); if (resultOK == true) { context.Response.Write("OK"); } else { context.Response.Write("Error"); } } if (context.Request["action"] == "mediaitem_submission") { var p = inputProvider as OCM.API.InputProviders.HTMLFormInputProvider; MediaItem m = new MediaItem(); bool accepted = false; string msg = ""; try { accepted = p.ProcessMediaItemSubmission(context, ref m, user.ID); } catch (Exception exp) { msg += exp.ToString(); } if (accepted) { submissionManager.PerformSubmission(m, user); //OutputSubmissionReceivedMessage(context, "OK :" + m.ID, true); context.Response.Write("OK"); } else { //OutputBadRequestMessage(context, "Error, could not accept submission: " + msg); context.Response.Write("Error"); } } } }
/// <summary> /// For a list of ChargePoint objects, attempt to populate the AddressInfo (at least country) based on lat/lon if not already populated /// </summary> /// <param name="itemList"></param> /// <param name="coreRefData"></param> public void UpdateImportedPOIList(ImportReport poiResults, User user) { var submissionManager = new SubmissionManager(); int itemCount = 1; foreach (var newPOI in poiResults.Added) { Log("Importing New POI " + itemCount + ": " + newPOI.AddressInfo.ToString()); submissionManager.PerformPOISubmission(newPOI, user, false); } foreach (var updatedPOI in poiResults.Updated) { Log("Importing Updated POI " + itemCount + ": " + updatedPOI.AddressInfo.ToString()); submissionManager.PerformPOISubmission(updatedPOI, user, performCacheRefresh: false, disablePOISuperseding: true); } foreach (var delisted in poiResults.Delisted) { Log("Delisting Removed POI " + itemCount + ": " + delisted.AddressInfo.ToString()); delisted.SubmissionStatus = null; delisted.SubmissionStatusTypeID = (int)StandardSubmissionStatusTypes.Delisted_RemovedByDataProvider; submissionManager.PerformPOISubmission(delisted, user, false); } //refresh POI cache var cacheTask = Task.Run(async () => { return await OCM.Core.Data.CacheManager.RefreshCachedData(); }); cacheTask.Wait(); //temp get all providers references for recognised duplicates /*var dupeRefs = from dupes in poiResults.Duplicates where !String.IsNullOrEmpty(dupes.DataProvidersReference) select dupes.DataProvidersReference; string dupeOutput = ""; foreach(var d in dupeRefs) { dupeOutput += ",'"+d+"'"; } System.Diagnostics.Debug.WriteLine(dupeOutput);*/ if (poiResults.ProviderDetails.DefaultDataProvider != null) { //update date last updated for this provider new DataProviderManager().UpdateDateLastImport(poiResults.ProviderDetails.DefaultDataProvider.ID); } }