private AppendedResults Transform(ArchivedVisit archivedVisit)
        {
            var appender = new FieldDataResultsAppender
            {
                Client          = Client,
                LocationCache   = LocationCache,
                LocationAliases = Context.LocationAliases,
                Log             = Log
            };

            var mapper = new ArchivedVisitMapper
            {
                Appender            = appender,
                ReferencePointCache = ReferencePointCache,
                ParameterIdLookup   = ParameterIdLookup,
                MethodLookup        = MethodLookup
            };

            var visit = mapper.Map(archivedVisit);

            return(new AppendedResults
            {
                FrameworkAssemblyQualifiedName = typeof(IFieldDataPlugin).AssemblyQualifiedName,
                PluginAssemblyQualifiedTypeName = mapper.GetJsonPluginAQFN(),
                AppendedVisits = new List <FieldVisitInfo>
                {
                    visit
                }
            });
        }
        private void ExportVisitAndAttachments(string visitPath, string zipPath, ArchivedVisit archivedVisit)
        {
            using (var stream = File.OpenWrite(zipPath))
                using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
                {
                    var rootJsonEntry = zipArchive.CreateEntry(Path.GetFileName(visitPath), CompressionLevel.Fastest);

                    using (var writer = new StreamWriter(rootJsonEntry.Open()))
                    {
                        writer.Write(TransformToJson(archivedVisit));
                    }

                    var attachmentCount = 0;
                    foreach (var attachment in archivedVisit.Activities.Attachments)
                    {
                        ++attachmentCount;
                        var attachmentEntry =
                            zipArchive.CreateEntry($"Attachment{attachmentCount}/{Path.GetFileName(attachment.FileName)}");

                        var contentBytes = DownloadAttachmentContent(attachmentEntry.FullName, attachment);

                        using (var writer = new BinaryWriter(attachmentEntry.Open()))
                        {
                            writer.Write(contentBytes);
                        }
                    }
                }
        }
        private void ExportVisit(string visitPath, string zipPath, ArchivedVisit archivedVisit)
        {
            File.Delete(visitPath);
            File.Delete(zipPath);

            if (!archivedVisit.Activities.Attachments.Any())
            {
                Log.Info($"Saving '{visitPath}' ...");

                File.WriteAllText(visitPath, TransformToJson(archivedVisit));
                return;
            }

            Log.Info($"Saving '{zipPath}' ...");

            var deleteZipOnCleanup = true;

            try
            {
                ExportVisitAndAttachments(visitPath, zipPath, archivedVisit);

                deleteZipOnCleanup = false;
            }
            finally
            {
                if (deleteZipOnCleanup)
                {
                    File.Delete(zipPath);
                }
            }
        }
        public FieldVisitInfo Map(ArchivedVisit archivedVisit)
        {
            VisitIdentifier = $"{archivedVisit.Summary.StartTime:O}@{archivedVisit.Summary.LocationIdentifier}";

            LocationInfo = Appender.GetLocationByIdentifier(archivedVisit.Summary.LocationIdentifier);
            var visit = Appender.AddFieldVisit(LocationInfo, Map(archivedVisit.Summary));

            MapActivities(visit, archivedVisit.Activities);

            return(visit);
        }
 private AppendedResults Transform(ArchivedVisit archivedVisit)
 {
     return(new AppendedResults
     {
         FrameworkAssemblyQualifiedName = typeof(IFieldDataPlugin).AssemblyQualifiedName,
         PluginAssemblyQualifiedTypeName = Mapper.GetJsonPluginAQFN(),
         AppendedVisits = new List <FieldVisitInfo>
         {
             Mapper.Map(archivedVisit)
         }
     });
 }
        private string TransformToJson(ArchivedVisit archivedVisit)
        {
            var json = Transform(archivedVisit).ToJson();

            if (Context.ExportUtcOverride.HasValue)
            {
                var adjustedUtcOffset = FormatUtcOffset(Context.ExportUtcOverride.Value);

                json = DateTimeOffsetRegex.Replace(json, match => $"\"{match.Groups["datetime"].Value}{adjustedUtcOffset}\"");
            }

            return(json.IndentJson());
        }
        private void ExportVisit(string locationPath, FieldVisitDescription fieldVisitDescription)
        {
            var visitPath = Path.Combine(locationPath, FileProcessor.SanitizeFilename($"{fieldVisitDescription.LocationIdentifier}@{fieldVisitDescription.StartTime:yyyy-MM-dd_HH_MM}.json"));
            var zipPath   = Path.ChangeExtension(visitPath, ".zip");

            var targetPath = File.Exists(zipPath)
                ? zipPath
                : visitPath;

            if (!Context.ExportOverwrite && File.Exists(targetPath))
            {
                Log.Info($"Skipping existing '{targetPath}'");
                ++SkipCount;
                return;
            }

            var archivedVisit = new ArchivedVisit
            {
                Summary    = fieldVisitDescription,
                Activities = Client.Publish.Get(new FieldVisitDataServiceRequest
                {
                    FieldVisitIdentifier             = fieldVisitDescription.Identifier,
                    IncludeNodeDetails               = true,
                    IncludeInvalidActivities         = true,
                    IncludeCrossSectionSurveyProfile = true,
                    IncludeVerticals = true
                })
            };

            try
            {
                ExportVisit(visitPath, zipPath, archivedVisit);

                ++VisitCount;
            }
            catch (Exception exception)
            {
                ++ErrorCount;

                var errorPath = Path.ChangeExtension(visitPath, ".error.json");

                File.WriteAllText(errorPath, archivedVisit.ToJson().IndentJson());

                Log.Error(exception is ExpectedException
                    ? $"'{visitPath}': {exception.Message}"
                    : $"'{visitPath}': {exception.Message}\n{exception.StackTrace}");
            }
        }
Beispiel #8
0
        private void ArchiveExistingVisit(FieldVisitDescription visit)
        {
            var archivedVisit = new ArchivedVisit
            {
                Summary    = visit,
                Activities = Client.Publish.Get(new FieldVisitDataServiceRequest
                {
                    FieldVisitIdentifier             = visit.Identifier,
                    IncludeNodeDetails               = true,
                    IncludeInvalidActivities         = true,
                    IncludeCrossSectionSurveyProfile = true,
                    IncludeVerticals = true
                })
            };

            CreateTargetFolder(ArchivedFolder);

            var archiveFilenameBase = Path.Combine(ArchivedFolder, $"{visit.Identifier}_{visit.StartTime?.Date:yyyy-MM-dd}_{visit.LocationIdentifier}");

            Log.Info($"Archiving existing visit '{archiveFilenameBase}'.json");
            File.WriteAllText(archiveFilenameBase + ".json", archivedVisit.ToJson().IndentJson());

            var publishClient = Client.Publish as ServiceClientBase;

            if (publishClient == null)
            {
                return;
            }

            foreach (var attachment in archivedVisit.Activities.Attachments)
            {
                var attachmentUrl      = $"{publishClient.BaseUri}/{attachment.Url}";
                var attachmentFilename = $"{archiveFilenameBase}_{attachment.FileName}";

                Log.Info($"Archiving attachment '{attachmentFilename}' from {attachmentUrl}");
                File.WriteAllBytes(
                    attachmentFilename,
                    attachmentUrl.GetBytesFromUrl(requestFilter: SetAuthenticationHeaders));
            }
        }