public HttpResponseMessage Post(EstablishmentPostModel model)
        {
            //System.Threading.Thread.Sleep(2000); // test api latency

            var command = new CreateEstablishment(User)
            {
                OfficialName = new CreateEstablishmentName(User),
                OfficialUrl = new CreateEstablishmentUrl(User),
                Location = new UpdateEstablishmentLocation(0, User),
            };
            Mapper.Map(model, command);

            try
            {
                _createHandler.Handle(command);
            }
            catch (ValidationException ex)
            {
                var badRequest = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message, "text/plain");
                return badRequest;
            }

            var response = Request.CreateResponse(HttpStatusCode.Created, "Establishment was successfully created.");
            var url = Url.Link(null, new
            {
                controller = "Establishments",
                action = "GetOne",
                establishmentId = command.CreatedEstablishmentId,
            });
            Debug.Assert(url != null);
            response.Headers.Location = new Uri(url);

            return response;
        }
        protected Establishment Seed(CreateEstablishment command)
        {
            // make sure establishment does not already exist
            var establishment = _queryProcessor.Execute(new EstablishmentByOfficialName(command.OfficialName));
            if (!string.IsNullOrWhiteSpace(command.OfficialWebsiteUrl))
                establishment = _queryProcessor.Execute(new GetEstablishmentByUrlQuery(command.OfficialWebsiteUrl));
            if (establishment != null) return establishment;

            _createEstablishment.Handle(command);
            _unitOfWork.SaveChanges();
            return command.CreatedEstablishment;
        }
Example #3
0
        public void Handle(ImportUsfEstablishments command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            var reportBuilder = command.ReportBuilder ?? new WorkReportBuilder("Import USF Departments");
            var service       = command.Service;

            try
            {
                #region Skip import if not necessary

                // do we need to re-import?
                reportBuilder.Report("Determining whether USF department import should be skipped.");
                var isSkippable = !string.IsNullOrWhiteSpace(service.LastUpdate) // data has already been sync'd at least once
                                                                                 // and the last sync date equals the command's requesting sync date
                                  && (string.IsNullOrWhiteSpace(command.LastUpdate) || service.LastUpdate.Equals(command.LastUpdate))
                                                                                 // and the status does not indicate a failure or in-progress state
                                  && service.Status == UsfFacultyProfileAttribute.Ready.ToString();
                if (isSkippable)
                {
                    reportBuilder.Report("USF department import will be skipped.");
                    return; // note this will also happen when command.SyncDate == null
                }

                #endregion
                #region Wait on import already in progress

                // is there already an import in progress?
                // if so, wait on it to change
                reportBuilder.Report("Determining whether or not USF department import is in progress.");
                _entities.Reload(command.Service.Integration); // freshen status from db
                if (service.Status == UsfFacultyProfileAttribute.InProgress.ToString())
                {
                    // wait for the process to complete
                    reportBuilder.Report("USF Department import is in progress, wait for completion.");
                    WaitOnProgress(command, reportBuilder);

                    // if was in progress, and is now ready, do not import again
                    if (command.Service.Status == UsfFacultyProfileAttribute.Ready.ToString())
                    {
                        reportBuilder.Report("USF Department import is ready, parallel import succeeded.");
                        return;
                    }
                }

                #endregion
                #region Update status, invoke USF service, and load comparison data

                // begin progress
                reportBuilder.Report("Setting status to '{0}'.", UsfFacultyProfileAttribute.InProgress.ToString());
                command.Service.Status = UsfFacultyProfileAttribute.InProgress.ToString();
                _entities.Update(command.Service.Integration);
                _entities.SaveChanges();
                reportBuilder.Report("Status set to '{0}'.", UsfFacultyProfileAttribute.InProgress.ToString());

                // invoke departments service
                reportBuilder.Report("Invoking USF department service.");
                var departmentsData = _queryProcessor.Execute(new UsfDepartments(command.Service)
                {
                    ReportBuilder = reportBuilder
                });
                if (departmentsData == null || departmentsData.Departments == null)
                {
                    throw new InvalidOperationException(string.Format(
                                                            "Could not obtain departments data for '{0}_{1}'service integration.",
                                                            command.Service.Integration.Name, command.Service.Integration.TenantId));
                }
                reportBuilder.Report("Deserialized USF department response to CLR object.");
                reportBuilder.Report("JSON value of deserialized response is:");
                reportBuilder.Report(JsonConvert.SerializeObject(departmentsData));

                // establishment types will be used later when creating establishments
                reportBuilder.Report("Loading establishment type entities.");
                var establishmentTypeName = KnownEstablishmentType.UniversityCampus.AsSentenceFragment();
                var campusType            = _entities.Query <EstablishmentType>().Single(t => t.EnglishName == establishmentTypeName);
                establishmentTypeName = KnownEstablishmentType.College.AsSentenceFragment();
                var collegeType = _entities.Query <EstablishmentType>().Single(t => t.EnglishName == establishmentTypeName);
                establishmentTypeName = KnownEstablishmentType.Department.AsSentenceFragment();
                var departmentType = _entities.Query <EstablishmentType>().Single(t => t.EnglishName == establishmentTypeName);

                // order the service results by campus, college, deptid
                departmentsData.Departments = departmentsData.Departments
                                              .OrderBy(x => x.CampusName).ThenBy(x => x.CollegeName).ThenBy(x => x.DepartmentId).ToArray();

                // all units will be children of USF
                reportBuilder.Report("Loading USF establishment entity.");
                var usf = _entities.Get <Establishment>()
                          .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
                {
                    x => x.Children,
                    x => x.Offspring.Select(y => y.Offspring),
                })
                          .Single(x => x.RevisionId == command.Service.Integration.TenantId);
                reportBuilder.Report("USF establishment entity loaded with id '{0}'.", usf.RevisionId);
                var isChanged = false;

                #endregion
                #region Populate campuses

                // populate the campuses first (they are ranked)
                var campusRanks = new Dictionary <string, int>
                {
                    { "Tampa", 1 },
                    { "Petersburg", 2 },
                    { "Sarasota", 3 },
                };
                var campusNames = departmentsData.Departments.Select(x => x.CampusName).Distinct().ToArray();
                //campusNames = campusNames.Union(new[] { "Unidentified Campus" }).ToArray();
                if (campusNames.Any(x => !campusRanks.Keys.Any(x.Contains)))
                {
                    throw new InvalidOperationException(string.Format(
                                                            "USF Service returned unexpected campus '{0}'.",
                                                            campusNames.First(x => !campusRanks.Keys.Any(x.Contains))));
                }
                reportBuilder.Report("Iterating over {0} campus names.", campusNames.Length);
                foreach (var campusName in campusNames)
                {
                    // note that the service returns USF Sarasota, but we want official name to be USF Sarasota-Manatee
                    var campusByName = usf.Children.SingleOrDefault(x => x.Names.Any(y => y.Text.Equals(campusName)));
                    if (campusByName == null)
                    {
                        var createCampusCommand = new CreateEstablishment(command.Principal)
                        {
                            NoCommit     = true,
                            NoHierarchy  = true,
                            Parent       = usf,
                            TypeId       = campusType.RevisionId,
                            Rank         = campusRanks.Single(x => campusName.Contains(x.Key)).Value,
                            OfficialName = new CreateEstablishmentName(command.Principal)
                            {
                                NoCommit       = true,
                                IsOfficialName = true,
                                Text           = !campusName.Contains("Sarasota") ? campusName : "USF Sarasota-Manatee",
                            },
                        };
                        _createEstablishment.Handle(createCampusCommand);
                        if (campusName.Contains("Sarasota"))
                        {
                            var createCampusNameCommand = new CreateEstablishmentName(command.Principal)
                            {
                                NoCommit = true,
                                Owner    = createCampusCommand.Created,
                                Text     = campusName,
                            };
                            _createEstablishmentName.Handle(createCampusNameCommand);
                        }
                        isChanged = true;
                    }
                }

                #endregion
                #region Populate Colleges

                // get all unique combinations of campus / college
                var campusColleges = departmentsData.Departments
                                     .Select(x => new { x.CampusName, x.CollegeName, }).Distinct()
                                     .OrderBy(x => x.CampusName).ThenBy(x => x.CollegeName)
                                     .ToArray();
                reportBuilder.Report("Iterating over {0} campus colleges.", campusColleges.Length);
                foreach (var campusCollege in campusColleges)
                {
                    var collegeInCampus = campusCollege;

                    // does the college already exist?
                    var campus = usf.Children
                                 .Single(x => x.Names.Any(y => y.Text.Equals(collegeInCampus.CampusName)));
                    var officialName  = string.Format("{0}, {1}", collegeInCampus.CollegeName, campus.OfficialName);
                    var collegeByName = campus.Children
                                        .SingleOrDefault(x => x.OfficialName.Equals(officialName) ||
                                                         x.Names.Any(y => y.Text.Equals(collegeInCampus.CollegeName) && y.IsContextName));
                    if (collegeByName != null)
                    {
                        continue;
                    }

                    var createCollegeCommand = new CreateEstablishment(command.Principal)
                    {
                        NoCommit     = true,
                        NoHierarchy  = true,
                        Parent       = campus,
                        TypeId       = collegeType.RevisionId,
                        OfficialName = new CreateEstablishmentName(command.Principal)
                        {
                            NoCommit       = true,
                            IsOfficialName = true,
                            Text           = officialName,
                        },
                    };
                    _createEstablishment.Handle(createCollegeCommand);
                    var createCollegeNameCommand = new CreateEstablishmentName(command.Principal)
                    {
                        NoCommit      = true,
                        IsContextName = true,
                        Owner         = createCollegeCommand.Created,
                        Text          = collegeInCampus.CollegeName,
                    };
                    _createEstablishmentName.Handle(createCollegeNameCommand);
                    isChanged = true;

                    // does this college have any department id's?
                    var customIds = departmentsData.Departments
                                    .Where(x => x.CollegeName == collegeInCampus.CollegeName &&
                                           x.CampusName == collegeInCampus.CampusName &&
                                           string.IsNullOrWhiteSpace(x.DepartmentName)
                                           )
                                    .Select(x => x.DepartmentId).Distinct()
                                    // it is possible that this custom DEPTID is named elsewhere
                                    .Where(x => !departmentsData.Departments
                                           .Any(y => y.DepartmentId == x && !string.IsNullOrWhiteSpace(y.DepartmentName)))
                                    .ToArray();
                    //var doubleCheckCustomIds = departmentsData.Departments
                    //    .Where(x => customIds.Contains(x.DepartmentId)).ToArray();

                    foreach (var customId in customIds)
                    {
                        createCollegeCommand.Created.CustomIds.Add(new EstablishmentCustomId
                        {
                            Owner = createCollegeCommand.Created,
                            Value = customId,
                        });
                    }
                }

                #endregion
                #region Populate Departments

                // we are going to skip all unnamed departments
                var namedDepartments = departmentsData.Departments
                                       .Where(x => !string.IsNullOrWhiteSpace(x.DepartmentName))
                                       .ToArray();
                reportBuilder.Report("Iterating over {0} named departments.", namedDepartments.Length);
                foreach (var departmentData in namedDepartments)
                {
                    var campus = usf.Children
                                 .Single(x => x.Names.Any(y => y.Text.Equals(departmentData.CampusName)));
                    var college = campus.Children
                                  .Single(x => x.Names.Any(y => y.Text.Equals(departmentData.CollegeName)));
                    var officialName = string.Format("{0}, {1}, {2}", departmentData.DepartmentName, departmentData.CollegeName, campus.OfficialName);

                    // is the name of the department the same as the name of the college?
                    if (departmentData.DepartmentName == departmentData.CollegeName)
                    {
                        if (college.CustomIds.All(x => x.Value != departmentData.DepartmentId))
                        {
                            college.CustomIds.Add(new EstablishmentCustomId
                            {
                                Owner = college,
                                Value = departmentData.DepartmentId,
                            });
                            isChanged = true;
                        }
                        continue;
                    }

                    // does the department already exist?
                    var departmentByName = college.Children
                                           .SingleOrDefault(x => x.OfficialName.Equals(officialName) ||
                                                            x.Names.Any(y => y.Text.Equals(departmentData.DepartmentName)));
                    var departmentById = college.Children
                                         .SingleOrDefault(x => x.CustomIds.Select(y => y.Value).Contains(departmentData.DepartmentId));

                    if (departmentByName == null)
                    {
                        if (departmentById == null)
                        {
                            var createDepartmentCommand = new CreateEstablishment(command.Principal)
                            {
                                NoCommit     = true,
                                NoHierarchy  = true,
                                Parent       = college,
                                TypeId       = departmentType.RevisionId,
                                OfficialName = new CreateEstablishmentName(command.Principal)
                                {
                                    NoCommit       = true,
                                    IsOfficialName = true,
                                    Text           = officialName,
                                },
                            };
                            _createEstablishment.Handle(createDepartmentCommand);
                            var createDepartmentNameCommand = new CreateEstablishmentName(command.Principal)
                            {
                                NoCommit      = true,
                                IsContextName = true,
                                Owner         = createDepartmentCommand.Created,
                                Text          = departmentData.DepartmentName,
                            };
                            _createEstablishmentName.Handle(createDepartmentNameCommand);
                            departmentByName = createDepartmentCommand.Created;
                        }
                        else
                        {
                            var formerContextNames = departmentById.Names.Where(x => x.IsContextName).ToArray();
                            foreach (var formerContextName in formerContextNames)
                            {
                                formerContextName.IsFormerName = true;
                            }

                            var newContextName = formerContextNames.SingleOrDefault(x => x.Text == departmentData.DepartmentName);
                            if (newContextName != null)
                            {
                                newContextName.IsFormerName = false;
                            }

                            else
                            {
                                departmentById.Names.Add(new EstablishmentName
                                {
                                    ForEstablishment = departmentById,
                                    IsContextName    = true,
                                    Text             = departmentData.DepartmentName,
                                });
                            }
                            departmentById.Names.Single(x => x.IsOfficialName).Text = officialName;
                            departmentById.OfficialName = officialName;
                            departmentByName            = departmentById;
                        }
                        isChanged = true;
                    }

                    if (departmentById == null)
                    {
                        departmentByName.CustomIds.Add(new EstablishmentCustomId
                        {
                            Owner = departmentByName,
                            Value = departmentData.DepartmentId,
                        });
                        isChanged = true;
                    }
                }

                #endregion
                #region Populate Unnamed Department Id's

                var unnamedDepartments = departmentsData.Departments
                                         .Where(x => string.IsNullOrWhiteSpace(x.DepartmentName))
                                         .ToArray();
                foreach (var departmentData in unnamedDepartments)
                {
                    var campus = usf.Children
                                 .Single(x => x.Names.Any(y => y.Text.Equals(departmentData.CampusName)));
                    var college = campus.Children
                                  .Single(x => x.Names.Any(y => y.Text.Equals(departmentData.CollegeName)));
                    if (college.CustomIds.All(x => x.Value != departmentData.DepartmentId))
                    {
                        college.CustomIds.Add(new EstablishmentCustomId
                        {
                            EstablishmentId = college.RevisionId,
                            Owner           = college,
                            Value           = departmentData.DepartmentId,
                        });
                        isChanged = true;
                    }
                }

                #endregion

                if (isChanged)
                { // TODO: only update hierarchy when establishments are added or removed
                    reportBuilder.Report("USF Department import has pending database changes.");
                    _updateHierarchy.Handle(new UpdateEstablishmentHierarchy(usf));
                    _entities.SaveChanges();
                    reportBuilder.Report("USF Department import pending database changes have been committed.");
                }

                reportBuilder.Report("USF Department import is complete.");
                reportBuilder.Report("Setting status to '{0}'.", UsfFacultyProfileAttribute.Ready.ToString());
                command.Service.Status = UsfFacultyProfileAttribute.Ready.ToString();
                reportBuilder.Report("Setting last update to '{0}'.", command.LastUpdate);
                command.Service.LastUpdate = command.LastUpdate;
                _entities.Update(command.Service.Integration);
                _entities.SaveChanges();
                reportBuilder.Report("Status set to '{0}'.", UsfFacultyProfileAttribute.Ready.ToString());
            }
            catch
            {
                reportBuilder.Report("USF Department import encountered an exception.");
                _entities.DiscardChanges();
                reportBuilder.Report("Setting status to '{0}'.", UsfFacultyProfileAttribute.FailedState.ToString());
                command.Service.Status = UsfFacultyProfileAttribute.FailedState.ToString();
                _entities.Update(command.Service.Integration);
                _entities.SaveChanges();
                reportBuilder.Report("Status set to '{0}'.", UsfFacultyProfileAttribute.FailedState.ToString());
                reportBuilder.Report("Rethrowing exception.");
                throw;
            }
        }
        public virtual ActionResult RunEstablishmentImport()
        {
            var placeMarks = new KmlPlaceMarks(Server.MapPath(string.Format("~{0}",
                                                                            Links.content.kml.establishment_import_kml)));
            var establishmentRows = new EstablishmentRows(Server.MapPath(string.Format("~{0}",
                                                                                       Links.content.kml.establishment_import_tsv)));
            var en = _entities.Get <Language>().SingleOrDefault(x => x.TwoLetterIsoCode.Equals("en", StringComparison.OrdinalIgnoreCase));

            if (en == null)
            {
                throw new InvalidOperationException("Could not find entity for 'English' language");
            }
            var university = _entities.Get <EstablishmentType>().SingleOrDefault(x =>
                                                                                 x.EnglishName.Equals("University", StringComparison.OrdinalIgnoreCase));

            if (university == null)
            {
                throw new InvalidOperationException("Could not find entity for 'University' establishment type");
            }

            foreach (var placeMark in placeMarks)
            {
                ConsoleLog(); // write a new line

                // skip placemarks with no name / title
                if (string.IsNullOrWhiteSpace(placeMark.Name))
                {
                    ConsoleLog("Skipping Placemark with no name", false, true);
                    continue;
                }

                // begin processing the placemark
                ConsoleLog(string.Format("Processing Placemark '{0}'", placeMark.Name));

                // look in spreadsheet for row where official name or english name matches placemark name / title
                var placeMarkClosure     = placeMark;
                var establishmentMatches = establishmentRows.Where(e => placeMarkClosure.Name.Equals(e.OfficialName) ||
                                                                   placeMarkClosure.Name.Equals(e.EnglishName)).ToList();
                if (establishmentMatches.Count > 1)
                {
                    // skip when there are multiple matching rows in the spreadsheet
                    ConsoleLog(string.Format("Skipping, found multiple establishments with name '{0}'.", placeMark.Name), false, true);
                    continue;
                }
                if (establishmentMatches.Count < 1)
                {
                    // skip when there are no matching rows in the spreadsheet
                    ConsoleLog(string.Format("Skipping, found no establishments with name '{0}'.", placeMark.Name), false, true);
                    continue;
                }

                // settle on the row found in the spreadsheet
                ConsoleLog(string.Format("Found 1 establishment with name '{0}'.", placeMark.Name), true);
                var establishmentRow = establishmentMatches.Single();

                // skip if the spreadsheet row has no website URL
                if (string.IsNullOrWhiteSpace(establishmentRow.WebsiteUrl))
                {
                    ConsoleLog(string.Format("Skipping, establishment with name '{0}' has no website URL.", placeMark.Name), false, true);
                    continue;
                }

                // make sure the spreadsheet does not contain any duplicates for this row
                var duplicateEstablishmentRows = establishmentRows.Where(r =>
                                                                         establishmentRow.WebsiteUrl.Equals(r.WebsiteUrl) ||
                                                                         r.OtherUrls.Contains(establishmentRow.WebsiteUrl) ||
                                                                         establishmentRow.OfficialName.Equals(r.OfficialName, StringComparison.OrdinalIgnoreCase) ||
                                                                         establishmentRow.OfficialName.Equals(r.EnglishName, StringComparison.OrdinalIgnoreCase) ||
                                                                         establishmentRow.OfficialName.Equals(r.UCosmic1Name, StringComparison.OrdinalIgnoreCase) ||
                                                                         establishmentRow.EnglishName.Equals(r.OfficialName, StringComparison.OrdinalIgnoreCase) ||
                                                                         establishmentRow.EnglishName.Equals(r.EnglishName, StringComparison.OrdinalIgnoreCase) ||
                                                                         establishmentRow.EnglishName.Equals(r.UCosmic1Name, StringComparison.OrdinalIgnoreCase)
                                                                         ).ToList();
                if (duplicateEstablishmentRows.Count > 1)
                {
                    ConsoleLog(string.Format("Skipping, spreadsheet has duplicate rows for '{0}' / '{1}'.", establishmentRow.OfficialName, establishmentRow.WebsiteUrl), false, true);
                    continue;
                }

                // lookup the establishment in the database by URL
                var establishment = _queryProcessor.Execute(new GetEstablishmentByUrlQuery(establishmentRow.WebsiteUrl));

                // when exactly 1 establishment exists in the db, check its geography
                if (establishment != null)
                {
                    ConsoleLog(string.Format("Establishment with website URL '{0}' is already seeded", establishmentRow.WebsiteUrl));
                    //establishment = establishments.Single();

                    // check geography
                    if (!establishment.Location.Center.HasValue)
                    {
                        var result = _placeFinder.Find(new PlaceByCoordinates(placeMark.Latitude, placeMark.Longitude)).Single();
                        var places = new List <Place>();
                        if (!result.WoeId.HasValue)
                        {
                            ConsoleLog(string.Format("Unable to determine WOE ID for establishment with website URL '{0}'.", establishmentRow.WebsiteUrl), null, true);
                        }
                        else
                        {
                            //var place = placeFactory.FromWoeId(result.WoeId.Value);
                            var place = _queryProcessor.Execute(
                                new GetPlaceByWoeIdQuery
                            {
                                WoeId = result.WoeId.Value,
                            });
                            places = place.Ancestors.OrderByDescending(n => n.Separation).Select(a => a.Ancestor).ToList();
                            places.Add(place);
                        }

                        var placesCount = places.Count();
                        var lastPlace   = places.Last();
                        var command     = new UpdateEstablishment
                        {
                            Id              = establishment.RevisionId,
                            CenterLatitude  = placeMark.Latitude,
                            CenterLongitude = placeMark.Longitude,
                            NorthLatitude   = placesCount > 0 ? lastPlace.BoundingBox.Northeast.Latitude : null,
                            EastLongitude   = placesCount > 0 ? lastPlace.BoundingBox.Northeast.Longitude : null,
                            SouthLatitude   = placesCount > 0 ? lastPlace.BoundingBox.Southwest.Latitude : null,
                            WestLongitude   = placesCount > 0 ? lastPlace.BoundingBox.Southwest.Longitude : null,
                            PlaceIds        = places.Select(p => p.RevisionId),
                        };
                        _updateEstablishment.Handle(command);
                        _unitOfWork.SaveChanges();
                        //establishment.Location.Center = new Coordinates { Latitude = placeMark.Latitude, Longitude = placeMark.Longitude };
                        //establishment.Location.Places = places;
                        //establishment.Location.BoundingBox = (places.Count > 0) ? places.Last().BoundingBox : new BoundingBox();
                        //_unitOfWork.SaveChanges();
                        ConsoleLog(string.Format("Updated location of seeded establishment with website URL '{0}'.", establishmentRow.WebsiteUrl), true, true);
                        continue;
                    }
                    ConsoleLog(string.Format("Bypassing seeded establishment with website URL '{0}' as it already has location information", establishmentRow.WebsiteUrl), true, true);
                }
                else
                {
                    ConsoleLog(string.Format("Seeding establishment with website URL '{0}'...", establishmentRow.WebsiteUrl));
                    //var result = _placeFinder.Find(new PlaceByCoordinates(placeMark.Latitude, placeMark.Longitude)).Single();
                    //var places = new List<Place>();
                    //if (!result.WoeId.HasValue)
                    //{
                    //    ConsoleLog(string.Format("Unable to determine WOE ID for establishment with website URL '{0}'.", establishmentRow.WebsiteUrl), null, true);
                    //}
                    //else
                    //{
                    //    var place = _queryProcessor.Execute(
                    //        new GetPlaceByWoeIdQuery
                    //        {
                    //            WoeId = result.WoeId.Value,
                    //        });
                    //    places = place.Ancestors.OrderByDescending(n => n.Separation).Select(a => a.Ancestor).ToList();
                    //    places.Add(place);
                    //}

                    var command = new CreateEstablishment
                    {
                        TypeId                  = university.RevisionId,
                        OfficialName            = establishmentRow.OfficialName,
                        OfficialWebsiteUrl      = establishmentRow.WebsiteUrl,
                        FindPlacesByCoordinates = true,
                        CenterLatitude          = placeMark.Latitude,
                        CenterLongitude         = placeMark.Longitude,
                        UCosmicCode             = establishmentRow.CeebCode,
                    };

                    var nonOfficialNames = new List <CreateEstablishment.NonOfficialName>();
                    if (!establishmentRow.OfficialName.Equals(establishmentRow.EnglishName, StringComparison.OrdinalIgnoreCase))
                    {
                        nonOfficialNames.Add(new CreateEstablishment.NonOfficialName
                        {
                            Text = establishmentRow.EnglishName,
                            TranslationToLanguageId = en.Id,
                        });
                    }
                    command.NonOfficialNames = nonOfficialNames.ToArray();

                    var nonOfficialUrls = new List <CreateEstablishment.NonOfficialUrl>();
                    if (establishmentRow.OtherUrls != null && establishmentRow.OtherUrls.Length > 0)
                    {
                        nonOfficialUrls.AddRange(establishmentRow.OtherUrls.Select(u =>
                                                                                   new CreateEstablishment.NonOfficialUrl
                        {
                            Value = u,
                        })
                                                 );
                    }
                    command.NonOfficialUrls = nonOfficialUrls.ToArray();
                    _createEstablishment.Handle(command);
                    establishment = command.CreatedEstablishment;

                    _entities.Create(establishment);
                    _unitOfWork.SaveChanges();
                    ConsoleLog(string.Format("Establishment with website URL '{0}' has been seeded.", establishmentRow.WebsiteUrl), true, true);
                }
            }

            //// set up new member(s)
            //var hpu = _entities.Get<Establishment>().SingleOrDefault(
            //    e => e.Urls.Any(u => u.Value.Equals("www.hpu.edu", StringComparison.OrdinalIgnoreCase)));
            //if (hpu != null)
            //{
            //    if (!hpu.IsMember)
            //    {
            //        hpu.IsMember = true;
            //        hpu.EmailDomains.Add(new EstablishmentEmailDomain
            //        {
            //            Value = "@hpu.edu",
            //        });

            //        var lindseyCreator = new CreatePersonHandler(_entities);
            //        var createLindsey = new CreatePersonCommand
            //        {
            //            DisplayName = "Lindsey Garbenis",
            //            UserName = "******",
            //            UserIsRegistered = false,
            //            FirstName = "Lindsey",
            //            LastName = "Garbenis",
            //        };
            //        lindseyCreator.Handle(createLindsey);
            //        createLindsey.CreatedPerson.Emails
            //            .Add(new EmailAddress { IsDefault = true, Value = createLindsey.UserName });

            //        _entities.Update(hpu);
            //        _unitOfWork.SaveChanges();
            //    }
            //}
            //var uwm = _entities.Get<Establishment>().SingleOrDefault(
            //    e => e.Urls.Any(u => u.Value.Equals("www.uwm.edu", StringComparison.OrdinalIgnoreCase)));
            //if (uwm != null)
            //{
            //    if (!uwm.IsMember)
            //    {
            //        uwm.IsMember = true;
            //        uwm.EmailDomains.Add(new EstablishmentEmailDomain
            //                                 {
            //                                     Value = "@uwm.edu",
            //                                 });
            //        _entities.Update(uwm);
            //        _unitOfWork.SaveChanges();
            //    }
            //}

            //// add former name(s)
            //const string upvName = "Universidad Privada del Valle";
            //var univalle = _entities.Get<Establishment>().SingleOrDefault(e => e.WebsiteUrl == "www.univalle.edu");
            //var upv = univalle.Names.SingleOrDefault(n => n.Text == upvName);
            //if (upv == null)
            //{
            //    univalle.Names.Add(new EstablishmentName { Text = upvName, IsFormerName = true });
            //    _entities.Update(univalle);
            //}

            //// fix name
            //var uniatlantico = _entities.Get<Establishment>().Single(e => e.WebsiteUrl == "www.uniatlantico.edu.co");
            //var engName = uniatlantico.Names.SingleOrDefault(n => n.Text == "Atlantic University, Atlantico");
            //if (engName != null)
            //{
            //    engName.Text = "Atlantic University";
            //    _unitOfWork.SaveChanges();
            //}

            ViewBag.Console = _consoleLog.ToString();
            return(View());
        }
        public HttpResponseMessage ValidateUCosmicCode(int establishmentId, EstablishmentApiScalarModel model)
        {
            //System.Threading.Thread.Sleep(2000); // test api latency

            model.Id = establishmentId;

            ValidationResult validationResult;
            string propertyName;
            if (model.Id < 1)
            {
                var command = new CreateEstablishment(User);
                Mapper.Map(model, command);
                validationResult = _createValidator.Validate(command);
                propertyName = command.PropertyName(y => y.UCosmicCode);
            }
            else
            {
                var command = new UpdateEstablishment(model.Id, User);
                Mapper.Map(model, command);
                validationResult = _updateValidator.Validate(command);
                propertyName = command.PropertyName(y => y.UCosmicCode);
            }

            Func<ValidationFailure, bool> forText = x => x.PropertyName == propertyName;
            if (validationResult.Errors.Any(forText))
                return Request.CreateResponse(HttpStatusCode.BadRequest,
                    validationResult.Errors.First(forText).ErrorMessage, "text/plain");

            return Request.CreateResponse(HttpStatusCode.OK);
        }