Ejemplo n.º 1
0
        public async Task <ReviewSettings> GetReviewSettingsAsync(int reviewId, int userId, int?versionId = null)
        {
            var revisionId = await _itemInfoRepository.GetRevisionId(reviewId, userId, versionId);

            var reviewInfo = await GetReviewInfoAsync(reviewId, userId, revisionId);

            if (!await _permissionsRepository.HasReadPermissions(reviewId, userId, revisionId: revisionId))
            {
                throw ReviewsExceptionHelper.UserCannotAccessReviewException(reviewId);
            }

            var reviewData = await _reviewsRepository.GetReviewAsync(reviewId, userId, revisionId);

            return(await GetReviewSettingsFromReviewData(reviewData, reviewInfo));
        }
Ejemplo n.º 2
0
        public async Task <QuerySingleResult <WorkflowState> > GetStateForArtifactAsync(int userId, int artifactId, int?versionId = null, bool addDrafts = true)
        {
            var revisionId = await _itemInfoRepository.GetRevisionId(artifactId, userId, versionId);

            var state = await _stateChangeExecutorRepositories.WorkflowRepository.GetStateForArtifactAsync(userId, artifactId, revisionId, addDrafts);

            if (state == null || state.WorkflowId <= 0 || state.Id <= 0)
            {
                return(new QuerySingleResult <WorkflowState>
                {
                    ResultCode = QueryResultCode.Failure,
                    Message = I18NHelper.FormatInvariant("State information could not be retrieved for Artifact (Id:{0}).", artifactId)
                });
            }
            return(new QuerySingleResult <WorkflowState>
            {
                ResultCode = QueryResultCode.Success,
                Item = state
            });
        }
Ejemplo n.º 3
0
        public async Task <VersionControlArtifactInfo> GetVersionControlArtifactInfoAsync(int itemId, int?baselineId, int userId)
        {
            var artifactBasicDetails = await _artifactRepository.GetArtifactBasicDetails(itemId, userId);

            if (artifactBasicDetails == null)
            {
                var errorMessage = I18NHelper.FormatInvariant("Item (Id:{0}) is not found.", itemId);
                throw new ResourceNotFoundException(errorMessage, ErrorCodes.ResourceNotFound);
            }

            // Always getting permissions for the Head version of an artifact.
            // But, just in case, RevisionId and AddDrafts are available in ArtifactBasicDetails.
            var itemIdsPermissions = await _artifactPermissionsRepository.GetArtifactPermissions(Enumerable.Repeat(artifactBasicDetails.ArtifactId, 1), userId);

            if (!itemIdsPermissions.ContainsKey(artifactBasicDetails.ArtifactId) || !itemIdsPermissions[artifactBasicDetails.ArtifactId].HasFlag(RolePermissions.Read))
            {
                var errorMessage = I18NHelper.FormatInvariant("User does not have permissions for Artifact (Id:{0}).", artifactBasicDetails.ArtifactId);
                throw new AuthorizationException(errorMessage, ErrorCodes.UnauthorizedAccess);
            }

            var artifactInfo = new VersionControlArtifactInfo
            {
                Id            = artifactBasicDetails.ArtifactId,
                SubArtifactId = artifactBasicDetails.ArtifactId != artifactBasicDetails.ItemId
                    ? (int?)artifactBasicDetails.ItemId
                    : null,
                Name           = artifactBasicDetails.Name,
                ProjectId      = artifactBasicDetails.ProjectId,
                ParentId       = artifactBasicDetails.ParentId,
                OrderIndex     = artifactBasicDetails.OrderIndex,
                ItemTypeId     = artifactBasicDetails.ItemTypeId,
                Prefix         = artifactBasicDetails.Prefix,
                PredefinedType = (ItemTypePredefined)artifactBasicDetails.PrimitiveItemTypePredefined,
                Version        = artifactBasicDetails.VersionIndex != null && artifactBasicDetails.VersionIndex.Value <= 0 ? -1 : artifactBasicDetails.VersionIndex,
                VersionCount   = artifactBasicDetails.VersionsCount,
                IsDeleted      = artifactBasicDetails.DraftDeleted || artifactBasicDetails.LatestDeleted,
                HasChanges     = artifactBasicDetails.LockedByUserId != null && artifactBasicDetails.LockedByUserId.Value == userId ||
                                 artifactBasicDetails.HasDraftRelationships,
                LockedByUser = artifactBasicDetails.LockedByUserId != null
                    ? new UserGroup {
                    Id = artifactBasicDetails.LockedByUserId.Value, DisplayName = artifactBasicDetails.LockedByUserName
                }
                    : null,
                LockedDateTime = artifactBasicDetails.LockedByUserTime != null
                    ? (DateTime?)DateTime.SpecifyKind(artifactBasicDetails.LockedByUserTime.Value, DateTimeKind.Utc)
                    : null
            };

            if (artifactBasicDetails.DraftDeleted)
            {
                artifactInfo.DeletedByUser = artifactBasicDetails.UserId != null
                    ? new UserGroup {
                    Id = artifactBasicDetails.UserId.Value, DisplayName = artifactBasicDetails.UserName
                }
                    : null;
                artifactInfo.DeletedDateTime = artifactBasicDetails.LastSaveTimestamp != null
                    ? (DateTime?)DateTime.SpecifyKind(artifactBasicDetails.LastSaveTimestamp.Value, DateTimeKind.Utc)
                    : null;
            }
            else if (artifactBasicDetails.LatestDeleted)
            {
                artifactInfo.DeletedByUser = artifactBasicDetails.LatestDeletedByUserId != null
                    ? new UserGroup {
                    Id = artifactBasicDetails.LatestDeletedByUserId.Value, DisplayName = artifactBasicDetails.LatestDeletedByUserName
                }
                    : null;
                artifactInfo.DeletedDateTime = artifactBasicDetails.LatestDeletedByUserTime != null
                    ? (DateTime?)DateTime.SpecifyKind(artifactBasicDetails.LatestDeletedByUserTime.Value, DateTimeKind.Utc)
                    : null;
            }

            artifactInfo.Permissions = itemIdsPermissions[artifactBasicDetails.ArtifactId];

            if (baselineId != null)
            {
                var baselineRevisionId = await _itemInfoRepository.GetRevisionId(itemId, userId, null, baselineId.Value);

                var itemInfo = await _artifactPermissionsRepository.GetItemInfo(itemId, userId, false, baselineRevisionId);

                if (itemInfo == null)
                {
                    artifactInfo.IsNotExistsInBaseline = true;
                }

                artifactInfo.IsIncludedInBaseline = await IsArtifactInBaseline(artifactBasicDetails.ArtifactId, baselineId.Value, userId);
            }

            return(artifactInfo);
        }
        public async Task <RelationshipResultSet> GetRelationships(
            int artifactId,
            int userId,
            int?subArtifactId = null,
            bool addDrafts    = true,
            bool allLinks     = false,
            int?versionId     = null,
            int?baselineId    = null)
        {
            var revisionId = await _itemInfoRepository.GetRevisionId(artifactId, userId, versionId, baselineId);

            var itemId = subArtifactId ?? artifactId;
            var types  = new List <int> {
                (int)LinkType.Manual,
                (int)LinkType.Association,
                (int)LinkType.ActorInheritsFrom,
                (int)LinkType.DocumentReference
            };

            if (allLinks)
            {
                types.AddRange(new[] { (int)LinkType.ParentChild, (int)LinkType.Reuse });
            }

            if (baselineId != null)
            {
                addDrafts = false;
            }

            var results     = (await GetLinkInfo(itemId, userId, addDrafts, revisionId, types)).ToList();
            var manualLinks = results.Where(a => a.LinkType == LinkType.Manual).ToList();
            // filter out Parent/Child links between artifact and its subartifact if exist
            var excludeParentChildLinks = results.Where(link =>
                                                        link.LinkType == LinkType.ParentChild &&
                                                        ((link.SourceArtifactId != link.SourceItemId || link.DestinationArtifactId != link.DestinationItemId)) || ////internal links
                                                        (link.SourceItemId == link.SourceProjectId))                                                              ////to artifact's project
                                          .ToList();
            // get reuse links to to modify them separaratly.
            var reuseLinks = results.Where(a => a.LinkType == LinkType.Reuse).ToList();
            // get collection of other links exept exclude parent/child links and reuse links
            var otherLinks = results.Except(excludeParentChildLinks).Except(reuseLinks).Where(link => link.LinkType != LinkType.Manual).ToList();

            // modify reuse links by combining matching pais (source match destination on other) and add them back to coolection of otherlinks
            otherLinks.AddRange(UpdateReuseLinks(reuseLinks, itemId));

            var manualTraceRelationships = GetManualTraceRelationships(manualLinks, itemId);
            var otherTraceRelationships  = new List <Relationship>();

            foreach (var otherLink in otherLinks)
            {
                var          traceDirection = otherLink.SourceItemId == itemId ? TraceDirection.To : TraceDirection.From;
                Relationship relationship   = null;
                if (otherLink.LinkType == LinkType.ActorInheritsFrom)
                {
                    var itemInfo = await _artifactPermissionsRepository.GetItemInfo(otherLink.DestinationArtifactId, userId, addDrafts, revisionId);

                    if (itemInfo != null)
                    {
                        relationship = ComposeRelationship(otherLink, traceDirection);
                    }
                }
                else if (otherLink.LinkType == LinkType.Reuse)
                {
                    traceDirection = TraceDirection.TwoWay;
                    var itemInfo = await _artifactPermissionsRepository.GetItemInfo(otherLink.DestinationArtifactId, userId, addDrafts, revisionId);

                    if (itemInfo != null)
                    {
                        relationship = ComposeRelationship(otherLink, traceDirection);
                    }
                }
                else
                {
                    relationship = ComposeRelationship(otherLink, traceDirection);
                }
                if (relationship != null)
                {
                    otherTraceRelationships.Add(relationship);
                }
            }

            var distinctItemIds = new HashSet <int>();

            foreach (var result in results)
            {
                distinctItemIds.Add(result.SourceArtifactId);
                distinctItemIds.Add(result.DestinationArtifactId);
                distinctItemIds.Add(result.SourceItemId);
                distinctItemIds.Add(result.DestinationItemId);
                distinctItemIds.Add(result.SourceProjectId);
                distinctItemIds.Add(result.DestinationProjectId);
            }

            var itemDetailsDictionary = (await _itemInfoRepository.GetItemsDetails(userId, distinctItemIds, true, revisionId)).ToDictionary(a => a.HolderId);
            var itemLabelsDictionary  = (await _itemInfoRepository.GetItemsLabels(userId, distinctItemIds, true, revisionId)).ToDictionary(a => a.ItemId);

            PopulateRelationshipInfos(manualTraceRelationships, itemDetailsDictionary, itemLabelsDictionary);
            PopulateRelationshipInfos(otherTraceRelationships, itemDetailsDictionary, itemLabelsDictionary);
            return(new RelationshipResultSet
            {
                RevisionId = revisionId,
                ManualTraces = manualTraceRelationships,
                OtherTraces = otherTraceRelationships
            });
        }
        public async Task <FilesInfo> GetAttachmentsAndDocumentReferences(
            int artifactId,
            int userId,
            int?versionId     = null,
            int?subArtifactId = null,
            bool addDrafts    = true,
            int?baselineId    = null)
        {
            var itemId = artifactId;

            if (subArtifactId.HasValue)
            {
                itemId = subArtifactId.Value;
            }
            var revisionId = await _itemInfoRepository.GetRevisionId(artifactId, userId, versionId, baselineId);

            if (baselineId != null)
            {
                addDrafts = false;
            }

            var attachments           = (await GetAttachments(itemId, userId, revisionId, addDrafts)).ToList();
            var referencedArtifacts   = (await GetDocumentReferenceArtifacts(itemId, userId, revisionId, addDrafts)).ToList();
            var referencedArtifactIds = referencedArtifacts.Select(a => a.ArtifactId).Distinct().ToList();

            var documentReferenceArtifactInfos          = (await GetDocumentArtifactInfos(referencedArtifactIds, userId, revisionId, addDrafts)).ToList();
            var documentReferenceArtifactInfoDictionary = documentReferenceArtifactInfos.ToDictionary(a => a.ArtifactId);

            var distinctUsers      = attachments.Select(a => a.UserId).Union(referencedArtifacts.Select(b => b.UserId)).Distinct().ToList();
            var userInfoDictionary = (await _userRepository.GetUserInfos(distinctUsers)).ToDictionary(a => a.UserId);

            var referenceArtifactsToBeRemoved = new List <DocumentReference>();

            foreach (var attachment in attachments)
            {
                UserInfo userInfo;
                userInfoDictionary.TryGetValue(attachment.UserId, out userInfo);
                attachment.UserName     = userInfo.DisplayName;
                attachment.UploadedDate = attachment.UploadedDate == null ? (DateTime?)null : DateTime.SpecifyKind(attachment.UploadedDate.Value, DateTimeKind.Utc);
            }

            foreach (var referencedArtifact in referencedArtifacts)
            {
                UserInfo userInfo;
                userInfoDictionary.TryGetValue(referencedArtifact.UserId, out userInfo);
                LinkedArtifactInfo linkedArtifactInfo;
                documentReferenceArtifactInfoDictionary.TryGetValue(referencedArtifact.ArtifactId, out linkedArtifactInfo);
                if (linkedArtifactInfo != null)
                {
                    referencedArtifact.UserName       = userInfo.DisplayName;
                    referencedArtifact.ArtifactName   = linkedArtifactInfo.ArtifactName;
                    referencedArtifact.ItemTypePrefix = linkedArtifactInfo.ItemTypePrefix;
                    if (referencedArtifact.ReferencedDate != null)
                    {
                        referencedArtifact.ReferencedDate = DateTime.SpecifyKind(referencedArtifact.ReferencedDate.Value, DateTimeKind.Utc);
                    }
                }
                else
                {
                    // if the linked artifactInfo is not available, do not return this artifact references
                    referenceArtifactsToBeRemoved.Add(referencedArtifact);
                }
            }

            referencedArtifacts = referencedArtifacts.Except(referenceArtifactsToBeRemoved).ToList();

            var result = new FilesInfo(attachments, referencedArtifacts)
            {
                ArtifactId    = artifactId,
                SubartifactId = subArtifactId
            };

            return(result);
        }