示例#1
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            string recordTypeName = null;

            if (comboImportOptions.SelectedIndex == 0)
            {
                recordTypeName = txtRecordTypeName.Text;
            }
            else if (comboImportOptions.SelectedIndex == 1)
            {
                recordTypeName = comboRecordTypeSelector.SelectedText;
            }

            var selectedHeaders = new Dictionary <int, string>();

            foreach (DataGridViewRow row in gridFieldAssignment.Rows)
            {
                if (row.Cells[0].Value != null && ValidRow(row))
                {
                    var colIndex = Convert.ToInt32(row.Cells[0].Value);
                    var attrName = row.Cells[1].Value.ToString();
                    selectedHeaders.Add(colIndex, attrName);
                }
            }

            StatusObject importStatus = null;

            if (comboImportOptions.SelectedIndex == 0)
            {
                RecordType rT = null;
                using (var oH = new ObjectHelper(_dbSettings, _user))
                {
                    var addRecordTypeStatus = oH.AddRecordType(recordTypeName, selectedHeaders.Values.ToList(), true);
                    if (addRecordTypeStatus.Success)
                    {
                        var fetchRecordType = oH.GetRecordTypeById((int)addRecordTypeStatus.Result);
                        if (fetchRecordType.Success)
                        {
                            rT = (RecordType)fetchRecordType.Result;
                        }
                    }
                }

                if (rT != null)
                {
                    var records = _dP.GetRecordsFromSheet(rT, selectedHeaders, _fileImportPath);
                    if (records.Length > 0)
                    {
                        using (var oH = new ObjectHelper(_dbSettings, _user))
                        {
                            importStatus = oH.AddRecords(records, rT);
                        }
                    }
                }
            }
            else
            {
                var rT      = _recordTypes.First(x => x.Name.Equals(comboRecordTypeSelector.SelectedItem));
                var records = _dP.GetRecordsFromSheet(rT, selectedHeaders, _fileImportPath);
                if (records.Length > 0)
                {
                    using (var oH = new ObjectHelper(_dbSettings, _user))
                    {
                        importStatus = oH.AddRecords(records, rT);
                    }
                }
            }

            if (importStatus != null && importStatus.Success)
            {
                NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Information,
                                                    "Successfully imported " + (int)importStatus.Result + " records!");
            }
            else
            {
                NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                    "Failed to import records. Please try again.");
            }
        }
示例#2
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var needsDbAction = false;
            var newAttrs      = new List <string>();

            //Remove removed attributes in existing records
            if (_recordType != null && _removedAttrs.Count > 0)
            {
                foreach (var rAttr in _removedAttrs)
                {
                    _recordType.Attributes.Remove(rAttr);
                }
                needsDbAction = true;
            }

            if (_recordType != null && !_recordType.Name.Equals(txtRecTypeName.Text))
            {
                _recordType.Name = txtRecTypeName.Text;
                needsDbAction    = true;
            }

            if (_removedAttrs.Count > 0)
            {
                needsDbAction = true;
            }


            foreach (var ctrl in _txtControls)
            {
                var attrVal = ctrl.Text;
                if (ctrl.Tag.Equals("ADDED"))
                {
                    //This is a new attribute, add its value to the new attr list
                    newAttrs.Add(ctrl.Text);
                    needsDbAction = true;
                }
                else
                {
                    //This is an existing attribute, retrieve its id and value
                    var attrId = ctrl.Tag.ToString();
                    _recordType.Attributes.TryGetValue(attrId, out var currAttrVal);
                    if (!attrVal.Equals(currAttrVal) && _recordType.Attributes.ContainsKey(attrId))
                    {
                        //new value doesn't match current value, update it
                        _recordType.Attributes[attrId] = attrVal;
                        needsDbAction = true;
                    }
                }
            }

            if (_recordType == null && needsDbAction)
            {
                //Adding a new one
                using (var oH = new ObjectHelper(_dbSettings, _user))
                {
                    var addStatus = oH.AddRecordType(txtRecTypeName.Text, newAttrs, true);
                    if (addStatus.Success)
                    {
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    else
                    {
                        NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                            "Failed to add this new record type. Please try again.");
                    }
                }
            }
            else if (needsDbAction)
            {
                foreach (var attr in newAttrs)
                {
                    var newId = DataProcessor.GetStrId();
                    while (_recordType.Attributes.ContainsKey(newId))
                    {
                        newId = DataProcessor.GetStrId(); //Keep generating a new strId until it's unique
                    }
                    _recordType.Attributes.Add(newId, attr);
                }


                using (var oH = new ObjectHelper(_dbSettings, _user))
                {
                    var updateStatus = oH.UpdateRecordType(_recordType);
                    if (!updateStatus.Success)
                    {
                        NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                            "Failed to update this record type. Please try again.");
                        return;
                    }

                    var cleanupStatus = oH.CleanupRecordAttributes(_recordType, _removedAttrs);
                    if (!cleanupStatus.Success)
                    {
                        NotificationHelper.ShowNotification(this, NotificationHelper.NotificationType.Error,
                                                            "Failed to remove attributes from orphaned records. Please try again.");
                        return;
                    }

                    if (updateStatus.Success && cleanupStatus.Success)
                    {
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
            }
            else
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }