public AttributeMetadataRow ToAttributeMetadataRow() { var typeName = GetType().Name; var result = new AttributeMetadataRow { AttributeType = typeName.Substring(0, typeName.Length - 9) }; var columns = AttributeMetadataRow.GetColumns(); var attributeMetadataRowProperties = typeof(AttributeMetadataRow).GetProperties(); var attributeMetadataProperties = GetType().GetProperties(); for (var i = 0; i < columns.Length; i++) { var column = columns[i]; var attributeMetadataRowProperty = attributeMetadataRowProperties.First(x => (x.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute)?.Header == column.Header); var attributeProperty = attributeMetadataProperties.FirstOrDefault(x => x.Name == attributeMetadataRowProperty.Name); if (attributeProperty != null) { var value = attributeProperty.GetValue(this); attributeMetadataRowProperty.SetValue(result, value); } } return(result); }
public void LoadFromAttributeMetadataRow(AttributeMetadataRow row) { var columns = AttributeMetadataRow.GetColumns(); var attributeMetadataRowProperties = typeof(AttributeMetadataRow).GetProperties(); var attributeMetadataProperties = GetType().GetProperties(); Entity = row.Entity; for (var i = 0; i < columns.Length; i++) { var column = columns[i]; var attributeMetadataRowProperty = attributeMetadataRowProperties.First(x => (x.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute)?.Header == column.Header); var attributeProperty = attributeMetadataProperties.FirstOrDefault(x => x.Name == attributeMetadataRowProperty.Name); if (attributeProperty != null) { var value = attributeMetadataRowProperty.GetValue(row); attributeProperty.SetValue(this, value); } } }
private void ExportAttributes() { if (cmbEntities.SelectedItem == null) { MessageBox.Show("You must select an entity", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } ManageWorkingState(true); var selectedLogicalName = (EntityItem)cmbEntities.SelectedItem; var saveFileDlg = new SaveFileDialog() { Title = "Save the entity template", Filter = "Excel Workbook|*.xlsx", FileName = string.Format("{0}_attributeeditor.xlsx", selectedLogicalName?.LogicalName) }; saveFileDlg.ShowDialog(); // If the file name is not an empty string open it for saving. if (!string.IsNullOrEmpty(saveFileDlg.FileName)) { informationPanel = InformationPanel.GetInformationPanel(this, "Exporting attributes...", 340, 150); SendMessageToStatusBar(this, new StatusBarMessageEventArgs("Initializing attributes...")); var bwTransferData = new BackgroundWorker { WorkerReportsProgress = true }; bwTransferData.DoWork += (sender, e) => { var attributes = (List <AttributeMetadata>)e.Argument; var entityitem = selectedLogicalName; var errors = new List <Tuple <string, string> >(); try { using (SpreadsheetDocument document = SpreadsheetDocument.Create(saveFileDlg.FileName, SpreadsheetDocumentType.Workbook)) { WorkbookPart workbookPart; Sheets sheets; SheetData sheetData; TemplateHelper.InitDocument(document, out workbookPart, out sheets); TemplateHelper.CreateSheet(workbookPart, sheets, entityitem.LogicalName, out sheetData); #region Header var headerRow = new Row(); foreach (var field in AttributeMetadataRow.GetColumns()) { headerRow.AppendChild(TemplateHelper.CreateCell(field.Type, field.Header)); } sheetData.AppendChild(headerRow); #endregion #region Data foreach (var attribute in attributes) { if (attribute == null) { continue; } var attributeType = EntityHelper.GetAttributeFromTypeName(attribute.AttributeType.Value.ToString()); if (attributeType == null) { continue; } attributeType.LoadFromAttributeMetadata(attribute); var metadataRow = attributeType.ToAttributeMetadataRow(); sheetData.AppendChild(metadataRow.ToTableRow()); } #endregion workbookPart.Workbook.Save(); } } catch (FaultException <OrganizationServiceFault> error) { errors.Add(new Tuple <string, string>(entityitem.LogicalName, error.Message)); } e.Result = errors; }; bwTransferData.RunWorkerCompleted += (sender, e) => { Controls.Remove(informationPanel); informationPanel.Dispose(); SendMessageToStatusBar(this, new StatusBarMessageEventArgs(string.Empty)); ManageWorkingState(false); var errors = (List <Tuple <string, string> >)e.Result; if (errors.Count > 0) { var errorDialog = new ErrorList((List <Tuple <string, string> >)e.Result); errorDialog.ShowDialog(ParentForm); } }; bwTransferData.ProgressChanged += (sender, e) => { InformationPanel.ChangeInformationPanelMessage(informationPanel, e.UserState.ToString()); SendMessageToStatusBar(this, new StatusBarMessageEventArgs(e.UserState.ToString())); }; bwTransferData.RunWorkerAsync(lvAttributes.Items.Cast <ListViewItem>().Select(v => (AttributeMetadata)v.Tag).ToList()); } }
private void PopulateAttributes() { if (!workingstate) { // Reinit other controls lvAttributes.Items.Clear(); // Setup Column Headings lvAttributes.Columns.Clear(); lvAttributes.Columns.Add("Action"); foreach (var column in AttributeMetadataRow.GetColumns()) { lvAttributes.Columns.Add(column.Header, column.Width); } if (cmbEntities.SelectedItem != null) { var entityitem = (EntityItem)cmbEntities.SelectedItem; if (!string.IsNullOrEmpty(entityitem.LogicalName)) { ManageWorkingState(true); // Launch treatment var bwFill = new BackgroundWorker(); bwFill.DoWork += (sender, e) => { // Retrieve var entity = MetadataHelper.RetrieveEntity(entityitem.LogicalName, service); // Prepare list of items var itemList = new List <ListViewItem>(); foreach (AttributeMetadata att in entity.Attributes) { if (att.IsValidForUpdate.Value && att.IsCustomizable.Value) { var attribute = EntityHelper.GetAttributeFromTypeName(att.AttributeType.Value.ToString()); if (attribute == null) { continue; } attribute.LoadFromAttributeMetadata(att); var row = attribute.ToAttributeMetadataRow(); var item = row.ToListViewItem(); item.Tag = att; itemList.Add(item); } } UpdateInformation(entity.Attributes); e.Result = itemList; }; bwFill.RunWorkerCompleted += (sender, e) => { if (e.Error != null) { MessageBox.Show(this, "An error occured: " + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { var items = (List <ListViewItem>)e.Result; if (items.Count == 0) { MessageBox.Show(this, "The entity does not contain any attributes", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { lvAttributes.Items.AddRange(items.ToArray()); SendMessageToStatusBar(this, new StatusBarMessageEventArgs(string.Format("{0} customizable attributes loaded", items.Count))); } } ManageWorkingState(false); }; bwFill.RunWorkerAsync(); } } } }