public static DatabaseQueryProcessorSettings RetrieveCompanySettings(MySqlDataManipulator manipulator, int companyId) { DatabaseQueryProcessorSettings companySettings = new DatabaseQueryProcessorSettings(); List <CompanySettingsEntry> settings = manipulator.GetCompanySettings(companyId); foreach (CompanySettingsEntry setting in settings) { if (CompanySettingsKey.KeywordClusterer.Equals(setting.SettingKey)) { if (setting.SettingValue.Equals("Similarity")) { companySettings.KeywordClustererIdString = "DatabaseKeywordSimilarityClusterer"; } else { companySettings.KeywordClustererIdString = "DatabaseKeywordSimilarityClusterer"; } } else if (CompanySettingsKey.KeywordPredictor.Equals(setting.SettingKey)) { if (setting.SettingValue.Equals("Bayesian")) { companySettings.KeywordPredictorIdString = "NaiveBayesKeywordPredictor"; } else { companySettings.KeywordPredictorIdString = "NaiveBayesKeywordPredictor"; } } else if (CompanySettingsKey.ProblemPredictor.Equals(setting.SettingKey)) { if (setting.SettingValue.Equals("Database")) { companySettings.ProblemPredictorIdString = "DatabaseQueryProblemPredictor"; } else { companySettings.ProblemPredictorIdString = "DatabaseQueryProblemPredictor"; } } } return(companySettings); }
/// <summary> /// GET request format located in the Web Api Enumeration v2 /// under the tab Company/Settings, starting row 1 /// </summary> /// <param name="ctx">HttpListenerContext to respond to</param> private void HandlePutRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body"); return; } CompanySettingsApiPutRequest entry = JsonDataObjectUtil <CompanySettingsApiPutRequest> .ParseObject(ctx); if (entry == null) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format"); return; } if (!ValidatePutRequest(entry)) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Not all fields in the request were filled"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed"); return; } #region User Validation var user = connection.GetUserById(entry.UserId); if (user == null) { WriteBodyResponse(ctx, 404, "Not Found", "User was not found on the server"); return; } #endregion #region Action Handling List <CompanySettingsEntry> entries = connection.GetCompanySettings(user.Company); if (entries == null) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while retrieving settings: " + connection.LastException.Message); return; } JsonListStringConstructor retConstructor = new JsonListStringConstructor(); entries.ForEach(obj => retConstructor.AddElement(WriteSettingToOutput(obj))); WriteBodyResponse(ctx, 200, "OK", retConstructor.ToString()); #endregion } } catch (HttpListenerException) { //HttpListeners dispose themselves when an exception occurs, so we can do no more. } catch (Exception e) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occurred while processing request: " + e.Message); } }
/// <summary> /// PATCH request format located in the Web Api Enumeration v2 /// under the tab Company/Settings, starting row 27 /// </summary> /// <param name="ctx">HttpListenerContext to respond to</param> private void HandlePatchRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "No Body", "Request lacked a body"); return; } CompanySettingsApiPatchRequest entry = JsonDataObjectUtil <CompanySettingsApiPatchRequest> .ParseObject(ctx); if (entry == null) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Request was in the wrong format"); return; } if (!ValidatePatchRequest(entry)) { WriteBodyResponse(ctx, 400, "Incorrect Format", "Not all fields in the request were filled"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected ServerError", "Connection to database failed"); return; } #region User Validation var user = connection.GetUserById(entry.UserId); if (user == null) { WriteBodyResponse(ctx, 404, "Not Found", "User was not found on the server"); return; } List <CompanySettingsEntry> entries = connection.GetCompanySettings(user.Company); if (entries == null) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while retrieving settings: " + connection.LastException.Message); return; } #endregion #region Action Handling var toModify = entries.Where(obj => obj.SettingKey.Equals(entry.SettingsKey)).FirstOrDefault(); if (toModify == null) { WriteBodyResponse(ctx, 404, "Not Found", "Setting with the specified key was not found on the server"); return; } toModify.SettingValue = entry.SettingsValue; if (!connection.UpdateCompanySettings(user.Company, toModify)) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occurred while updating company's settings: " + connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); #endregion } } catch (HttpListenerException) { //HttpListeners dispose themselves when an exception occurs, so we can do no more. } catch (Exception e) { WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occurred while processing request: " + e.Message); } }