/// <summary> /// Get the next free serial numbers for this device class from the d/b. Also check there are no devices with these serials in the d/b /// </summary> private void ButtonGetSerialNumbersClick() { Device device; buttonGo.Enabled = false; foreach (TestItem testItem in Globals.TestList.TestItems) testItem.Status = !testItem.Enabled ? EnumTestStatus.Disabled : EnumTestStatus.NotStarted; dataGridView1.Columns["Execute"].Visible = checkBoxLocalTest.Checked; dataGridView1.Refresh(); // // Note: AJL: Need to think about whether somebody else could get the ID we are working with? // try { // Check customer serial is within range var serial = long.Parse(textBoxEndUserSerial.Text); bool ok; if (Globals.DeviceClass.MaxCustomerSerial != null) ok = serial >= long.Parse(Globals.DeviceClass.MinCustomerSerial) || serial <= long.Parse(Globals.DeviceClass.MaxCustomerSerial); else ok = serial >= long.Parse(Globals.DeviceClass.MinCustomerSerial); if (!ok) { MessageBoxEx.Show("The serial number is outside the allowed range (" + Globals.DeviceClass.MinCustomerSerial + "-" + ((Globals.DeviceClass.MaxCustomerSerial != null) ? (Globals.DeviceClass.MaxCustomerSerial) : "\u221E") + ")"); return; } using (var transaction = Session.BeginTransaction()) { try { // Check if customer serial is in d/b ICriteria criteria = Session.CreateCriteria(typeof (Device)); criteria.Add( Restrictions.Eq("CustomerSerialNumber", textBoxEndUserSerial.Text) ); var results = criteria.List<Device>(); if (results.Count > 1) { // Shouldn't happen MessageBoxEx.Show( "There are " + results.Count + " devices in the database with this serial number. Please contact board vendor"); Logger.Warn("There are " + results.Count + " devices in the database with the entered serial number (" + textBoxEndUserSerial.Text + ")"); transaction.Rollback(); return; } if (results.Count == 1) { // There have been tests for this customer serial number. // Check if any of those tests have been successful int tests = 0; int testSuccesses = 0; foreach (var d in results) { foreach (var tlr in d.TestListResults) { if (tlr.Result) testSuccesses++; tests++; } } if (testSuccesses > 0) { DialogResult dr = MessageBoxEx.Show( "This customer serial number has already tested successfully " + testSuccesses + "/" + tests + " time(s). Please confirm you are retesting.", "Retesting?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr != DialogResult.Yes) { transaction.Rollback(); return; } } if (tests > 0) { DialogResult dr = MessageBoxEx.Show( "This serial number has failed testing " + tests + " time(s). Please confirm you are retesting.", "Retesting?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr != DialogResult.Yes) { transaction.Rollback(); return; } } device = results[0]; // Set existing board vendor serial textBoxProducerSerial.Text = device.ProducerSerialNumber; } else { // Ensure device class is refreshed Session.Refresh(Globals.DeviceClass); // Set new board vendor serial number var lastProducerSerial = Globals.DeviceClass.LastProducerSerial; // TBD: Check this doesn't exist? int lvs; if (lastProducerSerial == null) { lastProducerSerial = Globals.DeviceClass.MinProducerSerial; lvs = int.Parse(lastProducerSerial); } else { lvs = int.Parse(lastProducerSerial); lvs = lvs + 1; } if (Globals.DeviceClass.MaxProducerSerial != null) { if (lvs > int.Parse(Globals.DeviceClass.MaxProducerSerial)) { MessageBoxEx.Show( "Problem - there are no free producer serial numbers. Contact board board vendor"); transaction.Rollback(); return; } } textBoxProducerSerial.Text = lvs.ToString(); // Update last serials Globals.DeviceClass.LastCustomerSerial = textBoxEndUserSerial.Text; Globals.DeviceClass.LastProducerSerial = textBoxProducerSerial.Text; Session.SaveOrUpdate(Globals.DeviceClass); // Create new device device = new Device { CreationDate = DateTime.Now, DeviceClass = Globals.DeviceClass, CustomerSerialNumber = textBoxEndUserSerial.Text, ProducerSerialNumber = textBoxProducerSerial.Text, Creator = Globals.LoggedInUser, }; } // Whatever we just did, make sure last test date/person are correct device.LastTestDate = DateTime.Now; device.LastTester = Globals.LoggedInUser; // - Store it to d/b Session.SaveOrUpdate(device); if (Globals.Device != null) { // Debug.Assert(Globals.Device.Equals(device), "Problem as devices different?"); Session.Evict(Globals.Device); } // Store new device Globals.Device = device; // All done transaction.Commit(); } catch (Exception ex) { Logger.Warn("Exception dealing with serial numbers: " + ex.Message); transaction.Rollback(); return; } } // Update Globals Globals.ProducerSerialNumber = textBoxProducerSerial.Text; Globals.CustomerSerialNumber = textBoxEndUserSerial.Text; // Update UI pictureBoxSerials.Image = imageList1.Images["tick"]; pictureBoxRunTest.Image = imageList1.Images["tick"]; buttonGo.Enabled = true; buttonGo.Select(); } catch (Exception e) { Logger.Warn("Exception getting/dealing with serial numbers: " + e.Message); } }
/// <summary> /// Store test results to local XML file /// </summary> /// <param name="device"></param> private static void PersistResultsXML(Device device) { var strDoc = new StringBuilder(); strDoc.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n\r\n"); strDoc.Append("<device>\r\n"); strDoc.Append(" <creationdate>" + device.CreationDate + "</creationdate>\r\n"); strDoc.Append(" <creator>" + XmlTextEncoder.Encode(device.Creator.Forename + " " + device.Creator.Surname) + "</creator>\r\n"); strDoc.Append(" <customerserialnumber>" + XmlTextEncoder.Encode(device.CustomerSerialNumber) + "</customerserialnumber>\r\n"); strDoc.Append(" <deviceclass>" + XmlTextEncoder.Encode(device.DeviceClass.Name) + "</deviceclass>\r\n"); strDoc.Append(" <lasttestdate>" + XmlTextEncoder.Encode(device.LastTestDate.ToString()) + "</lasttestdate>\r\n"); strDoc.Append(" <lasttester>" + XmlTextEncoder.Encode(device.LastTester.Forename + " " + device.LastTester.Surname) + "</lasttester>\r\n"); strDoc.Append(" <producerserialnumber>" + XmlTextEncoder.Encode(device.ProducerSerialNumber) + "</producerserialnumber>\r\n"); strDoc.Append(" <testlistresults>\r\n"); var tl = device.TestListResults[device.TestListResults.Count - 1]; //foreach (var tl in device.TestListResults) { strDoc.Append(" <testlistresult>\r\n"); strDoc.Append(" <creationdate>" + XmlTextEncoder.Encode(tl.CreationDate.ToString()) + "</creationdate>\r\n"); strDoc.Append(" <result>" + XmlTextEncoder.Encode(tl.Result.ToString()) + "</result>\r\n"); strDoc.Append(" <testlist>" + XmlTextEncoder.Encode(tl.TestList.Name) + "</testlist>\r\n"); strDoc.Append(" <testlocation>" + XmlTextEncoder.Encode(tl.TestLocation.Name) + "</testlocation>\r\n"); strDoc.Append(" <tester>" + XmlTextEncoder.Encode(tl.Employee.Forename + " " + tl.Employee.Surname) + "</tester>\r\n"); strDoc.Append(" <company>" + XmlTextEncoder.Encode(tl.Employee.Company.Name) + "</company>\r\n"); strDoc.Append(" <testitemresults>\r\n"); foreach (var ti in tl.TestItemResults) { strDoc.Append(" <testitemresult>\r\n"); strDoc.Append(" <creationdate>" + XmlTextEncoder.Encode(ti.CreationDate.ToString()) + "</creationdate>\r\n"); strDoc.Append(" <name>" + XmlTextEncoder.Encode(ti.TestItem.Name) + "</name>\r\n"); strDoc.Append(" <result>" + XmlTextEncoder.Encode(ti.Result.ToString()) + "</result>\r\n"); strDoc.Append(" <notes>" + XmlTextEncoder.Encode(ti.Notes) + "</notes>\r\n"); strDoc.Append(" </testitemresult>\r\n"); } strDoc.Append(" </testitemresults>\r\n"); strDoc.Append(" </testlistresult>\r\n"); } strDoc.Append(" </testlistresults>\r\n"); strDoc.Append("</device>\r\n"); using (var fs = new FileStream(Globals.LocalResultFile, FileMode.OpenOrCreate)) { var writer = new StreamWriter(fs); writer.Write(strDoc.ToString()); writer.Close(); } }