Skip to content

Commit

Permalink
Added feature to insert records into excel files
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankshetty committed Jul 18, 2009
1 parent 134b700 commit 99a47c3
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 39 deletions.
Binary file modified lib/ExcelToDTOMapper.DTO.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion src/BuildDTOsFromExcel.Tests/EngineTests.cs
Expand Up @@ -23,7 +23,7 @@ public class When_given_a_list_of_excel_files
[SetUp]
public void SetUp()
{
ExcelMapperServiceLocator.SetUp();
ExcelMapper.Configuration.ExcelMapper.SetUp();
_engine = new Engine(new FileParser(new FileSystemService()), ServiceLocator.Current.GetInstance<IExcelToDTOMapper>());
}

Expand Down
12 changes: 5 additions & 7 deletions src/BuildDTOsFromExcel/Program.cs
@@ -1,10 +1,6 @@
using System;

using BuildDTOsFromExcel.FileService;

using ExcelMapper;
using ExcelMapper.Configuration;

using Microsoft.Practices.ServiceLocation;

namespace BuildDTOsFromExcel
Expand All @@ -25,15 +21,17 @@ public static void Main(string[] args)
return;
}

ExcelMapperServiceLocator.SetUp();
IEngine buildDtoFromExcelEngine = new Engine(new FileParser(new FileSystemService()), ServiceLocator.Current.GetInstance<IExcelToDTOMapper>());
ExcelMapper.Configuration.ExcelMapper.SetUp();
IEngine buildDtoFromExcelEngine = new Engine(new FileParser(new FileSystemService()),
ServiceLocator.Current.GetInstance<IExcelToDTOMapper>());

Console.WriteLine(buildDtoFromExcelEngine.Run(args));
}

private static void DisplayUsage()
{
Console.WriteLine("Maps non-empty workSheets in excel files to an assembly containing strongly typed objects");
Console.WriteLine(
"Maps non-empty workSheets in excel files to an assembly containing strongly typed objects");
Console.WriteLine();
Console.WriteLine("BuildDTOsFromExcel [/Assembly[[:]assemblyName]] [Excel Files]");
Console.WriteLine("/Assembly [Optional] Assembly name");
Expand Down
14 changes: 7 additions & 7 deletions src/ExcelMapper.Tests/ExcelToDTOMapperTests.cs
Expand Up @@ -61,8 +61,8 @@ public void Should_create_one_dto_for_each_of_the_worksheet()
AddProperties();

_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(null)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(true);

Expand All @@ -73,8 +73,8 @@ public void Should_create_one_dto_for_each_of_the_worksheet()
public void Should_not_create_dto_if_there_are_no_properties()
{
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetDTOClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(_classAttributes1)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(true);

Expand All @@ -87,8 +87,8 @@ public void Should_not_create_an_assembly_if_the_compile_fails()
AddProperties();

_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes2);
_excelRepository.Expect(x => x.GetDTOClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetDTOClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(null)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(false);

Expand All @@ -112,7 +112,7 @@ public class SystemTest
[SetUp]
public void SetUp()
{
ExcelMapperServiceLocator.SetUp();
Configuration.ExcelMapper.SetUp();
_excelToDtoMapper = ServiceLocator.Current.GetInstance<IExcelToDTOMapper>();
_assemblyName = String.Format("{0}.dll", TestData.AssemblyName);
}
Expand Down
57 changes: 44 additions & 13 deletions src/ExcelMapper.Tests/Repository/ExcelRepositoryTests.cs
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;

using ExcelMapper.Configuration;
using ExcelMapper.Repository;
using ExcelMapper.Repository.Connection;
using ExcelMapper.Tests.DTO;

using NUnit.Framework;

using Rhino.Mocks;

using RunTimeCodeGenerator.ClassGeneration;

namespace ExcelMapper.Tests.Repository
Expand Down Expand Up @@ -63,7 +59,8 @@ public class When_given_a_WorkSheet : ExcelRepositoryTestsBase
public void Should_return_Class_Properties_object_that_includes_all_the_properties_in_the_class()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
ClassAttributes classAttributes = _excelRepository.GetClassAttributes(String.Format("{0}$", _workSheetName));
ClassAttributes classAttributes =
_excelRepository.GetDTOClassAttributes(String.Format("{0}$", _workSheetName));
Assert.IsNotNull(classAttributes);
Assert.AreEqual(_workSheetName, classAttributes.Name);
Assert.IsTrue(classAttributes.Properties.Count == 4);
Expand Down Expand Up @@ -92,20 +89,54 @@ public void Should_map_the_excel_columns_to_the_given_dto_object()
public void Should_return_dto_objects_for_each_row_in_the_Xlsx_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
List<User> expectedCourts = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
List<User> actualCourts = _excelRepository.Get<User>(_workSheetName).ToList();
Assert.IsNotNull(actualCourts);
Assert.AreEqual(expectedCourts.Count, actualCourts.Count);
List<User> expectedUsers = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
List<User> actualUsers = _excelRepository.Get<User>(_workSheetName).ToList();
Assert.IsNotNull(actualUsers);
Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
}

[Test]
public void Should_return_dto_objects_for_each_row_in_the_Xls_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
List<User> expectedSuits = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
List<User> actualSuits = _excelRepository.Get<User>(_workSheetName).ToList();
Assert.IsNotNull(actualSuits);
Assert.AreEqual(expectedSuits.Count, actualSuits.Count);
List<User> expectedUsers = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
List<User> actualUsers = _excelRepository.Get<User>(_workSheetName).ToList();
Assert.IsNotNull(actualUsers);
Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
}

[Test]
public void Should_insert_the_values_provided_in_the_list_of__dto_objects_in_to_the_Xls_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
List<User> users = new List<User>
{
new User
{
Id = 5,
FirstName = "John",
LastName = "Doe",
DateOfBirth = Convert.ToDateTime("1/1/2008")
}
};
_excelRepository.Put(users);
}

[Test]
public void Should_insert_the_values_provided_in_the_list_of__dto_objects_in_to_the_Xlsx_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
List<User> users = new List<User>
{
new User
{
Id = 5,
FirstName = "John",
LastName = "Doe",
DateOfBirth = Convert.ToDateTime("1/1/2008")
}
};
_excelRepository.Put(users);
}
}
}
Expand Down
Expand Up @@ -2,7 +2,7 @@

namespace ExcelMapper.Configuration
{
public class ExcelMapperServiceLocator
public class ExcelMapper
{
public static void SetUp()
{
Expand Down
4 changes: 2 additions & 2 deletions src/ExcelMapper/ExcelMapper.csproj
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CB8A10B6-9E2E-4228-B945-9C375577AF88}</ProjectGuid>
<OutputType>Library</OutputType>
Expand Down Expand Up @@ -73,7 +73,7 @@
<Compile Include="Configuration\ExcelMapperRegistry.cs" />
<Compile Include="Configuration\FileConfiguration.cs" />
<Compile Include="Configuration\StructureMapServiceLocator.cs" />
<Compile Include="Configuration\ExcelMapperServiceLocator.cs" />
<Compile Include="Configuration\ExcelMapper.cs" />
<Compile Include="IExcelToDTOMapper.cs" />
<Compile Include="ExcelToDTOMapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/ExcelMapper/ExcelToDTOMapper.cs
Expand Up @@ -35,7 +35,7 @@ public bool Run(string assemblyName, List<string> files)
_fileConfiguration.FileName = file;
foreach (string workSheet in _excelRepository.GetWorkSheetNames())
{
ClassAttributes classAttributes = _excelRepository.GetClassAttributes(workSheet);
ClassAttributes classAttributes = _excelRepository.GetDTOClassAttributes(workSheet);
classAttributes.Namespace = String.Format("{0}.{1}", assemblyName, Path.GetFileNameWithoutExtension(file));

if (classAttributes.Properties.Count == 0)
Expand Down
43 changes: 38 additions & 5 deletions src/ExcelMapper/Repository/ExcelRepository.cs
Expand Up @@ -4,11 +4,10 @@
using System.Data.OleDb;
using System.Linq;
using System.Reflection;

using System.Text;
using ExcelMapper.Configuration;
using ExcelMapper.Repository.Connection;
using ExcelMapper.Repository.Extensions;

using RunTimeCodeGenerator.ClassGeneration;

namespace ExcelMapper.Repository
Expand Down Expand Up @@ -39,7 +38,7 @@ public IEnumerable<string> GetWorkSheetNames()
}
}

public ClassAttributes GetClassAttributes(string workSheet)
public ClassAttributes GetDTOClassAttributes(string workSheet)
{
string className = workSheet.Replace("$", "");
ClassAttributes classAttributes = new ClassAttributes(className);
Expand All @@ -55,7 +54,8 @@ public ClassAttributes GetClassAttributes(string workSheet)
{
for (int i = 0; i < reader.FieldCount; i++)
{
classAttributes.Properties.Add(new Property(reader.GetFieldType(i).ToString(), reader.GetName(i)));
classAttributes.Properties.Add(new Property(reader.GetFieldType(i).ToString(),
reader.GetName(i)));
}
}
}
Expand All @@ -73,7 +73,7 @@ public IEnumerable<T> Get<T>(string workSheet)
command.CommandText = String.Format("SELECT * FROM [{0}$]", workSheet);
using (OleDbDataReader reader = command.ExecuteReader())
{
PropertyInfo[] properties = typeof(T).GetProperties();
PropertyInfo[] properties = typeof (T).GetProperties();

while (reader.Read())
{
Expand All @@ -90,5 +90,38 @@ public IEnumerable<T> Get<T>(string workSheet)
}
}
}

private static string ConstructQueryValues(int length)
{
StringBuilder query = new StringBuilder();
for (int i = 0; i < length - 1; i++)
{
query.Append("?, ");
}
query.Append("?");
return query.ToString();
}

public void Put<T>(IEnumerable<T> values)
{
PropertyInfo[] properties = typeof (T).GetProperties();
string query = String.Format("INSERT INTO [{0}$] VALUES ({1})", typeof (T).Name,
ConstructQueryValues(properties.Length));
using (OleDbConnection connection = _connectionBuilder.GetConnection(_fileConfiguration.FileName))
{
using (OleDbCommand command = connection.CreateCommand())
{
command.CommandText = query;
foreach (var value in values)
{
foreach (var property in properties)
{
command.Parameters.AddWithValue(property.Name, property.GetValue(value, null));
}
command.ExecuteNonQuery();
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/ExcelMapper/Repository/IRepository.cs
Expand Up @@ -7,7 +7,8 @@ namespace ExcelMapper.Repository
public interface IRepository
{
IEnumerable<string> GetWorkSheetNames();
ClassAttributes GetClassAttributes(string workSheet);
ClassAttributes GetDTOClassAttributes(string workSheet);
IEnumerable<T> Get<T>(string workSheet);
void Put<T>(IEnumerable<T> values);
}
}
2 changes: 1 addition & 1 deletion src/Samples.Tests/Tests.cs
Expand Up @@ -13,7 +13,7 @@ public class Tests
[Test]
public void Test()
{
ExcelMapperServiceLocator.SetUp();
ExcelMapper.Configuration.ExcelMapper.SetUp();
ServiceLocator.Current.GetInstance<IFileConfiguration>().FileName = "Excel\\Users.xlsx";

IRepository repository = ServiceLocator.Current.GetInstance<IRepository>();
Expand Down

0 comments on commit 99a47c3

Please sign in to comment.