/// <summary> /// Initialzes the current master ids, by inserting a new row to the table CurrenMasterDataIds with all values of Ids = 1 /// </summary> /// Erstellt von Joshua Frey, am 11.01.2016 private void initialzeCurrentMasterIds() { CurrentMasterDataIds newDataRow = new CurrentMasterDataIds { CurrentProjectId = 1, CurrentCriterionId = 1, CurrentProductId = 1 }; base.DataContext.CurrentMasterDataIds.InsertOnSubmit(newDataRow); base.DataContext.SubmitChanges(); }
/// <summary> /// Checks if ids already initialized. /// </summary> /// <param name="masterDataIds">The master data ids.</param> /// <returns> /// boolean if there are any Ids /// </returns> /// Erstellt von Joshua Frey, am 11.01.2016 private bool checkIfIdsAlreadyInitialized(CurrentMasterDataIds masterDataIds) { if (masterDataIds != null) { return(true); } else { return(false); } }
/// <summary> /// Increments the current product identifier. /// </summary> /// Erstellt von Joshua Frey, am 11.01.2016 public void incrementCurrentProductId() { CurrentMasterDataIds currentMasterDataIds = GetCurrentMasterDataIds(); currentMasterDataIds.CurrentProductId += 1; bool success = updateCurrentMasterDataIds(currentMasterDataIds); if (!success) { throw new NWATException(MessageUpdateFailed()); } }
/// <summary> /// Gets the current master data ids. /// </summary> /// <returns> /// An instance of CurrentMasterDataIds /// </returns> /// Erstellt von Joshua Frey, am 11.01.2016 public CurrentMasterDataIds GetCurrentMasterDataIds() { CurrentMasterDataIds result = base.DataContext.CurrentMasterDataIds.FirstOrDefault(); if (checkIfIdsAlreadyInitialized(result)) { return(result); } else { initialzeCurrentMasterIds(); return(base.DataContext.CurrentMasterDataIds.First()); } }
/// <summary> /// Updates the current master data ids. /// </summary> /// <param name="alteredDataSet">The altered data set.</param> /// <returns></returns> /// Erstellt von Joshua Frey, am 11.01.2016 private bool updateCurrentMasterDataIds(CurrentMasterDataIds alteredDataSet) { CurrentMasterDataIds resultDataSet = base.DataContext.CurrentMasterDataIds.Single(currIds => currIds.Id == alteredDataSet.Id); resultDataSet.CurrentProjectId = alteredDataSet.CurrentProjectId; resultDataSet.CurrentCriterionId = alteredDataSet.CurrentCriterionId; resultDataSet.CurrentProductId = alteredDataSet.CurrentProductId; base.DataContext.SubmitChanges(); CurrentMasterDataIds checkDataset = GetCurrentMasterDataIds(); bool sameProjId = checkDataset.CurrentProjectId == alteredDataSet.CurrentProjectId; bool sameCritId = checkDataset.CurrentCriterionId == alteredDataSet.CurrentCriterionId; bool sameProdId = checkDataset.CurrentProductId == alteredDataSet.CurrentProductId; return(sameCritId && sameProdId && sameProjId); }
/// <summary> /// Inserts the product into database. /// </summary> /// <param name="newProduct">The new product.</param> /// <returns> /// bool if insert of new product was successfull. /// </returns> /// Erstellt von Joshua Frey, am 14.12.2015 /// <exception cref="NWATException"> /// </exception> public bool InsertProductIntoDb(Product newProduct) { if (newProduct != null) { // if insert Id is != 0 then this project will be imported at the index of insertId bool willBeImported = newProduct.Product_Id != 0; string newProductName = newProduct.Name; if (!CheckIfProductNameAlreadyExists(newProductName)) { if (willBeImported) { if (CheckIfProductIdAlreadyExists(newProduct.Product_Id)) { throw (new NWATException(MessageProductIdAlreadyExists(newProduct.Product_Id))); } else { base.DataContext.Product.InsertOnSubmit(newProduct); base.DataContext.SubmitChanges(); } } else { using (CurrentMasterDataIdsController masterDataIdsContr = new CurrentMasterDataIdsController()) { CurrentMasterDataIds masterDataIdsSet = masterDataIdsContr.GetCurrentMasterDataIds(); int currentProdId = masterDataIdsSet.CurrentProductId; // if you inserted a product manually and forgot to adjust the currentProductId it will // increment to the free place and will use new id to insert new product while (GetProductById(currentProdId) != null) { masterDataIdsContr.incrementCurrentProductId(); currentProdId = masterDataIdsSet.CurrentProductId; } newProduct.Product_Id = currentProdId; base.DataContext.Product.InsertOnSubmit(newProduct); base.DataContext.SubmitChanges(); masterDataIdsContr.incrementCurrentProductId(); } } } else { throw (new NWATException((MessageProductAlreadyExists(newProductName)))); } } else { throw (new NWATException(MessageProductCouldNotBeSavedEmptyObject())); } Product newProductFromDb = (from prod in base.DataContext.Product where prod.Name == newProduct.Name && prod.Producer == newProduct.Producer && prod.Price == newProduct.Price select prod).FirstOrDefault(); return(CheckIfEqualProducts(newProduct, newProductFromDb)); }
/// <summary> /// Inserts the criterion into database. /// </summary> /// <param name="newCriterion">The new criterion.</param> /// <returns> /// bool if insert of new criterion was successfull. /// </returns> /// Erstellt von Joshua Frey, am 14.12.2015 /// <exception cref="NWATException"> /// </exception> public bool InsertCriterionIntoDb(Criterion newCriterion) { if (newCriterion != null) { // if insert Id is != 0 then this criterion will be imported at the index of insertId bool willBeImported = newCriterion.Criterion_Id != 0; string newCriterionName = newCriterion.Name; if (!CheckIfCriterionNameAlreadyExists(newCriterionName)) { if (willBeImported) { if (CheckIfCriterionIdAlreadyExists(newCriterion.Criterion_Id)) { throw (new NWATException(MessageCriterionIdAlreadyExists(newCriterion.Criterion_Id))); } else { base.DataContext.Criterion.InsertOnSubmit(newCriterion); base.DataContext.SubmitChanges(); } } else { using (CurrentMasterDataIdsController masterDataIdsContr = new CurrentMasterDataIdsController()) { CurrentMasterDataIds masterDataIdsSet = masterDataIdsContr.GetCurrentMasterDataIds(); int currentCritId = masterDataIdsSet.CurrentCriterionId; // if you inserted a criterion manually and forgot to adjust the currentCriterionId it will // increment to the free place and will use new id to insert new criterion while (GetCriterionById(currentCritId) != null) { masterDataIdsContr.incrementCurrentCriterionId(); currentCritId = masterDataIdsSet.CurrentCriterionId; } newCriterion.Criterion_Id = currentCritId; base.DataContext.Criterion.InsertOnSubmit(newCriterion); base.DataContext.SubmitChanges(); masterDataIdsContr.incrementCurrentCriterionId(); } } } else { throw (new NWATException((MessageCriterionAlreadyExists(newCriterionName)))); } } else { throw (new NWATException(MessageCriterionCouldNotBeSavedEmptyObject())); } Criterion newCriterionFromDb = (from crit in base.DataContext.Criterion where crit.Name == newCriterion.Name && crit.Description == newCriterion.Description select crit).FirstOrDefault(); return(CheckIfEqualCriterions(newCriterion, newCriterionFromDb)); }