/
FormController.cs
297 lines (237 loc) · 11.1 KB
/
FormController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
Copyright (c) 2012 Azavea, Inc.
This file is part of ACS Alchemist.
ACS Alchemist is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ACS Alchemist 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with ACS Alchemist. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.IO;
using Azavea.NijPredictivePolicing.Common;
using log4net.Appender;
using log4net.Layout;
using Azavea.NijPredictivePolicing.ACSAlchemist;
using Azavea.NijPredictivePolicing.ACSAlchemistLibrary;
using Azavea.NijPredictivePolicing.ACSAlchemistLibrary.FileFormats;
namespace Azavea.NijPredictivePolicing.AcsAlchemistGui
{
/// <summary>
/// This class is meant to act as our interface between the GUI and the rest of the importer logic
/// </summary>
public class FormController
{
static FormController()
{
_instance = new FormController();
}
private static FormController _instance;
public static FormController Instance
{
get { return _instance; }
}
#region FormController Properties
/// <summary>
/// Our resident instance of the current 'job', typically holds
/// command line params, runs an import, does exports, etc.
/// </summary>
public ImportJob JobInstance { get; set; }
/// <summary>
/// Returns a modifiable list of years that correspond to ACS data that
/// can be used as a live binding source for a form
/// </summary>
public List<string> AvailableYears
{
get
{
if (_availableYears != null) { return _availableYears; }
//a quick initialization
_availableYears = new List<string>(Settings.LoadYearConfigs().Keys);
_availableYears.Insert(0, string.Empty); //blank option
return _availableYears;
}
set { _availableYears = value; }
}
protected List<string> _availableYears;
/// <summary>
/// Returns a modifiable list of states that correspond to ACS data that
/// can be used as a live binding source for a form
/// </summary>
public List<AcsState> AvailableStates
{
get
{
if (_availableStates != null) { return _availableStates; }
//a quick initialization
_availableStates = Utilities.GetEnumAsList<AcsState>(
new HashSet<AcsState>(new AcsState[] { AcsState.None })
);
_availableStates.Insert(0, AcsState.None); //blank option
return _availableStates;
}
set { _availableStates = value; }
}
protected List<AcsState> _availableStates;
/// <summary>
/// Returns a modifiable list of summary levels that correspond to ACS data that
/// can be used as a live binding source for a form
/// </summary>
public List<BoundaryLevels> AvailableLevels
{
get
{
if (_availableLevels != null) { return _availableLevels; }
//a quick initialization
_availableLevels = Utilities.GetEnumAsList<BoundaryLevels>(
new HashSet<BoundaryLevels>(new BoundaryLevels[] { BoundaryLevels.None })
);
_availableLevels.Insert(0, BoundaryLevels.None); //blank option
return _availableLevels;
}
set { _availableLevels = value; }
}
protected List<BoundaryLevels> _availableLevels;
/// <summary>
/// Returns a modifiable list of projections that correspond to SRID.csv
/// </summary>
public List<string> AvailableProjections
{
get
{
if (_availableProjections != null) { return _availableProjections; }
//a quick initialization
_availableProjections = Utilities.ListAllCoordinateSystemIDs();
return _availableProjections;
}
set { _availableProjections = value; }
}
protected List<string> _availableProjections;
#endregion FormController Properties
#region Initialization Boilerplate
private static ILog _log = null;
public const string DefaultConfigPath = "configs/AcsAlchemist.json.config";
internal void InitLogging(IAppender appenderObj)
{
if (_log != null)
{
_log.Debug("Logging already initialized");
return;
}
try
{
//attempt to load logging configuration
System.IO.FileInfo configFile = new System.IO.FileInfo(Path.Combine(Settings.ApplicationPath, "Logging.config"));
log4net.Config.XmlConfigurator.ConfigureAndWatch(configFile);
_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
catch
{
ConsoleAppender con = new ConsoleAppender();
con.Layout = new PatternLayout("%message%newline");
//con.Threshold = log4net.Core.Level.Info;
log4net.Config.BasicConfigurator.Configure(con);
_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
if (appenderObj != null)
{
((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetLoggerRepository()).Root.AddAppender(appenderObj);
}
}
protected void ShowWelcomeScreen()
{
_log.Debug("Welcome to ACS Alchemist");
_log.Debug("");
_log.Debug(@"This project was supported by Award No. 2010-DE-BX-K004, awarded by the National Institute of Justice, Office of Justice Programs, U.S. Department of Justice. The opinions, findings, and conclusions or recommendations expressed in this software are those of the author(s) and do not necessarily reflect those of the Department of Justice or Temple University. The software was developed by Azavea in connection with the National Institute of Justice grant awarded to Jerry Ratcliffe and Ralph Taylor of Temple University's Center for Security and Crime Science. The source code is released under a GPLv3 license and is available at: https://github.com/azavea/acs-alchemist");
_log.Debug("");
}
protected void ShowCopyrightAndLicense()
{
_log.Info("");
_log.Info("ACS Alchemist - Copyright (c) 2011-2012 Azavea Inc.");
_log.Info("This program comes with ABSOLUTELY NO WARRANTY; This is free software, and you are welcome to redistribute it under the terms of the GNU General Public License");
//TODO: libraries we need to list here?
_log.Info("");
}
/// <summary>
/// Searches for our config file in the application path, if it can't find it,
/// just uses defaults
/// </summary>
protected void LoadConfigFile()
{
string configName = Path.Combine(Settings.ApplicationPath, DefaultConfigPath);
Settings.ConfigFile = new Config(configName);
if (Settings.ConfigFile.IsEmpty())
{
_log.FatalFormat("Config file {0} missing or empty! Cannot continue, exiting", DefaultConfigPath);
Environment.Exit((int)Constants.ExitCodes.BadConfig);
}
Settings.LoadYearConfigs();
}
/// <summary>
/// Run initialization sequence (logging, load config, welcome, copyright, license, create job instance)
/// </summary>
internal void Initialize()
{
LoadConfigFile();
ShowWelcomeScreen();
ShowCopyrightAndLicense();
NewDefaultJobInstance();
}
#endregion Initialization Boilerplate
/// <summary>
/// Performs some sanity checks on our variables file
/// (does it exist, can I read it, does it have at least one variable, etc.)
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
internal bool ValidateVariablesFile(string filename, out string errorMessage)
{
if (!File.Exists(filename))
{
errorMessage = "File does not exist";
return false;
}
//EXTRA CREDIT: read / parse file, make sure it is a valid variables file
//DOUBLE BONUS EXTRA CREDIT: read / parse file, lookup corresponding description in sequence file, display to user
errorMessage = string.Empty;
return true;
}
/// <summary>
/// replaces our current job instance with a new one loaded from a saved file
/// </summary>
/// <param name="filename"></param>
internal void LoadNewJobInstance(string filename)
{
FormController.Instance.JobInstance = new ImportJob();
FormController.Instance.JobInstance.Load(new string[] { filename });
}
/// <summary>
/// There is no structure ensuring these defaults are the same that will show up when the form loads,
/// This function is meant to reflect whatever the form defaults are.
/// </summary>
internal void NewDefaultJobInstance()
{
FormController.Instance.JobInstance = new ImportJob();
//we need to set some defaults here, to keep things happy
FormController.Instance.JobInstance.PreserveJam = "true";
FormController.Instance.JobInstance.AddStrippedGEOIDcolumn = "true";
FormController.Instance.JobInstance.AddGeometryAttributesToOutput = "true";
FormController.Instance.JobInstance.IncludeEmptyGridCells = "true";
FormController.Instance.JobInstance.Year = string.Empty;
FormController.Instance.JobInstance.State = AcsState.None;
FormController.Instance.JobInstance.SummaryLevel = BoundaryLevels.None.ToString();
FormController.Instance.JobInstance.WorkingFolder = Settings.AppDataDirectory;
}
public bool IsOffline { get; set; }
}
}