示例#1
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();
            }
        }