/// <summary>
 /// Write the header file to the zip export archive.
 /// </summary>
 /// <param name="echExchangeFileHeader">
 /// The <see cref="ExchangeFileHeader"/> that is to be written to the <see cref="zipFile"/>
 /// </param>
 /// <param name="zipFile">
 /// The zip archive instance to add the information to.
 /// </param>
 /// <param name="filePath">
 /// The path of the file.
 /// </param>
 private void WriteHeaderToZipFile(ExchangeFileHeader echExchangeFileHeader, ZipFile zipFile, string filePath)
 {
     using (var memoryStream = new MemoryStream())
     {
         this.JsonSerializer.SerializeToStream(echExchangeFileHeader, memoryStream);
         using (var outputStream = new MemoryStream(memoryStream.ToArray()))
         {
             var zipEntry = zipFile.AddEntry("Header.json", outputStream);
             zipEntry.Comment = "The Header for this file based source";
             zipFile.Save(filePath);
         }
     }
 }
        /// <summary>
        /// Factory method that creates a <see cref="ExchangeFileHeader"/> based on the provided <see cref="person"/>
        /// </summary>
        /// <param name="person">
        /// The <see cref="Person"/> that is used to create the <see cref="ExchangeFileHeader"/>
        /// </param>
        /// <param name="transaction">
        /// The current transaction to the database.
        /// </param>
        /// <param name="authorizedContext">
        /// The security context of the container instance.
        /// </param>
        /// <param name="partition">
        /// The database partition (schema) where the requested resource is stored.
        /// </param>
        /// <returns>
        /// An instance of <see cref="ExchangeFileHeader"/>
        /// </returns>
        private ExchangeFileHeader CreateExchangeFileHeader(
            Person person,
            NpgsqlTransaction transaction,
            RequestSecurityContext authorizedContext,
            string partition)
        {
            EmailAddress email = null;

            if (person.DefaultEmailAddress != null)
            {
                email = this.EmailAddressService.GetShallow(
                    transaction,
                    partition,
                    new List <Guid> {
                    (Guid)person.DefaultEmailAddress
                },
                    authorizedContext).OfType <EmailAddress>().ToList()[0];
            }

            var exchangeFileInitiator =
                new ExchangeFileInitiator
            {
                Iid       = person.Iid,
                GivenName = person.GivenName,
                Surname   = person.Surname,
                Email     = email != null ? email.Value : string.Empty
            };

            Organization organizationDto = null;

            if (person.Organization != null)
            {
                organizationDto = this.OrganizationService
                                  .GetShallow(transaction, partition, new List <Guid> {
                    (Guid)person.Organization
                }, authorizedContext)
                                  .OfType <Organization>().ToList()[0];
            }

            var organization = organizationDto != null
                                   ? new OrganizationInfo
            {
                Iid  = organizationDto.Iid,
                Name = organizationDto.Name,
                Site = null,
                Unit = !string.IsNullOrEmpty(person.OrganizationalUnit)
                                                        ? person.OrganizationalUnit
                                                        : null
            }
                                   : null;

            var exchangeFileHeader =
                new ExchangeFileHeader
            {
                DataModelVersion    = "2.4.1",
                Remark              = ExchangeHeaderRemark,
                Copyright           = ExchangeHeaderCopyright,
                Extensions          = null,
                CreatorPerson       = exchangeFileInitiator,
                CreatorOrganization = organization
            };

            return(exchangeFileHeader);
        }