initial commit
[CPE_learningsite] / CPE / CPE.App / CPE.App.NotifyConsole / Extensions / ApplicationExtensions.cs
1 /*
2 Developer: Tyler Allen
3 Date Created: 08/24/2016
4 ---------------------------------------------------
5 */
6
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using CPE.App.Notify.Extensions;
11 using CPE.App.Notify.Models.Enums;
12
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"}; }
18         }
19
20         // Identifies the archive parameter 
21         private static string[] archiveParameters {
22             get { return new[] {"a", "-a", "/a", "archive", "r", "-r", "/r", "recording"}; }
23         }
24
25         // Identifies the meeting session key parameter
26         private static string[] meetingSessionParameters {
27             get { return new[] {"m", "-m", "/m"}; }
28         }
29
30         // Identifies the nowait parameter
31         private static string[] noWaitParameters {
32             get { return new[] {"nowait", "-nowait", "/nowait", "n", "-n", "/n"}; }
33         }
34
35         // Identifies the help parameter
36         private static string[] helpParameters {
37             get { return new[] {"help", "-help", "/help", "?", "-?", "/?"}; }
38         }
39
40         // Identifies the help parameter
41         private static string[] testParameters {
42             get { return new[] {"test", "-test", "/test", "t", "-t", "/t"}; }
43         }
44
45         // Identifies unattended parameters
46         // Will run without interaction
47         private static string[] unattendedModeParameters {
48             get { return new[] {"u", "-u", "/u"}; }
49         }
50
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()));
55         }
56
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()));
61         }
62
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()));
67         }
68
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()));
74         }
75
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;
80             }
81
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;
87             }
88
89
90             return args.parse();
91         }
92
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;
96             try {
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))
103                     .ToArray();
104                 if(returnValues.Length == 0) {
105                     throw new Exception("Arugument value keys were not found");
106                 }
107             } catch (Exception exception) {
108                 exception.Log(LoggingLevels.Fatal);
109                 throw new KeyNotFoundException("There are no valid keys!");
110             }
111             return returnValues;
112         }
113
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;
118             }
119
120             if(args.Any(x => archiveParameters.Contains(x.ToLower()))) {
121                 return ApplicationMethods.ArchiveSession;
122             }
123
124             if(args.Any(x => helpParameters.Contains(x.ToLower()))) {
125                 return ApplicationMethods.Help;
126             }
127
128             return ApplicationMethods.NotFound;
129         }
130
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) {
134             int value;
135             return int.TryParse(text, out value) ? (int?) value : null;
136         }
137     }
138 }