#region Copyright © 2003-2008 Richard Beauchamp /* * DomainObjects for .NET * Copyright © 2003-2008 Richard Beauchamp * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endregion using System; using System.Text; namespace DomainObjects.Tutorial { public class TutorialClient { /// /// The main entry point for the application. Parses the command line entry. /// [STAThread] private static void Main(string[] args) { if (args == null || args.Length == 0) { DisplayHelpMessage(); return; } try { string starName = null; string planetName = null; int nextArgType = 0; foreach (string arg in args) { switch (arg) { case "/hello": Console.WriteLine(); DisplayHelloWorldResult(); return; case "/create": if (args.Length != 4) { DisplayHelpMessage(); return; } nextArgType = 1; break; case "/?": case "/help": DisplayHelpMessage(); if (args.Length == 1) { return; //if only '/?' switch is specified, don't proceed } break; default: if (nextArgType == 1) { starName = arg; nextArgType = 2; break; } else if (nextArgType == 2) { planetName = arg; nextArgType = 3; break; } else if (nextArgType == 3) { Console.WriteLine(); CreateNewStarAndPlanet(starName, planetName, arg); break; } else { Console.WriteLine(); Console.WriteLine("Incorrect command-line specified. Use the '/?' switch for help."); break; } } } } catch (Exception ex) { Console.WriteLine("An error occured. Description -> " + GetExceptionMessage(ex)); } } private static string GetExceptionMessage(Exception exception) { StringBuilder exceptionMessage = new StringBuilder(); exceptionMessage.Append(exception.Message); exceptionMessage.Append(exception.StackTrace); exceptionMessage.Append("\n\r"); while (exception.InnerException != null) { exception = exception.InnerException; exceptionMessage.Append(exception.Message); exceptionMessage.Append(exception.StackTrace); exceptionMessage.Append("\n\r"); } return exceptionMessage.ToString(); } /// /// Handle the /hello command line switch /// private static void DisplayHelloWorldResult() { // get the singleton instance of TransactionFacade TransactionFacade facade = TransactionFacade.Instance; Planet planet = facade.HelloWorld(); if (planet == null) { Console.WriteLine("No instance of Planet with the name of 'Earth' was found! " + "You need to create a row in both the SUN and PLANET tables and set the value of the PLANET.NAME column to 'Earth'."); } else { Console.WriteLine("Hello from planet earth!! You found me in the database!!"); } } /// /// Handle the /create command line switch /// private static void CreateNewStarAndPlanet(string starName, string newPlanetName, string rotationalPeriodString) { decimal rotationalPeriod; // validate and parse the rotationalPeriodString parameter try { rotationalPeriod = Decimal.Parse(rotationalPeriodString); } catch (FormatException) { Console.WriteLine("Could not create the new star and planet: Please enter " + "a decimal value for the rotational period."); return; } // get the singleton instance of TransactionFacade TransactionFacade facade = TransactionFacade.Instance; // create the new star and planet try { facade.CreateNewStarAndPlanet(starName, newPlanetName, rotationalPeriod); // successful! Console.WriteLine("Created a new star with name '" + starName + "' and created a new planet with name '" + newPlanetName + "' and rotational period of '" + rotationalPeriodString + "'!"); } catch (Exception ex) { Console.WriteLine("Could not create a new star and planet because of an " + "exception! The message is: " + ex); } } private static void DisplayHelpMessage() { Console.WriteLine(); Console.WriteLine("USAGE: "); Console.WriteLine("helloworld [/hello /create ]"); Console.WriteLine("Available command-line switches:"); Console.WriteLine("/hello - Call the HelloWorld() method on the TransactionFacade class."); Console.WriteLine("/create - Create a new instance of Star with the given "); Console.WriteLine(" and, in the same transaction, create a new instance Planet "); Console.WriteLine(" associated with the new Star, that has the given and ."); Console.WriteLine(); } } }