Skip to content

Commit

Permalink
Added CSV File Writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Penny79 committed Feb 14, 2017
1 parent f4108f5 commit c01e8a6
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 187 deletions.
2 changes: 1 addition & 1 deletion HkwgConverter/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<setting name="OutboundErrorFolder" serializeAs="String">
<value>D:\FileStore\Outbound\NOK\</value>
</setting>
<setting name="AppDataFolder" serializeAs="String">
<setting name="WorkflowStoreFolder" serializeAs="String">
<value>D:\FileStore\AppData\</value>
</setting>
</HkwgConverter.Settings>
Expand Down
22 changes: 12 additions & 10 deletions HkwgConverter/Core/InboundConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ public class InboundConverter
#region fields

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private AppDataAccessor appDataAccessor;
private WorkflowStore workflowStore;
private Settings configData;

#endregion

#region ctor

public InboundConverter()
{
appDataAccessor = new AppDataAccessor(Settings.Default.AppDataFolder);
public InboundConverter(WorkflowStore store, Settings config)
{
this.workflowStore = store;
this.configData = config;
}

#endregion
Expand Down Expand Up @@ -199,7 +201,7 @@ private void ProcessFile(FileInfo csvFile)
content = this.Transform(content);

var deliveryDay = DateTime.Parse(content.FirstOrDefault().Time).Date;
var version = appDataAccessor.GetNextInputVersionNumer(deliveryDay);
var version = workflowStore.GetNextInputVersionNumer(deliveryDay);

// Only generate the file if there any non zero demand values
if (content.Any(x => x.FlexPos != 0.0m))
Expand All @@ -213,7 +215,7 @@ private void ProcessFile(FileInfo csvFile)
flexNegFile = this.GenerateKissFile(csvFile, content, deliveryDay, version, false);
}

var appDataItem = new AppDataInputItem()
var appDataItem = new Workflow()
{
CsvFile = csvFile.Name,
DeliveryDay = deliveryDay,
Expand All @@ -223,7 +225,7 @@ private void ProcessFile(FileInfo csvFile)
Version = version,
};

this.appDataAccessor.AppendInputLogItem(appDataItem);
this.workflowStore.Add(appDataItem);
}

#endregion
Expand All @@ -236,7 +238,7 @@ private void ProcessFile(FileInfo csvFile)
public void Run()
{
log.Info("Suche nach neuen Dateien.");
var filesToProcess = Directory.GetFiles(Settings.Default.InboundWatchFolder, "*.csv");
var filesToProcess = Directory.GetFiles(this.configData.InboundWatchFolder, "*.csv");

if(filesToProcess.Count() > 0)
{
Expand All @@ -254,15 +256,15 @@ public void Run()
try
{
this.ProcessFile(file);
newFileName = Path.Combine(Settings.Default.InboundSuccessFolder, file.Name);
newFileName = Path.Combine(this.configData.InboundSuccessFolder, file.Name);
File.Move(file.FullName, newFileName);
log.InfoFormat("Datei '{0}' wurde erfolgreich verarbeitet.", file.Name);
}
catch (Exception ex)
{
log.Error(ex);
newFileName = file.Name.Replace(".csv", "_" + DateTime.Now.Ticks + ".csv");
newFileName = Path.Combine(Settings.Default.InboundErrorFolder, newFileName);
newFileName = Path.Combine(this.configData.InboundErrorFolder, newFileName);

File.Move(file.FullName, newFileName);

Expand Down
74 changes: 65 additions & 9 deletions HkwgConverter/Core/OutboundConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HkwgConverter.Core
{
Expand All @@ -15,17 +16,22 @@ public class OutboundConverter
#region fields

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private AppDataAccessor appDataAccessor;
private WorkflowStore workflowStore;
private Settings configData;
private const string outputCsvFilePrefix = "OB_NewSchedule_";


#endregion

#region ctor

public OutboundConverter()
{
appDataAccessor = new AppDataAccessor(Settings.Default.AppDataFolder);
public OutboundConverter(WorkflowStore store, Settings config)
{
this.workflowStore = store;
this.configData = config;
}


#endregion

#region private methods
Expand Down Expand Up @@ -58,16 +64,66 @@ private List<HkwgInputItem> ReadFile(string fileName)
return lines.ToList();
}



/// <summary>
/// Performs the conversion process from HKWG CSV into the KISS-Excel Format
/// </summary>
private void ProcessFile(FileInfo csvFile)
private void ProcessFile(FileInfo excelFile)
{
var content = this.ReadFile(csvFile.FullName);
var content = this.ReadFile(excelFile.FullName);

var deliveryDay = DateTime.Parse(content.FirstOrDefault().Time).Date;

var latestWorkflow = this.workflowStore.GetLatest(deliveryDay);

if (latestWorkflow == null)
{
log.ErrorFormat("Die Datei {0} kann nicht verarbeitet werden weil es für den Liefertag keinen offenen Prozess gibt.");
return;
}

var originalData = this.ReadOriginalCsvFile(latestWorkflow.CsvFile);

this.WriteCsvFile(latestWorkflow, content, originalData);
}

private List<HkwgInputItem> ReadOriginalCsvFile(string fileName)
{
var lines = File.ReadAllLines(Path.Combine(this.configData.InboundSuccessFolder, fileName))
.Skip(2)
.Select(x => x.Split(';'))
.Select(x => new HkwgInputItem()
{
Time = x[0],
FPLast = decimal.Parse(x[1]),
FlexPos = decimal.Parse(x[2]),
FlexNeg = decimal.Parse(x[3]),
MarginalCost = decimal.Parse(x[4]),
});

return lines.ToList();
}

private void WriteCsvFile(Workflow workflow, List<HkwgInputItem> newData, List<HkwgInputItem> originalData)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Startzeit; FP - Änderung");
sb.AppendLine("[yyyy-mm-dd hh:mm:ss];[mw,kw]");

for (int i = 0; i < originalData.Count; i++)
{

var newDemandValue = originalData[i].FPLast + newData[i].FlexPos - newData[i].FlexNeg;
string time = DateTime.Parse(originalData[i].Time).ToString("yyyy-MM-dd HH:mm:ss");

sb.Append(time);
sb.Append(";");
sb.AppendLine(newDemandValue.ToString("N3"));
}

var targetFile = Path.Combine(this.configData.OutboundWatchFolder, outputCsvFilePrefix+ "_" + workflow.CsvFile);


File.WriteAllText(targetFile, sb.ToString());

}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using HkwgConverter.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
using System.IO;
using System.Linq;

Expand All @@ -9,31 +12,31 @@ namespace HkwgConverter.Core
/// <summary>
/// Encapsulates the work with some metadata about the processing itself. This data is kept in a simple csv file.
/// </summary>
internal class AppDataAccessor
public class WorkflowStore
{
#region fields

DirectoryInfo appDataDirectory;
private string inputLogFile;
private string dataFile;

#endregion

#region private methods

private void EnsureFilesExist()
{
if (!File.Exists(inputLogFile))
if (!File.Exists(dataFile))
{
File.WriteAllText(inputLogFile, AppDataInputItem.GetHeaderLine());
File.WriteAllText(dataFile, Workflow.GetHeaderLine());
}
}

private List<AppDataInputItem> ReadInputFile()
private List<Workflow> ReadInputFile()
{
var lines = File.ReadAllLines(inputLogFile)
var lines = File.ReadAllLines(dataFile)
.Skip(1)
.Select(x => x.Split(';'))
.Select(x => new Model.AppDataInputItem()
.Select(x => new Model.Workflow()
{
Timestamp = DateTime.Parse(x[0]),
DeliveryDay = DateTime.Parse(x[1]),
Expand All @@ -50,11 +53,11 @@ private List<AppDataInputItem> ReadInputFile()

#region ctor

internal AppDataAccessor(string appDataPath)
public WorkflowStore(string appDataPath)
{
appDataDirectory = new DirectoryInfo(appDataPath);

this.inputLogFile = Path.Combine(appDataDirectory.FullName, "inputlog.csv");
this.dataFile = Path.Combine(appDataDirectory.FullName, "workflowDb.dat");

this.EnsureFilesExist();
}
Expand All @@ -69,7 +72,7 @@ internal AppDataAccessor(string appDataPath)
/// </summary>
/// <param name="deliveryday"></param>
/// <returns></returns>
internal int GetNextInputVersionNumer(DateTime deliveryday)
public int GetNextInputVersionNumer(DateTime deliveryday)
{
var previousFilesForDay = this.ReadInputFile().Where(x => x.DeliveryDay == deliveryday);
var nextVersionNumber = 1;
Expand All @@ -82,11 +85,24 @@ internal int GetNextInputVersionNumer(DateTime deliveryday)
return nextVersionNumber;
}

internal void AppendInputLogItem(AppDataInputItem item)
public void Add(Workflow item)
{
File.AppendAllText(this.inputLogFile, item.GetValuesLine());
File.AppendAllText(this.dataFile, item.GetValuesLine());
}

/// <summary>
/// Returns the latest Workflow in the system
/// </summary>
/// <param name="deliveryDay"></param>
/// <returns></returns>
public Workflow GetLatest(DateTime deliveryDay)
{
var latestWorkFlow = this.ReadInputFile().Where(x => x.DeliveryDay == deliveryDay && x.Status == WorkflowState.Open)
.LastOrDefault();

return latestWorkFlow;
}

#endregion
}
}
11 changes: 8 additions & 3 deletions HkwgConverter/HkwgConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\AppDataAccessor.cs" />
<Compile Include="Core\WorkflowStore.cs" />
<Compile Include="Core\OutboundConverter.cs" />
<Compile Include="Model\ConstantsSale.cs" />
<Compile Include="Core\InboundConverter.cs" />
<Compile Include="Model\AppDataInputItem.cs" />
<Compile Include="Model\Workflow.cs" />
<Compile Include="Model\HkwgInputItem.cs" />
<Compile Include="Model\WorkflowState.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resource.Designer.cs">
Expand All @@ -98,7 +99,7 @@
<None Include="Sample\OB_NewSchedule_20160705_20160704050122_+02.csv" />
<None Include="Sample\Template_Kiss_Input.xlsx" />
<None Include="Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<Generator>PublicSettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
Expand All @@ -121,6 +122,10 @@
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace HkwgConverter.Model
/// <summary>
/// Abstraction for the items in the app data input log
/// </summary>
public class AppDataInputItem
public class Workflow
{
public DateTime Timestamp { get; set; }

Expand All @@ -18,15 +18,17 @@ public class AppDataInputItem

public string FlexNegFile { get; set; }

public int Version { get; set; }
public int Version { get; set; }

public WorkflowState Status { get; set; }

/// <summary>
/// Generates a header lines for the CSV files via reflection
/// </summary>
/// <returns></returns>
public static string GetHeaderLine()
{
return string.Join(";", typeof(AppDataInputItem).GetProperties().Select(x => x.Name).ToArray()) + Environment.NewLine;
return string.Join(";", typeof(Workflow).GetProperties().Select(x => x.Name).ToArray()) + Environment.NewLine;
}

/// <summary>
Expand All @@ -35,7 +37,7 @@ public static string GetHeaderLine()
/// <returns></returns>
public string GetValuesLine()
{
return string.Join(";", typeof(AppDataInputItem).GetProperties().Select(x => x.GetValue(this)).ToArray()) + Environment.NewLine;
return string.Join(";", typeof(Workflow).GetProperties().Select(x => x.GetValue(this)).ToArray()) + Environment.NewLine;
}
}
}
8 changes: 8 additions & 0 deletions HkwgConverter/Model/WorkflowState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace HkwgConverter.Model
{
public enum WorkflowState
{
Open,
Closed
}
}
8 changes: 5 additions & 3 deletions HkwgConverter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ static void Main(string[] args)

if (isValid)
{
//var inboundConversion = new InboundConverter();
//inboundConversion.Run();
var appDataAccessor = new WorkflowStore(Settings.Default.WorkflowStoreFolder);

var outboundConversion = new OutboundConverter();
var inboundConversion = new InboundConverter(appDataAccessor, Settings.Default);
inboundConversion.Run();

var outboundConversion = new OutboundConverter(appDataAccessor, Settings.Default);
outboundConversion.Run();
}

Expand Down
Loading

0 comments on commit c01e8a6

Please sign in to comment.