-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from CityofSantaMonica/v05-integration
Release v0.5.0
- Loading branch information
Showing
16 changed files
with
283 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using SODA.Utilities.Tests.Mocks; | ||
|
||
namespace SODA.Utilities.Tests | ||
{ | ||
[TestFixture] | ||
public class ExcelDataReaderHelperTests | ||
{ | ||
[Test] | ||
[Category("ExcelDataReaderHelper")] | ||
public void GetRowsFromDataSheets_With_Empty_Filename_Throws_ArgumentNullException() | ||
{ | ||
string nullInput = null; | ||
string emptyInput = String.Empty; | ||
|
||
Assert.That( | ||
() => ExcelDataReaderHelper.GetRowsFromDataSheets(nullInput), | ||
Throws.InstanceOf<ArgumentNullException>() | ||
); | ||
|
||
Assert.That( | ||
() => ExcelDataReaderHelper.GetRowsFromDataSheets(emptyInput), | ||
Throws.InstanceOf<ArgumentNullException>() | ||
); | ||
} | ||
|
||
[TestCase("something.txt")] | ||
[TestCase("something_xls")] | ||
[TestCase("something_xlsx")] | ||
[TestCase("something.xls.txt")] | ||
[TestCase("something.xlsx.txt")] | ||
[Category("ExcelDataReaderHelper")] | ||
public void GetRowsFromDataSheets_With_NonExcel_Filename_Throws_InvalidOperationException(string nonExcelFileName) | ||
{ | ||
Assert.That( | ||
() => ExcelDataReaderHelper.GetRowsFromDataSheets(nonExcelFileName), | ||
Throws.InstanceOf<InvalidOperationException>() | ||
); | ||
} | ||
|
||
[TestCase("not-there.xls")] | ||
[TestCase("not-there.xlsx")] | ||
[Category("ExcelDataReaderHelper")] | ||
public void GetRowsFromDataSheets_With_NonExistent_Excel_File_Throws_FileNotFoundException(string nonExistentExcelFileName) | ||
{ | ||
Assert.That( | ||
() => ExcelDataReaderHelper.GetRowsFromDataSheets(nonExistentExcelFileName), | ||
Throws.InstanceOf<FileNotFoundException>() | ||
); | ||
} | ||
|
||
[Test] | ||
[TestCaseSource(typeof(FileMocks), "ExcelMocks")] | ||
[Category("ExcelDataReaderHelper")] | ||
public void GetRowsFromDataSheets_Get_All_Rows_From_All_Data_Sheets(string excelFileName) | ||
{ | ||
var rows = ExcelDataReaderHelper.GetRowsFromDataSheets(excelFileName); | ||
|
||
Assert.AreEqual(3, rows.Count()); | ||
|
||
for (int i = 0; i < rows.Count(); i++) | ||
{ | ||
Assert.AreEqual(String.Format("baz{0}", i + 1), rows.ElementAt(i)["foo"]); | ||
Assert.AreEqual(String.Format("qux{0}", i + 1), rows.ElementAt(i)["bar"]); | ||
} | ||
} | ||
|
||
[Test] | ||
[TestCaseSource(typeof(FileMocks), "ExcelMocks")] | ||
[Category("ExcelDataReaderHelper")] | ||
public void GetRowsFromDataSheets_Get_All_Rows_From_All_Data_Sheets_Without_Column_Names(string excelFileName) | ||
{ | ||
var rows = ExcelDataReaderHelper.GetRowsFromDataSheets(excelFileName, false); | ||
int dataCounter = 1; | ||
|
||
Assert.AreEqual(6, rows.Count()); | ||
|
||
for (int i = 0; i < rows.Count(); i++) | ||
{ | ||
if (i % 2 == 0) | ||
{ | ||
Assert.AreEqual(String.Format("foo", i + 1), rows.ElementAt(i)[0]); | ||
Assert.AreEqual(String.Format("bar", i + 1), rows.ElementAt(i)[1]); | ||
} | ||
else | ||
{ | ||
Assert.AreEqual(String.Format("baz{0}", dataCounter), rows.ElementAt(i)[0]); | ||
Assert.AreEqual(String.Format("qux{0}", dataCounter), rows.ElementAt(i)[1]); | ||
|
||
dataCounter++; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="EWS-Api-2.1" version="1.0.0" targetFramework="net45" /> | ||
<package id="ExcelDataReader" version="2.1.2.3" targetFramework="net45" /> | ||
<package id="NUnit" version="2.6.4" targetFramework="net45" /> | ||
<package id="SharpZipLib" version="0.86.0" targetFramework="net45" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Exchange.WebServices.Data; | ||
|
||
namespace SODA.Utilities | ||
{ | ||
/// <summary> | ||
/// EwsClient implementation for Exchange Server 2010 SP2 | ||
/// </summary> | ||
public class Ews2010Sp2Client : EwsClient | ||
{ | ||
/// <summary> | ||
/// Initialize a new EwsClient targeting Exchange Web Services 2007 SP1. | ||
/// </summary> | ||
/// <param name="username">A user with login rights on the specified <paramref name="domain"/>.</param> | ||
/// <param name="password">The password for the specified <paramref name="username"/>.</param> | ||
/// <param name="domain">The Exchange domain.</param> | ||
public Ews2010Sp2Client(string username, string password, string domain) | ||
: base(username, password, domain, ExchangeVersion.Exchange2010_SP2) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Search the Inbox for the first unread email with an attachment matching the specified regular expression, and if found, download the attachment to the specified directory. | ||
/// </summary> | ||
/// <param name="attachmentNamePattern">The attachment filename pattern to search for.</param> | ||
/// <param name="targetDirectory">The (writable) directory where a found attachment will be saved.</param> | ||
/// <returns>True if a matching attachment was found and downloaded. False otherwise.</returns> | ||
/// <remarks> | ||
/// If the <paramref name="targetDirectory"/> does not exist, it will be created before searching for an attachment to download. | ||
/// </remarks> | ||
/// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="attachmentNamePattern"/> regular expression is null.</exception> | ||
public override bool DownloadAttachment(Regex attachmentNamePattern, string targetDirectory) | ||
{ | ||
if (attachmentNamePattern == null) | ||
throw new ArgumentNullException("attachmentNamePattern"); | ||
|
||
if (!Directory.Exists(targetDirectory)) | ||
Directory.CreateDirectory(targetDirectory); | ||
|
||
return base.DownloadAttachment(attachmentNamePattern, targetDirectory); | ||
} | ||
|
||
/// <summary> | ||
/// Send an email message with the specified subject and body to the specified list of recipient email addresses. | ||
/// </summary> | ||
/// <param name="messageSubject">The subject line of the email message.</param> | ||
/// <param name="messageBody">The plain-text content of the email message.</param> | ||
/// <param name="recipients">One or more email addresses that will be recipients of the email message.</param> | ||
/// <exception cref="System.ArgumentException">Thrown if the specified list of recipients is null or empty.</exception> | ||
public override void SendMessage(string messageSubject, string messageBody, params string[] recipients) | ||
{ | ||
if (recipients == null || !recipients.Any()) | ||
throw new ArgumentException("Must specify at least 1 email recipient.", "recipients"); | ||
|
||
base.SendMessage(messageSubject, messageBody, recipients); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Excel; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace SODA.Utilities | ||
{ | ||
/// <summary> | ||
/// A helper class for working with Excel using ExcelDataReader | ||
/// </summary> | ||
public class ExcelDataReaderHelper | ||
{ | ||
/// <summary> | ||
/// Opens an Excel file and collects all data rows in every sheet | ||
/// </summary> | ||
/// <param name="excelFileName">The path to a readable Excel (.xls or .xlsx) file.</param> | ||
/// <param name="isFirstRowColumnNames">Whether or not the first row of a worksheet is the name of the column; if it is, it won't be added to the IEnumerable</param> | ||
/// <returns>The combined collection of rows from each sheet of data in the underlying Excel file.</returns> | ||
public static IEnumerable<DataRow> GetRowsFromDataSheets(string excelFileName, bool isFirstRowColumnNames = true) | ||
{ | ||
if (String.IsNullOrEmpty(excelFileName)) | ||
{ | ||
throw new ArgumentNullException("A file path cannot be null or empty."); | ||
} | ||
|
||
if (!excelFileName.EndsWith(".xls") && !excelFileName.EndsWith(".xlsx")) | ||
{ | ||
throw new InvalidOperationException("Not a valid Excel (.xls or .xlsx) file."); | ||
} | ||
|
||
if (!File.Exists(excelFileName)) | ||
{ | ||
throw new FileNotFoundException("The specified file does not exist."); | ||
} | ||
|
||
var allDataRows = new List<DataRow>(); | ||
|
||
using (IExcelDataReader reader = MakeExcelReader(excelFileName)) | ||
{ | ||
reader.IsFirstRowAsColumnNames = isFirstRowColumnNames; | ||
|
||
foreach (DataTable table in reader.AsDataSet().Tables) | ||
{ | ||
allDataRows.AddRange(table.Rows.OfType<DataRow>()); | ||
} | ||
} | ||
|
||
return allDataRows; | ||
} | ||
|
||
private static IExcelDataReader MakeExcelReader(string excelFileName) | ||
{ | ||
FileStream stream = File.Open(excelFileName, FileMode.Open, FileAccess.Read); | ||
|
||
if (excelFileName.EndsWith(".xls")) | ||
{ | ||
return ExcelReaderFactory.CreateBinaryReader(stream); | ||
} | ||
else | ||
{ | ||
return ExcelReaderFactory.CreateOpenXmlReader(stream); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="EWS-Api-2.1" version="1.0.0" targetFramework="net45" /> | ||
<package id="ExcelDataReader" version="2.1.2.3" targetFramework="net45" /> | ||
<package id="SharpZipLib" version="0.86.0" targetFramework="net45" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.