From 21fb8438271206e02613fd19f34c3e1c414eeb3b Mon Sep 17 00:00:00 2001 From: Aidan Weatherill Date: Mon, 19 Jun 2017 10:29:37 +0100 Subject: [PATCH] Moving new database development into the mongoose-2000 project. Moving new database development into the mongoose-2000 project. Previous development happened on this branch https://github.com/fo-am/MongooseDatabaseDotNet --- database/.gitignore | 287 +++++++++ .../pgDataImporter.Core/PostgresCore.cs | 334 +++++++++++ .../Properties/AssemblyInfo.cs | 36 ++ .../pgDataImporter.Core/packages.config | 4 + .../pgDataImporter.Core.csproj | 63 ++ .../psDataImporter.Console/App.config | 14 + .../psDataImporter.Console/NLog.config | 14 + .../psDataImporter.Console/Program.cs | 39 ++ .../Properties/AssemblyInfo.cs | 36 ++ .../psDataImporter.Console/packages.config | 7 + .../psDataImporter.Console.csproj | 86 +++ .../Access/NewLifeHistory.cs | 26 + .../Access/RadioCollar.cs | 17 + .../Access/Ultrasound.cs | 39 ++ .../Access/weights.cs | 21 + .../Postgres/Individual.cs | 22 + .../Postgres/IndividualEventCode.cs | 8 + .../psDataImporter.Contracts/Postgres/Pack.cs | 21 + .../Postgres/PackEventCode.cs | 9 + .../Postgres/PackHistory.cs | 12 + .../Postgres/Weight.cs | 17 + .../Properties/AssemblyInfo.cs | 36 ++ .../dtos/LifeHistoryDto.cs | 11 + .../dtos/PackHistoryDto.cs | 18 + .../psDataImporter.Contracts.csproj | 59 ++ .../psDataImporter.Data/AccessRepository.cs | 149 +++++ .../psDataImporter.Data/PostgresRepository.cs | 399 +++++++++++++ .../Properties/AssemblyInfo.cs | 36 ++ .../psDataImporter.Data/packages.config | 7 + .../psDataImporter.Data.csproj | 70 +++ database/code/Application/psDataImporter.sln | 40 ++ database/data/mongooseModel.dbm | 554 ++++++++++++++++++ database/data/new_database.sql | 345 +++++++++++ 33 files changed, 2836 insertions(+) create mode 100644 database/.gitignore create mode 100644 database/code/Application/pgDataImporter.Core/PostgresCore.cs create mode 100644 database/code/Application/pgDataImporter.Core/Properties/AssemblyInfo.cs create mode 100644 database/code/Application/pgDataImporter.Core/packages.config create mode 100644 database/code/Application/pgDataImporter.Core/pgDataImporter.Core.csproj create mode 100644 database/code/Application/psDataImporter.Console/App.config create mode 100644 database/code/Application/psDataImporter.Console/NLog.config create mode 100644 database/code/Application/psDataImporter.Console/Program.cs create mode 100644 database/code/Application/psDataImporter.Console/Properties/AssemblyInfo.cs create mode 100644 database/code/Application/psDataImporter.Console/packages.config create mode 100644 database/code/Application/psDataImporter.Console/psDataImporter.Console.csproj create mode 100644 database/code/Application/psDataImporter.Contracts/Access/NewLifeHistory.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Access/RadioCollar.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Access/Ultrasound.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Access/weights.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Postgres/Individual.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Postgres/IndividualEventCode.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Postgres/Pack.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Postgres/PackEventCode.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Postgres/PackHistory.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Postgres/Weight.cs create mode 100644 database/code/Application/psDataImporter.Contracts/Properties/AssemblyInfo.cs create mode 100644 database/code/Application/psDataImporter.Contracts/dtos/LifeHistoryDto.cs create mode 100644 database/code/Application/psDataImporter.Contracts/dtos/PackHistoryDto.cs create mode 100644 database/code/Application/psDataImporter.Contracts/psDataImporter.Contracts.csproj create mode 100644 database/code/Application/psDataImporter.Data/AccessRepository.cs create mode 100644 database/code/Application/psDataImporter.Data/PostgresRepository.cs create mode 100644 database/code/Application/psDataImporter.Data/Properties/AssemblyInfo.cs create mode 100644 database/code/Application/psDataImporter.Data/packages.config create mode 100644 database/code/Application/psDataImporter.Data/psDataImporter.Data.csproj create mode 100644 database/code/Application/psDataImporter.sln create mode 100644 database/data/mongooseModel.dbm create mode 100644 database/data/new_database.sql diff --git a/database/.gitignore b/database/.gitignore new file mode 100644 index 0000000..a752eac --- /dev/null +++ b/database/.gitignore @@ -0,0 +1,287 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs \ No newline at end of file diff --git a/database/code/Application/pgDataImporter.Core/PostgresCore.cs b/database/code/Application/pgDataImporter.Core/PostgresCore.cs new file mode 100644 index 0000000..17e9a13 --- /dev/null +++ b/database/code/Application/pgDataImporter.Core/PostgresCore.cs @@ -0,0 +1,334 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using NLog; +using psDataImporter.Contracts.Access; +using psDataImporter.Contracts.dtos; +using psDataImporter.Contracts.Postgres; +using psDataImporter.Data; + +namespace pgDataImporter.Core +{ + public class PostgresCore + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + public void ProcessWeights(IEnumerable weights) + { + Logger.Info("Starting to add weights."); + weights = weights as IList ?? weights.ToList(); + var pg = new PostgresRepository(); + + pg.AddPacks(weights.Select(s => s.Group).Distinct()); + pg.AddIndividuals(weights.GroupBy(s => new {Name = s.Indiv, s.Sex}) + .Select(i => new Individual {Name = i.Key.Name, Sex = i.Key.Sex})); + + var pgPacks = pg.GetAllPacks(); + var pgIndividuals = pg.GetAllIndividuals(); + AddPackHistories( + weights.Select(weight => new PackHistoryDto(weight.Indiv, weight.Group, weight.TimeMeasured)), pgPacks, + pgIndividuals, pg); + + pg.AddWeights(weights, pgIndividuals); + Logger.Info("Done adding weights."); + } + + public void ProccessUltrasoundData(IEnumerable ultrasoundData) + { + Logger.Info("Starting to add ultrasound data."); + ultrasoundData = ultrasoundData as IList ?? ultrasoundData.ToList(); + var pg = new PostgresRepository(); + pg.AddPacks(ultrasoundData.Select(s => s.PACK).Distinct()); + pg.AddIndividuals(ultrasoundData.Select(s => new Individual {Name = s.INDIV}).Distinct()); + + var pgPacks = pg.GetAllPacks(); + var pgIndividuals = pg.GetAllIndividuals(); + AddPackHistories( + ultrasoundData.Select( + ultrasound => new PackHistoryDto(ultrasound.INDIV, ultrasound.PACK, ultrasound.DATE)), pgPacks, + pgIndividuals, pg); + + AddUltrasoundData(ultrasoundData, pgIndividuals, pg); + Logger.Info("Done adding ultrasound data."); + } + + public void ProccessRadioCollarData(IEnumerable radioCollarData) + { + Logger.Info("Starting to add radio collar data."); + radioCollarData = radioCollarData as IList ?? radioCollarData.ToList(); + var pg = new PostgresRepository(); + pg.AddPacks(radioCollarData.Select(s => s.PACK).Distinct()); + pg.AddIndividuals(radioCollarData.Select(s => new Individual {Name = s.INDIVIDUAL}).Distinct()); + + var pgPacks = pg.GetAllPacks(); + var pgIndividuals = pg.GetAllIndividuals(); + + AddPackHistories( + radioCollarData.Select(collar => new PackHistoryDto(collar.INDIVIDUAL, collar.PACK, + GetMinimumDateFromRadioCollar(collar))), pgPacks, + pgIndividuals, pg); + + AddRadioCollarData(radioCollarData, pgIndividuals, pg); + + Logger.Info("Done adding radio collar data."); + } + + public void ProcessLifeHistories(IEnumerable lifeHistories) + { + Logger.Info("Starting to add life history data."); + lifeHistories = lifeHistories as IList ?? lifeHistories.ToList(); + var pg = new PostgresRepository(); + pg.AddPacks(lifeHistories.Select(s => s.Pack).Distinct()); + pg.AddIndividuals(lifeHistories.GroupBy(lh => new {lh.Indiv}) + .Select(s => new Individual {Name = s.Key.Indiv})); + + AddLitterInfo(lifeHistories.GroupBy(l => new {l.Pack, l.Indiv, l.Litter}).Select( + l => new LifeHistoryDto {Pack = l.Key.Pack, Individual = l.Key.Indiv, Litter = l.Key.Litter}) + .ToList(), + pg); + + AddLitterEvents(lifeHistories.Where(l => string.IsNullOrEmpty(l.Pack) && string.IsNullOrEmpty(l.Indiv) && + !string.IsNullOrEmpty(l.Code))); + AddPackEvents( // note this contains IGI between packs... need to pull them out seperatly or something. + lifeHistories.Where(l => !string.IsNullOrEmpty(l.Pack) && string.IsNullOrEmpty(l.Indiv) && + !string.IsNullOrEmpty(l.Code)), pg); + AddIndividualEvents(lifeHistories.Where(l => !string.IsNullOrEmpty(l.Indiv) && + !string.IsNullOrEmpty(l.Code) && + !string.IsNullOrEmpty(l.Pack)),pg); + + + Logger.Info("Done adding life history data."); + } + + private void AddIndividualEvents(IEnumerable individualEvents, PostgresRepository pg) + { + // add individual event codes + pg.AddIndividualEventCodes(individualEvents.Select(e => e.Code).Distinct()); + + //get all the codes + var pgIndividualCodes = pg.GetIndividualCodes(); + // get individuals + var pgIndividuals = pg.GetAllIndividuals(); + + // add events with individual ids added + foreach(var individualEvent in individualEvents) + { + pg.LinkIndividualEvents(pgIndividuals.Single(i => i.Name == individualEvent.Indiv).IndividualId, + pgIndividualCodes.Single(ic => ic.Code == individualEvent.Code).IndividualEventCodeId, + individualEvent.Latitude, individualEvent.Longitude, individualEvent.Status, + individualEvent.Date, individualEvent.Exact, individualEvent.Comment); + } + } + + private void AddPackEvents(IEnumerable packEvents, PostgresRepository pg) + { + pg.AddPackEventCodes(packEvents.Select(e => e.Code).Distinct()); + // get pack codes and ids + var pgPackCodes = pg.GetPackEventCodes(); + // get packs and ids + var pgPacks = pg.GetAllPacks(); + // link packs to codes. + foreach (var packEvent in packEvents) + { + pg.linkPackEvents(pgPacks.Single(p => p.Name == packEvent.Pack).PackId, + pgPackCodes.Single(p => p.Code == packEvent.Code).PackEventCodeId, packEvent.Status, packEvent.Date, + packEvent.Exact, + packEvent.Comment, packEvent.Latitude, packEvent.Longitude); + } + } + + private void AddLitterEvents(IEnumerable litterEvents) + { + // do this some other time, there are 3 in the database and look like bad data. + } + + private void AddLitterInfo(IEnumerable litters, PostgresRepository pg) + { + var pgPacks = pg.GetAllPacks(); + var pgIndividuals = pg.GetAllIndividuals(); + + foreach (var litter in litters) + { + if (string.IsNullOrEmpty(litter.Pack) || string.IsNullOrEmpty(litter.Individual) || + string.IsNullOrEmpty(litter.Litter)) + { + Logger.Warn( + $"Something was null for this litter. pack:{litter.Pack} Individual:{litter.Individual} Litter {litter.Litter}"); + continue; + } + litter.pgIndividualId = pgIndividuals.Single(i => i.Name == litter.Individual).IndividualId; + litter.pgPackId = pgPacks.Single(p => p.Name == litter.Pack).PackId; + + pg.AddLitter(litter); + } + } + + private void AddRadioCollarData(IEnumerable radioCollarData, List pgIndividuals, + PostgresRepository pg) + { + pg.RemoveRadioCollarData(); + + foreach (var radioCollar in radioCollarData) + { + if (string.IsNullOrEmpty(radioCollar.INDIVIDUAL)) + { + Logger.Warn("individual name null"); + continue; + } + var individualId = pgIndividuals.Single(i => i.Name == radioCollar.INDIVIDUAL).IndividualId; + + pg.AddRadioCollar(individualId, radioCollar.FITTED, radioCollar.TURNED_ON, radioCollar.REMOVED, + radioCollar.FREQUENCY, + radioCollar.WEIGHT, radioCollar.DATE_ENTERED, radioCollar.COMMENT); + } + } + + private static DateTime GetMinimumDateFromRadioCollar(RadioCollar ph) + { + return new List {ph.DATE_ENTERED, ph.FITTED, ph.REMOVED, ph.TURNED_ON}.Min() + .GetValueOrDefault(); + } + + private void AddUltrasoundData(IEnumerable ultrasoundData, List pgIndividuals, + PostgresRepository pg) + { + pg.RemoveUltrasoundData(); + foreach (var ultrasound in ultrasoundData) + { + var individualId = pgIndividuals.Single(i => i.Name == ultrasound.INDIV).IndividualId; + + for (var i = 1; i <= 6; i++) + { + if (i == 1) + { + if (!(ultrasound.FOETUS_1_CROSS_VIEW_WIDTH == null && + ultrasound.FOETUS_1_CROSS_VIEW_LENGTH == null && + ultrasound.FOETUS_1_LONG_VIEW_LENGTH == null && + ultrasound.FOETUS_1_LONG_VIEW_WIDTH == null)) + { + Logger.Info($"Adding ultrasound data indiviudal:{ultrasound.INDIV} Foetus:{i}"); + pg.AddFoetus(individualId, i, ultrasound.DATE, ultrasound.FOETUS_SIZE, + ultrasound.FOETUS_1_CROSS_VIEW_WIDTH, + ultrasound.FOETUS_1_CROSS_VIEW_LENGTH, + ultrasound.FOETUS_1_LONG_VIEW_LENGTH, + ultrasound.FOETUS_1_LONG_VIEW_WIDTH, ultrasound.COMMENT, ultrasound.OBSERVER); + } + } + + if (i == 2) + { + if (!(ultrasound.FOETUS_2_CROSS_VIEW_WIDTH == null && + ultrasound.FOETUS_2_CROSS_VIEW_LENGTH == null && + ultrasound.FOETUS_2_LONG_VIEW_LENGTH == null && + ultrasound.FOETUS_2_LONG_VIEW_WIDTH == null)) + { + Logger.Info($"Adding ultrasound data indiviudal:{ultrasound.INDIV} Foetus:{i}"); + pg.AddFoetus(individualId, i, ultrasound.DATE, ultrasound.FOETUS_SIZE, + ultrasound.FOETUS_2_CROSS_VIEW_WIDTH, + ultrasound.FOETUS_2_CROSS_VIEW_LENGTH, + ultrasound.FOETUS_2_LONG_VIEW_LENGTH, + ultrasound.FOETUS_2_LONG_VIEW_WIDTH, ultrasound.COMMENT, ultrasound.OBSERVER); + } + } + + if (i == 3) + { + if (!(ultrasound.FOETUS_3_CROSS_VIEW_WIDTH == null && + ultrasound.FOETUS_3_CROSS_VIEW_LENGTH == null && + ultrasound.FOETUS_3_LONG_VIEW_LENGTH == null && + ultrasound.FOETUS_3_LONG_VIEW_WIDTH == null)) + { + Logger.Info($"Adding ultrasound data indiviudal:{ultrasound.INDIV} Foetus:{i}"); + pg.AddFoetus(individualId, i, ultrasound.DATE, ultrasound.FOETUS_SIZE, + ultrasound.FOETUS_3_CROSS_VIEW_WIDTH, + ultrasound.FOETUS_3_CROSS_VIEW_LENGTH, + ultrasound.FOETUS_3_LONG_VIEW_LENGTH, + ultrasound.FOETUS_3_LONG_VIEW_WIDTH, ultrasound.COMMENT, ultrasound.OBSERVER); + } + } + + if (i == 4) + { + if (!(ultrasound.FOETUS_4_CROSS_VIEW_WIDTH == null && + ultrasound.FOETUS_4_CROSS_VIEW_LENGTH == null && + ultrasound.FOETUS_4_LONG_VIEW_LENGTH == null && + ultrasound.FOETUS_4_LONG_VIEW_WIDTH == null)) + { + Logger.Info($"Adding ultrasound data indiviudal:{ultrasound.INDIV} Foetus:{i}"); + pg.AddFoetus(individualId, i, ultrasound.DATE, ultrasound.FOETUS_SIZE, + ultrasound.FOETUS_4_CROSS_VIEW_WIDTH, + ultrasound.FOETUS_4_CROSS_VIEW_LENGTH, + ultrasound.FOETUS_4_LONG_VIEW_LENGTH, + ultrasound.FOETUS_4_LONG_VIEW_WIDTH, ultrasound.COMMENT, ultrasound.OBSERVER); + } + } + + if (i == 5) + { + if (!(ultrasound.FOETUS_5_CROSS_VIEW_WIDTH == null && + ultrasound.FOETUS_5_CROSS_VIEW_LENGTH == null && + ultrasound.FOETUS_5_LONG_VIEW_LENGTH == null && + ultrasound.FOETUS_5_LONG_VIEW_WIDTH == null)) + { + Logger.Info($"Adding ultrasound data indiviudal:{ultrasound.INDIV} Foetus:{i}"); + pg.AddFoetus(individualId, i, ultrasound.DATE, ultrasound.FOETUS_SIZE, + ultrasound.FOETUS_5_CROSS_VIEW_WIDTH, + ultrasound.FOETUS_5_CROSS_VIEW_LENGTH, + ultrasound.FOETUS_5_LONG_VIEW_LENGTH, + ultrasound.FOETUS_5_LONG_VIEW_WIDTH, ultrasound.COMMENT, ultrasound.OBSERVER); + } + } + + if (i == 6) + { + if (!(ultrasound.FOETUS_6_CROSS_VIEW_WIDTH == null && + ultrasound.FOETUS_6_CROSS_VIEW_LENGTH == null && + ultrasound.FOETUS_6_LONG_VIEW_LENGTH == null && + ultrasound.FOETUS_6_LONG_VIEW_WIDTH == null)) + { + Logger.Info($"Adding ultrasound data indiviudal:{ultrasound.INDIV} Foetus:{i}"); + pg.AddFoetus(individualId, i, ultrasound.DATE, ultrasound.FOETUS_SIZE, + ultrasound.FOETUS_6_CROSS_VIEW_WIDTH, + ultrasound.FOETUS_6_CROSS_VIEW_LENGTH, + ultrasound.FOETUS_6_LONG_VIEW_LENGTH, + ultrasound.FOETUS_6_LONG_VIEW_WIDTH, ultrasound.COMMENT, ultrasound.OBSERVER); + } + } + } + } + } + + private void AddPackHistories(IEnumerable packHistorys, IEnumerable pgPacks, + IEnumerable pgIndividuals, PostgresRepository pg) + { + //select and see if we have an entry + + foreach (var membership in packHistorys.OrderByDescending(ph => ph.DateJoined)) + { + if (string.IsNullOrEmpty(membership.IndividualName) || string.IsNullOrEmpty(membership.PackName)) + { + Logger.Warn( + $"Null found entering pack history. pack name:{membership.PackName} individual name {membership.IndividualName}"); + continue; + } + var packId = pgPacks.Single(p => p.Name == membership.PackName).PackId; + var individualId = pgIndividuals.Single(i => i.Name == membership.IndividualName).IndividualId; + + var databasePackHistory = pg.GetPackHistory(packId, individualId); + + if (databasePackHistory != null) + { + if (databasePackHistory.DateJoined > membership.DateJoined) + { + pg.UpdatePackHistory(membership, databasePackHistory); + } + } + else + { + // if not insert new info + pg.InsertPackHistory(packId, individualId, membership); + } + } + } + } +} \ No newline at end of file diff --git a/database/code/Application/pgDataImporter.Core/Properties/AssemblyInfo.cs b/database/code/Application/pgDataImporter.Core/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..009c36e --- /dev/null +++ b/database/code/Application/pgDataImporter.Core/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("pgDataImporter.Core")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("pgDataImporter.Core")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("07bcdee6-58f4-44ef-878f-ade153889068")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/database/code/Application/pgDataImporter.Core/packages.config b/database/code/Application/pgDataImporter.Core/packages.config new file mode 100644 index 0000000..463ccb7 --- /dev/null +++ b/database/code/Application/pgDataImporter.Core/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/database/code/Application/pgDataImporter.Core/pgDataImporter.Core.csproj b/database/code/Application/pgDataImporter.Core/pgDataImporter.Core.csproj new file mode 100644 index 0000000..60b9354 --- /dev/null +++ b/database/code/Application/pgDataImporter.Core/pgDataImporter.Core.csproj @@ -0,0 +1,63 @@ + + + + + Debug + AnyCPU + {07BCDEE6-58F4-44EF-878F-ADE153889068} + Library + Properties + pgDataImporter.Core + pgDataImporter.Core + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NLog.4.4.8\lib\net45\NLog.dll + + + + + + + + + + + + + + + + + {D9A58F31-4B33-41EF-A251-428FD101A693} + psDataImporter.Contracts + + + {a8b9e290-7183-4a0d-a0be-8c84fd8154be} + psDataImporter.Data + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Console/App.config b/database/code/Application/psDataImporter.Console/App.config new file mode 100644 index 0000000..518b50d --- /dev/null +++ b/database/code/Application/psDataImporter.Console/App.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Console/NLog.config b/database/code/Application/psDataImporter.Console/NLog.config new file mode 100644 index 0000000..7849eb5 --- /dev/null +++ b/database/code/Application/psDataImporter.Console/NLog.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Console/Program.cs b/database/code/Application/psDataImporter.Console/Program.cs new file mode 100644 index 0000000..0bd1659 --- /dev/null +++ b/database/code/Application/psDataImporter.Console/Program.cs @@ -0,0 +1,39 @@ +using System.Collections.Generic; +using NLog; +using pgDataImporter.Core; +using psDataImporter.Contracts.Access; +using psDataImporter.Data; + +namespace psDataImporter.Console +{ + internal class Program + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + private static void Main(string[] args) + { + var accessdata = new AccessRepository(); + var postgresData = new PostgresCore(); + + // var weights = accessdata.GetWeights(); + // postgresData.ProcessWeights(weights); +// + // var ultrasoundData = accessdata.GetUltrasounds(); + // postgresData.ProccessUltrasoundData(ultrasoundData); + + // var radioCollarData = accessdata.GetRadioCollars(); + // postgresData.ProccessRadioCollarData(radioCollarData); + + var lifeHistories = accessdata.GetLifeHistorys(); + postgresData.ProcessLifeHistories(lifeHistories); + + Logger.Info("done"); + System.Console.ReadLine(); + } + } +} + + +// use raw sql import +// log errors not success +// create lib of useful db calls. \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Console/Properties/AssemblyInfo.cs b/database/code/Application/psDataImporter.Console/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bc5bce8 --- /dev/null +++ b/database/code/Application/psDataImporter.Console/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("psDataImporter.Console")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("psDataImporter.Console")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8ba2fc00-7008-447c-a988-2a31055c74c3")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/database/code/Application/psDataImporter.Console/packages.config b/database/code/Application/psDataImporter.Console/packages.config new file mode 100644 index 0000000..f08668b --- /dev/null +++ b/database/code/Application/psDataImporter.Console/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Console/psDataImporter.Console.csproj b/database/code/Application/psDataImporter.Console/psDataImporter.Console.csproj new file mode 100644 index 0000000..cfe8266 --- /dev/null +++ b/database/code/Application/psDataImporter.Console/psDataImporter.Console.csproj @@ -0,0 +1,86 @@ + + + + + Debug + AnyCPU + {8BA2FC00-7008-447C-A988-2A31055C74C3} + Exe + psDataImporter.Console + psDataImporter.Console + v4.6.1 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Dapper.1.50.2\lib\net451\Dapper.dll + + + ..\packages\NLog.4.4.8\lib\net45\NLog.dll + + + ..\packages\Npgsql.3.2.2\lib\net451\Npgsql.dll + + + + + + ..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll + + + + + + + + + + + + + + + Designer + + + PreserveNewest + + + + + + {07BCDEE6-58F4-44EF-878F-ADE153889068} + pgDataImporter.Core + + + {d9a58f31-4b33-41ef-a251-428fd101a693} + psDataImporter.Contracts + + + {a8b9e290-7183-4a0d-a0be-8c84fd8154be} + psDataImporter.Data + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Access/NewLifeHistory.cs b/database/code/Application/psDataImporter.Contracts/Access/NewLifeHistory.cs new file mode 100644 index 0000000..60b1134 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Access/NewLifeHistory.cs @@ -0,0 +1,26 @@ +using System; + +namespace psDataImporter.Contracts.Access +{ + public class NewLifeHistory + { + public string AgeCat; + public string Cause; + public string Code; + public string Comment; + public DateTime Date; + public DateTime date_entered; + public string Edited; + public string Exact; + public string Indiv; + public string Litter; + public string Latitude; + public string Longitude; + public DateTime Lseen; + public string Pack; + public string PrevName; + public string Sex; + public string StartEnd; + public string Status; + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Access/RadioCollar.cs b/database/code/Application/psDataImporter.Contracts/Access/RadioCollar.cs new file mode 100644 index 0000000..eac761b --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Access/RadioCollar.cs @@ -0,0 +1,17 @@ +using System; + +namespace psDataImporter.Contracts.Access +{ + public class RadioCollar + { + public string PACK { get; set; } + public string INDIVIDUAL { get; set; } + public int? FREQUENCY { get; set; } + public DateTime? TURNED_ON { get; set; } + public DateTime? FITTED { get; set; } + public DateTime? REMOVED { get; set; } + public int WEIGHT { get; set; } + public string COMMENT { get; set; } + public DateTime? DATE_ENTERED { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Access/Ultrasound.cs b/database/code/Application/psDataImporter.Contracts/Access/Ultrasound.cs new file mode 100644 index 0000000..bd8e669 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Access/Ultrasound.cs @@ -0,0 +1,39 @@ +using System; + +namespace psDataImporter.Contracts.Access +{ + public class Ultrasound + { + public DateTime DATE { get; set; } + public string INDIV { get; set; } + public string PACK { get; set; } + public string FOETUS_NUMBER { get; set; } + public string FOETUS_SIZE { get; set; } + public float? FOETUS_1_CROSS_VIEW_LENGTH { get; set; } + public float? FOETUS_1_CROSS_VIEW_WIDTH { get; set; } + public float? FOETUS_1_LONG_VIEW_LENGTH { get; set; } + public float? FOETUS_1_LONG_VIEW_WIDTH { get; set; } + public float? FOETUS_2_CROSS_VIEW_LENGTH { get; set; } + public float? FOETUS_2_CROSS_VIEW_WIDTH { get; set; } + public float? FOETUS_2_LONG_VIEW_LENGTH { get; set; } + public float? FOETUS_2_LONG_VIEW_WIDTH { get; set; } + public float? FOETUS_3_CROSS_VIEW_LENGTH { get; set; } + public float? FOETUS_3_CROSS_VIEW_WIDTH { get; set; } + public float? FOETUS_3_LONG_VIEW_LENGTH { get; set; } + public float? FOETUS_3_LONG_VIEW_WIDTH { get; set; } + public float? FOETUS_4_CROSS_VIEW_LENGTH { get; set; } + public float? FOETUS_4_CROSS_VIEW_WIDTH { get; set; } + public float? FOETUS_4_LONG_VIEW_LENGTH { get; set; } + public float? FOETUS_4_LONG_VIEW_WIDTH { get; set; } + public float? FOETUS_5_CROSS_VIEW_LENGTH { get; set; } + public float? FOETUS_5_CROSS_VIEW_WIDTH { get; set; } + public float? FOETUS_5_LONG_VIEW_LENGTH { get; set; } + public float? FOETUS_5_LONG_VIEW_WIDTH { get; set; } + public float? FOETUS_6_CROSS_VIEW_LENGTH { get; set; } + public float? FOETUS_6_CROSS_VIEW_WIDTH { get; set; } + public float? FOETUS_6_LONG_VIEW_LENGTH { get; set; } + public float? FOETUS_6_LONG_VIEW_WIDTH { get; set; } + public string OBSERVER { get; set; } + public string COMMENT { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Access/weights.cs b/database/code/Application/psDataImporter.Contracts/Access/weights.cs new file mode 100644 index 0000000..da7db3f --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Access/weights.cs @@ -0,0 +1,21 @@ +using System; + +namespace psDataImporter.Contracts.Access +{ + public class Weights + { + public string Group { get; set; } + public DateTime Date { get; set; } + public string Indiv { get; set; } + public string Sex { get; set; } + public int Weight { get; set; } + public DateTime Time { get; set; } + public int? Accuracy { get; set; } + public string Session { get; set; } + public int? Collar { get; set; } + public string Comment { get; set; } + public string Latitude { get; set; } + public string Longitude { get; set; } + public DateTime TimeMeasured { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Postgres/Individual.cs b/database/code/Application/psDataImporter.Contracts/Postgres/Individual.cs new file mode 100644 index 0000000..3c7367b --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Postgres/Individual.cs @@ -0,0 +1,22 @@ +namespace psDataImporter.Contracts.Postgres +{ + public class Individual + { + public Individual() + { + } + + public Individual(int pgIndivididualId, string name, string sex, int? litterId = null) + { + IndividualId = pgIndivididualId; + Name = name; + LitterId = litterId; + Sex = sex; + } + + public int IndividualId { get; set; } + public string Name { get; set; } + public string Sex { get; set; } + public int? LitterId { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Postgres/IndividualEventCode.cs b/database/code/Application/psDataImporter.Contracts/Postgres/IndividualEventCode.cs new file mode 100644 index 0000000..12a89aa --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Postgres/IndividualEventCode.cs @@ -0,0 +1,8 @@ +namespace psDataImporter.Contracts.Postgres +{ + public class IndividualEventCode + { + public int IndividualEventCodeId { get; set; } + public string Code { get; set; } + } +} diff --git a/database/code/Application/psDataImporter.Contracts/Postgres/Pack.cs b/database/code/Application/psDataImporter.Contracts/Postgres/Pack.cs new file mode 100644 index 0000000..16f6ad0 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Postgres/Pack.cs @@ -0,0 +1,21 @@ +using System; + +namespace psDataImporter.Contracts.Postgres +{ + public class Pack + { + public Pack() + { + } + + public Pack(int newid, string name) + { + PackId = newid; + Name = name; + } + + public int PackId { get; set; } + public string Name { get; set; } + public DateTime? CreatedDate { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Postgres/PackEventCode.cs b/database/code/Application/psDataImporter.Contracts/Postgres/PackEventCode.cs new file mode 100644 index 0000000..9f4aaaf --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Postgres/PackEventCode.cs @@ -0,0 +1,9 @@ +namespace psDataImporter.Contracts.Postgres +{ + public class PackEventCode + { + public int PackEventCodeId { get; set; } + public string Code { get; set; } + public string Detail { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Postgres/PackHistory.cs b/database/code/Application/psDataImporter.Contracts/Postgres/PackHistory.cs new file mode 100644 index 0000000..6fe6071 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Postgres/PackHistory.cs @@ -0,0 +1,12 @@ +using System; + +namespace psDataImporter.Contracts.Postgres +{ + public class PackHistory + { + public int PackHistoryId { get; set; } + public int PackId { get; set; } + public int IndividualId { get; set; } + public DateTime DateJoined { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Postgres/Weight.cs b/database/code/Application/psDataImporter.Contracts/Postgres/Weight.cs new file mode 100644 index 0000000..86c7881 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Postgres/Weight.cs @@ -0,0 +1,17 @@ +using System; + +namespace psDataImporter.Contracts.Postgres +{ + public class Weight + { + public int WeightId { get; set; } + public int IndividualId { get; set; } + public int WeightGrams { get; set; } + public DateTime Time { get; set; } + public int Accuracy { get; set; } + public string Session { get; set; } + public int CollarWeight { get; set; } + public string Location { get; set; } //Geography + public string Comment { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/Properties/AssemblyInfo.cs b/database/code/Application/psDataImporter.Contracts/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..22c2a0f --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("psDataImporter.Contracts")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("psDataImporter.Contracts")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d9a58f31-4b33-41ef-a251-428fd101a693")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/database/code/Application/psDataImporter.Contracts/dtos/LifeHistoryDto.cs b/database/code/Application/psDataImporter.Contracts/dtos/LifeHistoryDto.cs new file mode 100644 index 0000000..a560249 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/dtos/LifeHistoryDto.cs @@ -0,0 +1,11 @@ +namespace psDataImporter.Contracts.dtos +{ + public class LifeHistoryDto + { + public int pgPackId { get; set; } + public string Pack { get; set; } + public int pgIndividualId { get; set; } + public string Individual { get; set; } + public string Litter { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/dtos/PackHistoryDto.cs b/database/code/Application/psDataImporter.Contracts/dtos/PackHistoryDto.cs new file mode 100644 index 0000000..c76d0c8 --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/dtos/PackHistoryDto.cs @@ -0,0 +1,18 @@ +using System; + +namespace psDataImporter.Contracts.dtos +{ + public class PackHistoryDto + { + public PackHistoryDto(string individalName, string packName, DateTime dateJoined) + { + IndividualName = individalName; + PackName = packName; + DateJoined = dateJoined; + } + + public string IndividualName { get; set; } + public string PackName { get; set; } + public DateTime DateJoined { get; set; } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Contracts/psDataImporter.Contracts.csproj b/database/code/Application/psDataImporter.Contracts/psDataImporter.Contracts.csproj new file mode 100644 index 0000000..dc2466f --- /dev/null +++ b/database/code/Application/psDataImporter.Contracts/psDataImporter.Contracts.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + {D9A58F31-4B33-41EF-A251-428FD101A693} + Library + Properties + psDataImporter.Contracts + psDataImporter.Contracts + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Data/AccessRepository.cs b/database/code/Application/psDataImporter.Data/AccessRepository.cs new file mode 100644 index 0000000..5f33cd1 --- /dev/null +++ b/database/code/Application/psDataImporter.Data/AccessRepository.cs @@ -0,0 +1,149 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data.OleDb; +using System.Linq; +using Dapper; +using NLog; +using psDataImporter.Contracts.Access; + +namespace psDataImporter.Data +{ + public class AccessRepository + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + public IEnumerable GetWeights() + { + IEnumerable weights = new List(); + try + { + using (var conn = new OleDbConnection(ConfigurationManager + .ConnectionStrings["accessConnectionString"] + .ConnectionString)) + { + Logger.Info("Getting weight data"); + conn.Open(); + weights = conn.Query( + @"SELECT *, [DATE] + IIF(ISNULL([TIME]),0,[TIME]) as [TimeMeasured] FROM WEIGHTS"); // null times are turned to a value. + conn.Close(); + } + } + catch (Exception ex) + { + Logger.Error(ex, "Access error" + ex.Message); + } + return weights; + } + + public IEnumerable GetUltrasounds() + { + IEnumerable ultrasounds = new List(); + try + { + using (var conn = new OleDbConnection(ConfigurationManager + .ConnectionStrings["accessConnectionString"] + .ConnectionString)) + { + Logger.Info("Getting ultrasound data"); + conn.Open(); + ultrasounds = conn.Query( + @"SELECT + [DATE] , + [INDIV] , + [PACK] , + [FOETUS NUMBER] , + [FOETUS SIZE] , + [FOETUS 1 - CROSS VIEW LENGTH] as FOETUS_1_CROSS_VIEW_LENGTH , + [FOETUS 1 - CROSS VIEW WIDTH] as FOETUS_1_CROSS_VIEW_WIDTH , + [FOETUS 1 - LONG VIEW LENGTH] as FOETUS_1_LONG_VIEW_LENGTH , + [FOETUS 1 - LONG VIEW WIDTH] as FOETUS_1_LONG_VIEW_WIDTH , + [FOETUS 2 - CROSS VIEW LENGTH] as FOETUS_2_CROSS_VIEW_LENGTH , + [FOETUS 2 - CROSS VIEW WIDTH] as FOETUS_2_CROSS_VIEW_WIDTH , + [FOETUS 2 - LONG VIEW LENGTH] as FOETUS_2_LONG_VIEW_LENGTH , + [FOETUS 2 - LONG VIEW WIDTH] as FOETUS_2_LONG_VIEW_WIDTH , + [FOETUS 3 - CROSS VIEW LENGTH] as FOETUS_3_CROSS_VIEW_LENGTH , + [FOETUS 3 - CROSS VIEW WIDTH] as FOETUS_3_CROSS_VIEW_WIDTH , + [FOETUS 3 - LONG VIEW LENGTH] as FOETUS_3_LONG_VIEW_LENGTH , + [FOETUS 3 - LONG VIEW WIDTH] as FOETUS_3_LONG_VIEW_WIDTH , + [FOETUS 4 - CROSS VIEW LENGTH] as FOETUS_4_CROSS_VIEW_LENGTH , + [FOETUS 4 - CROSS VIEW WIDTH] as FOETUS_4_CROSS_VIEW_WIDTH , + [FOETUS 4 - LONG VIEW LENGTH] as FOETUS_4_LONG_VIEW_LENGTH , + [FOETUS 4 - LONG VIEW WIDTH] as FOETUS_4_LONG_VIEW_WIDTH , + [FOETUS 5 - CROSS VIEW LENGTH] as FOETUS_5_CROSS_VIEW_LENGTH , + [FOETUS 5 - CROSS VIEW WIDTH] as FOETUS_5_CROSS_VIEW_WIDTH , + [FOETUS 5 - LONG VIEW LENGTH] as FOETUS_5_LONG_VIEW_LENGTH , + [FOETUS 5 - LONG VIEW WIDTH] as FOETUS_5_LONG_VIEW_WIDTH , + [FOETUS 6 - CROSS VIEW LENGTH] as FOETUS_6_CROSS_VIEW_LENGTH , + [FOETUS 6 - CROSS VIEW WIDTH] as FOETUS_6_CROSS_VIEW_WIDTH , + [FOETUS 6 - LONG VIEW LENGTH] as FOETUS_6_LONG_VIEW_LENGTH , + [FOETUS 6 - LONG VIEW WIDTH] as FOETUS_6_LONG_VIEW_WIDTH , + [OBSERVER] , + [COMMENT] + FROM + [ULTRASOUND DATA]"); + conn.Close(); + } + } + catch (Exception ex) + { + Logger.Error(ex, "Access error" + ex.Message); + } + return ultrasounds; + } + + public IEnumerable GetRadioCollars() + { + using (var conn = new OleDbConnection(ConfigurationManager + .ConnectionStrings["accessConnectionString"] + .ConnectionString)) + { + Logger.Info("Getting Radio Collar data"); + return conn.Query(@" + SELECT + PACK, + INDIVIDUAL, + FREQUENCY, + [TURNED ON] AS TURNED_ON, + FITTED, + REMOVED, + [WEIGHT(G)] AS WEIGHT, + DATE_ENTERED + FROM [RADIOCOLLAR RECORDS]").ToList(); + } + } + + public List GetLifeHistorys() + { + using (var conn = new OleDbConnection(ConfigurationManager + .ConnectionStrings["accessConnectionString"] + .ConnectionString)) + { + Logger.Info("Getting Life History data"); + + return conn.Query(@" + SELECT DATE, + PACK, + INDIV, + SEX, + [AGE CAT] as AgeCat, + STATUS, + [START/END] as StartEnd, + CODE, + EXACT, + LSEEN, + CAUSE, + LITTER, + [PREV NAME] as PrevName, + COMMENT, + EDITED, + Latitude, + Longitude, + date_entered + FROM [NEW LIFE HISTORY]; + ").ToList(); + } + + } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Data/PostgresRepository.cs b/database/code/Application/psDataImporter.Data/PostgresRepository.cs new file mode 100644 index 0000000..4d484fb --- /dev/null +++ b/database/code/Application/psDataImporter.Data/PostgresRepository.cs @@ -0,0 +1,399 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using Dapper; +using NLog; +using Npgsql; +using psDataImporter.Contracts.Access; +using psDataImporter.Contracts.dtos; +using psDataImporter.Contracts.Postgres; + +namespace psDataImporter.Data +{ + public class PostgresRepository + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + public List GetAllPacks() + { + var pgPacks = new List(); + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + pgPacks = conn + .Query("Select pack_id as PackId, name, pack_created_date as CreatedDate from mongoose.pack") + .ToList(); + } + return pgPacks; + } + + public List GetAllIndividuals() + { + var pgIndividuals = new List(); + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + pgIndividuals = conn + .Query( + "Select individual_id as IndividualId, litter_id as LitterId, name, sex from mongoose.individual") + .ToList(); + } + return pgIndividuals; + } + + public void AddWeights(IEnumerable weights, List pgIndividuals) + { + foreach (var weight in weights) + { + using (var conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + + { + // create geography if lat and long are present. + var locationString = "NULL"; + if (!string.IsNullOrEmpty(weight.Latitude) && !string.IsNullOrEmpty(weight.Longitude)) + { + locationString = + $"ST_GeographyFromText('SRID=4326;POINT({weight.Latitude} {weight.Longitude})')"; + } + var sql = + "Insert into mongoose.weight (individual_id, weight, time, accuracy, session, collar_weight, comment, location)" + + $" values (@IndividualId, @Weight, @Time, @Accuracy, @Session, @CollarWeight, @Comment, {locationString})"; + + using (var cmd = new NpgsqlCommand()) + { + cmd.Connection = conn; + conn.Open(); + cmd.AllResultTypesAreUnknown = true; + var individualId = pgIndividuals.Single(i => i.Name == weight.Indiv).IndividualId; + + cmd.CommandText = sql; + + cmd.Parameters.AddWithValue("IndividualId", individualId); + cmd.Parameters.AddWithValue("Weight", weight.Weight); + cmd.Parameters.AddWithValue("Time", weight.TimeMeasured); + cmd.Parameters.AddWithValue("Accuracy", (object)weight.Accuracy ?? DBNull.Value); + cmd.Parameters.AddWithValue("Session", (object)weight.Session ?? DBNull.Value); + cmd.Parameters.AddWithValue("CollarWeight", (object)weight.Collar ?? DBNull.Value); + cmd.Parameters.AddWithValue("Comment", (object)weight.Comment ?? DBNull.Value); + + cmd.ExecuteNonQuery(); + } + Logger.Info($"Insert weight for: {weight.Indiv} weight {weight.Weight}"); + } + } + } + + public void InsertPackHistory(int packId, int individualId, PackHistoryDto membership) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + + { + conn.Execute( + "Insert into mongoose.pack_history (pack_id, individual_id, date_joined) values (@PackId, @IndividualId, @DateJoined)", + new { PackId = packId, IndividualId = individualId, membership.DateJoined }); + + Logger.Info($"Insert pack history: {membership.DateJoined}"); + } + } + + public void UpdatePackHistory(PackHistoryDto membership, PackHistory packHistory) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + + { + //if we do then check the date, if our date is older then update with the new older date + conn.Execute( + "update mongoose.pack_history set date_joined = @date where pack_history_id = @packHistoryId", + new { date = membership.DateJoined, packHistoryId = packHistory.PackHistoryId }); + + Logger.Info($"update pack history: {packHistory.PackHistoryId}"); + } + } + + + public void AddIndividualEventCodes(IEnumerable codes) + { + foreach (var code in codes) + { + if (!string.IsNullOrEmpty(code)) + { + Logger.Info($"Adding individual code: {code}"); + + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.Execute( + "Insert into mongoose.individual_event_code (code) values (@codeValue) ON CONFLICT DO NOTHING", + new { codeValue = code }); + } + } + } + } + + public PackHistory GetPackHistory(int packId, int individualId) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + + { + return conn.Query( + "SELECT pack_history_id as PackHistoryId, pack_id as PackId, individual_id as IndividualId, date_joined as DateJoined from mongoose.pack_history where pack_id = @pack and individual_id = @individual", + new { pack = packId, individual = individualId }).FirstOrDefault(); + } + } + + + public void AddIndividuals(IEnumerable individuals) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + foreach (var newIndividual in individuals) + { + if (string.IsNullOrEmpty(newIndividual.Name)) + { + Logger.Warn("Individual with null name."); + continue; + } + // see if we have the individual + // if so do we have all the data we have at this point? + // if not then update it with what we have... + // what if we have the same data but it conflicts (Sex eg) + // + var inDatabaseIndividual = conn + .Query( + "Select individual_id as IndividualId, litter_id as LitterId, name, sex from mongoose.individual where name = @name", + new { newIndividual.Name }).SingleOrDefault(); + + if (inDatabaseIndividual != null) + { + //If database has no sex, but our new individual does + if (string.IsNullOrEmpty(inDatabaseIndividual.Sex) && !string.IsNullOrEmpty(newIndividual.Sex)) + { + Logger.Info( + $"Added sex: '{newIndividual.Sex}' to individiual with Id : '{newIndividual.IndividualId}'"); + + conn.Execute("Update mongoose.Individual set sex = @sex where individual_id = @id", + new { sex = newIndividual.Sex, id = newIndividual.IndividualId }); + } + // if litter id is set then do the litter thing... worry about that when I have some data! + } + + conn.ExecuteScalar( + "Insert into mongoose.individual (name, sex) values (@name, @sex) ON CONFLICT DO NOTHING", + new { newIndividual.Name, newIndividual.Sex }); + Logger.Info($"created indiv: {newIndividual.Name}"); + } + } + } + + public void AddPacks(IEnumerable packNames) + { + foreach (var packName in packNames) + { + if (string.IsNullOrEmpty(packName)) + { + Logger.Warn("Tried to create a null pack."); + continue; + } + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.ExecuteScalar("Insert into mongoose.pack (name) values (@name) ON CONFLICT DO NOTHING ", + new { name = packName }); + } + Logger.Info($"created pack: {packName}"); + } + } + + public void AddFoetus(int individualId, int foetusNumber, DateTime ultrasoundDate, string foetusSize, + float? crossViewWidth, float? crossViewLength, float? longViewLength, float? longViewWidth, + string comment, string observer) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.Execute( + "Insert into mongoose.ultrasound (individual_id, observation_date, foetus_number, foetus_size, cross_view_length, cross_view_width, long_view_length, long_view_width, observer, comment)" + + " values(@individualId, @observationDate, @foetusNumber, @foetusSize, @crossViewLength, @crossViewWidth, @longViewLength, @longViewWidth, @Observer, @comment)", + new + { + individualId, + observationDate = ultrasoundDate, + foetusNumber, + foetusSize, + crossViewWidth, + crossViewLength, + longViewLength, + longViewWidth, + comment, + observer + }); + } + } + + public void RemoveUltrasoundData() + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.Execute("truncate mongoose.ultrasound"); + Logger.Info("Truncated ultrasound table"); + } + } + + public void RemoveRadioCollarData() + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.Execute("truncate mongoose.radiocollar"); + Logger.Info("Truncated radiocollar table"); + } + } + + public void AddRadioCollar(int individualId, DateTime? fitted, DateTime? turnedOn, DateTime? removed, + int? frequency, int weight, DateTime? dateEntered, string comment) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.Execute( + "insert into mongoose.radiocollar (individual_id, frequency, weight, fitted, turned_on, removed, comment, date_entered) " + + "values (@individualId, @frequency, @weight, @fitted, @turnedOn, @removed, @comment, @dateEntered)", + new { individualId, frequency, weight, fitted, turnedOn, removed, comment, dateEntered }); + Logger.Info("Added radio collar data."); + } + } + + + public void AddLitter(LifeHistoryDto litter) + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + //Insert new litter and get back Id + var litterId = conn.ExecuteScalar( + "insert into mongoose.litter (pack_id, name)" + + " values(@packId, @name)" + + " on conflict(name) do update set name = @name RETURNING litter_id", + new { packid = litter.pgPackId, name = litter.Litter }); + + //update individual with litter id + conn.Execute( + "update mongoose.individual set litter_id = @litterId where individual_id = @individual_id", + new { litterId, individual_id = litter.pgIndividualId }); + + Logger.Info($"Added litter {litter.Litter}."); + } + } + + public void AddPackEventCodes(IEnumerable codes) + { + foreach (var code in codes) + { + if (!string.IsNullOrEmpty(code)) + { + Logger.Info($"Adding pack code:{code}"); + + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + conn.Execute( + "Insert into mongoose.pack_event_code (code) values (@codeValue) ON CONFLICT DO NOTHING", + new { codeValue = code }); + } + } + } + } + public List GetIndividualCodes() + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + return conn.Query( + "Select individual_event_code_id as IndividualEventCodeId, code from mongoose.individual_event_code") + .ToList(); + } + } + public List GetPackEventCodes() + { + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + return conn.Query( + "Select pack_event_code_id as PackEventCodeId, code, detail from mongoose.pack_event_code") + .ToList(); + } + } + public void LinkIndividualEvents(int individualId, int individualEventCodeId, string latitude, string longitude, string status, DateTime date, string exact, string comment) + { + // create geography if lat and long are present. + var locationString = "NULL"; + if (!string.IsNullOrEmpty(latitude) && !string.IsNullOrEmpty(longitude)) + { + locationString = + $"ST_GeographyFromText('SRID=4326;POINT({latitude} {longitude})')"; + } + + var sql = + "Insert into mongoose.individual_event (individual_id, individual_event_code_id, status, date, exact, comment, location )" + + $" values (@individualId, @individualEventCodeId, @status, @date, @exact, @Comment, {locationString})"; + + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + Logger.Info($"Linking individualId: {individualId} with codeId: {individualEventCodeId}"); + conn.Execute(sql, new { individualId, individualEventCodeId, status, date, exact, comment }); + } + } + + + public void linkPackEvents(int packId, int packEventCodeId, string status, DateTime date, + string exact, string comment, string latitude, string longitude) + { + // create geography if lat and long are present. + var locationString = "NULL"; + if (!string.IsNullOrEmpty(latitude) && !string.IsNullOrEmpty(longitude)) + { + locationString = + $"ST_GeographyFromText('SRID=4326;POINT({latitude} {longitude})')"; + } + + var sql = + "Insert into mongoose.pack_event (pack_id, pack_event_code_id, status, date, exact, comment, location )" + + $" values (@PackId, @packEventCodeId, @status, @date, @exact, @Comment, {locationString})"; + + using (IDbConnection conn = new NpgsqlConnection(ConfigurationManager + .ConnectionStrings["postgresConnectionString"] + .ConnectionString)) + { + Logger.Info($"Linking packId:{packId} with codeId:{packEventCodeId}"); + conn.Execute(sql, new {packId, packEventCodeId, status, date, exact, comment}); + } + } + } +} \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Data/Properties/AssemblyInfo.cs b/database/code/Application/psDataImporter.Data/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..94bc92e --- /dev/null +++ b/database/code/Application/psDataImporter.Data/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("psDataImporter.Data")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("psDataImporter.Data")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a8b9e290-7183-4a0d-a0be-8c84fd8154be")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/database/code/Application/psDataImporter.Data/packages.config b/database/code/Application/psDataImporter.Data/packages.config new file mode 100644 index 0000000..fc856fa --- /dev/null +++ b/database/code/Application/psDataImporter.Data/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.Data/psDataImporter.Data.csproj b/database/code/Application/psDataImporter.Data/psDataImporter.Data.csproj new file mode 100644 index 0000000..7eea931 --- /dev/null +++ b/database/code/Application/psDataImporter.Data/psDataImporter.Data.csproj @@ -0,0 +1,70 @@ + + + + + Debug + AnyCPU + {A8B9E290-7183-4A0D-A0BE-8C84FD8154BE} + Library + Properties + psDataImporter.Data + psDataImporter.Data + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Dapper.1.50.2\lib\net451\Dapper.dll + + + ..\packages\NLog.4.4.8\lib\net45\NLog.dll + + + ..\packages\Npgsql.3.2.2\lib\net451\Npgsql.dll + + + + + + ..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll + + + + + + + + + + + + + + + + {D9A58F31-4B33-41EF-A251-428FD101A693} + psDataImporter.Contracts + + + + + + + \ No newline at end of file diff --git a/database/code/Application/psDataImporter.sln b/database/code/Application/psDataImporter.sln new file mode 100644 index 0000000..922c290 --- /dev/null +++ b/database/code/Application/psDataImporter.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26403.7 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "psDataImporter.Console", "psDataImporter.Console\psDataImporter.Console.csproj", "{8BA2FC00-7008-447C-A988-2A31055C74C3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "psDataImporter.Contracts", "psDataImporter.Contracts\psDataImporter.Contracts.csproj", "{D9A58F31-4B33-41EF-A251-428FD101A693}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "psDataImporter.Data", "psDataImporter.Data\psDataImporter.Data.csproj", "{A8B9E290-7183-4A0D-A0BE-8C84FD8154BE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pgDataImporter.Core", "pgDataImporter.Core\pgDataImporter.Core.csproj", "{07BCDEE6-58F4-44EF-878F-ADE153889068}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8BA2FC00-7008-447C-A988-2A31055C74C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8BA2FC00-7008-447C-A988-2A31055C74C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8BA2FC00-7008-447C-A988-2A31055C74C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8BA2FC00-7008-447C-A988-2A31055C74C3}.Release|Any CPU.Build.0 = Release|Any CPU + {D9A58F31-4B33-41EF-A251-428FD101A693}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9A58F31-4B33-41EF-A251-428FD101A693}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9A58F31-4B33-41EF-A251-428FD101A693}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9A58F31-4B33-41EF-A251-428FD101A693}.Release|Any CPU.Build.0 = Release|Any CPU + {A8B9E290-7183-4A0D-A0BE-8C84FD8154BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A8B9E290-7183-4A0D-A0BE-8C84FD8154BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A8B9E290-7183-4A0D-A0BE-8C84FD8154BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A8B9E290-7183-4A0D-A0BE-8C84FD8154BE}.Release|Any CPU.Build.0 = Release|Any CPU + {07BCDEE6-58F4-44EF-878F-ADE153889068}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07BCDEE6-58F4-44EF-878F-ADE153889068}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07BCDEE6-58F4-44EF-878F-ADE153889068}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07BCDEE6-58F4-44EF-878F-ADE153889068}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/database/data/mongooseModel.dbm b/database/data/mongooseModel.dbm new file mode 100644 index 0000000..cd7156c --- /dev/null +++ b/database/data/mongooseModel.dbm @@ -0,0 +1,554 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + +
diff --git a/database/data/new_database.sql b/database/data/new_database.sql new file mode 100644 index 0000000..d972314 --- /dev/null +++ b/database/data/new_database.sql @@ -0,0 +1,345 @@ +-- Database generated with pgModeler (PostgreSQL Database Modeler). +-- pgModeler version: 0.9.0-beta +-- PostgreSQL version: 9.6 +-- Project Site: pgmodeler.com.br +-- Model Author: --- + + +-- Database creation must be done outside an multicommand file. +-- These commands were put in this file only for convenience. +-- -- object: "Foam_auto" | type: DATABASE -- +-- -- DROP DATABASE IF EXISTS "Foam_auto"; +-- CREATE DATABASE "Foam_auto" +-- ; +-- -- ddl-end -- +-- + +-- object: mongoose | type: SCHEMA -- +-- DROP SCHEMA IF EXISTS mongoose CASCADE; + +-- Prepended SQL commands -- + CREATE EXTENSION Postgis; +-- ddl-end -- + +CREATE SCHEMA mongoose; +-- ddl-end -- +ALTER SCHEMA mongoose OWNER TO postgres; +-- ddl-end -- + +SET search_path TO pg_catalog,public,mongoose; +-- ddl-end -- + +-- object: mongoose.pack | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.pack CASCADE; +CREATE TABLE mongoose.pack( + pack_id serial NOT NULL, + name text NOT NULL, + pack_created_date timestamp, + CONSTRAINT pack_pk PRIMARY KEY (pack_id), + CONSTRAINT name_unique UNIQUE (name) + +); +-- ddl-end -- +ALTER TABLE mongoose.pack OWNER TO postgres; +-- ddl-end -- + +-- object: mongoose.litter | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.litter CASCADE; +CREATE TABLE mongoose.litter( + litter_id serial NOT NULL, + pack_id integer, + name text NOT NULL, + dateformed date, + CONSTRAINT litter_pk PRIMARY KEY (litter_id), + CONSTRAINT "UQ_Litter_name_unique" UNIQUE (name) + +); +-- ddl-end -- +ALTER TABLE mongoose.litter OWNER TO postgres; +-- ddl-end -- + +-- object: pack_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.litter DROP CONSTRAINT IF EXISTS pack_fk CASCADE; +ALTER TABLE mongoose.litter ADD CONSTRAINT pack_fk FOREIGN KEY (pack_id) +REFERENCES mongoose.pack (pack_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.individual | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.individual CASCADE; +CREATE TABLE mongoose.individual( + individual_id serial NOT NULL, + litter_id integer, + name text NOT NULL, + sex text, + CONSTRAINT individual_pk PRIMARY KEY (individual_id), + CONSTRAINT unique_name UNIQUE (name) + +); +-- ddl-end -- +ALTER TABLE mongoose.individual OWNER TO postgres; +-- ddl-end -- + +-- object: litter_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.individual DROP CONSTRAINT IF EXISTS litter_fk CASCADE; +ALTER TABLE mongoose.individual ADD CONSTRAINT litter_fk FOREIGN KEY (litter_id) +REFERENCES mongoose.litter (litter_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.pack_history | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.pack_history CASCADE; +CREATE TABLE mongoose.pack_history( + pack_history_id serial NOT NULL, + pack_id integer, + individual_id integer, + date_joined timestamp, + CONSTRAINT packhistory_pk PRIMARY KEY (pack_history_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.pack_history OWNER TO postgres; +-- ddl-end -- + +-- object: pack_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.pack_history DROP CONSTRAINT IF EXISTS pack_fk CASCADE; +ALTER TABLE mongoose.pack_history ADD CONSTRAINT pack_fk FOREIGN KEY (pack_id) +REFERENCES mongoose.pack (pack_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: individual_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.pack_history DROP CONSTRAINT IF EXISTS individual_fk CASCADE; +ALTER TABLE mongoose.pack_history ADD CONSTRAINT individual_fk FOREIGN KEY (individual_id) +REFERENCES mongoose.individual (individual_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.interaction | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.interaction CASCADE; +CREATE TABLE mongoose.interaction( + interaction_id serial NOT NULL, + focalpack_id integer, + secondpack_id integer, + "time" timestamp NOT NULL, + location geography, + comment text, + CONSTRAINT interaction_pk PRIMARY KEY (interaction_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.interaction OWNER TO postgres; +-- ddl-end -- + +-- object: pack_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.interaction DROP CONSTRAINT IF EXISTS pack_fk CASCADE; +ALTER TABLE mongoose.interaction ADD CONSTRAINT pack_fk FOREIGN KEY (focalpack_id) +REFERENCES mongoose.pack (pack_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.pack_event | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.pack_event CASCADE; +CREATE TABLE mongoose.pack_event( + pack_event_id serial NOT NULL, + pack_id integer, + pack_event_code_id integer, + date date NOT NULL, + exact text, + status text, + location geography, + comment text, + CONSTRAINT event_pk PRIMARY KEY (pack_event_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.pack_event OWNER TO postgres; +-- ddl-end -- + +-- object: pack_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.pack_event DROP CONSTRAINT IF EXISTS pack_fk CASCADE; +ALTER TABLE mongoose.pack_event ADD CONSTRAINT pack_fk FOREIGN KEY (pack_id) +REFERENCES mongoose.pack (pack_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.pack_event_code | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.pack_event_code CASCADE; +CREATE TABLE mongoose.pack_event_code( + pack_event_code_id serial NOT NULL, + code text NOT NULL, + detail text, + CONSTRAINT packevnettypes_pk PRIMARY KEY (pack_event_code_id), + CONSTRAINT uq_code_is_unique UNIQUE (code) + +); +-- ddl-end -- +ALTER TABLE mongoose.pack_event_code OWNER TO postgres; +-- ddl-end -- + +-- object: mongoose.individual_event | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.individual_event CASCADE; +CREATE TABLE mongoose.individual_event( + individual_event_id serial NOT NULL, + individual_event_code_id integer, + individual_id integer, + date date NOT NULL, + exact text, + status text, + location geography, + comment text, + CONSTRAINT individualevent_pk PRIMARY KEY (individual_event_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.individual_event OWNER TO postgres; +-- ddl-end -- + +-- object: individual_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.individual_event DROP CONSTRAINT IF EXISTS individual_fk CASCADE; +ALTER TABLE mongoose.individual_event ADD CONSTRAINT individual_fk FOREIGN KEY (individual_id) +REFERENCES mongoose.individual (individual_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.individual_event_code | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.individual_event_code CASCADE; +CREATE TABLE mongoose.individual_event_code( + individual_event_code_id serial NOT NULL, + code text NOT NULL, + CONSTRAINT individualeventcode_pk PRIMARY KEY (individual_event_code_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.individual_event_code OWNER TO postgres; +-- ddl-end -- + +-- object: individual_event_code_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.individual_event DROP CONSTRAINT IF EXISTS individual_event_code_fk CASCADE; +ALTER TABLE mongoose.individual_event ADD CONSTRAINT individual_event_code_fk FOREIGN KEY (individual_event_code_id) +REFERENCES mongoose.individual_event_code (individual_event_code_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.pregnancy | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.pregnancy CASCADE; +CREATE TABLE mongoose.pregnancy( + pregnancy_id serial NOT NULL, + individual_id integer, + litter_id integer, + CONSTRAINT pregnancy_pk PRIMARY KEY (pregnancy_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.pregnancy OWNER TO postgres; +-- ddl-end -- + +-- object: individual_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.pregnancy DROP CONSTRAINT IF EXISTS individual_fk CASCADE; +ALTER TABLE mongoose.pregnancy ADD CONSTRAINT individual_fk FOREIGN KEY (individual_id) +REFERENCES mongoose.individual (individual_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: litter_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.pregnancy DROP CONSTRAINT IF EXISTS litter_fk CASCADE; +ALTER TABLE mongoose.pregnancy ADD CONSTRAINT litter_fk FOREIGN KEY (litter_id) +REFERENCES mongoose.litter (litter_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.weight | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.weight CASCADE; +CREATE TABLE mongoose.weight( + weight_id serial NOT NULL, + individual_id integer, + weight integer NOT NULL, + "time" timestamp NOT NULL, + accuracy smallint, + session text, + collar_weight integer, + location geography(POINT, 4326), + comment text, + CONSTRAINT weight_pk PRIMARY KEY (weight_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.weight OWNER TO postgres; +-- ddl-end -- + +-- object: individual_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.weight DROP CONSTRAINT IF EXISTS individual_fk CASCADE; +ALTER TABLE mongoose.weight ADD CONSTRAINT individual_fk FOREIGN KEY (individual_id) +REFERENCES mongoose.individual (individual_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.ultrasound | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.ultrasound CASCADE; +CREATE TABLE mongoose.ultrasound( + ultrasound_id serial NOT NULL, + individual_id integer, + observation_date timestamp NOT NULL, + foetus_number smallint, + foetus_size text, + cross_view_length decimal, + cross_view_width decimal, + long_view_length decimal, + long_view_width decimal, + observer text, + comment text, + CONSTRAINT ultrasound_pk PRIMARY KEY (ultrasound_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.ultrasound OWNER TO postgres; +-- ddl-end -- + +-- object: individual_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.ultrasound DROP CONSTRAINT IF EXISTS individual_fk CASCADE; +ALTER TABLE mongoose.ultrasound ADD CONSTRAINT individual_fk FOREIGN KEY (individual_id) +REFERENCES mongoose.individual (individual_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: mongoose.radiocollar | type: TABLE -- +-- DROP TABLE IF EXISTS mongoose.radiocollar CASCADE; +CREATE TABLE mongoose.radiocollar( + radiocollar_id serial NOT NULL, + individual_id integer, + frequency smallint, + weight smallint, + fitted timestamp, + turned_on timestamp, + removed timestamp, + comment text, + date_entered timestamp, + CONSTRAINT radiocollar_pk PRIMARY KEY (radiocollar_id) + +); +-- ddl-end -- +ALTER TABLE mongoose.radiocollar OWNER TO postgres; +-- ddl-end -- + +-- object: individual_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.radiocollar DROP CONSTRAINT IF EXISTS individual_fk CASCADE; +ALTER TABLE mongoose.radiocollar ADD CONSTRAINT individual_fk FOREIGN KEY (individual_id) +REFERENCES mongoose.individual (individual_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: pack_event_code_fk | type: CONSTRAINT -- +-- ALTER TABLE mongoose.pack_event DROP CONSTRAINT IF EXISTS pack_event_code_fk CASCADE; +ALTER TABLE mongoose.pack_event ADD CONSTRAINT pack_event_code_fk FOREIGN KEY (pack_event_code_id) +REFERENCES mongoose.pack_event_code (pack_event_code_id) MATCH FULL +ON DELETE SET NULL ON UPDATE CASCADE; +-- ddl-end -- + +-- object: pack_secondary | type: CONSTRAINT -- +-- ALTER TABLE mongoose.interaction DROP CONSTRAINT IF EXISTS pack_secondary CASCADE; +ALTER TABLE mongoose.interaction ADD CONSTRAINT pack_secondary FOREIGN KEY (secondpack_id) +REFERENCES mongoose.pack (pack_id) MATCH FULL +ON DELETE NO ACTION ON UPDATE NO ACTION; +-- ddl-end -- + +