/// <summary>
        /// Instantiate and deserialize the properties of a <paramref name="FileRevision"/>
        /// </summary>
        /// <param name="jObject">The <see cref="JObject"/> containing the data</param>
        /// <returns>The <see cref="FileRevision"/> to instantiate</returns>
        public static CDP4Common.DTO.FileRevision FromJsonObject(JObject jObject)
        {
            var iid            = jObject["iid"].ToObject <Guid>();
            var revisionNumber = jObject["revisionNumber"].IsNullOrEmpty() ? 0 : jObject["revisionNumber"].ToObject <int>();
            var fileRevision   = new CDP4Common.DTO.FileRevision(iid, revisionNumber);

            if (!jObject["containingFolder"].IsNullOrEmpty())
            {
                fileRevision.ContainingFolder = jObject["containingFolder"].ToObject <Guid?>();
            }

            if (!jObject["contentHash"].IsNullOrEmpty())
            {
                fileRevision.ContentHash = jObject["contentHash"].ToObject <string>();
            }

            if (!jObject["createdOn"].IsNullOrEmpty())
            {
                fileRevision.CreatedOn = jObject["createdOn"].ToObject <DateTime>();
            }

            if (!jObject["creator"].IsNullOrEmpty())
            {
                fileRevision.Creator = jObject["creator"].ToObject <Guid>();
            }

            if (!jObject["excludedDomain"].IsNullOrEmpty())
            {
                fileRevision.ExcludedDomain.AddRange(jObject["excludedDomain"].ToObject <IEnumerable <Guid> >());
            }

            if (!jObject["excludedPerson"].IsNullOrEmpty())
            {
                fileRevision.ExcludedPerson.AddRange(jObject["excludedPerson"].ToObject <IEnumerable <Guid> >());
            }

            if (!jObject["fileType"].IsNullOrEmpty())
            {
                fileRevision.FileType.AddRange(jObject["fileType"].ToOrderedItemCollection());
            }

            if (!jObject["modifiedOn"].IsNullOrEmpty())
            {
                fileRevision.ModifiedOn = jObject["modifiedOn"].ToObject <DateTime>();
            }

            if (!jObject["name"].IsNullOrEmpty())
            {
                fileRevision.Name = jObject["name"].ToObject <string>();
            }

            if (!jObject["thingPreference"].IsNullOrEmpty())
            {
                fileRevision.ThingPreference = jObject["thingPreference"].ToObject <string>();
            }

            return(fileRevision);
        }
        /// <summary>
        /// Finds the <see cref="CDP4Common.DTO.File"/>'s Iid property from <see cref="Dto"/>s coming from the data-source, or from the Cache
        /// </summary>
        /// <param name="fileRevision">The <see cref="FileRevision"/></param>
        /// <param name="dtoThings">List of all &lt;see cref="Dto"/&gt;s coming from the data-source</param>
        /// <param name="guid">If found, this contains the <see cref="CDP4Common.DTO.File"/>'s Iid</param>
        /// <returns>True if found, otherwise false.</returns>
        /// <remarks>
        /// This is part of a temporary solution of dealing with <see cref="Folder"/>, <see cref="File"/> and <see cref="FileRevision"/> <see cref="Dto"/>s.
        /// </remarks>
        private bool TryGetFileIid(CDP4Common.DTO.FileRevision fileRevision, IReadOnlyList <Dto> dtoThings, out Guid guid)
        {
            guid = Guid.Empty;

            if (dtoThings.Where(x => x.ClassKind == ClassKind.File)
                .Cast <CDP4Common.DTO.File>()
                .SingleOrDefault(x => x.FileRevision.Any(y => y == fileRevision.Iid)) is CDP4Common.DTO.File file)
            {
                guid = file.Iid;
                return(true);
            }

            if (this.Cache.Values.Where(x => x.Value.ClassKind == ClassKind.File)
                .Select(x => x.Value)
                .Cast <File>()
                .SingleOrDefault(x => x.FileRevision.Any(y => y.Iid == fileRevision.Iid)) is File cachedFile)
            {
                guid = cachedFile.Iid;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        public async Task VerifyThatFileCanBeUploaded()
        {
            this.dal = new WspDal {
                Session = this.session
            };

            var filename  = @"TestData\testfile.pdf";
            var directory = TestContext.CurrentContext.TestDirectory;
            var filepath  = Path.Combine(directory, filename);
            var files     = new List <string> {
                filepath
            };

            var contentHash = "F73747371CFD9473C19A0A7F99BCAB008474C4CA";
            var uri         = new Uri("https://cdp4services-test.cdp4.org");

            this.credentials = new Credentials("admin", "pass", uri);

            var returned = await this.dal.Open(this.credentials, this.cancelationTokenSource.Token);

            var engineeringModeliid  = Guid.Parse("9ec982e4-ef72-4953-aa85-b158a95d8d56");
            var iterationiid         = Guid.Parse("e163c5ad-f32b-4387-b805-f4b34600bc2c");
            var domainFileStoreIid   = Guid.Parse("da7dddaa-02aa-4897-9935-e8d66c811a96");
            var fileIid              = Guid.NewGuid();
            var fileRevisionIid      = Guid.NewGuid();
            var domainOfExpertiseIid = Guid.Parse("0e92edde-fdff-41db-9b1d-f2e484f12535");
            var fileTypeIid          = Guid.Parse("b16894e4-acb5-4e81-a118-16c00eb86d8f"); //PDF
            var participantIid       = Guid.Parse("284334dd-e8e5-42d6-bc8a-715c507a7f02");

            var originalDomainFileStore = new CDP4Common.DTO.DomainFileStore(domainFileStoreIid, 0);

            originalDomainFileStore.AddContainer(ClassKind.Iteration, iterationiid);
            originalDomainFileStore.AddContainer(ClassKind.EngineeringModel, engineeringModeliid);

            var modifiedDomainFileStore = new CDP4Common.DTO.DomainFileStore(domainFileStoreIid, 0);

            modifiedDomainFileStore.File.Add(fileIid);
            modifiedDomainFileStore.AddContainer(ClassKind.Iteration, iterationiid);
            modifiedDomainFileStore.AddContainer(ClassKind.EngineeringModel, engineeringModeliid);

            var file = new CDP4Common.DTO.File(fileIid, 0)
            {
                Owner = domainOfExpertiseIid
            };

            file.FileRevision.Add(fileRevisionIid);
            file.AddContainer(ClassKind.DomainFileStore, domainFileStoreIid);
            file.AddContainer(ClassKind.Iteration, iterationiid);
            file.AddContainer(ClassKind.EngineeringModel, engineeringModeliid);

            var fileRevision = new CDP4Common.DTO.FileRevision(fileRevisionIid, 0);

            fileRevision.Name        = "testfile";
            fileRevision.ContentHash = contentHash;
            fileRevision.FileType.Add(new OrderedItem()
            {
                K = 1, V = fileTypeIid
            });
            fileRevision.Creator = participantIid;
            fileRevision.AddContainer(ClassKind.File, fileIid);
            fileRevision.AddContainer(ClassKind.DomainFileStore, domainFileStoreIid);
            fileRevision.AddContainer(ClassKind.Iteration, iterationiid);
            fileRevision.AddContainer(ClassKind.EngineeringModel, engineeringModeliid);

            var context            = $"/EngineeringModel/{engineeringModeliid}/iteration/{iterationiid}";
            var operationContainer = new OperationContainer(context);

            var updateCommonFileStoreOperation = new Operation(originalDomainFileStore, modifiedDomainFileStore, OperationKind.Update);

            operationContainer.AddOperation(updateCommonFileStoreOperation);

            var createFileOperation = new Operation(null, file, OperationKind.Create);

            operationContainer.AddOperation(createFileOperation);

            var createFileRevisionOperation = new Operation(null, fileRevision, OperationKind.Create);

            operationContainer.AddOperation(createFileRevisionOperation);

            Assert.DoesNotThrowAsync(async() => await dal.Write(operationContainer, files));
        }