-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.aspx.cs
198 lines (179 loc) · 7.11 KB
/
Search.aspx.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Seattle.DoIT.PoliceReports.Web.UI;
using Seattle.DoIT.PoliceReports.Web.PoliceReportServices;
namespace Seattle.DoIT.PoliceReports.Web
{
public partial class Search : SecuredPage
{
private const string _defaultDateTimeFormat = "MM/dd/yyyy";
private const string _reportDescriptionFormat = "{0} - {1} - {2}{3}";
private const string _reportDescriptionDateTimeFormat = "MMM d yyyy h:mmtt";
private const string _manualReportReleaseType = "1";
private const string _manualPoliceReportSuffix = "*";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitializeOffenseDropDown();
InitializeSearchDateRange();
}
}
private void InitializeOffenseDropDown()
{
OffenseTypeDropDownList.DataSource = GetAllOffenseTypes();
OffenseTypeDropDownList.DataBind();
}
private void InitializeSearchDateRange()
{
DateTime today = DateTime.Today;
DateRangeBoundary2 = today;
DateRangeBoundary1 = today.AddDays(-7);
}
protected void SearchButton_Click(object sender, EventArgs e)
{
if (IsValid)
{
try
{
ResultsRepeater.DataSource = null;
PoliceReportSummaryQuery query = CreateQuery();
IEnumerable<PoliceReportSummary> reportSummaries = GetQueryResults(query);
UpdateSearchResults(reportSummaries);
}
catch (Exception x)
{
DisplayEmptyResults();
}
}
}
private string ResultCountMessage
{
get { return ResultCountMessageLabel.Text; }
set { ResultCountMessageLabel.Text = value; }
}
private void DisplayEmptyResults()
{
ResultsRepeater.DataSource = null;
ResultsRepeater.DataBind();
ResultsRepeater.Visible = false;
ResultCountMessage = Resources.Messages.NoReportsFoundMessage;
}
private DateTime DateRangeBoundary1
{
get
{
DateTime dateRangeBoundary;
return DateTime.TryParse(DateRange1TextBox.Text, out dateRangeBoundary) ?
dateRangeBoundary.Date : DateTime.Today;
}
set
{
DateRange1TextBox.Text = value.Date.ToString(_defaultDateTimeFormat);
}
}
private DateTime DateRangeBoundary2
{
get
{
DateTime dateRangeBoundary;
return DateTime.TryParse(DateRange2TextBox.Text, out dateRangeBoundary) ?
dateRangeBoundary.Date : DateTime.Today;
}
set
{
DateRange2TextBox.Text = value.Date.ToString(_defaultDateTimeFormat);
}
}
private PoliceReportSummaryQuery CreateQuery()
{
string[] offenseCodeFilter = OffenseTypeDropDownList.SelectedValue == "0" ?
null : new string[1] { OffenseTypeDropDownList.SelectedValue };
DateTime dateRangeBoundary1 = DateRangeBoundary1;
DateTime dateRangeBoundary2 = DateRangeBoundary2;
DateTime startDate = dateRangeBoundary1 <= dateRangeBoundary2 ? dateRangeBoundary1 : dateRangeBoundary2;
DateTime endDate = dateRangeBoundary1 > dateRangeBoundary2 ? dateRangeBoundary1 : dateRangeBoundary2;
return PoliceReportSummaryQueryFactory.Create(offenseCodeFilter, startDate, endDate);
}
private IEnumerable<PoliceReportSummary> GetQueryResults(PoliceReportSummaryQuery query)
{
IEnumerable<PoliceReportSummary> sortedReportSummaries = null;
if (query != null)
{
PoliceReportSummary[] reportSummaries = null;
PoliceReportService svc = PoliceReportServiceFactory.Create();
using (svc as IDisposable)
{
reportSummaries = svc.GetReportSummariesByQuery(query);
}
if (reportSummaries != null)
{
sortedReportSummaries =
reportSummaries.OrderByDescending(r => r.OccurrenceDate).ThenBy(r => r.OffenseCode).ThenBy(r => r.AddressBlock);
}
}
return sortedReportSummaries;
}
private void UpdateSearchResults(IEnumerable<PoliceReportSummary> reportSummaries)
{
if (reportSummaries == null || reportSummaries.Count() == 0)
{
DisplayEmptyResults();
}
else
{
int actualResultCount = reportSummaries.Count();
int maxResultCount = PoliceReportSummaryQuerySettings.MaxQueryResultCount;
IEnumerable<PoliceReportSummary> resultData = reportSummaries.Take(maxResultCount);
/*
var resultData = from s in reportSummaries.Take(maxResultCount)
select new
{
GoNumber = s.GoNumber,
Description = string.Format(descriptionFormat, s.OccurrenceDate.ToString("MMM d yyyy h:mmtt"), s.OffenseCode, s.AddressBlock)
};
*/
/*
IEnumerable<PoliceReportSummary> displayedReportSummaries = actualResultCount > maxResultCount ?
reportSummaries.Take(maxResultCount) : reportSummaries;
int displayedResultCount = displayedReportSummaries.Count();
*/
int displayedResultCount = resultData.Count();
ResultsRepeater.DataSource = resultData;
ResultsRepeater.DataBind();
ResultsRepeater.Visible = true;
string msgFormat = actualResultCount > displayedResultCount ?
Resources.Messages.TooManyReportsFoundMessageFormat : Resources.Messages.ReportsFoundMessageFormat;
ResultCountMessage = string.Format(msgFormat, displayedResultCount);
}
}
public string FormatReportDescription(PoliceReportSummary report)
{
string desc = string.Empty;
if (report != null)
{
desc = string.Format(_reportDescriptionFormat,
report.OccurrenceDate.ToString(_reportDescriptionDateTimeFormat),
report.OffenseCode,
report.AddressBlock,
string.Compare(report.ReleaseType, _manualReportReleaseType, true) == 0 ? _manualPoliceReportSuffix : string.Empty);
}
return desc;
}
private string[] GetAllOffenseTypes()
{
string[] offenseTypes = null;
PoliceReportService svc = PoliceReportServiceFactory.Create();
using (svc as IDisposable)
{
offenseTypes = svc.GetAllOffenseTypes();
}
return offenseTypes;
}
}
}