Пример #1
0
        public bool Import()
        {
            int             elementCounter  = 0;
            int             samplingRecords = 0;
            bool            proceed         = false;
            fadSubgridStyle subgridStyle    = fadSubgridStyle.SubgridStyleNone;
            fadUTMZone      zone            = fadUTMZone.utmZone_Undefined;
            XmlTextReader   xmlReader       = new XmlTextReader(XMLFilename);
            string          gearClassGuid   = "";
            int             mapCount        = 0;

            VesselType           vesselType         = VesselType.NotDetermined;
            List <FishingGround> listFishingGrounds = new List <FishingGround>();
            Sampling             sampling           = new Sampling();
            FishingVessel        fishingVessel      = new FishingVessel(VesselType.NotDetermined);
            string gearVariationGuid = "";
            string refCode           = "";
            string usageCodeID       = "";

            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    switch (xmlReader.Name)
                    {
                    case "FishCatchMonitoring":
                        SamplingEventArgs sev = new SamplingEventArgs(SamplingRecordStatus.StartImport);
                        sev.SamplingToFromXML = this;
                        sev.TargetAreaName    = TargetAreaName;
                        OnExportSamplingStatus?.Invoke(null, sev);
                        string targetAreaCode = xmlReader.GetAttribute("TargetAreaCode");
                        subgridStyle = (fadSubgridStyle)Enum.Parse(typeof(fadSubgridStyle), xmlReader.GetAttribute("SubGridStyle"));
                        zone         = (fadUTMZone)Enum.Parse(typeof(fadUTMZone), xmlReader.GetAttribute("UTMZone"));
                        if (NewTargetArea &&
                            !TargetArea.AddNewTargetArea(TargetAreaName, TargetAreaGuid, targetAreaCode, subgridStyle, zone))
                        {
                        }
                        else
                        {
                            TargetArea = new TargetArea(TargetAreaGuid);
                            TargetArea.TargetAreaName   = TargetAreaName;
                            TargetArea.TargetAreaLetter = targetAreaCode;
                        }
                        break;

                    case "FishingGroundMaps":
                        break;

                    case "FishingGroundMap":
                        string ulg         = xmlReader.GetAttribute("ulg");
                        string lrg         = xmlReader.GetAttribute("lrg");
                        string description = xmlReader.GetAttribute("description");
                        FishingGrid.AddGrid25Map(TargetAreaGuid, subgridStyle, zone, ulg, lrg, description, mapCount == 0);
                        mapCount++;
                        break;

                    case "Enumerators":
                        break;

                    case "Enumerator":
                        string   enumeratorGUID = xmlReader.GetAttribute("EnumeratorGuid");
                        string   enumeratorName = xmlReader.GetAttribute("Name");
                        DateTime hiredate       = DateTime.Parse(xmlReader.GetAttribute("DateHired"));
                        bool     active         = bool.Parse(xmlReader.GetAttribute("Active"));
                        Enumerators.SaveNewTargetAreaEnumerator(TargetAreaGuid, enumeratorName, hiredate, active, enumeratorGUID);
                        break;

                    case "LandingSites":
                        break;

                    case "LandingSite":
                        string landingsiteGuid = xmlReader.GetAttribute("LandingSiteGuid");
                        string landingsiteName = xmlReader.GetAttribute("LandingSiteName");
                        int    munNo           = int.Parse(xmlReader.GetAttribute("MunicipalityNumber"));
                        string sx = xmlReader.GetAttribute("x_coordinate");
                        string sy = xmlReader.GetAttribute("y_coordinate");
                        double?x  = sx.Length == 0 ? null : (double?)double.Parse(sx);
                        double?y  = sy.Length == 0 ? null : (double?)double.Parse(sy);
                        Landingsite.AddNewLandingSite(TargetAreaGuid, landingsiteGuid, landingsiteName, munNo, x, y);
                        break;

                    case "FishingGears":
                        SamplingEventArgs sve = new SamplingEventArgs(SamplingRecordStatus.BeginFishingGears);
                        sve.RecordCount = int.Parse(xmlReader.GetAttribute("GearVariationsCount"));
                        OnExportSamplingStatus?.Invoke(null, sve);
                        break;

                    case "GearLocalNames":
                        sve             = new SamplingEventArgs(SamplingRecordStatus.BeginGearLocalNames);
                        sve.RecordCount = int.Parse(xmlReader.GetAttribute("LocalNameCount"));
                        OnExportSamplingStatus?.Invoke(null, sve);
                        break;

                    case "GearLocalName":

                        string gearLocalName = xmlReader.GetAttribute("LocalName");

                        NewFisheryObjectName nfo = new NewFisheryObjectName(gearLocalName, FisheryObjectNameType.GearLocalName);
                        Gears.SaveNewLocalName(nfo, xmlReader.GetAttribute("LocalNameGuid"));

                        sve = new SamplingEventArgs(SamplingRecordStatus.GearLocalName);
                        sve.GearLocalName = gearLocalName;
                        OnExportSamplingStatus?.Invoke(null, sve);

                        break;

                    case "GearClasses":

                        break;

                    case "GearClass":
                        gearClassGuid = xmlReader.GetAttribute("ClassGuid");
                        string gearClassName   = xmlReader.GetAttribute("ClassName");
                        string gearClassLetter = xmlReader.GetAttribute("ClassLetter");
                        GearClass.AddGearClass(gearClassName, gearClassLetter, gearClassGuid);
                        break;

                    case "GearVariation":
                        string gearVariationName = xmlReader.GetAttribute("name");
                        gearVariationGuid = xmlReader.GetAttribute("guid");
                        Gears.AddGearVariation(gearClassGuid, gearVariationName, gearVariationGuid);

                        sve = new SamplingEventArgs(SamplingRecordStatus.FishingGears);
                        sve.GearVariationName = gearVariationName;
                        OnExportSamplingStatus?.Invoke(null, sve);
                        break;

                    case "Specifications":
                        break;

                    case "Specification":
                        GearSpecification gs = new GearSpecification(xmlReader.GetAttribute("ElementName"),
                                                                     xmlReader.GetAttribute("ElementType"),
                                                                     xmlReader.GetAttribute("SpecRowGUID"),
                                                                     int.Parse(xmlReader.GetAttribute("Sequence")));
                        gs.Notes = xmlReader.GetAttribute("Description");
                        ManageGearSpecsClass.SaveGearSpec(gearVariationGuid, gs);
                        break;

                    case "GearRefCodes":
                        break;

                    case "GearRefCode":
                        refCode = xmlReader.GetAttribute("RefCode");
                        Gears.SaveNewGearReferenceCode(refCode, gearVariationGuid, bool.Parse(xmlReader.GetAttribute("IsSubVariation").ToString()));
                        break;

                    case "RefCodeUsage":
                        usageCodeID = xmlReader.GetAttribute("UsageRowID");
                        if (usageCodeID.Length > 0)
                        {
                            var addUsageResult = Gears.AddGearCodeUsageTargetArea(refCode, TargetArea.TargetAreaGuid, usageCodeID);
                            usageCodeID = addUsageResult.NewRow;
                        }
                        break;

                    case "LocalNamesInUse":
                        break;

                    case "LocalNameUsed":
                        if (usageCodeID.Length > 0)
                        {
                            Gears.AddUsageLocalName(usageCodeID, xmlReader.GetAttribute("LocalNameGuid"), xmlReader.GetAttribute("UsageGuid"));
                        }
                        break;

                    case "Taxa":
                        break;

                    case "TaxaItem":
                        int    taxaNumber = int.Parse(xmlReader.GetAttribute("TaxaNo"));
                        string taxaName   = xmlReader.GetAttribute("Taxa");
                        TaxaCategory.AddTaxa(taxaNumber, taxaName);
                        break;

                    case "AllCatchNames":
                        sve             = new SamplingEventArgs(SamplingRecordStatus.BeginCatchNames);
                        sve.RecordCount = int.Parse(xmlReader.GetAttribute("NamesOfCatchCount"));
                        OnExportSamplingStatus?.Invoke(null, sve);
                        break;

                    case "CatchName":
                        string nameGuid = xmlReader.GetAttribute("NameGuid");
                        string name1    = xmlReader.GetAttribute("Name1");
                        string name2    = xmlReader.GetAttribute("Name2");

                        int?taxaNo = null;
                        if (int.TryParse(xmlReader.GetAttribute("TaxaNumber"), out int tn))
                        {
                            taxaNo = tn;
                        }

                        string identification = xmlReader.GetAttribute("Identification");
                        bool   inFishbase     = bool.Parse(xmlReader.GetAttribute("IsListedInFishbase"));

                        int?fbNumber = null;
                        if (int.TryParse(xmlReader.GetAttribute("FBSpeciesNumber"), out int v))
                        {
                            fbNumber = v;
                        }
                        bool success = CatchName.AddCatchName(nameGuid, identification, name1, name2, taxaNo, inFishbase, fbNumber);
                        sve           = new SamplingEventArgs(SamplingRecordStatus.CatchNames);
                        sve.CatchName = $"{name1} {name2}";
                        OnExportSamplingStatus?.Invoke(null, sve);
                        break;

                    case "Samplings":
                        samplingRecords = int.Parse(xmlReader.GetAttribute("SamplingRecordsCount"));
                        OnExportSamplingStatus?.Invoke(null, new SamplingEventArgs(SamplingRecordStatus.BeginSamplings, samplingRecords));
                        break;

                    case "Sampling":
                        sampling = new Sampling(xmlReader.GetAttribute("TargetAreaGUID"),
                                                xmlReader.GetAttribute("SamplingGUID"),
                                                DateTime.Parse(xmlReader.GetAttribute("SamplingDateTime")),
                                                xmlReader.GetAttribute("LandingSiteGuid"),
                                                xmlReader.GetAttribute("ReferenceNumber"));
                        sampling.TargetAreaGuid = TargetAreaGuid;
                        sampling.EnumeratorGuid = xmlReader.GetAttribute("EnumeratorGuid");
                        sampling.SamplingType   = CatchMonitoringSamplingType.FisheryDependent;
                        if (xmlReader.GetAttribute("SamplingType") != null)
                        {
                            sampling.SamplingType = (CatchMonitoringSamplingType)Enum.Parse(typeof(CatchMonitoringSamplingType), xmlReader.GetAttribute("SamplingType"));
                        }

                        if (DateTime.TryParse(xmlReader.GetAttribute("DateEncoded"), out DateTime result))
                        {
                            sampling.DateEncoded = result;
                        }

                        if (DateTime.TryParse(xmlReader.GetAttribute("GearSetDateTime"), out result))
                        {
                            sampling.GearSettingDateTime = result;
                        }

                        if (DateTime.TryParse(xmlReader.GetAttribute("GearHaulDateTime"), out result))
                        {
                            sampling.GearHaulingDateTime = result;
                        }

                        sampling.GearVariationGuid = xmlReader.GetAttribute("GearVariationGuid");

                        if (bool.TryParse(xmlReader.GetAttribute("HasLiveFish"), out bool hlf))
                        {
                            sampling.HasLiveFish = hlf;
                        }

                        sampling.Notes = xmlReader.GetAttribute("Notes");

                        if (int.TryParse(xmlReader.GetAttribute("NumberOfFishers"), out int nf))
                        {
                            sampling.NumberOfFishers = nf;
                        }

                        if (int.TryParse(xmlReader.GetAttribute("NumberOfHauls"), out int nh))
                        {
                            sampling.NumberOfHauls = nh;
                        }

                        if (int.TryParse(xmlReader.GetAttribute("WeightCatch"), out int wc))
                        {
                            sampling.CatchWeight = wc;
                        }

                        if (int.TryParse(xmlReader.GetAttribute("WeightSample"), out int ws))
                        {
                            sampling.SampleWeight = ws;
                        }

                        break;

                    case "FishingVessel":
                        if (Enum.TryParse(xmlReader.GetAttribute("VesselType"), out VesselType t))
                        {
                            vesselType = t;
                        }
                        fishingVessel        = new FishingVessel(vesselType);
                        fishingVessel.Engine = xmlReader.GetAttribute("Engine");
                        if (int.TryParse(xmlReader.GetAttribute("EngineHp"), out int hp))
                        {
                            fishingVessel.EngineHorsepower = hp;
                        }

                        var dimension = xmlReader.GetAttribute("Dimension_BDL").Split(new char[] { 'x', ' ' });
                        if (dimension[0].Length > 0)
                        {
                            fishingVessel.Breadth = double.Parse(dimension[0]);
                            fishingVessel.Depth   = double.Parse(dimension[3]);
                            fishingVessel.Length  = double.Parse(dimension[6]);
                        }
                        sampling.FishingVessel = fishingVessel;
                        break;

                    case "FishingGrounds":
                        listFishingGrounds.Clear();
                        break;

                    case "FishingGround":
                        int?   sg = null;
                        string fg = xmlReader.GetAttribute("Name");
                        if (fg.Length > 0)
                        {
                            if (int.TryParse(xmlReader.GetAttribute("SubGrid"), out int s))
                            {
                                sg = s;
                            }
                            listFishingGrounds.Add(new FishingGround(fg, sg));
                        }
                        break;

                    case "CatchComposition":
                        sampling.FishingGroundList = listFishingGrounds;
                        Samplings sp = new Samplings();
                        if (sp.UpdateEffort(true, sampling))
                        {
                            OnExportSamplingStatus?.Invoke(null, new SamplingEventArgs(SamplingRecordStatus.Samplings, sampling.ReferenceNumber));
                        }
                        break;

                    case "Catch":
                        break;

                    case "LengthFrequency":
                        break;

                    case "LengthFrequencyItem":
                        break;

                    case "GonadalMaturityStage":
                        break;

                    case "GMSItem":
                        break;
                    }
                    elementCounter++;
                }
                //if (elementCounter > 0 && !proceed)
                //{
                //    OnExportSamplingStatus?.Invoke(null, new SamplingEventArgs(ExportSamplingStatus.Error, "XML file does not contain fish catch monitoring data", 0));
                //    proceed = false;
                //}
            }
            OnExportSamplingStatus?.Invoke(null, new SamplingEventArgs(SamplingRecordStatus.EndImport));
            xmlReader.Close();
            return(proceed);
        }