Skip to content

Commit

Permalink
Merge pull request #48 from CityofSantaMonica/v05-integration
Browse files Browse the repository at this point in the history
Release v0.5.0
  • Loading branch information
thekaveman committed Aug 12, 2015
2 parents 34494d3 + a1f2f3e commit 4b2dcae
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 15 deletions.
15 changes: 15 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
### New in 0.5.0 (Released 2015/08/12)

Dependency Updates

- `Newtonsoft.Json` upgraded to 7.0.1 [#43](https://github.com/CityofSantaMonica/SODA.NET/issues/43)

Deprecation Notices

- `ExcelOleDbHelper` in `SODA.Utilities` has been deprecated and replaced by `ExcelDataReaderHelper`. `ExcelOleDbHelper` will be removed in `v0.6.0`.

New features

- `ExcelDataReaderHelper` for reading data from Excel documents [#46](https://github.com/CityofSantaMonica/SODA.NET/pull/46) via @allejo
- `Ews2010Sp2Client` for utilizing EWS against Exchange 2010 SP2

### New in 0.4.1 (Released 2015/06/11)

Bug fixes
Expand Down
98 changes: 98 additions & 0 deletions SODA.Utilities.Tests/ExcelDataReaderHelperTests.cs
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++;
}
}
}
}
}
1 change: 1 addition & 0 deletions SODA.Utilities.Tests/ExcelOleDbHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void GetRowsFromDataSheets_Gets_All_Rows_From_All_Data_Sheets(string exce

[Test]
[Category("ExcelOleDbHelper")]
[Ignore("Unique OleDB registry hack and is not part of the SODA.NET project")]
public void System_Has_TypeGuessRows_Registry_Setting_To_Maximum_Value()
{
string key = @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel";
Expand Down
9 changes: 9 additions & 0 deletions SODA.Utilities.Tests/SODA.Utilities.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Excel, Version=2.1.2.3, Culture=neutral, PublicKeyToken=93517dbe6a4012fa, processorArchitecture=MSIL">
<HintPath>..\packages\ExcelDataReader.2.1.2.3\lib\net45\Excel.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Exchange.WebServices, Version=15.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\EWS-Api-2.1.1.0.0\lib\net35\Microsoft.Exchange.WebServices.dll</HintPath>
Expand Down Expand Up @@ -75,6 +83,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="DataRowExtensionsTests.cs" />
<Compile Include="ExcelDataReaderHelperTests.cs" />
<Compile Include="ExcelOleDbHelperTests.cs" />
<Compile Include="FileLoggingTests.cs" />
<Compile Include="Mocks\DataRowMocks.cs" />
Expand Down
2 changes: 2 additions & 0 deletions SODA.Utilities.Tests/packages.config
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>
61 changes: 61 additions & 0 deletions SODA.Utilities/Ews2010Sp2Client.cs
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);
}
}
}
67 changes: 67 additions & 0 deletions SODA.Utilities/ExcelDataReaderHelper.cs
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);
}
}
}
}
11 changes: 7 additions & 4 deletions SODA.Utilities/ExcelOleDbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ namespace SODA.Utilities
/// <summary>
/// A helper class for working with Excel over OleDb.
/// </summary>
[Obsolete("Usage of OleDb will be removed in v0.6.0. Use the ExcelDataReaderHelper class instead.")]
public class ExcelOleDbHelper
{
/// <summary>
/// <deprecated type="deprecate">
/// Creates an OleDbConnection to a specified Excel file.
/// </summary>
/// </deprecated>
/// <param name="excelFileName">The path to a readable Excel (.xls or .xlsx) file.</param>
/// <returns>An OleDbConnection to the specified Excel file.</returns>
[Obsolete("This method will be removed in v0.6.0.")]
public static OleDbConnection MakeConnection(string excelFileName)
{
if (!String.IsNullOrEmpty(excelFileName) && (excelFileName.EndsWith(".xls") || excelFileName.EndsWith(".xlsx")))
Expand All @@ -35,11 +37,12 @@ public static OleDbConnection MakeConnection(string excelFileName)
throw new ArgumentException("Not a valid Excel (.xls or .xlsx) file.", "excelFileName");
}

/// <summary>
/// <deprecated type="deprecate">
/// Opens the specified OleDbConnection, collects all data rows in every sheet in the underlying Excel file, and then closes the connection.
/// </summary>
/// </deprecated>
/// <param name="connection">An OleDbConnection to an Excel file with at least one sheet of data.</param>
/// <returns>The combined collection of rows from each sheet of data in the underlying Excel file.</returns>
[Obsolete("This method will be removed in v0.6.0.")]
public static IEnumerable<DataRow> GetRowsFromDataSheets(OleDbConnection connection)
{
if (connection == null)
Expand Down
10 changes: 10 additions & 0 deletions SODA.Utilities/SODA.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
<DocumentationFile>bin\Release\SODA.Utilities.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Excel, Version=2.1.2.3, Culture=neutral, PublicKeyToken=93517dbe6a4012fa, processorArchitecture=MSIL">
<HintPath>..\packages\ExcelDataReader.2.1.2.3\lib\net45\Excel.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Exchange.WebServices">
<HintPath>..\packages\EWS-Api-2.1.1.0.0\lib\net35\Microsoft.Exchange.WebServices.dll</HintPath>
</Reference>
Expand All @@ -60,7 +68,9 @@
<Link>SolutionInfo.cs</Link>
</Compile>
<Compile Include="DataFileExporter.cs" />
<Compile Include="Ews2010Sp2Client.cs" />
<Compile Include="Ews2007Sp1Client.cs" />
<Compile Include="ExcelDataReaderHelper.cs" />
<Compile Include="ExcelOleDbHelper.cs" />
<Compile Include="EwsClient.cs">
<SubType>Code</SubType>
Expand Down
2 changes: 1 addition & 1 deletion SODA.Utilities/SODA.Utilities.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<copyright>Copyright 2015 City of Santa Monica, CA</copyright>
<tags>EWS OleDb SODA</tags>
<dependencies>
<dependency id="CSM.SodaDotNet" version="0.4.1" />
<dependency id="CSM.SodaDotNet" version="0.5.0" />
<dependency id="EWS-Api-2.1" version="1.0.0" />
</dependencies>
</metadata>
Expand Down
2 changes: 2 additions & 0 deletions SODA.Utilities/packages.config
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>
4 changes: 2 additions & 2 deletions SODA.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30723.0
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SODA", "SODA\SODA.csproj", "{837EBDD7-EB58-44F8-834B-F2903A05A16E}"
EndProject
Expand Down
6 changes: 3 additions & 3 deletions SODA/SODA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
<DocumentationFile>bin\Release\SODA.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion SODA/SODA.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<copyright>Copyright 2015 City of Santa Monica, CA</copyright>
<tags>API OpenData Socrata SODA</tags>
<dependencies>
<dependency id="Newtonsoft.Json" version="6.0.8" />
<dependency id="Newtonsoft.Json" version="7.0.1" />
</dependencies>
</metadata>
</package>
Loading

0 comments on commit 4b2dcae

Please sign in to comment.