/// <summary>
        /// Method to create the measure in the database. This just creates the basic Measure information and 
        /// does not add values under the Measure. This process is based off creating SQL dynamically based on
        /// a template file.
        /// </summary>
        /// <param name="model">The populated model containing the data needed to create the Measure</param>
        public void createMeasure(CreateMeasureModel model)
        {
            Geographical_NeedsEntities context = new Geographical_NeedsEntities();

            Measure newMeasure = context.Measures.Create();
            newMeasure.MeasureName = model.MeasureName;

            createDimSets(model.selectedDimensions);

            IEnumerable<DimensionSet> dimSets = context.Dimensions.Where(x => model.selectedDimensions.Contains(x.DimensionID)).SelectMany(x => x.DimensionSetMembers).Select(x => x.DimensionSet).ToList();
            IEnumerable<DimensionSet> dimSetsForExclusion = context.Dimensions.Where(x => !model.selectedDimensions.Contains(x.DimensionID)).SelectMany(x => x.DimensionSetMembers).Select(x => x.DimensionSet).ToList();

            IEnumerable<DimensionSet> dimSetsFinal = dimSets.Where(x => !dimSetsForExclusion.Select(y => y.DimensionSetID).Contains(x.DimensionSetID)).Distinct();

            foreach (DimensionSet set in dimSetsFinal)
            {
                MeasureBreakdown newBreakdown = context.MeasureBreakdowns.Create();
                newBreakdown.MeasureBreakdownName = String.Format("{0} {1}", model.MeasureName, set.DimensionSetName);
                newBreakdown.DimensionSetID = set.DimensionSetID;
                newMeasure.MeasureBreakdowns.Add(newBreakdown);
            }

            context.Measures.Add(newMeasure);

            context.SaveChanges();

            context.Dispose();
        }
        public ActionResult CreateMeasure(CreateMeasureModel model)
        {
            DatasetDataAccess dataAccess = new DatasetDataAccess();

            if (ModelState.IsValid)
            {
                if (!dataAccess.isCreateMeasureModelValid(model))
                {
                    ModelState.AddModelError("", "An error occured when creating the Measure. Please ensure that the Measure name is unique and that Dimensions have been specified");

                    model = dataAccess.populateCreateMeasureModel(model.StagingDatasetID);

                    List<Breadcrumb> trail = new List<Breadcrumb>();

                    trail.Add(new Breadcrumb() { LinkText = "Home", Action = "Index", Controller = "Home", isCurrent = false });
                    trail.Add(new Breadcrumb() { LinkText = "Staging Index", Action = "Index", Controller = "Staging", isCurrent = false });
                    trail.Add(new Breadcrumb() { LinkText = "Create Measure", Action = "", Controller = "", isCurrent = true });

                    model.Breadcrumbs = trail;

                    return View(model);
                }
                dataAccess.createMeasure(model);

                IEnumerable<Tuple<int, int>> dimensionMappings = model.MeasureDetails.Where(x => x.DimensionValueID.HasValue).Select(x => new Tuple<int, int>(x.StagingColumnID, x.DimensionValueID.Value));

                TempData["DimensionMappings"] = dimensionMappings;

                return RedirectToAction("AddMeasureValues", new
                {
                    stagingTableName = model.StagingTableName,
                    measureName = model.MeasureName,
                    measureColumnStagingID = model.MeasureColumnID,
                    geographyColumnID = model.GeographyColumnID,
                    geographyTypeID = model.GeographyTypeID
                });
            }

            model = dataAccess.populateCreateMeasureModel(model.StagingDatasetID);

            return View(model);
        }
        ///MEASURES
        /// <summary>
        /// Method to create and populate a CreateMeasureModel ready for the View to use
        /// </summary>
        /// <param name="datasetID">The ID of the Staging Table this Measure is based off</param>
        /// <returns></returns>
        public CreateMeasureModel populateCreateMeasureModel(int datasetID)
        {
            CreateMeasureModel model = new CreateMeasureModel();
            Geographical_NeedsEntities context = new Geographical_NeedsEntities();

            model.StagingDatasetID = datasetID;

            model.StagingTableName = context.StagingDatasets.Single(x => x.StagingDatasetID.Equals(datasetID)).DatasetName;

            model.StagingColumnsForMeasure = context.StagingDatasets.Single(x => x.StagingDatasetID.Equals(datasetID)).StagingColumns.Select(x => new SelectListItem()
                                                                                                                                                                    {
                                                                                                                                                                        Text = x.ColumnName,
                                                                                                                                                                        Value = x.StagingColumnID.ToString()
                                                                                                                                                                    });

            model.StagingColumnsForGeography = context.StagingDatasets.Single(x => x.StagingDatasetID.Equals(datasetID)).StagingColumns.Select(x => new SelectListItem()
                                                                                                                                                                    {
                                                                                                                                                                        Text = x.ColumnName,
                                                                                                                                                                        Value = x.StagingColumnID.ToString()
                                                                                                                                                                    });

            model.GeographyTypes = context.GeographyTypes.Select(x => new SelectListItem()
                                                                                        {
                                                                                            Text = x.GeographyType1,
                                                                                            Value = x.GeographyTypeID.ToString()
                                                                                        });

            model.DimensionsForMeasureBreakdown = context.Dimensions.Select(x => new SelectListItem
            {
                Text = x.DimensionName,
                Value = x.DimensionID.ToString()
            });

            List<CreateMeasureDetailModel> modelDetails = new List<CreateMeasureDetailModel>();

            foreach (StagingColumn column in context.StagingDatasets.Single(x => x.StagingDatasetID.Equals(datasetID)).StagingColumns)
            {
                if (column.ColumnName != "UploadRef")
                {
                    CreateMeasureDetailModel detail = new CreateMeasureDetailModel();

                    detail.StagingColumnID = column.StagingColumnID;
                    detail.StagingColumnName = column.ColumnName;

                    detail.AvailableDimensions = context.Dimensions.ToList().Select(x => new SelectListItem() { Text = x.DimensionName, Value = x.DimensionID.ToString() });

                    modelDetails.Add(detail);
                }

            }

            model.MeasureDetails = modelDetails;

            return model;
        }
        /// <summary>
        /// Validate that the CreateMeasureModel is valid prior to
        /// submission to the database
        /// </summary>
        /// <param name="model">The model to be validated</param>
        /// <returns>Boolean indicating valid or not</returns>
        public Boolean isCreateMeasureModelValid(CreateMeasureModel model)
        {
            Geographical_NeedsEntities context = new Geographical_NeedsEntities();

            int count = context.Measures.Where(x => x.MeasureName.Equals(model.MeasureName)).Count();

            return count == 0;
        }