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 Validate()
        {
            if (!Directory.Exists(Context.ExportFolder))
            {
                throw new ExpectedException($"Export folder '{Context.ExportFolder}' does not exist.");
            }

            var appender = new FieldDataResultsAppender
            {
                Client          = Client,
                LocationCache   = LocationCache,
                LocationAliases = Context.LocationAliases,
                Log             = Log
            };

            Mapper = new ArchivedVisitMapper
            {
                Appender            = appender,
                Plugins             = Plugins,
                ReferencePointCache = ReferencePointCache,
                ParameterIdLookup   = ParameterIdLookup,
                MethodLookup        = MethodLookup
            };
        }
예제 #3
0
        private UploadContext ParseLocalFile(string path)
        {
            var fileBytes = LoadFileBytes(path);

            var appender = new FieldDataResultsAppender
            {
                Client          = Client,
                LocationCache   = LocationCache,
                LocationAliases = Context.LocationAliases,
                Log             = Log
            };

            foreach (var plugin in Plugins)
            {
                appender.SettingsFunc = () => GetPluginSettings(plugin);

                var pluginName = PluginLoader.GetPluginNameAndVersion(plugin);

                try
                {
                    var resultWithAttachments = new ZipLoader
                    {
                        Plugin       = plugin,
                        Appender     = appender,
                        Logger       = Log,
                        LocationInfo = null
                    }
                    .ParseFile(fileBytes);

                    if (resultWithAttachments.Result.Status == ParseFileStatus.CannotParse)
                    {
                        continue;
                    }

                    if (resultWithAttachments.Result.Status != ParseFileStatus.SuccessfullyParsedAndDataValid)
                    {
                        throw new ArgumentException(
                                  $"Error parsing '{path}' with {pluginName}: {resultWithAttachments.Result.ErrorMessage}");
                    }

                    if (!appender.AppendedResults.AppendedVisits.Any())
                    {
                        throw new ArgumentException($"{pluginName} did not parse any field visits.");
                    }

                    var attachmentCount = resultWithAttachments.Attachments?.Count ?? 0;

                    if (appender.AppendedResults.AppendedVisits.Count > 1 && attachmentCount > 0)
                    {
                        throw new ArgumentException($"Only single-visit data files can be uploaded with attachments.");
                    }

                    Log.Info(
                        $"{pluginName} parsed '{path}' with {appender.AppendedResults.AppendedVisits.Count} visits: {string.Join(", ", appender.AppendedResults.AppendedVisits.Take(10).Select(v => v.FieldVisitIdentifier))}");

                    appender.AppendedResults.PluginAssemblyQualifiedTypeName = plugin.GetType().AssemblyQualifiedName;

                    return(new UploadContext
                    {
                        Path = path,
                        AppendedResults = appender.AppendedResults,
                        Attachments = resultWithAttachments.Attachments
                    });
                }
                catch (Exception e)
                {
                    Log.Warn($"{pluginName} skipping '{path}': {e.Message}");
                }
            }

            throw new ArgumentException($"'{path}' was not parsed by any plugin.");
        }