/// <summary> /// Creates a new Material with the type, line, aspect and recipe codes stored in the Command Instance in the given context /// Looks for existing Entities and creates new ones if they don't exist /// </summary> /// <param name="template"> A disconnected material entry containing the informations required to create the new material</param> /// <returns>A new connected Material entry</returns> internal Material CreateMaterial(Material template) { /// Instantiate new Material Material newMaterial = new Material(); _context.Materials.Add(newMaterial); // Retrieves the MaterialType entity with the given code and sets reference in the new material // If no such entity exists, throws exception MaterialType typeInstance = _context.MaterialTypes.FirstOrDefault(mtyp => mtyp.Code == template.MaterialType.Code); newMaterial.MaterialType = typeInstance ?? throw new InvalidOperationException("MaterialType with code " + template.MaterialType.Code + " does not exist."); // Check if a MaterialLine instance with the given code exists // If no such entity exists, throws exception MaterialLine lineInstance = _context.MaterialLines.FirstOrDefault(mlin => mlin.Code == template.MaterialLine.Code); newMaterial.MaterialLine = lineInstance ?? throw new InvalidOperationException("MaterialLine with code " + template.MaterialLine.Code + " does not exist."); // Check if an Aspect instance with the given code exists // If no such entity exists, throws exception Aspect aspectInstance = _context.Aspects.FirstOrDefault(asp => asp.Code == template.Aspect.Code); newMaterial.Aspect = aspectInstance ?? throw new InvalidOperationException("Aspect with code " + template.Aspect.Code + " does not exist."); // Check if a Recipe instance with the given code exists // If no such entity exists, throws exception Recipe connectedrecipe = _context.Recipes.FirstOrDefault(rec => rec.Code == template.Recipe.Code); newMaterial.Recipe = connectedrecipe ?? throw new InvalidOperationException("Recipe with code " + template.Recipe.Code + " does not exist."); newMaterial.ExternalConstructionID = template.ExternalConstruction?.ID; newMaterial.ProjectID = template.Project?.ID; newMaterial.Recipe.ColourID = template.Recipe?.Colour?.ID; return(newMaterial); }
public void Execute(LabDbEntities context) { if (!context.Materials.Any(mat => mat.Aspect.Code == _aspectCode && mat.MaterialLine.Code == _lineCode && mat.Recipe.Code == _recipeCode && mat.MaterialType.Code == _typeCode)) { Aspect aspectInstance = context.Aspects.First(asp => asp.Code == _aspectCode); MaterialLine lineInstance = context.MaterialLines.First(lin => lin.Code == _lineCode); MaterialType typeInstance = context.MaterialTypes.First(typ => typ.Code == _typeCode); Recipe recipeInstance = context.Recipes.First(rec => rec.Code == _recipeCode); context.Materials.Add(new Material() { Aspect = aspectInstance, MaterialLine = lineInstance, MaterialType = typeInstance, Recipe = recipeInstance }); context.SaveChanges(); } }
public BatchCreationDialogViewModel(IDataService <LabDbEntities> labDbData) : base() { _labDbData = labDbData; ColourList = _labDbData.RunQuery(new ColorsQuery()).ToList(); ConstructionList = _labDbData.RunQuery(new ConstructionsQuery()).ToList(); DoNotTest = false; ProjectList = _labDbData.RunQuery(new ProjectsQuery()).ToList(); Notes = ""; CancelCommand = new DelegateCommand <Window>( parentDialog => { parentDialog.DialogResult = false; }); ConfirmCommand = new DelegateCommand <Window>( parentDialog => { if (AspectInstance == null) { AspectInstance = new Aspect() { Code = _aspectCode }; AspectInstance.Create(); } ; if (LineInstance == null) { LineInstance = new MaterialLine() { Code = _lineCode }; LineInstance.Create(); } if (RecipeInstance == null) { RecipeInstance = new Recipe() { Code = _recipeCode }; if (_selectedColour != null) { RecipeInstance.ColourID = _selectedColour.ID; } RecipeInstance.Create(); } else if (_selectedColour != null && RecipeInstance.ColourID != _selectedColour.ID) { _recipeInstance.ColourID = _selectedColour.ID; _recipeInstance.Update(); } Material tempMaterial = _labDbData.RunQuery(new MaterialQuery() { AspectID = _aspectInstance.ID, MaterialLineID = _lineInstance.ID, MaterialTypeID = _typeInstance.ID, RecipeID = _recipeInstance.ID }); if (tempMaterial != null) { bool requiresUpdate = false; if (_selectedConstruction != null && tempMaterial.ExternalConstructionID != _selectedConstruction.ID) { tempMaterial.ExternalConstructionID = _selectedConstruction.ID; requiresUpdate = true; } if (_selectedProject != null && tempMaterial.ProjectID != _selectedProject.ID) { tempMaterial.ProjectID = _selectedProject.ID; requiresUpdate = true; } if (requiresUpdate) { tempMaterial.Update(); } } if (tempMaterial == null) { tempMaterial = new Material() { AspectID = AspectInstance.ID, LineID = LineInstance.ID, RecipeID = RecipeInstance.ID, TypeID = TypeInstance.ID }; if (_selectedConstruction != null) { tempMaterial.ExternalConstructionID = _selectedConstruction.ID; } if (_selectedProject != null) { tempMaterial.ProjectID = _selectedProject.ID; } tempMaterial.Create(); } BatchInstance = new Batch() { DoNotTest = DoNotTest, FirstSampleArrived = false, MaterialID = tempMaterial.ID, ArchiveStock = 0, LongTermStock = 0, Notes = Notes, Number = _batchNumber }; if (SelectedTrialArea != null) { BatchInstance.TrialAreaID = SelectedTrialArea.ID; } BatchInstance.Create(); parentDialog.DialogResult = true; }, parentDialog => !HasErrors); BatchNumber = ""; TypeCode = ""; LineCode = ""; AspectCode = ""; RecipeCode = ""; }