Esempio n. 1
0
        /// <summary>
        /// Get default reference from revision
        /// </summary>
        /// <param name="userContext"></param>
        /// <param name="newObject"></param>
        /// <param name="taxonRevision"></param>
        /// <param name="referencesToAdd"> </param>
        /// <returns></returns>
        public static ReferenceRelationList GetDefaultReferences(IUserContext userContext, object newObject, ITaxonRevision taxonRevision, ReferenceRelationList referencesToAdd)
        {
            // Check if list is null the create a new one
            var referencesToAddTemp = new ReferenceRelationList();

            if (referencesToAdd.IsNotNull())
            {
                referencesToAddTemp = referencesToAdd;
            }

            if (taxonRevision.GetReferences(userContext).IsNotEmpty())
            {
                // Set all revision references of type Source as references
                foreach (var referenceRelation in taxonRevision.GetReferences(userContext))
                {
                    if (referenceRelation.Type.Id == (int)ReferenceRelationTypeId.Source)
                    {
                        IReference newReference = new ArtDatabanken.Data.Reference();
                        newReference.Id = referenceRelation.ReferenceId;
                        IReferenceRelation newReferenceRelation = GetReferenceRelation(newObject, newReference);
                        if (newReferenceRelation.IsNotNull())
                        {
                            referencesToAddTemp.Add(newReferenceRelation);
                        }
                    }
                }

                // Get first reference of type Used in and set as Source reference.
                if (referencesToAddTemp.Count == 0)
                {
                    IReference newReference = new ArtDatabanken.Data.Reference();
                    newReference.Id = taxonRevision.GetReferences(userContext).First().ReferenceId;
                    IReferenceRelation newReferenceRelation = GetReferenceRelation(newObject, newReference);
                    if (newReferenceRelation.IsNotNull())
                    {
                        referencesToAddTemp.Add(newReferenceRelation);
                    }
                }
            }

            // If no references found set dyntaxa as reference
            else
            {
                IReference newReference = new ArtDatabanken.Data.Reference();
                newReference.Id = DyntaxaReference;
                IReferenceRelation newReferenceRelation = GetReferenceRelation(newObject, newReference);
                if (newReferenceRelation.IsNotNull())
                {
                    referencesToAddTemp.Add(newReferenceRelation);
                }
            }

            return(referencesToAddTemp);
        }
        /// <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);
        }