/// <summary>
        /// Method to create a Dimension based off the model returned from a View. This creates a Dimension
        /// and then populates values with those found in the specified Staging Table column
        /// </summary>
        /// <param name="model">The populated model passed back from the View</param>
        public void CreateDimension(CreateDimensionModel model)
        {
            Geographical_NeedsEntities context = new Geographical_NeedsEntities();

            Dimension newDim = context.Dimensions.Create();

            newDim.DimensionName = model.DimensionName;

            //DimensionSet newSet = context.DimensionSets.Create();
            //newSet.DimensionSetName = String.Format("By {0}", model.DimensionName);

            //DimensionSetMember newMember = context.DimensionSetMembers.Create();

            //newDim.DimensionSetMembers.Add(newMember);
            //newSet.DimensionSetMembers.Add(newMember);

            String stagingColumnName = context.StagingColumns.Single(x => x.StagingColumnID.Equals(model.DimColumnInStaging)).ColumnName;
            String stagingTableName = context.StagingColumns.Single(x => x.StagingColumnID.Equals(model.DimColumnInStaging)).StagingDataset.DatasetName;

            String sqlQuery = String.Format("select distinct [{0}] from [{1}]", stagingColumnName, stagingTableName);
            IEnumerable<String> stagingDimensionValues = context.Database.SqlQuery<String>(sqlQuery);

            foreach (String dimValue in stagingDimensionValues)
            {
                DimensionValue value = context.DimensionValues.Create();

                value.DimensionValue1 = dimValue;

                newDim.DimensionValues.Add(value);
            }

            context.Dimensions.Add(newDim);
            //context.DimensionSets.Add(newSet);
            //context.DimensionSetMembers.Add(newMember);

            context.SaveChanges();

            context.Dispose();
        }
        public ActionResult CreateDimension(CreateDimensionModel model)
        {
            DatasetDataAccess dataAccess = new DatasetDataAccess();

            if (!dataAccess.IsCreateDimensionModelValid(model))
            {
                ModelState.AddModelError("DimensionName", "The Dimension Name must be unique");

                model = dataAccess.PopulateCreateDimensionModel(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 Dimension", Action = "", Controller = "", isCurrent = true });

                model.Breadcrumbs = trail;

                return View(model);
            }

            try
            {
                dataAccess.CreateDimension(model);
            }
            catch (SqlException)
            {
                ModelState.AddModelError("", "An error occured when creating the dimension. Please ensure that the Dimension Name is unique and that the values are unique within it.");

                model = dataAccess.PopulateCreateDimensionModel(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 Dimension", Action = "", Controller = "", isCurrent = true });

                model.Breadcrumbs = trail;

                return View(model);
            }

            TempData["SuccessMessage"] = String.Format("The Dimension - {0}, was successfully created", model.DimensionName);
            return RedirectToAction("Index", "Staging");
        }
        /// DIMENSIONS 
        /// <summary>
        /// Method to create and populate a CreateDimensionModel ready for the View to use
        /// </summary>
        /// <param name="datasetID">The ID of the Staging Table this Dimension is based off</param>
        /// <returns></returns>
        public CreateDimensionModel PopulateCreateDimensionModel(int datasetID)
        {
            CreateDimensionModel model = new CreateDimensionModel();

            Geographical_NeedsEntities context = new Geographical_NeedsEntities();

            model.StagingDatasetID = datasetID;

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

            return model;
        }
        /// <summary>
        /// Validate that the CreateDimensionModel 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 IsCreateDimensionModelValid(CreateDimensionModel model)
        {
            Geographical_NeedsEntities context = new Geographical_NeedsEntities();

            int count = context.Dimensions.Where(x => x.DimensionName.Equals(model.DimensionName)).Count();

            return count == 0;
        }