public void UpdateSpeciesFact(IUserContext loggedInUser, RevisionEditViewModel model, ITaxon taxon)
        {
            try
            {
                // Check logged in user
                if (loggedInUser.IsNotNull())
                {
                    SpeciesFactModelManager speciesModel = new SpeciesFactModelManager(taxon, loggedInUser);

                    // Create quality values
                    if (model.RevisionQualityId != 0)
                    {
                        speciesModel.QualityStatusId = model.RevisionQualityId;
                    }

                    if (model.RevisionQualityDescription.IsNotNull())
                    {
                        speciesModel.QualityDescription = model.RevisionQualityDescription;
                    }
                    // Now we update
                    speciesModel.UpdateDyntaxaSpeciesFacts();
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception(Resources.DyntaxaResource.SharedNotPossibleToUpdateSpeciesFactError, e);
                throw ex;
            }
        }
        public void InitUserListsAndLinks(RevisionEditViewModel model)
        {
            if (model.UserAdminLink.IsNull())
            {
                model.UserAdminLink = new LinkItem();
            }

            if (model.UserList.IsNull())
            {
                model.UserList = new List <RevisionUserItemModelHelper>();
            }

            if (model.SelectedUserList.IsNull())
            {
                model.SelectedUserList = new List <RevisionUserItemModelHelper>();
            }

            if (model.RevisionQualityList.IsNull())
            {
                model.RevisionQualityList = new List <TaxonDropDownModelHelper>();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="loggedInUser"></param>
        /// <param name="dyntaxaApplicationUserContext"></param>
        /// <param name="taxonRevision"></param>
        /// <param name="isTaxonInRevision"></param>
        /// <param name="revisionUsers"></param>
        /// <param name="allUsers"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public RevisionEditViewModel GetRevisionEditViewModel(
            IUserContext loggedInUser,
            IUserContext dyntaxaApplicationUserContext,
            ITaxonRevision taxonRevision,
            ITaxon taxon,
            bool isTaxonInRevision,
            IList <RevisionUserItemModelHelper> revisionUsers,
            IList <RevisionUserItemModelHelper> allUsers,
            int roleId)
        {
            RevisionEditViewModel model = new RevisionEditViewModel();

            if (loggedInUser.IsNotNull() && dyntaxaApplicationUserContext.IsNotNull())
            {
                // Set locale to logged in user.
                dyntaxaApplicationUserContext.Locale = loggedInUser.Locale;

                if (taxonRevision.IsNotNull())
                {
                    if (taxon.IsNotNull())
                    {
                        model.ExpectedStartDate             = taxonRevision.ExpectedStartDate;
                        model.ExpectedPublishingDate        = taxonRevision.ExpectedEndDate;
                        model.IsSpeciesFactPublished        = taxonRevision.IsSpeciesFactPublished;
                        model.IsReferenceRelationsPublished = taxonRevision.IsReferenceRelationsPublished;
                        model.GUID = taxonRevision.Guid;
                        model.RevisionReferencesList = new List <int>();
                        model.NoOfRevisionReferences = 0;
                        foreach (ReferenceRelation referenceRelation in taxonRevision.GetReferences(loggedInUser))
                        {
                            model.RevisionReferencesList.Add(referenceRelation.Id);
                            model.NoOfRevisionReferences++;
                        }

                        model.SelectedUserList = new List <RevisionUserItemModelHelper>();
                        model.UserList         = new List <RevisionUserItemModelHelper>();
                        foreach (RevisionUserItemModelHelper user in allUsers)
                        {
                            bool userAssigedToRevision = false;
                            foreach (RevisionUserItemModelHelper revisionUser in revisionUsers)
                            {
                                if (user.Id == revisionUser.Id)
                                {
                                    //We found a alreday assigned user
                                    userAssigedToRevision = true;
                                    break;
                                }
                            }

                            if (userAssigedToRevision)
                            {
                                model.SelectedUserList.Add(user);
                            }
                            else
                            {
                                model.UserList.Add(user);
                            }
                        }
                        string url = Resources.DyntaxaResource.SharedRevisionUserAdminLinkText;
                        model.RoleId        = roleId.ToString();
                        model.UserAdminLink = new LinkItem(LinkType.Url, LinkQuality.Automatic, url, userAdminRoleLink);

                        model.RevisionDescription = taxonRevision.Description;

                        // Get specices fact information
                        try
                        {
                            SpeciesFactModelManager speciesModel = new SpeciesFactModelManager(taxon, loggedInUser);
                            try
                            {
                                if (speciesModel.QualityStatus.IsNotNull())
                                {
                                    model.RevisionQualityId = speciesModel.QualityStatus.Id;
                                }
                                else if (speciesModel.QualityStatusList.IsNotNull() && speciesModel.QualityStatusList.Count > 1)
                                {
                                    model.RevisionQualityId = speciesModel.QualityStatusList.ElementAt(0).Id;
                                }
                                else
                                {
                                    model.RevisionQualityId = 0;
                                }

                                model.RevisionQualityList = new List <TaxonDropDownModelHelper>();
                                foreach (var status in speciesModel.QualityStatusList)
                                {
                                    model.RevisionQualityList.Add(new TaxonDropDownModelHelper(status.Id, status.Label));
                                }
                                model.RevisionQualityDescription = speciesModel.QualityDescription;
                            }
                            catch (Exception)
                            {
                                model.ErrorMessage = Resources.DyntaxaResource.SharedNotPossibleToReadSpeciesFactError;
                            }
                        }
                        catch (Exception)
                        {
                            model.ErrorMessage = Resources.DyntaxaResource.SharedNotPossibleToReadSpeciesFactError;
                        }

                        model.RevisionTaxonInfoViewModel = new RevisionTaxonInfoViewModel();

                        model.RevisionTaxonInfoViewModel.Id                = taxonRevision.Id.ToString();
                        model.RevisionTaxonInfoViewModel.CommonName        = taxon.CommonName.IsNotEmpty() ? taxon.CommonName : string.Empty;
                        model.RevisionTaxonInfoViewModel.ScientificName    = taxon.ScientificName;
                        model.RevisionTaxonInfoViewModel.Category          = taxon.Category.Name;
                        model.RevisionTaxonInfoViewModel.CategorySortOrder = taxon.Category.SortOrder;

                        model.RevisionTaxonInfoViewModel.RevisionText   = Resources.DyntaxaResource.SharedRevisionIdLabelText;
                        model.RevisionTaxonInfoViewModel.MainHeaderText = Resources.DyntaxaResource.RevisionEditMainHeaderFullText;

                        model.RevisionId          = taxonRevision.Id.ToString();
                        model.ShowFinalizeButton  = false;
                        model.ShowInitalizeButton = false;
                        model.ShowDeleteButton    = false;
                        ITaxon revisionTaxon = taxonRevision.RootTaxon;
                        int    state         = taxonRevision.State.Id;
                        if (state == (int)TaxonRevisionStateId.Created)
                        {
                            model.RevisionStatus      = Resources.DyntaxaResource.RevisionListSelectedRevisionStatusCreatedText;
                            model.ShowInitalizeButton = true;
                            model.IsTaxonInRevision   = revisionTaxon.IsInRevision;
                            model.ShowDeleteButton    = true;
                        }
                        else if (state == (int)TaxonRevisionStateId.Ongoing)
                        {
                            model.RevisionStatus     = Resources.DyntaxaResource.RevisionListSelectedRevisionStatusOngoingText;
                            model.ShowFinalizeButton = true;
                        }
                        else if (state == (int)TaxonRevisionStateId.Closed)
                        {
                            model.RevisionStatus = Resources.DyntaxaResource.RevisionListSelectedRevisionStatusClosedText;
                            model.ShowUpdateSpeciesFactButton        = !taxonRevision.IsSpeciesFactPublished;
                            model.ShowUpdateReferenceRelationsButton = !taxonRevision.IsReferenceRelationsPublished;
                        }
                        model.RevisionTaxonId = revisionTaxon.Id.ToString();

                        return(model);
                    }
                    else
                    {
                        model.ErrorMessage = Resources.DyntaxaResource.RevisonAddInvalidTaxonErrorText;
                    }
                }
                else
                {
                    model.ErrorMessage = Resources.DyntaxaResource.RevisionSharedNoValidRevisionIdErrorText + taxonRevision.Id + ".";
                }
            }
            else
            {
                model.ErrorMessage = Resources.DyntaxaResource.SharedInvalidUserContext + " " + Resources.DyntaxaResource.SharedInvalidApplicationUserContext;
            }
            return(model);
        }
        public RevisionEditViewModel ReloadRevisionEditViewModel(
            IUserContext userContext,
            IUserContext dyntaxaApplicationUserContext,
            ITaxon taxon,
            ITaxonRevision taxonRevision,
            RevisionEditViewModel model,
            IList <RevisionUserItemModelHelper> revisionUsers,
            IList <RevisionUserItemModelHelper> allUsers)
        {
            IUserContext loggedInUser = userContext;

            model.RevisionQualityList        = new List <TaxonDropDownModelHelper>();
            model.SelectedUserList           = new List <RevisionUserItemModelHelper>();
            model.UserList                   = new List <RevisionUserItemModelHelper>();
            model.RevisionReferencesList     = new List <int>();
            model.RevisionTaxonInfoViewModel = new RevisionTaxonInfoViewModel();
            if (loggedInUser.IsNotNull() && dyntaxaApplicationUserContext.IsNotNull())
            {
                if (taxonRevision.IsNotNull())
                {
                    foreach (ReferenceRelation referenceRelation in taxonRevision.GetReferences(userContext))
                    {
                        model.RevisionReferencesList.Add(referenceRelation.Id);
                    }
                }

                foreach (RevisionUserItemModelHelper user in allUsers)
                {
                    bool userAssigedToRevision = false;
                    if (revisionUsers.IsNotNull())
                    {
                        foreach (RevisionUserItemModelHelper revisionUser in revisionUsers)
                        {
                            if (user.Id == revisionUser.Id)
                            {
                                //We found a alreday assigned user
                                userAssigedToRevision = true;
                                break;
                            }
                        }
                    }
                    if (userAssigedToRevision)
                    {
                        model.SelectedUserList.Add(user);
                    }
                    else
                    {
                        model.UserList.Add(user);
                    }
                }

                if (taxon.IsNotNull())
                {
                    SpeciesFactModelManager speciesModel = new SpeciesFactModelManager(taxon, loggedInUser);
                    foreach (var status in speciesModel.QualityStatusList)
                    {
                        model.RevisionQualityList.Add(new TaxonDropDownModelHelper(status.Id, status.Label));
                    }
                }
                if (taxon.IsNotNull() && taxonRevision.IsNotNull())
                {
                    model.RevisionTaxonInfoViewModel = new RevisionTaxonInfoViewModel();

                    model.RevisionTaxonInfoViewModel.Id                = taxonRevision.Id.ToString();
                    model.RevisionTaxonInfoViewModel.CommonName        = taxon.CommonName.IsNotEmpty() ? taxon.CommonName : string.Empty;
                    model.RevisionTaxonInfoViewModel.ScientificName    = taxon.ScientificName;
                    model.RevisionTaxonInfoViewModel.Category          = taxon.Category.Name;
                    model.RevisionTaxonInfoViewModel.CategorySortOrder = taxon.Category.SortOrder;

                    model.RevisionTaxonInfoViewModel.RevisionText   = Resources.DyntaxaResource.SharedRevisionIdLabelText;
                    model.RevisionTaxonInfoViewModel.MainHeaderText = Resources.DyntaxaResource.RevisionEditMainHeaderFullText;
                }

                string url = Resources.DyntaxaResource.SharedRevisionUserAdminLinkText;
                model.UserAdminLink = new LinkItem(LinkType.Url, LinkQuality.Automatic, url, userAdminRoleLink);
            }
            else
            {
                model.ErrorMessage = Resources.DyntaxaResource.SharedInvalidUserContext + " " + Resources.DyntaxaResource.SharedInvalidApplicationUserContext;
            }
            return(model);
        }