private Preparation GetNextPreparation(AssignmentPreparationMethod apm, Guid labId, ref List <Guid> exIds, ref int nextPrepNum) { Preparation p = null; if (exIds == null) { p = new Preparation(); p.AssignmentId = SelectedOrderId; p.InstanceStatusId = InstanceStatus.Active; p.LaboratoryId = labId; p.Number = nextPrepNum++; p.PreparationMethodId = apm.PreparationMethodId; p.SampleId = mSample.Id; p.WorkflowStatusId = WorkflowStatus.Construction; mSample.Preparations.Add(p); } else { if (exIds.Count < 1) { throw new Exception("Missing external preparation ids in list"); } Guid pid = exIds[0]; exIds.RemoveAt(0); p = mSample.Preparations.Find(x => x.Id == pid); if (p == null) { throw new Exception("External preparation with id " + pid + " was not found on sample " + mSample.Number); } } return(p); }
public FormImportAnalysisLIS(Preparation preparation, Analysis analysis) { InitializeComponent(); mPreparation = preparation; mAnalysis = analysis; }
public FormPrepAnalAddAnal(Sample samp, Preparation prep) { InitializeComponent(); mSample = samp; mPrep = prep; tbCount.KeyPress += CustomEvents.Integer_KeyPress; }
public void LoadFromDB(SqlConnection conn, SqlTransaction trans, Guid sampleId) { using (SqlDataReader reader = DB.GetDataReader(conn, trans, "csp_select_sample", CommandType.StoredProcedure, new SqlParameter("@id", sampleId))) { if (!reader.HasRows) { throw new Exception("Error: Sample with id " + sampleId.ToString() + " was not found"); } reader.Read(); Id = reader.GetGuid("id"); Number = reader.GetInt32("number"); LaboratoryId = reader.GetGuid("laboratory_id"); SampleTypeId = reader.GetGuid("sample_type_id"); SampleStorageId = reader.GetGuid("sample_storage_id"); SampleComponentId = reader.GetGuid("sample_component_id"); ProjectSubId = reader.GetGuid("project_sub_id"); StationId = reader.GetGuid("station_id"); SamplerId = reader.GetGuid("sampler_id"); SamplingMethodId = reader.GetGuid("sampling_method_id"); TransformFromId = reader.GetGuid("transform_from_id"); TransformToId = reader.GetGuid("transform_to_id"); ImportedFrom = reader.GetString("imported_from"); ImportedFromId = reader.GetString("imported_from_id"); MunicipalityId = reader.GetGuid("municipality_id"); LocationType = reader.GetString("location_type"); Location = reader.GetString("location"); Latitude = reader.GetDoubleNullable("latitude"); Longitude = reader.GetDoubleNullable("longitude"); Altitude = reader.GetDoubleNullable("altitude"); SamplingDateFrom = reader.GetDateTimeNullable("sampling_date_from"); SamplingDateTo = reader.GetDateTimeNullable("sampling_date_to"); ReferenceDate = reader.GetDateTimeNullable("reference_date"); ExternalId = reader.GetString("external_id"); WetWeight_g = reader.GetDoubleNullable("wet_weight_g"); DryWeight_g = reader.GetDoubleNullable("dry_weight_g"); Volume_l = reader.GetDoubleNullable("volume_l"); LodWeightStart = reader.GetDoubleNullable("lod_weight_start"); LodWeightEnd = reader.GetDoubleNullable("lod_weight_end"); LodTemperature = reader.GetDoubleNullable("lod_temperature"); LodWaterPercent = reader.GetDoubleNullable("lod_water_percent"); LodFactor = reader.GetDoubleNullable("lod_factor"); LodWeightStartAsh = reader.GetDoubleNullable("lod_weight_ash"); LodWeightEndAsh = reader.GetDoubleNullable("lod_weight_end_ash"); LodTemperatureAsh = reader.GetDoubleNullable("lod_temperature_ash"); LodWaterPercentAsh = reader.GetDoubleNullable("lod_water_percent_ash"); LodFactorAsh = reader.GetDoubleNullable("lod_factor_ash"); LodWeightStartAsh2 = reader.GetDoubleNullable("lod_weight_ash2"); LodWeightEndAsh2 = reader.GetDoubleNullable("lod_weight_end_ash2"); LodTemperatureAsh2 = reader.GetDoubleNullable("lod_temperature_ash2"); LodWaterPercentAsh2 = reader.GetDoubleNullable("lod_water_percent_ash2"); LodFactorAsh2 = reader.GetDoubleNullable("lod_factor_ash2"); Confidential = reader.GetBoolean("confidential"); InstanceStatusId = reader.GetInt32("instance_status_id"); LockedId = reader.GetGuid("locked_id"); Comment = reader.GetString("comment"); CreateDate = reader.GetDateTime("create_date"); CreateId = reader.GetGuid("create_id"); UpdateDate = reader.GetDateTime("update_date"); UpdateId = reader.GetGuid("update_id"); } // Load parameters Parameters.Clear(); List <Guid> sampParamIds = new List <Guid>(); using (SqlDataReader reader = DB.GetDataReader(conn, trans, "select id from sample_parameter where sample_id = @id", CommandType.Text, new SqlParameter("@id", sampleId))) { while (reader.Read()) { sampParamIds.Add(reader.GetGuid("id")); } } foreach (Guid sampParamId in sampParamIds) { SampleParameter p = new SampleParameter(); p.LoadFromDB(conn, trans, sampParamId); Parameters.Add(p); } // Load preparations Preparations.Clear(); List <Guid> preparationIds = new List <Guid>(); using (SqlDataReader reader = DB.GetDataReader(conn, trans, "select id from preparation where sample_id = @id", CommandType.Text, new SqlParameter("@id", Id))) { while (reader.Read()) { preparationIds.Add(reader.GetGuid("id")); } } foreach (Guid pId in preparationIds) { Preparation p = new Preparation(); p.LoadFromDB(conn, trans, pId); Preparations.Add(p); } Dirty = false; }
private void btnOk_Click(object sender, EventArgs e) { if (!Utils.IsValidGuid(cboxLaboratory.SelectedValue)) { MessageBox.Show("You must select a laboratory first"); return; } if (gridOrders.SelectedRows.Count < 1) { MessageBox.Show("You must select an order first"); return; } if (!mAssignment.ApprovedLaboratory) { MessageBox.Show("Can not add samples to orders that is not approved by laboratory"); return; } if (!mAssignment.ApprovedCustomer) { MessageBox.Show("Can not add samples to orders that is not approved by customer"); return; } if (mAssignment.WorkflowStatusId != WorkflowStatus.Construction) { MessageBox.Show("Can not add samples to orders that is not under construction"); return; } if (treeOrderLines.SelectedNode == null) { MessageBox.Show("You must select a order line first"); return; } TreeNode tnode = treeOrderLines.SelectedNode; if (tnode.Level != 0) { MessageBox.Show("You must select a top level order line for this sample"); return; } SqlConnection conn = null; SqlTransaction trans = null; try { Guid astId = Guid.Parse(tnode.Name); AssignmentSampleType ast = mAssignment.SampleTypes.Find(x => x.Id == astId); if (ast == null) { throw new Exception("No assignment sample type found with id " + ast.Id.ToString()); } conn = DB.OpenConnection(); trans = conn.BeginTransaction(); AstInfo astInfo = tnode.Tag as AstInfo; int nSamplesFree = DB.GetAvailableSamplesOnAssignmentSampleType(conn, trans, astId); if (nSamplesFree <= 0) { MessageBox.Show("This order line is full"); return; } string[] items = tnode.Text.Split(new[] { " -> " }, StringSplitOptions.RemoveEmptyEntries); if (items.Length < 2) { throw new Exception("Invalid sample type found in assignment: " + tnode.Text); } string st1 = items[1]; string st2 = mSample.GetSampleTypePath(conn, trans); if (!st2.StartsWith(st1)) { MessageBox.Show("Wrong sample type for sample " + mSample.Number); return; } if (astInfo.SampleComponentId != Guid.Empty && astInfo.SampleComponentId != mSample.SampleComponentId) { MessageBox.Show("Wrong sample component for sample " + mSample.Number); return; } Guid labId = Utils.MakeGuid(cboxLaboratory.SelectedValue); SelectedOrderId = mAssignment.Id; SelectedOrderName = mAssignment.Name; foreach (AssignmentPreparationMethod apm in ast.PreparationMethods) { if (apm.PreparationLaboratoryId != mAssignment.LaboratoryId) { // Check that external preparations exists TreeNode[] tn = treeOrderLines.Nodes.Find(apm.Id.ToString(), true); if (tn.Length < 1) { throw new Exception("No assignment preparation methods found with id " + apm.Id.ToString()); } if (tn[0].Tag == null) { MessageBox.Show("You must specify external preparations for this order line"); return; } } } if (mSample.HasOrder(conn, trans, SelectedOrderId)) { MessageBox.Show("Sample " + mSample.Number + " is already added to order " + mAssignment.Name); return; } int nextPrepNum = DB.GetNextPreparationNumber(conn, trans, mSample.Id); foreach (AssignmentPreparationMethod apm in ast.PreparationMethods) { List <Guid> exIds = null; if (apm.PreparationLaboratoryId != mAssignment.LaboratoryId) { // External preparations TreeNode[] tn = treeOrderLines.Nodes.Find(apm.Id.ToString(), true); if (tn.Length < 1) { throw new Exception("No assignment preparation method node found in tree with id " + apm.Id.ToString()); } exIds = tn[0].Tag as List <Guid>; } for (int i = 0; i < apm.PreparationMethodCount; i++) { Preparation p = GetNextPreparation(apm, labId, ref exIds, ref nextPrepNum); int nextAnalNum = DB.GetNextAnalysisNumber(conn, trans, p.Id); foreach (AssignmentAnalysisMethod aam in apm.AnalysisMethods) { for (int j = 0; j < aam.AnalysisMethodCount; j++) { Analysis a = new Analysis(); a.PreparationId = p.Id; a.AnalysisMethodId = aam.AnalysisMethodId; a.InstanceStatusId = InstanceStatus.Active; a.WorkflowStatusId = WorkflowStatus.Construction; a.Number = nextAnalNum++; a.AssignmentId = SelectedOrderId; a.LaboratoryId = labId; p.Analyses.Add(a); } } } } mSample.ConnectToOrderLine(conn, trans, ast.Id); mSample.StoreToDB(conn, trans); string json = JsonConvert.SerializeObject(mSample); DB.AddAuditMessage(conn, trans, "sample", mSample.Id, AuditOperationType.Update, json, ""); trans.Commit(); DialogResult = DialogResult.OK; } catch (Exception ex) { trans?.Rollback(); Common.Log.Error(ex); MessageBox.Show(ex.Message); DialogResult = DialogResult.Abort; } finally { conn?.Close(); } Close(); }
public void StoreToDB(SqlConnection conn, SqlTransaction trans) { if (Id == Guid.Empty) { throw new Exception("Error: Can not store a preparation with empty id"); } SqlCommand cmd = new SqlCommand("", conn, trans); if (!Preparation.IdExists(conn, trans, Id)) { // Insert new preparation cmd.CommandText = "csp_insert_preparation"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", Id); cmd.Parameters.AddWithValue("@sample_id", SampleId, Guid.Empty); cmd.Parameters.AddWithValue("@number", Number); cmd.Parameters.AddWithValue("@assignment_id", AssignmentId, Guid.Empty); cmd.Parameters.AddWithValue("@laboratory_id", LaboratoryId, Guid.Empty); cmd.Parameters.AddWithValue("@preparation_geometry_id", PreparationGeometryId, Guid.Empty); cmd.Parameters.AddWithValue("@preparation_method_id", PreparationMethodId, Guid.Empty); cmd.Parameters.AddWithValue("@workflow_status_id", WorkflowStatusId); cmd.Parameters.AddWithValue("@amount", Amount, null); cmd.Parameters.AddWithValue("@prep_unit_id", PrepUnitId); cmd.Parameters.AddWithValue("@quantity", Quantity, null); cmd.Parameters.AddWithValue("@quantity_unit_id", QuantityUnitId); cmd.Parameters.AddWithValue("@fill_height_mm", FillHeightMM, null); cmd.Parameters.AddWithValue("@instance_status_id", InstanceStatusId); cmd.Parameters.AddWithValue("@comment", Comment, String.Empty); cmd.Parameters.AddWithValue("@create_date", DateTime.Now); cmd.Parameters.AddWithValue("@create_id", Common.UserId, Guid.Empty); cmd.Parameters.AddWithValue("@update_date", DateTime.Now); cmd.Parameters.AddWithValue("@update_id", Common.UserId, Guid.Empty); cmd.Parameters.AddWithValue("@volume_l", Volume_l, null); cmd.Parameters.AddWithValue("@preprocessing_volume_l", PreprocessingVolume_l, null); cmd.ExecuteNonQuery(); Dirty = false; } else { if (Dirty) { // Update existing preparation cmd.CommandText = "csp_update_preparation"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", Id); cmd.Parameters.AddWithValue("@preparation_geometry_id", PreparationGeometryId, Guid.Empty); cmd.Parameters.AddWithValue("@workflow_status_id", WorkflowStatusId); cmd.Parameters.AddWithValue("@amount", Amount, null); cmd.Parameters.AddWithValue("@prep_unit_id", PrepUnitId); cmd.Parameters.AddWithValue("@quantity", Quantity, null); cmd.Parameters.AddWithValue("@quantity_unit_id", QuantityUnitId); cmd.Parameters.AddWithValue("@fill_height_mm", FillHeightMM, null); cmd.Parameters.AddWithValue("@comment", Comment, String.Empty); cmd.Parameters.AddWithValue("@update_date", DateTime.Now); cmd.Parameters.AddWithValue("@update_id", Common.UserId, Guid.Empty); cmd.Parameters.AddWithValue("@volume_l", Volume_l, null); cmd.Parameters.AddWithValue("@preprocessing_volume_l", PreprocessingVolume_l, null); cmd.ExecuteNonQuery(); Dirty = false; } } foreach (Analysis a in Analyses) { a.StoreToDB(conn, trans); } // Remove deleted analyses from DB List <Guid> storedAnalIds = new List <Guid>(); using (SqlDataReader reader = DB.GetDataReader(conn, trans, "select id from analysis where preparation_id = @id", CommandType.Text, new SqlParameter("@id", Id))) { while (reader.Read()) { storedAnalIds.Add(reader.GetGuid("id")); } } cmd.CommandText = "update analysis set instance_status_id = @status where id = @id"; cmd.CommandType = CommandType.Text; foreach (Guid aId in storedAnalIds) { if (Analyses.FindIndex(x => x.Id == aId) == -1) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@status", InstanceStatus.Deleted); cmd.Parameters.AddWithValue("@id", aId); cmd.ExecuteNonQuery(); } } }