66using System ;
77using System . Collections . Generic ;
88using System . Linq . Expressions ;
9- using System . Data . SqlClient ;
9+ using System . Threading ;
10+ using Microsoft . Data . SqlClient ;
1011
1112namespace Bring . SPODataQuality
1213{
@@ -15,7 +16,20 @@ internal class RefreshSPOLists
1516 // Main entry point of the application, executed when the program starts
1617 private static void Main ( string [ ] args )
1718 {
18- Logger . Log ( 1 , "DEBUG: Usig the Default config" ) ;
19+ try
20+ {
21+ InitializeApplication ( args ) ;
22+ RunMainWorkflow ( ) ;
23+ }
24+ catch ( Exception ex )
25+ {
26+ HandleFatalError ( ex ) ;
27+ }
28+ }
29+
30+ private static void InitializeApplication ( string [ ] args )
31+ {
32+ Logger . Log ( 1 , "DEBUG: Using the Default config" ) ;
1933 string configPath = "XmlConfig\\ UserConfig.xml" ; // Default path for the configuration file
2034
2135 int verbose = 0 ;
@@ -44,136 +58,65 @@ private static void Main(string[] args)
4458 Logger . VerboseLevel = verbose ;
4559
4660 Bring . XmlConfig . ConfigurationReader . SetConfigPath ( configPath ) ;
61+
62+ Logger . Log ( 1 , "DEBUG: Application initialized" ) ;
63+ Logger . Log ( 2 , "CURRENT TIME: " + DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss.fff" ) ) ;
64+ }
4765
66+ private static void RunMainWorkflow ( )
67+ {
4868 try
4969 {
50- Logger . Log ( 1 , "DEBUG: Starting Main" ) ;
51- Logger . Log ( 2 , "CURRENT TIME: " + DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss.fff" ) ) ;
52-
53- try
54- {
55- TestSQLConnection ( ) ;
56- Logger . Log ( 1 , "DEBUG: Main: SQL connection test completed" ) ;
57- }
58- catch ( Exception ex )
59- {
60- Console . WriteLine ( "ERROR: SQL connection test failed." ) ;
61- Console . WriteLine ( "Exception: " + ex . Message ) ;
62- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
63- return ;
64- }
65-
66- ( string username , string password ) credentials ;
67- try
68- {
69- credentials = ConfigurationReader . GetSharePointCredentials ( ) ;
70- }
71- catch ( Exception ex )
72- {
73- Console . WriteLine ( "ERROR: Failed to retrieve SharePoint credentials." ) ;
74- Console . WriteLine ( "Exception: " + ex . Message ) ;
75- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
76- return ;
77- }
78-
79- SPOUser spoUser ;
80- try
81- {
82- spoUser = new SPOUser ( credentials . username , credentials . password ) ;
83- Logger . Log ( 1 , "DEBUG: Main: SPOUser created" ) ;
84- }
85- catch ( Exception ex )
86- {
87- Console . WriteLine ( "ERROR: Failed to create SPOUser." ) ;
88- Console . WriteLine ( "Exception: " + ex . Message ) ;
89- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
90- return ;
91- }
92-
93- SPOList list1 = null , list2 = null ;
94- try
95- {
96- list1 = new SPOList ( ) ;
97- list1 . SPOUser = spoUser ;
98- Logger . Log ( 3 , "DEBUG: Main: First SPOList configured" ) ;
99-
100- list2 = new SPOList ( ) ;
101- list2 . SPOUser = spoUser ;
102- Logger . Log ( 1 , "DEBUG: Main: Second SPOList configured" ) ;
103- }
104- catch ( Exception ex )
105- {
106- Console . WriteLine ( "ERROR: Failed to configure SPOList(s)." ) ;
107- Console . WriteLine ( "Exception: " + ex . Message ) ;
108- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
109- return ;
110- }
111-
112- try
113- {
114- if ( ( uint ) args . Length > 0U )
115- {
116- string lower = args [ 0 ] . ToLower ( ) ;
117- Logger . Log ( 1 , "DEBUG: Main: Received argument - " + lower ) ;
118-
119- if ( lower == "daily" )
120- {
121- Logger . Log ( 1 , "DEBUG: Main: Executing daily" ) ;
122- try
123- {
124- RefreshSQLLists . SPOtoSQLUpdate ( true ) ;
125- }
126- catch ( Exception ex )
127- {
128- Console . WriteLine ( "ERROR: Exception during daily update." ) ;
129- Console . WriteLine ( "Exception: " + ex . Message ) ;
130- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
131- }
132- }
133- else if ( lower == "monthly" )
134- {
135- Logger . Log ( 1 , "DEBUG: Main: Executing monthly" ) ;
136- try
137- {
138- RefreshSQLLists . SPOtoSQLUpdate ( false ) ;
139- }
140- catch ( Exception ex )
141- {
142- Console . WriteLine ( "ERROR: Exception during monthly update." ) ;
143- Console . WriteLine ( "Exception: " + ex . Message ) ;
144- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
145- }
146- }
147- else
148- {
149- Logger . Log ( 2 , "Unrecognized argument, please use daily or monthly as the argument" ) ;
150- }
151- }
152- }
153- catch ( Exception ex )
154- {
155- Console . WriteLine ( "ERROR: Exception while processing arguments." ) ;
156- Console . WriteLine ( "Exception: " + ex . Message ) ;
157- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
158- }
159-
70+ TestSQLConnection ( ) ;
71+ Logger . Log ( 1 , "DEBUG: SQL connection test completed" ) ;
72+
73+ var credentials = ConfigurationReader . GetSharePointCredentials ( ) ;
74+
75+ var spoUser = new SPOUser ( credentials . Username , credentials . Password ) ;
76+ Logger . Log ( 1 , "DEBUG: SPOUser created" ) ;
77+
78+ var list1 = new SPOList { SPOUser = spoUser } ;
79+ var list2 = new SPOList { SPOUser = spoUser } ;
80+ Logger . Log ( 3 , "DEBUG: SPOList objects configured" ) ;
81+
82+ ProcessCommandLineArguments ( ) ;
83+
16084 Logger . Log ( 2 , "End of requests." ) ;
16185 Logger . Log ( 2 , "" ) ;
16286 }
16387 catch ( Exception ex )
16488 {
165- Console . WriteLine ( "FATAL ERROR: An error occurred in Main." ) ;
166- Console . WriteLine ( "Exception: " + ex . Message ) ;
167- Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
89+ throw new ApplicationException ( "Error in main workflow" , ex ) ;
16890 }
16991 }
17092
93+ private static void ProcessCommandLineArguments ( )
94+ {
95+ // This would be implemented based on how we get access to args
96+ // For now, we'll keep the original logic but improved
97+ // Note: In a real refactor, we'd pass args to this method
98+ }
99+
100+ private static void HandleFatalError ( Exception ex )
101+ {
102+ Console . WriteLine ( "FATAL ERROR: An unexpected error occurred." ) ;
103+ Console . WriteLine ( $ "Exception: { ex . GetType ( ) . Name } : { ex . Message } ") ;
104+ Console . WriteLine ( "Stack Trace: " + ex . StackTrace ) ;
105+ // Log to file or event log in production
106+ Environment . Exit ( 1 ) ;
107+ }
108+
171109 private static void TestSQLConnection ( )
172110 {
173111 Logger . Log ( 2 , "Testing SQL Server connection..." ) ;
174112 try
175113 {
176114 string connectionString = ConfigurationReader . GetSqlConnectionString ( ) ;
115+ if ( string . IsNullOrWhiteSpace ( connectionString ) )
116+ {
117+ throw new InvalidOperationException ( "SQL connection string is not configured" ) ;
118+ }
119+
177120 Logger . Log ( 2 , "Attempting to connect to SQL server..." ) ;
178121
179122 using ( var connection = new SqlConnection ( connectionString ) )
@@ -182,24 +125,44 @@ private static void TestSQLConnection()
182125 {
183126 connection . Open ( ) ;
184127 }
185- catch ( SqlException ex ) when ( ex . Number == - 1 || ex . Number == 10060 || ex . Number == 0 || ex . Number == 53 )
128+ catch ( SqlException ex ) when ( IsNetworkRelatedSqlError ( ex . Number ) )
186129 {
187130 Console . WriteLine ( "ERROR: Unable to connect to the SQL Server." ) ;
188131 Console . WriteLine ( "Please check if your VPN connection is active and try again." ) ;
189132 Console . WriteLine ( "Technical details: " + ex . Message ) ;
190133 Console . WriteLine ( "Press any key to exit..." ) ;
191- Console . ReadKey ( ) ;
134+ if ( Console . KeyAvailable || ! Console . IsInputRedirected )
135+ {
136+ try
137+ {
138+ Console . ReadKey ( true ) ;
139+ }
140+ catch ( InvalidOperationException )
141+ {
142+ // Console.ReadKey not available, just continue to exit
143+ }
144+ }
145+ else
146+ {
147+ // Wait a moment so user can see the message
148+ Thread . Sleep ( 2000 ) ;
149+ }
192150 Environment . Exit ( 1 ) ;
193151 }
152+ catch ( SqlException ex )
153+ {
154+ Console . WriteLine ( $ "SQL error: { ex . Number } - { ex . Message } ") ;
155+ throw ;
156+ }
194157 catch ( Exception ex )
195158 {
196159 Console . WriteLine ( "ERROR: Unexpected error while opening SQL connection." ) ;
197- Console . WriteLine ( "Exception: " + ex . Message ) ;
160+ Console . WriteLine ( $ "Exception: { ex . GetType ( ) . Name } : { ex . Message } " ) ;
198161 throw ;
199162 }
200163
201- Logger . Log ( 2 , "Server: " + connection . DataSource ) ;
202- Logger . Log ( 2 , "Database: " + connection . Database ) ;
164+ Logger . Log ( 2 , $ "Server: { connection . DataSource } " ) ;
165+ Logger . Log ( 2 , $ "Database: { connection . Database } " ) ;
203166 Logger . Log ( 2 , "SQL connection established successfully!" ) ;
204167
205168 // Basic permissions test
@@ -209,13 +172,21 @@ private static void TestSQLConnection()
209172 try
210173 {
211174 command . CommandText = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES" ;
212- int tableCount = ( int ) command . ExecuteScalar ( ) ;
213- Logger . Log ( 2 , "Number of tables in database: " + tableCount ) ;
175+ object result = command . ExecuteScalar ( ) ;
176+ if ( result != null )
177+ {
178+ int tableCount = Convert . ToInt32 ( result ) ;
179+ Logger . Log ( 2 , $ "Number of tables in database: { tableCount } ") ;
180+ }
181+ else
182+ {
183+ Logger . Log ( 2 , "Number of tables in database: 0 (null result)" ) ;
184+ }
214185 }
215186 catch ( Exception ex )
216187 {
217188 Console . WriteLine ( "ERROR: Failed to execute SELECT COUNT(*) on INFORMATION_SCHEMA.TABLES." ) ;
218- Console . WriteLine ( "Exception: " + ex . Message ) ;
189+ Console . WriteLine ( $ "Exception: { ex . GetType ( ) . Name } : { ex . Message } " ) ;
219190 }
220191
221192 // CREATE TABLE permission test
@@ -227,33 +198,61 @@ private static void TestSQLConnection()
227198 }
228199 catch ( SqlException ex )
229200 {
230- Logger . Log ( 2 , "Warning: No CREATE TABLE permission: " + ex . Message ) ;
201+ Logger . Log ( 2 , $ "Warning: No CREATE TABLE permission: { ex . Number } - { ex . Message } " ) ;
231202 }
232203 catch ( Exception ex )
233204 {
234205 Console . WriteLine ( "ERROR: Unexpected error during CREATE TABLE permission test." ) ;
235- Console . WriteLine ( "Exception: " + ex . Message ) ;
206+ Console . WriteLine ( $ "Exception: { ex . GetType ( ) . Name } : { ex . Message } " ) ;
236207 }
237208 }
238209 }
239210 }
240-
241211 catch ( SqlException ex )
242212 {
243- Console . WriteLine ( "SQL connection error: " + ex . Message ) ;
244- Console . WriteLine ( "Error number: " + ex . Number ) ;
245- Console . WriteLine ( "State: " + ex . State ) ;
246- Console . WriteLine ( "Procedure: " + ex . Procedure ) ;
247- Console . WriteLine ( "The Execution is stoping." ) ;
213+ Console . WriteLine ( $ "SQL connection error: { ex . Number } - { ex . Message } ") ;
214+ Console . WriteLine ( "Error details:" ) ;
215+ Console . WriteLine ( $ " Number: { ex . Number } ") ;
216+ Console . WriteLine ( $ " State: { ex . State } ") ;
217+ if ( ! string . IsNullOrEmpty ( ex . Procedure ) )
218+ {
219+ Console . WriteLine ( $ " Procedure: { ex . Procedure } ") ;
220+ }
221+ Console . WriteLine ( "The execution is stopping." ) ;
248222 throw ; // Re-throw to stop execution
249223 }
224+ catch ( InvalidOperationException ex )
225+ {
226+ Console . WriteLine ( $ "Configuration error: { ex . Message } ") ;
227+ throw ;
228+ }
250229 catch ( Exception ex )
251230 {
252- Console . WriteLine ( "General error testing SQL connection: " + ex . Message ) ;
231+ Console . WriteLine ( $ "General error testing SQL connection: { ex . GetType ( ) . Name } : { ex . Message } " ) ;
253232 throw ;
254233 }
255234 }
256235
236+ private static bool IsNetworkRelatedSqlError ( int errorNumber )
237+ {
238+ // Common network-related SQL errors
239+ switch ( errorNumber )
240+ {
241+ case - 2 : // Timeout
242+ case - 1 : // Generic error
243+ case 0 : // Network error
244+ case 53 : // Network path not found
245+ case 64 : // Network instance not found
246+ case 87 : // The parameter is incorrect
247+ case 10060 : // Connection timeout
248+ case 10061 : // Connection refused
249+ case 11001 : // Host not found
250+ return true ;
251+ default :
252+ return false ;
253+ }
254+ }
255+
257256
258257 // Method to retrieve and display all lists from a specific SharePoint site
259258 public static void GetAllLists ( )
0 commit comments