//dups must have the collection code public static int updateDups(DataTable records) { int updates = 0; foreach (DataRow row in records.Rows) { string rdespec = row["rdespec"].ToString().Trim(); string dups = row["dups"].ToString().Trim(); string barcode = row["barcode"].ToString().Trim(); string collCode = ""; if (String.IsNullOrEmpty(rdespec)) //no rdespec { //get the collection code collCode = getCollCodeFromBarcode(barcode); if (dups != collCode) { updates++; row["dups"] = collCode; } } else //there is rdespec { //deserialize it XMLSpecimenList speclist = new XMLSpecimenList(rdespec); string allDups = ""; bool updated = false; foreach (XMLSpecimen spec in speclist.Specimens) { string code = getCollCodeFromBarcode(spec.barcode); if (spec.ccid.Trim() != code) { spec.ccid = code; updated = true; } allDups += code + ", "; } if (updated) { //'save' it again row["rdespec"] = rdespecListToXML(speclist); row["dups"] = updates; updates++; } } } return(updates); }
public static int updateImageList(DataTable records, List <string> imagePaths) { if (imagePaths == null) { MessageBox.Show("Image paths not available. imagelist field will not be updated."); return(0); } int updateCount = 0; //update the imagelist field to include the image paths foreach (DataRow row in records.Rows) { string barcode = row["barcode"].ToString().Trim(); //this should never happen. We assume all records have barcodes. if (string.IsNullOrEmpty(barcode)) { continue; } //silent else BarcodeParts bcParts = new BarcodeParts(row["barcode"].ToString().Trim()); string root = bcParts.collectionCode + bcParts.number; List <string> paths = imagePaths.Where(s => s.Contains(root) && s.Contains("jpg")).ToList(); if (paths.Count > 0) { row["imagelist"] = String.Join(Environment.NewLine, paths); updateCount++; //check in rdespec string rdespec = row["rdespec"].ToString().Trim(); if (!String.IsNullOrEmpty(rdespec)) { XMLSpecimenList specs = new XMLSpecimenList(rdespec); foreach (XMLSpecimen spec in specs.Specimens) { spec.imagelink = paths.Where(p => p.Contains(spec.barcode.Trim())).First(); } row["rdespec"] = specs.ToXMLString(); } } } return(updateCount); }
public static string rdespecListToXML(XMLSpecimenList XMLList) { XmlSerializer serializer = new XmlSerializer(typeof(XMLSpecimenList)); using (var sww = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { serializer.Serialize(writer, XMLList); string xml = sww.ToString(); // Your XML xml = xml.Replace("<SpecimenList>", "").Replace("</SpecimenList>", "").Replace("&", "&"); return(xml); } } }
//add accession numbers //also moves old accession numbers to a oldbarcode if they exist and are different public static int addAccessionNumbers(DataTable records) { int updateCounter = 0; foreach (DataRow row in records.Rows) { string barcode = row["barcode"].ToString().Trim(); string rdeSpec = row["rdespec"].ToString().Trim(); string accession = row["accession"].ToString().Trim(); if (barcode.Length > 0) { //strip off the -# at the end char[] separators = { '-', '–', '—' }; barcode = barcode.Split(separators)[0]; //get the number part string numberString = Regex.Match(barcode, @"\d+").Value; if (numberString.Length > 0) { if (String.IsNullOrEmpty(accession)) //just put it in { row["accession"] = numberString; updateCounter++; } else //more complicatd { if (accession != numberString) //its a different accession and needs to be moved to rdespec { if (String.IsNullOrEmpty(rdeSpec)) //no rdespec { XMLSpecimen spec = new XMLSpecimen(); spec.accession = numberString; spec.barcode = barcode; spec.oldbarcode = accession; //this is why we have to do all this XMLSpecimenList XMLList = new XMLSpecimenList(); XMLList.Specimens = new XMLSpecimen[] { spec }; string xml = rdespecListToXML(XMLList); //TODO check that this does not have the root element row["rdespec"] = xml; updateCounter++; } else //we already have RDEspec { XMLSpecimenList speclist = new XMLSpecimenList(rdeSpec); foreach (XMLSpecimen spec in speclist.Specimens) { if (String.IsNullOrEmpty(spec.accession)) { spec.accession = numberString; spec.oldbarcode = accession; updateCounter++; } else { if (spec.accession != numberString) { spec.oldbarcode = spec.accession; spec.accession = numberString; updateCounter++; } } } } } //finally update the RDE row accession field row["accession"] = numberString; updateCounter++; } } } } return(updateCounter); }