/// <summary>
        /// Maps a single aggregate to the UI datasource
        /// returned by the web service, including any additional links
        /// </summary>
        /// <param name="aggregate">the Windfarm Data</param>
        /// <param name="generator">The instance of the LinkGenerator to generate the Links</param>
        /// <returns>UI data source and links</returns>
        //public Windfarm MapAggregate(Aggregate aggregate, IAggregateLinkGenerator generator)
        public Windfarm MapAggregate(Aggregate aggregate, ILinkGenerator<Core.Model.Aggregate> generator)
        {
            if (aggregate == null)
                return  new Windfarm();

            //  Create a data segment and a link for each datatype
            var data = new List<WindfarmData>();
            foreach (var d in aggregate.Data)
            {
                data.Add(new WindfarmData()
                {
                    Type = Enum.GetName(typeof(DataTypeEnum), d.DataType),
                    Data = d.Data
                });
            }
            //  Create the UiAggregate
            var uiAggregate = new Windfarm()
            {
                Id = aggregate.Id,
                Name = aggregate.Name,
                Data = data,
                Links = generator.GenerateItemLinks(aggregate)
            };
            return uiAggregate;
        }
 /// <summary>
 /// Adds a new aggregate object to the data store
 /// </summary>
 /// <param name="aggregate">The new Aggregate object</param>
 /// <returns>The assigned Id of the new Aggregate</returns>
 public int Add(Aggregate aggregate)
 {
     _uow.Aggregates.Add(aggregate);
     var newId = _uow.SaveChanges();
     return newId;
 }
        public void Initialise()
        {
            _mockRepository = new Mock<IAggregateRepository>();
            _mockDataSourceRepository = new Mock<IDataSourceRepository>();
            _mockDatasource = new Mock<ISnhDataSource>();
            _mockAlgorithm = new Mock<IMatchingAlgorithm>();
            _mockMapper = new Mock<IMapper<ImportAggregate, Aggregate>>();

            _importService = null;

            _importAggregates = new List<ImportAggregate>()
                {
                    new ImportAggregate()
                        {
                            Identifier = "Achany Estate",
                            ImportDate = _timestamp,
                            SourceId = 1,
                            Data = new List<ImportData>()
                                {
                                    new ImportData()
                                        {
                                            Data = "{achany estate}",
                                            DataType = DataTypeEnum.FootPrint
                                        }
                                }
                        },
                    new ImportAggregate()
                        {
                            Identifier = "Achlachan",
                            ImportDate = _timestamp,
                            SourceId = 1,
                            Data = new List<ImportData>()
                                {
                                    new ImportData()
                                        {
                                            Data = "{achlachan}",
                                            DataType = DataTypeEnum.FootPrint
                                        }
                                }
                        }
                };

            _aggregateAchany = new Aggregate()
                {
                    Id = 1,
                    Name = "Achany Estate",
                    Data = new Collection<AggregateData>()
                        {
                            new AggregateData()
                                {
                                    Id = 1,
                                    Name = "Achany Estate",
                                    AggregateId = 1,
                                    DataSourceId = 1,
                                    Data = "{achany estate}",
                                    DataType = DataTypeEnum.FootPrint,
                                    LastUpdated = _timestamp
                                }
                        }
                };

            _aggregates = new List<Aggregate>()
                {
                    _aggregateAchany
                };
        }
        /// <summary>
        /// Updates an existing aggregate
        /// </summary>
        /// <param name="aggregate">The Aggregate containing the aggregate to be applied</param>
        /// <remarks>
        /// The method combines the datatypes on the input aggregate with 
        /// those on the aggregate already in existance.  If the datatype
        /// on the input matches one of those on the aggregate it updates
        /// it, otherwise it treats it as a new datatype for the aggregate
        /// and adds it.
        /// </remarks>
        public void Update(Aggregate aggregate)
        {
            //  Retrieve from Datastore
            var agg = _uow.Aggregates.First(a => a.Id == aggregate.Id);

            //  Apply aggregate to the retrieved
            //  Apply the aggregate to the references Aggregate object
            agg.LastUpdated = aggregate.LastUpdated;

            //  Combine the datatypes.
            //  Loop each input datatype and update if there is one already
            //  Otherwise, add the new datatype to the aggregate.
            foreach (var inputData in aggregate.Data)
            {
                bool updated = false;

                foreach (var aggregateData in agg.Data)
                {
                    if (inputData.DataType == aggregateData.DataType)
                    {
                        //  Existing AggregateData, update it
                        aggregateData.Data = inputData.Data;
                        aggregateData.LastUpdated = inputData.LastUpdated;
                        //  Set as updated
                        updated = true;
                    }
                }
                if (!updated)
                {
                    //  No update taken place, must be a new data type
                    agg.Data.Add(inputData);
                }
            }
            _uow.SaveChanges();
        }
        public void Initialise()
        {
            //  Initialise some test data
            //  DataSources
            _dataSource1 = new DataSource
            {
                Id = 1,
                AccessPath = string.Empty,
                CopyRight = string.Empty,
                Description = "Test Service 1 (Internal)",
                LastImported = DateTime.Now,
                SourceData = new Collection<AggregateData>(),
                SourceType = SourceTypeEnum.Dataset,
                Title = "Test Service 1"
            };
            _dataSource2 = new DataSource
            {
                Id = 2,
                AccessPath = string.Empty,
                CopyRight = string.Empty,
                Description = "Test Service 2 (External)",
                LastImported = DateTime.Now,
                SourceData = new Collection<AggregateData>(),
                SourceType = SourceTypeEnum.Scraper,
                Title = "Test Service 2"
            };

            _datasources = new List<DataSource>
            {
                _dataSource1,
                _dataSource2
            };

            //  DataTypes
            _dataTypes = new string[]
            {
                "Footprint",
                "Status"
            }.AsQueryable();

            //  Aggregates
            _achanyEstate = new Aggregate
            {
                Id = 1,
                Name = "Achany Estate",
                Data = new Collection<AggregateData>
                        {
                            new AggregateData
                                {
                                    Id = 1,
                                    Name = "Achany Estate",
                                    AggregateId = 1,
                                    DataSourceId = 1,
                                    Data = "{achany estate}",
                                    DataType = DataTypeEnum.FootPrint,
                                    LastUpdated = DateTime.Now
                                }
                        }
            };
            _whiteleeWindfarm = new Aggregate
            {
                Id = 2,
                Name = "Whitelee Windfarm",
                Data = new Collection<AggregateData>
                        {
                            new AggregateData
                                {
                                    Id = 2,
                                    Name = "Whitelee Windfarm",
                                    AggregateId = 2,
                                    DataSourceId = 1,
                                    Data = "{whitelee windfarm}",
                                    DataType = DataTypeEnum.FootPrint,
                                    LastUpdated = DateTime.Now
                                },
                            new AggregateData
                                {
                                    Id = 3,
                                    Name = "Whitelee Windfarm",
                                    AggregateId = 3,
                                    DataSourceId = 2,
                                    Data = "{URL to Whiltelee Windfarm}",
                                    DataType = DataTypeEnum.Statistics,
                                    LastUpdated = DateTime.Now
                                }
                        }
            };
            _blacklawWindfarm = new Aggregate
            {
                Id = 4,
                Name = "Blacklaw Windfarm",
                Data = new Collection<AggregateData>
                        {
                            new AggregateData
                                {
                                    Id = 4,
                                    Name = "Blacklaw Windfarm",
                                    AggregateId = 4,
                                    DataSourceId = 2,
                                    Data = "{URL to Blacklaw Windfarm}",
                                    DataType = DataTypeEnum.Statistics,
                                    LastUpdated = DateTime.Now
                                }
                        }
            };

            _aggregates = new List<Aggregate>
            {
                _achanyEstate,
                _whiteleeWindfarm,
                _blacklawWindfarm
            }.AsQueryable();

            _snhAggregates = new List<Aggregate>
            {
                _achanyEstate,
                _whiteleeWindfarm
            }.AsQueryable();

            _renUkAggregates = new List<Aggregate>
            {
                _whiteleeWindfarm,
                _blacklawWindfarm
            }.AsQueryable();

            //  Mock the dependencies
            _mockDatasourceRepository = new Mock<IDataSourceRepository>();
            _mockAggregateRepository = new Mock<IAggregateRepository>();
            _mockDataTypeRepository = new Mock<IDataTypeRepository>();
            _mockImportServiceResolver = new Mock<IImportServiceResolver>();
            _mockDataSourceResolver = new Mock<IDataSourceResolver>();
        }