public void UpdateBackendDataModel(DataModelChanges detectedChanges, Application backendApplication) { backendApplication = ModelService.GetDomainObject <Application>(backendApplication.Id); MetaManagerServices.Helpers.HintCollectionHelper.UpdateHints(detectedChanges, backendApplication.HintCollection); MetaManagerServices.Helpers.BusinessEntityHelper.UpdateBuisinessEntities(detectedChanges, backendApplication); }
public void UpdateBuisinessEntities(DataModelChanges detectedChanges, Application backendApplication) { backendApplication = ModelService.GetDomainObject <Application>(backendApplication.Id); BusinessEntity lastSavedEntity = null; foreach (KeyValuePair <object, DataModelChange> keyValue in detectedChanges) { // Check if new or changed if ((keyValue.Value.ContainDataModelChangeType(DataModelChangeType.Modified) || keyValue.Value.ContainDataModelChangeType(DataModelChangeType.New)) && keyValue.Value.Apply) { BusinessEntity entity = null; if (keyValue.Key is BusinessEntity) { entity = (BusinessEntity)keyValue.Key; } else if (keyValue.Key is Property) { Property property = (Property)keyValue.Key; entity = (property).BusinessEntity; } if (entity != null && lastSavedEntity != entity) { // Save changes entity = (BusinessEntity)ModelService.MergeSaveDomainObject(entity); //CheckOut Business Entity CheckOutInObject(entity, true, backendApplication); // Add the view to the the application list backendApplication.BusinessEntities.Add(entity); // Save application ModelService.SaveDomainObject(backendApplication); lastSavedEntity = entity; } } } foreach (KeyValuePair <object, DataModelChange> keyValue in detectedChanges) { if (keyValue.Value.ContainDataModelChangeType(DataModelChangeType.Deleted) && keyValue.Value.Apply) { if (keyValue.Key is BusinessEntity) { BusinessEntity entity = (BusinessEntity)keyValue.Key; ModelService.DeleteDomainObject(entity); } else if (keyValue.Key is Property) { Property property = (Property)keyValue.Key; ModelService.DeleteDomainObject(property); } } } }
private void btnStart_Click(object sender, EventArgs e) { List <string> tableNames = new List <string>(); CallbackService callbackService = new CallbackService(); callbackService.SetCallback(DoCallback); callbackService.Initialize(lvTables.CheckedItems.Count); tbProgress.Clear(); foreach (ListViewItem item in lvTables.CheckedItems) { LocalTableItem tableItem = (LocalTableItem)item.Tag; if (tableItem.RemoteTable != null) { tableNames.Add(tableItem.RemoteTable.TableName); } } bool changesMade = false; if (tableNames.Count > 0) { IList <BusinessEntity> businessEntities = null; DataModelChanges detectedChanges = null; IList <BusinessEntity> existingBusinessEntities = null; try { this.Cursor = Cursors.WaitCursor; btnClose.Enabled = false; using (new SessionScope(MetaManagerServices.GetSessionFactory(), MetaManagerServices.GetDomainInterceptor(), true, FlushMode.Never, true)) { businessEntities = DataModelImporter.AnalyzeTables(currentSchema, tableNames, false); existingBusinessEntities = modelService.GetAllDomainObjectsByApplicationId <BusinessEntity>(BackendApplication.Id); AddToProgressText("\nAnalyzing fetched data compared to MetaManager database... "); detectedChanges = DataModelImporter.CompareEntities(businessEntities, existingBusinessEntities); AddToProgressText("done!\n"); } } catch (Exception ex) { // Exception caught. Do nothing further! MessageBox.Show(string.Format("Error caught when retreiving information from database!\r\nFix the problem and try again!\r\nError:\r\n\t{0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } finally { this.Cursor = Cursors.Default; btnClose.Enabled = true; } if (detectedChanges != null && detectedChanges.Count > 0) { AddToProgressText("\nShowing changes to user to decide what changes to apply...\n"); using (ShowDataModelChanges form = new ShowDataModelChanges()) { form.BackendApplication = BackendApplication; form.FrontendApplication = FrontendApplication; form.DetectedChanges = detectedChanges; if (form.ShowDialog() == DialogResult.OK) { AddToProgressText("\nSaving new and changed properties and business entities... "); // Update detected changes depending on user input from dialog. DataModelImporter.CompareEntities(businessEntities, existingBusinessEntities, detectedChanges); try { MetaManagerServices.Helpers.ApplicationHelper.UpdateBackendDataModel(detectedChanges, BackendApplication); changesMade = true; } catch (ModelAggregatedException ex) { string mess = ex.Message; string ids = string.Empty; foreach (string id in ((ModelAggregatedException)ex).Ids) { ids += id + "\r\n"; } Clipboard.SetText(ids); mess += "\r\n\r\nThe Ids has been copied to the clipboard"; AddToProgressText("\r\n"); AddToProgressText("\r\n"); AddToProgressText(mess); AddToProgressText("\r\n"); } catch (Exception ex) { AddToProgressText("\n\n"); AddToProgressText(ex.Message); AddToProgressText("\n"); } } else { AddToProgressText("\nUser canceled!\n"); } } } else { AddToProgressText("\nNo changes found!\n"); } } if (changesMade) { AddToProgressText("\nFetching all tables to repopulate the list...\n"); PopulateTables(); AddToProgressText("Done!\n"); } }