3 Date Created: 08/24/2016
4 ---------------------------------------------------
8 using System.Collections.Generic;
10 using CPE.App.Notify.Extensions;
11 using CPE.App.Notify.Models.Enums;
13 namespace CPE.App.NotifyConsole.Extensions {
14 public static class ApplicationExtensions {
15 // Identitifes the stop session parameter
16 private static string[] stopParameters {
17 get { return new[] {"s", "-s", "/s", "stop"}; }
20 // Identifies the archive parameter
21 private static string[] archiveParameters {
22 get { return new[] {"a", "-a", "/a", "archive", "r", "-r", "/r", "recording"}; }
25 // Identifies the meeting session key parameter
26 private static string[] meetingSessionParameters {
27 get { return new[] {"m", "-m", "/m"}; }
30 // Identifies the nowait parameter
31 private static string[] noWaitParameters {
32 get { return new[] {"nowait", "-nowait", "/nowait", "n", "-n", "/n"}; }
35 // Identifies the help parameter
36 private static string[] helpParameters {
37 get { return new[] {"help", "-help", "/help", "?", "-?", "/?"}; }
40 // Identifies the help parameter
41 private static string[] testParameters {
42 get { return new[] {"test", "-test", "/test", "t", "-t", "/t"}; }
45 // Identifies unattended parameters
46 // Will run without interaction
47 private static string[] unattendedModeParameters {
48 get { return new[] {"u", "-u", "/u"}; }
51 // Should the application run without any interface for user
52 // Default: Interface will show and cause a stop in the thread
53 public static bool Unattended(this string[] args) {
54 return args.Any(x => unattendedModeParameters.Contains(x.ToLower()));
57 // Should the application wait based on the arguments
58 // Default: Wait the determined amount of time from the app.config
59 public static bool Wait(this string[] args) {
60 return !args.Any(x => noWaitParameters.Contains(x.ToLower()));
63 // Should the application accept the key value as a MeetingSessionKey
64 // This is only used for archives (recordings)
65 public static bool IsMeetingSession(this string[] args) {
66 return args.Any(x => meetingSessionParameters.Contains(x.ToLower()));
69 // This is for testing the application
70 // The processes will not execute
71 // This is mainly used by the windows service for testing the execution of the console application
72 public static bool IsTesting(this string[] args) {
73 return args.Any(x => testParameters.Contains(x.ToLower()));
76 // Which method of the application is being processed based on the arguments
77 public static ApplicationMethods WhichMethod(this string[] args) {
78 if(args.Length == 0) {
79 return ApplicationMethods.NotFound;
82 var sessionParameters = stopParameters.Concat(archiveParameters);
83 var foundParameters = args.Where(x => sessionParameters.Contains(x));
84 if(foundParameters.Count() != 1) {
85 "Invalid use of the application. Please try again!".Log(LoggingLevels.Error);
86 return ApplicationMethods.Invalid;
93 // Return all integer values which are the keys for processing the session end
94 public static int[] ArgumentValues(this string[] args) {
95 int[] returnValues = null;
97 var sessionParameters = stopParameters.Concat(archiveParameters);
98 var foundValues = args.Where(x => !sessionParameters.Contains(x.ToLower()));
99 returnValues = foundValues
100 .Select(NullableTryParseInt32)
101 .Where(x => x.HasValue)
102 .Select(x => Convert.ToInt32(x))
104 if(returnValues.Length == 0) {
105 throw new Exception("Arugument value keys were not found");
107 } catch (Exception exception) {
108 exception.Log(LoggingLevels.Fatal);
109 throw new KeyNotFoundException("There are no valid keys!");
114 // Parsing method for return the application methods from the arguments
115 private static ApplicationMethods parse(this string[] args) {
116 if(args.Any(x => stopParameters.Contains(x.ToLower()))) {
117 return ApplicationMethods.StopSession;
120 if(args.Any(x => archiveParameters.Contains(x.ToLower()))) {
121 return ApplicationMethods.ArchiveSession;
124 if(args.Any(x => helpParameters.Contains(x.ToLower()))) {
125 return ApplicationMethods.Help;
128 return ApplicationMethods.NotFound;
131 // Utility to return only integer values from the string array
132 // This is used to extract the keys from the arguments
133 private static int? NullableTryParseInt32(string text) {
135 return int.TryParse(text, out value) ? (int?) value : null;