public bool FindMarkerObservationLike(Marker_Observation oMarkerObs) { bool bFoundSameObs = false; IQueryable <Marker_Observation> q = from mo in this.myWalkingDB.Marker_Observations where mo.MarkerID == oMarkerObs.MarkerID && mo.WalkID == oMarkerObs.WalkID && mo.DateOfObservation == oMarkerObs.DateOfObservation select mo; if (q.Any()) { bFoundSameObs = true; } return(bFoundSameObs); }
public int AssociateMarkersWithWalk(NameValueCollection oForm, int iWalkID) { int iRetval = 0; var arrMarkerIDs = oForm["markers_added"].Split(':'); // ----First associate any newly created markers (created with ajax) with the newly created walk---------------- for (int iCounter = 0; iCounter <= arrMarkerIDs.Count() - 1; iCounter++) { if ((arrMarkerIDs[iCounter] != null && (arrMarkerIDs[iCounter].Length > 0))) { try { int iMarkerId = int.Parse(arrMarkerIDs[iCounter]); // ----Update marker with walk ID------ Marker oMarker = this.myWalkingDB.Markers.SingleOrDefault(m => m.MarkerID == iMarkerId); oMarker.WalkID = iWalkID; // ----Write a "created" marker observation-------- Marker_Observation oMarkerObs = new Marker_Observation(); oMarkerObs.MarkerID = iMarkerId; oMarkerObs.FoundFlag = false; oMarkerObs.WalkID = iWalkID; oMarkerObs.ObservationText = "Set in place"; try { oMarkerObs.DateOfObservation = DateTime.Parse(oForm["WalkDate"]); } catch (Exception) { oMarkerObs.DateOfObservation = DateTime.MinValue; } this.myWalkingDB.Marker_Observations.InsertOnSubmit(oMarkerObs); } catch (Exception) { iRetval = 1; } } } int iImageCounter = 1; // ----Now write any marker observations necessary for newly added images either in create or edit----------------------------- while ((oForm[("imagerelpath" + iImageCounter)] != null && (oForm[("imagerelpath" + iImageCounter)].Length > 0))) { if (oForm[("imageismarker" + iImageCounter)] == "on" && !arrMarkerIDs.Contains(oForm["imagemarkerid" + iImageCounter])) { try { int iMarkerId = int.Parse(oForm[("imagemarkerid" + iImageCounter)]); // Retrieve the details of the marker from the db Marker oMarker = this.myWalkingDB.Markers.SingleOrDefault(m => m.MarkerID == iMarkerId); // ----Write any found/not found observations. Only do so if the walk is not the walk on which the marker was created-------- if ((oMarker.WalkID != iWalkID)) { //We know that the marker already exists - it is either not found or found again Marker_Observation oMarkerObs = new Marker_Observation(); // ---If the marker is not yet associated with a walk then associate it with this walk if ((oMarker.WalkID == null)) { oMarker.WalkID = iWalkID; } oMarkerObs.MarkerID = iMarkerId; oMarkerObs.WalkID = iWalkID; if ((oForm[("imagemarkernotfound" + iImageCounter)] == "on")) { oMarker.Status = "Marker Left - Revisited, not found "; oMarkerObs.ObservationText = "Revisited but not found"; oMarkerObs.FoundFlag = false; } else { oMarkerObs.ObservationText = "Revisited and found"; oMarker.Status = "Marker Left - Found again "; oMarkerObs.FoundFlag = true; } try { oMarkerObs.DateOfObservation = DateTime.Parse(oForm["WalkDate"]); } catch (Exception) { oMarkerObs.DateOfObservation = DateTime.MinValue; } // ----Need to test that this observation has not already been inserted------ bool bAlreadyAdded = this.FindMarkerObservationLike(oMarkerObs); if (!bAlreadyAdded) { this.myWalkingDB.Marker_Observations.InsertOnSubmit(oMarkerObs); } } } catch (Exception ex) { throw new Exception("An error occurred when preparing the marker observation for new images:" + ex.Message, ex); } } iImageCounter = (iImageCounter + 1); } int iNumExistingImages = 0; try { iNumExistingImages = int.Parse(oForm["numexistingimages"]); } catch (Exception) { } // ----Now write any marker observations necessary for existing images in edit walk----------------------------- List <int> observationsAdded = new List <int>(); for (int iExistingImageCount = 1; (iExistingImageCount <= iNumExistingImages); iExistingImageCount++) { if ((oForm[("existingimageismarker" + iExistingImageCount)] == "on") && !arrMarkerIDs.Contains(oForm[("existingimagemarkerid" + iExistingImageCount)])) { try { int iMarkerId = int.Parse(oForm[("existingimagemarkerid" + iExistingImageCount)]); Marker oMarker = this.myWalkingDB.Markers.SingleOrDefault(m => m.MarkerID == iMarkerId); // ----Write any found/not found observation. Only do so if the walk is not the walk on which the marker was created-------- if (((oMarker.WalkID != iWalkID) || (oMarker.WalkID == null))) { // ---If the marker is not yet associated with a walk then associate it with this walk if ((oMarker.WalkID == null)) { oMarker.WalkID = iWalkID; } Marker_Observation oMarkerObs = new Marker_Observation(); oMarkerObs.MarkerID = iMarkerId; oMarkerObs.WalkID = iWalkID; if ((oForm[("existingimagemarkernotfound" + iExistingImageCount)] == "on")) { oMarker.Status = "Marker Left - Revisited, not found "; oMarkerObs.ObservationText = "Revisited but not found"; oMarkerObs.FoundFlag = false; } else { oMarkerObs.ObservationText = "Revisited and found"; oMarker.Status = "Marker Left - Found again "; oMarkerObs.FoundFlag = true; } try { oMarkerObs.DateOfObservation = DateTime.Parse(oForm["WalkDate"]); } catch (Exception) { oMarkerObs.DateOfObservation = DateTime.MinValue; } // ----Need to test that this observation has not already been inserted------ bool bAlreadyAdded = this.FindMarkerObservationLike(oMarkerObs); if (!bAlreadyAdded) { //-- Additional check to stop adding multiple observations for the same marker-- if (!observationsAdded.Contains(oMarkerObs.MarkerID)) { this.myWalkingDB.Marker_Observations.InsertOnSubmit(oMarkerObs); observationsAdded.Add(oMarkerObs.MarkerID); } } } } catch (Exception ex) { throw new Exception("An error occurred when preparing the marker observation for existing images:" + ex.Message, ex); } } } this.myWalkingDB.SubmitChanges(); return(iRetval); }