/// <summary> /// Executes the query. Always call this method in a separate Thread to execute the query asynchronously! /// </summary> /// <param name="ASessionID">the id of the current session</param> /// <param name="AContext">Context in which this quite generic Method gets called (e.g. 'Partner Find'). This is /// optional but should be specified to aid in debugging as it gets logged in case Exceptions happen when the /// DB Transaction is taken out and the Query gets executed.</param> /// <param name="ADataBase">An instantiated <see cref="TDataBase" /> object, or null (default = null). If null /// gets passed then the Method executes DB commands with a new Database connection</param> /// <remarks>An instance of TAsyncFindParameters with set up Properties must exist before this procedure can get /// called! /// </remarks> public void ExecuteQuery(string ASessionID, string AContext = null, TDataBase ADataBase = null) { // need to initialize the database session TSession.InitThread(ASessionID); TDataBase db = DBAccess.Connect("ExecuteQuery", ADataBase); try { FProgressID = Guid.NewGuid().ToString(); TProgressTracker.InitProgressTracker(FProgressID, "Executing Query...", 100.0m); // Create SQL statement and execute it to return all records ExecuteFullQuery(AContext, db); } catch (Exception exp) { TLogging.Log(this.GetType().FullName + ".ExecuteQuery" + (AContext == null ? "" : " (Context: " + AContext + ")") + ": Exception occured: " + exp.ToString()); // Inform the caller that something has gone wrong... TProgressTracker.CancelJob(FProgressID); /* * WE MUST 'SWALLOW' ANY EXCEPTION HERE, OTHERWISE THE WHOLE * PETRASERVER WILL GO DOWN!!! (THIS BEHAVIOUR IS NEW WITH .NET 2.0.) * * --> ANY EXCEPTION THAT WOULD LEAVE THIS METHOD WOULD BE SEEN AS AN <-- * --> UNHANDLED EXCEPTION IN A THREAD, AND THE .NET/MONO RUNTIME <-- * --> WOULD BRING DOWN THE WHOLE PETRASERVER PROCESS AS A CONSEQUENCE! <-- * */ } }
/// <summary> /// run the report /// </summary> private void Run(string ASessionID) { // need to initialize the database session TSession.InitThread(ASessionID); IsolationLevel Level; if (FParameterList.Get("IsolationLevel").ToString().ToLower() == "readuncommitted") { // for long reports, that should not take out locks; // the data does not need to be consistent or will most likely not be changed during the generation of the report Level = IsolationLevel.ReadUncommitted; } else if (FParameterList.Get("IsolationLevel").ToString().ToLower() == "repeatableread") { // for financial reports: it is important to have consistent data; e.g. for totals Level = IsolationLevel.RepeatableRead; } else if (FParameterList.Get("IsolationLevel").ToString().ToLower() == "serializable") { // for creating extracts: we need to write to the database Level = IsolationLevel.Serializable; } else { // default behaviour for normal reports Level = IsolationLevel.ReadCommitted; } FSuccess = false; TDBTransaction Transaction = null; bool SubmissionOK = false; try { DBAccess.GDBAccessObj.BeginAutoTransaction(Level, ref Transaction, ref SubmissionOK, delegate { if (FDatacalculator.GenerateResult(ref FParameterList, ref FResultList, ref FErrorMessage)) { FSuccess = true; SubmissionOK = true; } else { TLogging.Log(FErrorMessage); } }); } catch (Exception e) { TLogging.Log("problem calculating report: " + e.Message); TLogging.Log(e.StackTrace, TLoggingType.ToLogfile); } TProgressTracker.FinishJob(FProgressID); }
/// main method public static void Main(string[] args) { new TAppSettingsManager(); new TLogging(); if (!TAppSettingsManager.HasValue("YmlGzFile") || !TAppSettingsManager.HasValue("Action")) { TLogging.Log("sample call: -C:../../etc/TestServer.config -Action:dump -YmlGzFile:test.yml.gz"); TLogging.Log("sample call: -C:../../etc/TestServer.config -Action:load -YmlGzFile:test.yml.gz"); Environment.Exit(-1); } string YmlFile = TAppSettingsManager.GetValue("YmlGzFile"); string Action = TAppSettingsManager.GetValue("Action"); TLogging.DebugLevel = TAppSettingsManager.GetInt32("Server.DebugLevel", 0); TSession.InitThread(); TServerManager.TheServerManager = new TServerManager(); bool ExitWithError = false; try { if (Action == "dump") { if (!DumpYmlGz(YmlFile)) { ExitWithError = true; } } else if (Action == "load") { if (!LoadYmlGz(YmlFile)) { ExitWithError = true; } } } catch (Exception e) { TLogging.Log(e.ToString()); ExitWithError = true; } TServerManager.TheServerManager.StopServer(); if (TAppSettingsManager.GetValue("interactive", "true") == "true") { Console.WriteLine("Please press Enter to continue..."); Console.ReadLine(); } if (ExitWithError) { Environment.Exit(-1); } }
private static bool TrainBankStatementsLastMonthThread(String ASessionID, Int32 AClientID, Int32 ALedgerNumber, DateTime AToday) { TSession.InitThread(ASessionID); DomainManager.GClientID = AClientID; string MyClientID = DomainManager.GClientID.ToString(); TProgressTracker.InitProgressTracker(MyClientID, Catalog.GetString("Training last months bank statements"), 30); DateTime startDateThisMonth = new DateTime(AToday.Year, AToday.Month, 1); DateTime endDateLastMonth = startDateThisMonth.AddDays(-1); DateTime startDateLastMonth = new DateTime(endDateLastMonth.Year, endDateLastMonth.Month, 1); // get all bank accounts TCacheable CachePopulator = new TCacheable(); Type typeofTable; GLSetupTDSAAccountTable accounts = (GLSetupTDSAAccountTable)CachePopulator.GetCacheableTable( TCacheableFinanceTablesEnum.AccountList, "", false, ALedgerNumber, out typeofTable); foreach (GLSetupTDSAAccountRow account in accounts.Rows) { // at OM Germany we don't have the bank account flags set if ((!account.IsBankAccountFlagNull() && (account.BankAccountFlag == true)) || (!account.IsCashAccountFlagNull() && (account.CashAccountFlag == true)) ) { string BankAccountCode = account.AccountCode; DateTime counter = startDateLastMonth; while (!counter.Equals(startDateThisMonth)) { TProgressTracker.SetCurrentState( MyClientID, String.Format(Catalog.GetString("Training {0} {1}"), BankAccountCode, counter.ToShortDateString()), counter.Day); // TODO: train only one bank statement per date and bank account? TrainBankStatement(ALedgerNumber, counter, BankAccountCode); counter = counter.AddDays(1); } } } TProgressTracker.FinishJob(MyClientID); return(true); }
public string SetNewPassword(string AUserID, string AToken, string ANewPassword) { // make sure we are logged out. especially SYSADMIN could be logged in when a new user is created. Logout(); TSession.InitThread("SetNewPassword", TAppSettingsManager.ConfigFileName); TVerificationResultCollection VerificationResult; bool Result = TMaintenanceWebConnector.SetNewPassword(AUserID, AToken, ANewPassword, out VerificationResult); return("{" + "\"AVerificationResult\": " + THttpBinarySerializer.SerializeObject(VerificationResult) + "," + "\"result\": " + THttpBinarySerializer.SerializeObject(Result) + "}"); }
/// <summary> /// Executes the query. Call this method in a separate Thread to execute the /// query asynchronously! /// </summary> /// <remarks>An instance of TAsyncFindParameters with set up Properties must /// exist before this procedure can get called! /// </remarks> /// <returns>void</returns> public void ExecuteQuery(string ASessionID) { bool ownDatabaseConnection = false; // need to initialize the database session TSession.InitThread(ASessionID); if (!DBAccess.GDBAccessObj.ConnectionOK) { // we need a separate database object for this thread, since we cannot access the session object DBAccess.GDBAccessObj.EstablishDBConnection(TSrvSetting.RDMBSType, TSrvSetting.PostgreSQLServer, TSrvSetting.PostgreSQLServerPort, TSrvSetting.PostgreSQLDatabaseName, TSrvSetting.DBUsername, TSrvSetting.DBPassword, ""); ownDatabaseConnection = true; } try { FProgressID = Guid.NewGuid().ToString(); TProgressTracker.InitProgressTracker(FProgressID, "Executing Query...", 100.0m); // Create SQL statement and execute it to return all records ExecuteFullQuery(); } catch (Exception exp) { TLogging.Log(this.GetType().FullName + ".ExecuteQuery: Exception occured: " + exp.ToString()); // Inform the caller that something has gone wrong... TProgressTracker.CancelJob(FProgressID); /* * WE MUST 'SWALLOW' ANY EXCEPTION HERE, OTHERWISE THE WHOLE * PETRASERVER WILL GO DOWN!!! (THIS BEHAVIOUR IS NEW WITH .NET 2.0.) * * --> ANY EXCEPTION THAT WOULD LEAVE THIS METHOD WOULD BE SEEN AS AN <-- * --> UNHANDLED EXCEPTION IN A THREAD, AND THE .NET/MONO RUNTIME <-- * --> WOULD BRING DOWN THE WHOLE PETRASERVER PROCESS AS A CONSEQUENCE! <-- * */ } if (ownDatabaseConnection) { DBAccess.GDBAccessObj.CloseDBConnection(); } }
/// <summary> /// Initialize the Petra server and connect to the database /// </summary> /// <param name="AConfigName">just provide the server config file, plus AutoLogin and AutoLoginPasswd</param> public static TServerManager Connect(string AConfigName) { TDBTransaction LoginTransaction; bool CommitLoginTransaction = false; bool SystemEnabled; string WelcomeMessage; Int32 ClientID; Int64 SiteKey; if (!File.Exists(AConfigName) && (AConfigName.Length > 0)) { TLogging.Log("cannot find config file " + Path.GetFullPath(AConfigName)); Environment.Exit(-1); } TSession.InitThread("NUnitPetraServer.TPetraServerConnector.Connect", AConfigName); CommonNUnitFunctions.InitRootPath(); Catalog.Init(); TServerManager.TheServerManager = new TServerManager(); ErrorCodeInventory.Init(); // initialise the cached tables and the delegates TSetupDelegates.Init(); TDataBase db = DBAccess.Connect( "Ict.Testing.NUnitPetraServer.TPetraServerConnector.Connect DB Connection"); // we need a serializable transaction, to store the session LoginTransaction = db.BeginTransaction(IsolationLevel.Serializable); try { TClientManager.PerformLoginChecks(TAppSettingsManager.GetValue("AutoLogin").ToUpper(), TAppSettingsManager.GetValue("AutoLoginPasswd"), "NUNITTEST", "127.0.0.1", out SystemEnabled, LoginTransaction); CommitLoginTransaction = true; } catch (EPetraSecurityException) { // We need to set this flag to true here to get the failed login to be stored in the DB!!! CommitLoginTransaction = true; } finally { if (CommitLoginTransaction) { LoginTransaction.Commit(); } else { LoginTransaction.Rollback(); } } TConnectedClient CurrentClient = TClientManager.ConnectClient( TAppSettingsManager.GetValue("AutoLogin").ToUpper(), TAppSettingsManager.GetValue("AutoLoginPasswd"), "NUNITTEST", "127.0.0.1", TFileVersionInfo.GetApplicationVersion().ToVersion(), TClientServerConnectionType.csctLocal, out ClientID, out WelcomeMessage, out SystemEnabled, out SiteKey, db); // the following values are stored in the session object DomainManager.GClientID = ClientID; DomainManager.CurrentClient = CurrentClient; DomainManager.GSiteKey = SiteKey; TSetupDelegates.Init(); db.CloseDBConnection(); return((TServerManager)TServerManager.TheServerManager); }
/// <summary> /// run the report /// </summary> private void Run(string ASessionID) { // need to initialize the database session TSession.InitThread(ASessionID); IsolationLevel Level; if (FParameterList.Get("IsolationLevel").ToString().ToLower() == "readuncommitted") { // for long reports, that should not take out locks; // the data does not need to be consistent or will most likely not be changed during the generation of the report Level = IsolationLevel.ReadUncommitted; } else if (FParameterList.Get("IsolationLevel").ToString().ToLower() == "repeatableread") { // for financial reports: it is important to have consistent data; e.g. for totals Level = IsolationLevel.RepeatableRead; } else if (FParameterList.Get("IsolationLevel").ToString().ToLower() == "serializable") { // for creating extracts: we need to write to the database Level = IsolationLevel.Serializable; } else { // default behaviour for normal reports Level = IsolationLevel.ReadCommitted; } FSuccess = false; TDBTransaction Transaction = null; bool SubmissionOK = false; try { DBAccess.GDBAccessObj.BeginAutoTransaction(Level, ref Transaction, ref SubmissionOK, delegate { if (FDatacalculator.GenerateResult(ref FParameterList, ref FResultList, ref FErrorMessage, ref FException)) { FSuccess = true; SubmissionOK = true; } else { TLogging.Log(FErrorMessage); } }); } catch (Exception Exc) { TLogging.Log("Problem calculating report: " + Exc.ToString()); TLogging.Log(Exc.StackTrace, TLoggingType.ToLogfile); FSuccess = false; FErrorMessage = Exc.Message; FException = Exc; } if (TDBExceptionHelper.IsTransactionSerialisationException(FException)) { // do nothing - we want this exception to bubble up } else if (FException is Exception && FException.InnerException is EOPDBException) { EOPDBException DbExc = (EOPDBException)FException.InnerException; if (DbExc.InnerException is Exception) { if (DbExc.InnerException is PostgresException) { PostgresException PgExc = (PostgresException)DbExc.InnerException; if (PgExc.SqlState == "57014") // SQL statement timeout problem { FErrorMessage = Catalog.GetString( "Error - Database took too long to respond. Try different parameters to return fewer results."); } } else { FErrorMessage = DbExc.InnerException.Message; } FException = null; } } TProgressTracker.FinishJob(FProgressID); }
/// <summary> /// initialise the server for each Web Request /// </summary> private static bool Init() { string ConfigFileName = string.Empty; // make sure the correct config file is used string Instance = HttpContext.Current.Request.Url.ToString().Replace("http://", "").Replace("https://", ""); Instance = Instance.Substring(0, Instance.IndexOf(".")).Replace("op_", "op").Replace("op", "op_"); // for demo etc if (!Instance.StartsWith("op_")) { Instance = "op_" + Instance; } ConfigFileName = "/home/" + Instance + "/etc/PetraServerConsole.config"; if (File.Exists(ConfigFileName)) { // we are in a multi tenant hosting scenario } else if (Environment.CommandLine.Contains("/appconfigfile=")) { // this happens when we use fastcgi-mono-server4 ConfigFileName = Environment.CommandLine.Substring( Environment.CommandLine.IndexOf("/appconfigfile=") + "/appconfigfile=".Length); if (ConfigFileName.IndexOf(" ") != -1) { ConfigFileName = ConfigFileName.Substring(0, ConfigFileName.IndexOf(" ")); } } else { // this is the normal behaviour when running with local http server ConfigFileName = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "web.config"; } TTypedDataTable.ResetStaticVariables(); TPdfPrinter.ResetStaticVariables(); THTTPUtils.ResetStaticVariables(); TSharedDataCache.TMPartner.ResetStaticVariables(); TServerManagerBase.ResetStaticVariables(); TClientManager.ResetStaticVariables(); TSession.InitThread("TOpenPetraOrgSessionManager.Init", ConfigFileName); if (HttpContext.Current != null) { HttpContext.Current.Server.ScriptTimeout = Convert.ToInt32( TimeSpan.FromMinutes(TAppSettingsManager.GetInt32("WebRequestTimeOutInMinutes", 15)). TotalSeconds); } // if the Login Method is called: reset cookie, ignore any old session if ((HttpContext.Current != null) && (HttpContext.Current.Request.PathInfo == "/Login")) { TSession.CloseSession(); TSession.InitThread("TOpenPetraOrgSessionManager.Init Reset", ConfigFileName); } Catalog.Init(); ErrorCodeInventory.Init(); ErrorCodeInventory.BuildErrorCodeInventory(typeof(Ict.Petra.Shared.PetraErrorCodes)); ErrorCodeInventory.BuildErrorCodeInventory(typeof(Ict.Common.Verification.TStringChecks)); TServerManager.TheServerManager = new TServerManager(); // initialise the cached tables and the delegates TSetupDelegates.Init(); TLogging.LogAtLevel(4, "Server has been initialised"); return(true); }
/// <summary> /// run the report /// </summary> private static void Run(string AConfigFileName, string ASessionID, string AReportID, TRptDataCalculator ADatacalculator, TParameterList AParameterList) { // need to initialize the database session TSession.InitThread("Reporting Webconnector", AConfigFileName, ASessionID); TDataBase db = DBAccess.Connect("TReportGeneratorWebConnector"); TDBTransaction Transaction = new TDBTransaction(); bool Success = false; bool Submit = true; string HTMLOutput = String.Empty; HtmlDocument HTMLDocument = new HtmlDocument(); string ErrorMessage = String.Empty; try { db.ReadTransaction(ref Transaction, delegate { Exception myException = null; if (ADatacalculator.GenerateResult(ref AParameterList, ref HTMLOutput, out HTMLDocument, ref ErrorMessage, ref myException, Transaction)) { Success = true; } else { TLogging.Log(ErrorMessage); } }); } catch (Exception Exc) { TLogging.Log("Problem calculating report: " + Exc.ToString()); TLogging.Log(Exc.StackTrace, TLoggingType.ToLogfile); Success = false; ErrorMessage = Exc.Message; } /* * if (TDBExceptionHelper.IsTransactionSerialisationException(FException)) * { * // do nothing - we want this exception to bubble up * } * else if (FException is Exception && FException.InnerException is EOPDBException) * { * EOPDBException DbExc = (EOPDBException)FException.InnerException; * * if (DbExc.InnerException is Exception) * { * if (DbExc.InnerException is PostgresException) * { * PostgresException PgExc = (PostgresException)DbExc.InnerException; * * if (PgExc.SqlState == "57014") // SQL statement timeout problem * { * FErrorMessage = Catalog.GetString( * "Error - Database took too long to respond. Try different parameters to return fewer results."); * } * } * else * { * FErrorMessage = DbExc.InnerException.Message; * } * * FException = null; * } * } */ try { // store the report result db.WriteTransaction(ref Transaction, ref Submit, delegate { // delete report results that are expired. string sql = "DELETE FROM PUB_s_report_result WHERE s_valid_until_d < NOW()"; db.ExecuteNonQuery(sql, Transaction); // TODO: only keep maximum of 10 report results per user (s_created_by_c) // store success, store parameter list, store html document SReportResultTable table = new SReportResultTable(); SReportResultRow row = table.NewRowTyped(); row.ReportId = AReportID; row.SessionId = TSession.GetSessionID(); row.ValidUntil = DateTime.Now.AddHours(12); row.ParameterList = AParameterList.ToJson(); row.ResultHtml = HTMLOutput; row.Success = Success; row.ErrorMessage = ErrorMessage; table.Rows.Add(row); SReportResultAccess.SubmitChanges(table, Transaction); Submit = true; }); } catch (Exception Exc) { TLogging.Log("Problem storing report result: " + Exc.ToString()); TLogging.Log(Exc.StackTrace, TLoggingType.ToLogfile); Success = false; ErrorMessage = Exc.Message; } db.CloseDBConnection(); TProgressTracker.FinishJob(AReportID); }
/// <summary> /// initialise the server once for everyone /// </summary> public static bool Init() { if (ConfigFileName == string.Empty) { // make sure the correct config file is used if (Environment.CommandLine.Contains("/appconfigfile=")) { // this happens when we use fastcgi-mono-server4 ConfigFileName = Environment.CommandLine.Substring( Environment.CommandLine.IndexOf("/appconfigfile=") + "/appconfigfile=".Length); if (ConfigFileName.IndexOf(" ") != -1) { ConfigFileName = ConfigFileName.Substring(0, ConfigFileName.IndexOf(" ")); } } else { // this is the normal behaviour when running with local http server ConfigFileName = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "web.config"; } } new TAppSettingsManager(ConfigFileName); new TLogging(TSrvSetting.ServerLogFile); TLogging.DebugLevel = TAppSettingsManager.GetInt16("Server.DebugLevel", 0); TSession.InitThread(); if (TLogging.DebugLevel >= 4) { TLogging.Log("TOpenPetraOrgSessionManager.Init"); TLogging.Log(HttpContext.Current.Request.PathInfo); } if (HttpContext.Current != null) { HttpContext.Current.Server.ScriptTimeout = Convert.ToInt32( TimeSpan.FromMinutes(TAppSettingsManager.GetInt32("WebRequestTimeOutInMinutes", 15)). TotalSeconds); } // if the Login Method is called: reset cookie, ignore any old session if ((HttpContext.Current != null) && (HttpContext.Current.Request.PathInfo == "/Login")) { TSession.CloseSession(); TSession.InitThread(); } if (TServerManager.TheServerManager == null) { Catalog.Init(); TServerManager.TheServerManager = new TServerManager(); try { // initialise the cached tables TSetupDelegates.Init(); } catch (Exception e) { TLogging.Log(e.Message); TLogging.Log(e.StackTrace); throw; } TLogging.Log("Server has been initialised"); return(true); } if (DomainManager.CurrentClient != null) { if (DomainManager.CurrentClient.FAppDomainStatus == TSessionStatus.adsStopped) { TLogging.Log("There is an attempt to reconnect to stopped session: " + DomainManager.CurrentClient.ClientName); if (HttpContext.Current.Request.PathInfo == "/IsUserLoggedIn") { // we want a clean json response saying the user is not logged in TSession.CloseSession(); return(true); } Dictionary <string, object> result = new Dictionary <string, object>(); result.Add("resultcode", "error"); result.Add("error", THTTPUtils.SESSION_ALREADY_CLOSED); HttpContext.Current.Response.Status = "403 " + THTTPUtils.SESSION_ALREADY_CLOSED; HttpContext.Current.Response.Write(JsonConvert.SerializeObject(result)); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close(); HttpContext.Current.Response.End(); return(false); } // TLogging.Log("Init(): WebService Method name that got called: " + HttpContext.Current.Request.PathInfo); if (HttpContext.Current.Request.PathInfo != "/PollClientTasks") { DomainManager.CurrentClient.UpdateLastAccessTime(); } } return(false); }
/// <summary> /// run the report /// </summary> private void Run(string ASessionID) { // need to initialize the database session TSession.InitThread(ASessionID); IsolationLevel Level; FSuccess = false; TDataBase db = DBAccess.Connect("TReportGeneratorUIConnector"); TDBTransaction Transaction = new TDBTransaction(); try { db.ReadTransaction(ref Transaction, delegate { if (FDatacalculator.GenerateResult(ref FParameterList, ref FHTMLOutput, out FHTMLDocument, ref FErrorMessage, ref FException, Transaction)) { FSuccess = true; } else { TLogging.Log(FErrorMessage); } }); } catch (Exception Exc) { TLogging.Log("Problem calculating report: " + Exc.ToString()); TLogging.Log(Exc.StackTrace, TLoggingType.ToLogfile); FSuccess = false; FErrorMessage = Exc.Message; FException = Exc; } if (TDBExceptionHelper.IsTransactionSerialisationException(FException)) { // do nothing - we want this exception to bubble up } else if (FException is Exception && FException.InnerException is EOPDBException) { EOPDBException DbExc = (EOPDBException)FException.InnerException; if (DbExc.InnerException is Exception) { if (DbExc.InnerException is PostgresException) { PostgresException PgExc = (PostgresException)DbExc.InnerException; if (PgExc.SqlState == "57014") // SQL statement timeout problem { FErrorMessage = Catalog.GetString( "Error - Database took too long to respond. Try different parameters to return fewer results."); } } else { FErrorMessage = DbExc.InnerException.Message; } FException = null; } } TProgressTracker.FinishJob(FProgressID); }