Ejemplo n.º 1
0
        public bool ConvertToTUGVehicles(string strVehicleTemplateFile,
                                         string strSignatureFile,
                                         string strAxleTemplateFile,
                                         string strVectoXsd,
                                         bool bEnsureSignatureIsPresent,
                                         RoadmapGroup group,
                                         Roadmap map,
                                         DatabaseContext dbx,
                                         string strFailedWinsLogFile,
                                         string strPatchListFile)
        {
            int nCount = 0;

            try
            {
                Log.Debug("=>ConvertToTUGVehicles");
                XNamespace ns = Reco3Common.Reco3_Defines.DeclarationNamespace;
                XElement   signatureTemplate = XElement.Load(strSignatureFile);
                XElement   signatureNode     = signatureTemplate.Descendants(ns + "Signature").FirstOrDefault();

                XElement axleTemplate     = XElement.Load(strAxleTemplateFile);
                XElement axleTemplateNode = axleTemplate.Descendants(ns + "Axle").FirstOrDefault();


                XElement vehicleTemplate = XElement.Load(strVehicleTemplateFile);

                /*
                 * Roadmap previousmap = group.Roadmaps
                 *                          .OrderByDescending(x => x.CurrentYear)
                 *                          .Where(x => x.CurrentYear < map.CurrentYear)
                 *                          .First();
                 */

                Log.Debug("=>ConvertToTUGVehicles.Initialize");
                if (true == Converter.Initialize(ns, signatureNode, axleTemplateNode, DBContext, bEnsureSignatureIsPresent, map.CurrentYear))
                {
                    using (XmlReader schemaReader = XmlReader.Create(strVectoXsd))
                    {
                        // Prepare schemaset
                        XmlSchemaSet schemaSet = new XmlSchemaSet();
                        schemaSet.Add(XmlSchema.Read(schemaReader,
                                                     new ValidationEventHandler(
                                                         delegate(Object sender, ValidationEventArgs e) { }
                                                         )));

                        DateTime dtStart = DateTime.Now;

                        // Protect the roadmap for the time we process it, will change the status afterwards but the protection remains.
                        map.ImprovedVehicleCount         = 0;
                        map.ConvertToVehicleInput_Status = Reco3_Enums.ConvertToVehicleInputStatus.Processing;
                        map.Validation_Status            = Reco3_Enums.ValidationStatus.ValidatedWithSuccess;
                        map.Protected = true;
                        dbx.SaveChanges();


                        Log.Debug("=>ConvertToTUGVehicles.Initialized. B4 vehicle-iteration");
                        List <Scania.Baseline.FailedPds.vehiclesVehicle> vehicleList = new List <Scania.Baseline.FailedPds.vehiclesVehicle>();
                        List <vehiclesVehicle> vehicles      = Vehicles.vehicle.ToList();
                        List <Vehicle>         readyVehicles = new List <Vehicle>();

                        // If we have a filtered VIN-list, then filter out the ones not listed
                        List <string> strlVinList = GetVINList(strPatchListFile);
                        if (strlVinList != null)
                        {
                            DateTime dtb4Filter = DateTime.Now;
                            var      hset       = new HashSet <string>(strlVinList);
                            vehicles = vehicles.Where(elem => hset.Contains(elem.VIN)).ToList();
                            DateTime dtafterFilter = DateTime.Now;

                            TimeSpan span = dtafterFilter.Subtract(dtb4Filter);
                        }


                        foreach (vehiclesVehicle vehicle in vehicles)
                        {
                            // Thread.Sleep(10);

                            nCount++;
                            Log.Debug("=>ConvertToTUGVehicles.Initialized. B4 conversion: " + nCount + " VIN: " + vehicle.VIN);
                            // 1: Make a copy of the template
                            XElement currentVehicle = new XElement(vehicleTemplate);

                            Console.WriteLine("=>ConvertToTUGVehicles.Initialized. B4 conversion: " + nCount + " VIN: " + vehicle.VIN);

                            // 2: Construct the vehicle.xml
                            bool bContainsImprovements = false;
                            if (true == Converter.Convert2TUGVehicle(ref currentVehicle, vehicle, ref bContainsImprovements))
                            {
                                // 3: Validate against the schema from TUG
                                Log.Debug("=>ConvertToTUGVehicles.Initialized. B4 validating against schema.");
                                bool      errors = false;
                                XDocument doc    = new XDocument(currentVehicle);
                                doc.Validate(schemaSet, (o, e) =>
                                {
                                    Log.Information("Schemafailure, vehicle: " + vehicle.VIN + ". " + e.Message);
                                    errors = true;
                                }, true);
                                if (errors == true)
                                {
                                    Log.Debug("=>ConvertToTUGVehicles.Initialized. Schema validated, with failures...");

                                    /*
                                     * // Failures in the schema?
                                     * Scania.Baseline.FailedPds.vehiclesVehicle VIN = new Scania.Baseline.FailedPds.vehiclesVehicle();
                                     * VIN.VIN = vehicle.VIN;
                                     * VIN.SchemaFailures = new Scania.Baseline.FailedPds.vehiclesVehicleSchemaFailure[strErrorList.Count];
                                     * int n = 0;
                                     * foreach (string strError in strErrorList)
                                     * {
                                     *  vehiclesVehicleSchemaFailure failure = new vehiclesVehicleSchemaFailure();
                                     *  failure.description = strError;
                                     *  VIN.SchemaFailures[n++] = failure;
                                     * }
                                     *
                                     * vehicleList.Add(VIN);
                                     */
                                }
                                else
                                {
                                    try
                                    {
                                        Vehicle v = null;
                                        if (strlVinList != null)
                                        {
                                            v = dbx.Vehicle
                                                .Where(x => x.VIN == vehicle.VIN)
                                                .Where(x => x.GroupId == map.RoadmapId)
                                                .First();
                                        }
                                        if (v != null)
                                        {
                                            // Update the existing vehicle
                                            v.XML = doc.AsString();
                                            dbx.Entry(v).State = System.Data.Entity.EntityState.Modified;
                                            dbx.SaveChanges();

                                            // Update the report for this vehicle too
                                            var pRoadmapGroupId = new SqlParameter("@pRoadmapGroupId", group.RoadmapGroupId);
                                            var pRoadmapId      = new SqlParameter("@pRoadmapId", map.RoadmapId);
                                            var pVehicleId      = new SqlParameter("@pVehicleId", v.VehicleId);

                                            // I know!!!! This is fu##ing ugly but hey, if it offends you, stop reading, ok?
                                            dbx.Database.ExecuteSqlCommand("exec GenerateRoadmapReportForVehicleId @pRoadmapGroupId , @pRoadmapId , @pVehicleId", pRoadmapGroupId, pRoadmapId, pVehicleId);
                                        }
                                        else
                                        {
                                            dbx.AddVehicle(doc.AsString(), vehicle.VIN, Reco3_Enums.VehicleMode.VectoDeclaration, map.RoadmapId);
                                        }

                                        /*
                                         * Vehicle v = new Vehicle(doc.AsString(), map.RoadmapId);
                                         * v.Vehicle_Mode = Reco3_Enums.VehicleMode.VectoDeclaration;
                                         * v.GroupId = map.RoadmapId;
                                         * dbx.Vehicle.Add(v);
                                         */
                                        if (bContainsImprovements == true)
                                        {
                                            map.ImprovedVehicleCount += 1;
                                        }
                                        // dbx.SaveChanges();
                                        //TriggerEvent(v);
                                        GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                                        GC.Collect();



/*
 *                                      // 2.1: If no improvements where added,...
 *                                      if (bContainsImprovements == false)
 *                                      {
 *
 *                                          // ...and we have the previous map,...
 *                                          if (previousmap != null)
 *                                          {
 *                                              Log.Debug("=>ConvertToTUGVehicles.Initialized. Schema validated, with success, no changes and we have a previous version, so making a copy.");
 *                                              // ...then fetch the previous version of the vehicle...
 *                                              Vehicle oldVehicle = dbx.Vehicle
 *                                                                      .Where(x => x.VIN == vehicle.VIN)
 *                                                                      .Where(x => x.GroupId == previousmap.RoadmapId)
 *                                                                      .First();
 *
 *                                              Vehicle clonedVehicle = oldVehicle.Clone();
 *                                              clonedVehicle.VehicleId = -1;
 *                                              clonedVehicle.GroupId = map.RoadmapId;
 *                                              dbx.Vehicle.Add(clonedVehicle);
 *                                              dbx.SaveChanges();
 *
 *                                              // ..and copy the vsum...
 *                                              List<VSumRecord> results = dbx.VSum.Where(x => x.VehicleId == oldVehicle.VehicleId).ToList();
 *                                              foreach (VSumRecord res in results)
 *                                              {
 *                                                  // ...clone the result, patch with the new vehicle-id and roadmap-id
 *                                                  VSumRecord resCopy = res.Clone();
 *                                                  resCopy.VehicleId = clonedVehicle.VehicleId;
 *                                                  resCopy.SimulationId = map.RoadmapId;
 *                                                  dbx.VSum.Add(resCopy);
 *                                              }
 *                                          }
 *                                          else
 *                                          {
 *                                              map.ImprovedVehicleCount += 1;
 *                                              Log.Debug("=>ConvertToTUGVehicles.Initialized. Schema validated, with success, no changes but we dont have a previous version, so making a fresh vehicle.");
 *                                              // ...and we dont have the previous map so just add it as a new one...
 *                                              //dbx.AddVehicle(doc.AsString(), vehicle.VIN, Reco3_Enums.VehicleMode.VectoDeclaration, map.RoadmapId);
 *                                          }
 *
 *                                      }
 *                                      else
 *                                      {
 *                                          map.ImprovedVehicleCount += 1;
 *                                          Log.Debug("=>ConvertToTUGVehicles.Initialized. Schema validated, with success, has changes so making a fresh vehicle.");
 *                                          // ...(has changes) so just add it as a new one...
 *                                          //dbx.AddVehicle(doc.AsString(), vehicle.VIN, Reco3_Enums.VehicleMode.VectoDeclaration, map.RoadmapId);
 *                                      }
 *                                      // ...and persist the changes...
 *                                      dbx.SaveChanges();
 *
 *                                      GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
 *                                      GC.Collect();
 */
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Fatal(ex, "ConvertToTUGVehicles.Exception while updating db:");
                                        Console.WriteLine("Exception while updating db: " + ex.Message);
                                    }
                                }
                                doc = null;
                            }
                            else
                            {
                                using (StreamWriter w = File.AppendText(strFailedWinsLogFile))
                                {
                                    w.WriteLine(vehicle.VIN);
                                }
                                Console.WriteLine("  ConvertToTUGVehicles==>Convert2TUGVehicle failed." + " (VIN: " + vehicle.VIN + ")");
                                Log.Debug("=>ConvertToTUGVehicles.Convert2TUGVehicle failed." + " (VIN: " + vehicle.VIN + ")");
                            }
                            currentVehicle = null;
                            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                            GC.Collect();
                        }

                        map.ConvertToVehicleInput_Status = Reco3_Enums.ConvertToVehicleInputStatus.ConvertedWithSuccess;
                        dbx.SaveChanges();
                        // Add the new failures,....
                        if (Converter.PDFailures.vehicle != null)
                        {
                            List <Scania.Baseline.FailedPds.vehiclesVehicle> tmpList = Converter.PDFailures.vehicle.ToList();
                            tmpList.AddRange(vehicleList);
                            Converter.PDFailures.vehicle = tmpList.ToArray();
                        }
                        else
                        {
                            Converter.PDFailures.vehicle = vehicleList.ToArray();
                        }

                        DateTime dtEnd = DateTime.Now;

                        Console.WriteLine("Conversion started: " + dtStart.ToShortDateString() + " " + dtStart.ToShortTimeString());
                        Console.WriteLine("Conversion done: " + dtEnd.ToShortDateString() + " " + dtEnd.ToShortTimeString());

                        Log.Debug("<=ConvertToTUGVehicles");
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "ConvertToTUGVehicles.OverallException");
                Console.WriteLine(ex);
            }
            Log.Debug("<=ConvertToTUGVehicles");
            return(false);
        }