Exemple #1
0
        public void Setup()
        {
            this.session           = new Mock <ISession>();
            this.assembler         = new Assembler(this.uri);
            this.permissionService = new Mock <IPermissionService>();
            this.session.Setup(x => x.PermissionService).Returns(this.permissionService.Object);
            this.thingDialogNavigationService = new Mock <IThingDialogNavigationService>();
            this.panelNavigationService       = new Mock <IPanelNavigationService>();
            this.dropinfo = new Mock <IDropInfo>();
            this.cache    = this.assembler.Cache;

            this.sitedir    = new SiteDirectory(Guid.NewGuid(), this.cache, this.uri);
            this.modelsetup = new EngineeringModelSetup(Guid.NewGuid(), this.cache, this.uri)
            {
                Name = "model"
            };
            this.iterationsetup = new IterationSetup(Guid.NewGuid(), this.cache, this.uri);
            this.person         = new Person(Guid.NewGuid(), this.cache, this.uri);
            this.domain         = new DomainOfExpertise(Guid.NewGuid(), this.cache, this.uri)
            {
                Name = "domain"
            };
            this.participant = new Participant(Guid.NewGuid(), this.cache, this.uri)
            {
                Person         = this.person,
                SelectedDomain = this.domain
            };

            this.sitedir.Model.Add(this.modelsetup);
            this.sitedir.Person.Add(this.person);
            this.sitedir.Domain.Add(this.domain);
            this.modelsetup.IterationSetup.Add(this.iterationsetup);
            this.modelsetup.Participant.Add(this.participant);

            this.model = new EngineeringModel(Guid.NewGuid(), this.cache, this.uri)
            {
                EngineeringModelSetup = this.modelsetup
            };
            this.iteration = new Iteration(Guid.NewGuid(), this.cache, this.uri)
            {
                IterationSetup = this.iterationsetup
            };
            this.model.Iteration.Add(this.iteration);

            this.session.Setup(x => x.RetrieveSiteDirectory()).Returns(this.sitedir);
            this.session.Setup(x => x.ActivePerson).Returns(this.person);
            this.session.Setup(x => x.OpenIterations).Returns(new Dictionary <Iteration, Tuple <DomainOfExpertise, Participant> >());
            this.session.Setup(x => x.Assembler).Returns(this.assembler);

            this.cache.TryAdd(new CacheKey(this.iteration.Iid, null), new Lazy <Thing>(() => this.iteration));
        }
        /// <summary>
        /// Retrieves all data necessary for the transfer of an iteration
        /// </summary>
        /// <param name="iteration">The <see cref="Iteration"/></param>
        /// <param name="siteDirectoryData">All SiteDirectory DTOs</param>
        /// <param name="zip">The zip file</param>
        /// <param name="siteDir">The <see cref="SiteDirectory"/> object</param>
        /// <returns>List of things relevant for a particular iteration</returns>
        private List <Thing> RetrieveIterationThings(CDP4Common.DTO.Iteration iteration, List <Thing> siteDirectoryData, ZipFile zip, CDP4Common.SiteDirectoryData.SiteDirectory siteDir)
        {
            var engineeringModelSetup =
                siteDir.Model.SingleOrDefault(x => x.IterationSetup.Any(y => y.IterationIid == iteration.Iid));

            if (engineeringModelSetup == null)
            {
                throw new ArgumentException("Could not locate the engineeringModel setup information");
            }

            // read engineeringmodel
            var engineeringModelFilePath = $"{engineeringModelSetup.EngineeringModelIid}.json";
            var engineeringModelZipEntry =
                zip.Entries.SingleOrDefault(x => x.FileName.EndsWith(engineeringModelFilePath));
            var returned = this.ReadInfoFromArchiveEntry(engineeringModelZipEntry, this.Credentials.Password);

            var iterationFilePath = $"{iteration.Iid}.json";
            var iterationZipEntry = zip.Entries.SingleOrDefault(x => x.FileName.EndsWith(iterationFilePath));

            returned.AddRange(this.ReadIterationArchiveEntry(iterationZipEntry, this.Credentials.Password));

            // use the loaded sitedirectory information to determine the required model reference data library
            var modelRdl = engineeringModelSetup.RequiredRdl.Single();

            // add the modelRdlDto to the returned collection to make sure it's content gets dereferenced
            if (returned.All(x => x.Iid != modelRdl.Iid))
            {
                var modelRdlDto = siteDirectoryData.Single(x => x.Iid == modelRdl.Iid);
                returned.Add(modelRdlDto);
            }

            // based on engineering model setup load rdl chain
            var modelRdlFilePath = $"{modelRdl.Iid}.json";
            var modelRdlZipEntry = zip.Entries.SingleOrDefault(x => x.FileName.EndsWith(modelRdlFilePath));
            var modelRdlItems    = this.ReadInfoFromArchiveEntry(modelRdlZipEntry, this.Credentials.Password);

            returned.AddRange(modelRdlItems);

            // load the reference data libraries as per the containment chain
            var requiredRdl = modelRdl.RequiredRdl;

            while (requiredRdl != null)
            {
                // add the rdlDto to the returned collection to make sure it's content gets dereferenced
                var requiredRdlDto = siteDirectoryData.Single(x => x.Iid == requiredRdl.Iid);
                returned.Add(requiredRdlDto);

                var siteRdlFilePath = $"{requiredRdl.Iid}.json";
                var siteRdlZipEntry = zip.Entries.SingleOrDefault(x => x.FileName.EndsWith(siteRdlFilePath));
                var siteRdlItems    = this.ReadInfoFromArchiveEntry(siteRdlZipEntry, this.Credentials.Password);
                returned.AddRange(siteRdlItems);

                // set the requiredRdl for the next iteration
                requiredRdl = requiredRdl.RequiredRdl;
            }

            return(returned);
        }
        /// <summary>
        /// Retrieves all data necessary for the transfer of a SRDL
        /// </summary>
        /// <param name="siteRdl">The <see cref="SiteReferenceDataLibrary"/></param>
        /// <param name="siteDirectoryData">All SiteDirectory DTOs</param>
        /// <param name="zip">The zip file</param>
        /// <param name="siteDir">The <see cref="SiteDirectory"/> object</param>
        /// <returns>List of things contained by the particular srdl</returns>
        private List <Thing> RetrieveSRDLThings(CDP4Common.DTO.SiteReferenceDataLibrary siteRdl, List <Thing> siteDirectoryData, ZipFile zip, CDP4Common.SiteDirectoryData.SiteDirectory siteDir)
        {
            var returned = new List <Thing>();

            var srdl = siteDir.SiteReferenceDataLibrary.FirstOrDefault(s => s.Iid.Equals(siteRdl.Iid));

            // load the reference data libraries as per the containment chain
            var requiredRdl = srdl;

            while (requiredRdl != null)
            {
                // add the rdlDto to the returned collection to make sure it's content gets dereferenced
                var requiredRdlDto = siteDirectoryData.Single(x => x.Iid == requiredRdl.Iid);
                returned.Add(requiredRdlDto);

                var siteRdlFilePath = $"{requiredRdl.Iid}.json";
                var siteRdlZipEntry = zip.Entries.SingleOrDefault(x => x.FileName.EndsWith(siteRdlFilePath));
                var siteRdlItems    = this.ReadInfoFromArchiveEntry(siteRdlZipEntry, this.Credentials.Password);
                returned.AddRange(siteRdlItems);

                // set the requiredRdl for the next iteration
                requiredRdl = requiredRdl.RequiredRdl;
            }

            return(returned);
        }
        /// <summary>
        /// Retrieves all data necessary for the transfer of a DomainOfExpertise
        /// </summary>
        /// <param name="domain">The <see cref="DomainOfExpertise"/></param>
        /// <param name="siteDirectoryData">All SiteDirectory DTOs</param>
        /// <param name="siteDir">The <see cref="SiteDirectory"/> object</param>
        /// <returns>List of things contained by the particular srdl</returns>
        private List <Thing> RetrieveDomainOfExpertiseThings(CDP4Common.DTO.DomainOfExpertise domain, List <Thing> siteDirectoryData, CDP4Common.SiteDirectoryData.SiteDirectory siteDir)
        {
            var returned = new List <Thing>();

            // wipe categories to avoide potential RDL irresolvable loop
            domain.Category.Clear();

            returned.Add(domain);

            foreach (var refThing in domain.Alias.ToList())
            {
                var thingDto = siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) as CDP4Common.DTO.Alias;

                if (thingDto != null)
                {
                    thingDto.ExcludedPerson.Clear();
                    thingDto.ExcludedDomain.Clear();
                    returned.Add(thingDto);
                }
                else
                {
                    domain.Alias.Remove(refThing);
                }
            }

            foreach (var refThing in domain.HyperLink.ToList())
            {
                var thingDto = siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) as CDP4Common.DTO.HyperLink;

                if (thingDto != null)
                {
                    thingDto.ExcludedPerson.Clear();
                    thingDto.ExcludedDomain.Clear();
                    returned.Add(thingDto);
                }
                else
                {
                    domain.HyperLink.Remove(refThing);
                }
            }

            domain.ExcludedPerson.Clear();
            domain.ExcludedDomain.Clear();

            foreach (var refThing in domain.Definition.ToList())
            {
                var thingDto = siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) as CDP4Common.DTO.Definition;

                if (thingDto != null)
                {
                    thingDto.ExcludedDomain.Clear();
                    thingDto.ExcludedPerson.Clear();

                    // remove citation due to possible irresolvable loop of references
                    thingDto.Citation.Clear();

                    returned.Add(thingDto);
                }
                else
                {
                    domain.ExcludedPerson.Remove(refThing);
                }
            }

            return(returned);
        }
        /// <summary>
        /// Write all the <see cref="Operation"/>s from all the <see cref="OperationContainer"/>s asynchronously.
        /// </summary>
        /// <param name="operationContainers">
        /// The provided <see cref="OperationContainer"/> to write
        /// </param>
        /// <param name="extensionFiles">
        /// The path to the files that need to be uploaded. If <paramref name="extensionFiles"/> is null, then no files are to be uploaded
        /// </param>
        /// <returns>
        /// A list of <see cref="Thing"/>s that has been created or updated since the last Read or Write operation.
        /// </returns>
        public override Task <IEnumerable <Thing> > Write(IEnumerable <OperationContainer> operationContainers, IEnumerable <string> extensionFiles = null)
        {
            this.ValidateOperationContainers(operationContainers);

            CDP4Common.SiteDirectoryData.SiteDirectory siteDirectory = null;
            var iterations = new HashSet <CDP4Common.EngineeringModelData.Iteration>();
            var siteReferenceDataLibraries  = new HashSet <CDP4Common.SiteDirectoryData.SiteReferenceDataLibrary>();
            var modelReferenceDataLibraries = new HashSet <CDP4Common.SiteDirectoryData.ModelReferenceDataLibrary>();
            var domainOfExpertises          = new HashSet <CDP4Common.SiteDirectoryData.DomainOfExpertise>();
            var persons                = new HashSet <CDP4Common.SiteDirectoryData.Person>();
            var personRoles            = new HashSet <CDP4Common.SiteDirectoryData.PersonRole>();
            var participantRoles       = new HashSet <CDP4Common.SiteDirectoryData.ParticipantRole>();
            var organizations          = new HashSet <CDP4Common.SiteDirectoryData.Organization>();
            var engineeringModelSetups = new HashSet <CDP4Common.SiteDirectoryData.EngineeringModelSetup>();
            var iterationSetups        = new HashSet <CDP4Common.SiteDirectoryData.IterationSetup>();

            foreach (var operationContainer in operationContainers)
            {
                var operation     = operationContainer.Operations.First(x => x.ModifiedThing is CDP4Common.DTO.Iteration);
                var iterationDto  = (CDP4Common.DTO.Iteration)operation.ModifiedThing;
                var iterationPoco = (CDP4Common.EngineeringModelData.Iteration)iterationDto.QuerySourceThing();

                JsonFileDalUtils.AddIteration(iterationPoco, ref iterations);

                var iterationSetup = iterationPoco.IterationSetup;
                JsonFileDalUtils.AddIterationSetup(iterationSetup, ref iterationSetups);

                var iterationRequiredRls = iterationPoco.RequiredRdls;
                JsonFileDalUtils.AddReferenceDataLibraries(iterationRequiredRls, ref siteReferenceDataLibraries, ref modelReferenceDataLibraries);

                var engineeringModelSetup = (CDP4Common.SiteDirectoryData.EngineeringModelSetup)iterationSetup.Container;
                JsonFileDalUtils.AddEngineeringModelSetup(engineeringModelSetup, ref engineeringModelSetups);

                if (siteDirectory == null)
                {
                    siteDirectory = (CDP4Common.SiteDirectoryData.SiteDirectory)engineeringModelSetup.Container;
                }

                // add the domains-of-expertise that are to be included in the File
                JsonFileDalUtils.AddDomainsOfExpertise(engineeringModelSetup, ref domainOfExpertises);

                // add the Persons that are to be included in the File
                JsonFileDalUtils.AddPersons(engineeringModelSetup, ref persons, ref personRoles, ref participantRoles, ref organizations);

                // add organizations that are referrenced by ReferencedSource
                JsonFileDalUtils.AddOrganizations(iterationRequiredRls, ref organizations);
            }

            var path = this.Session.Credentials.Uri.LocalPath;

            var prunedSiteDirectoryDtos = JsonFileDalUtils.CreateSiteDirectoryAndPrunedContainedThingDtos(
                siteDirectory,
                siteReferenceDataLibraries,
                domainOfExpertises,
                persons,
                personRoles,
                participantRoles,
                organizations,
                engineeringModelSetups,
                iterationSetups);

            var activePerson = JsonFileDalUtils.QueryActivePerson(this.Session.Credentials.UserName, siteDirectory);

            var exchangeFileHeader = this.FileHeader as ExchangeFileHeader ?? JsonFileDalUtils.CreateExchangeFileHeader(activePerson);

            try
            {
                using (var zipFile = new ZipFile())
                {
                    zipFile.Password = this.Session.Credentials.Password;

                    this.WriteHeaderToZipFile(exchangeFileHeader, zipFile, path);

                    this.WriteSiteDirectoryToZipFile(prunedSiteDirectoryDtos, zipFile, path);

                    this.WriteSiteReferenceDataLibraryToZipFile(siteReferenceDataLibraries, zipFile, path);

                    this.WriteModelReferenceDataLibraryToZipFile(modelReferenceDataLibraries, zipFile, path);

                    this.WriteIterationsToZipFile(iterations, zipFile, path);

                    this.WriteExtensionFilesToZipFile(extensionFiles, zipFile, path);
                }

                Logger.Info("Successfully exported the open session {1} to {0}.", path, this.Session.Credentials.Uri);
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to export the open session to {0}. Error: {1}", path, ex.Message);
            }

            return(Task.FromResult(Enumerable.Empty <Thing>()));
        }