Ejemplo n.º 1
0
        private UploadContext ParseLocalFile(string path, byte[] fileBytes, string locationIdentifier = null)
        {
            var appender = new FieldDataResultsAppender
            {
                Client          = Client,
                LocationCache   = LocationCache,
                LocationAliases = Context.LocationAliases,
                Log             = Log
            };

            foreach (var loadedPlugin in LoadedPlugins)
            {
                appender.SettingsFunc = () => GetPluginSettings(loadedPlugin);

                var pluginName = PluginLoader.GetPluginNameAndVersion(loadedPlugin.Plugin);

                try
                {
                    var resultWithAttachments = new ZipLoader
                    {
                        Plugin       = loadedPlugin.Plugin,
                        Appender     = appender,
                        Logger       = Log,
                        LocationInfo = string.IsNullOrEmpty(locationIdentifier)
                                ? null
                                : appender.GetLocationByIdentifier(locationIdentifier)
                    }
                    .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 = loadedPlugin.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.");
        }