Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UPCRMUndoRecord"/> class.
        /// </summary>
        /// <param name="recordIdentification">The record identification.</param>
        /// <param name="rollbackInfo">The rollback information.</param>
        /// <param name="undoRequest">The undo request.</param>
        public UPCRMUndoRecord(string recordIdentification, Dictionary <string, object> rollbackInfo, CrmUndoRequest undoRequest)
            : this(recordIdentification, rollbackInfo["mode"] as string, undoRequest)
        {
            List <object> fields = rollbackInfo["fields"] as List <object>;

            this.FieldDictionary = new Dictionary <string, UPCRMUndoField>();

            foreach (List <object> fieldInfo in fields)
            {
                UPCRMUndoField undoField = new UPCRMUndoField(fieldInfo);
                this.AddFieldValue(undoField);
            }
        }
Exemple #2
0
        /// <summary>
        /// Adds the field value.
        /// </summary>
        /// <param name="field">The field.</param>
        private void AddFieldValue(UPCRMUndoField field)
        {
            if (field == null)
            {
                return;
            }

            if (this.FieldDictionary == null)
            {
                this.FieldDictionary = new Dictionary <string, UPCRMUndoField>();
            }

            if (field.FieldName != null)
            {
                this.FieldDictionary[field.FieldName] = field;
            }
        }
Exemple #3
0
        /// <summary>
        /// Applies the changes from record.
        /// </summary>
        /// <param name="record">The record.</param>
        public void ApplyChangesFromRecord(UPCRMRecord record)
        {
            UPCRMTableInfo tableInfo = this.UndoRequest.DataStore.TableInfoForInfoArea(this.RecordIdentification.InfoAreaId());

            if (record.FieldValues != null)
            {
                foreach (UPCRMFieldValue value in record.FieldValues)
                {
                    UPCRMFieldInfo fieldInfo = tableInfo.FieldInfoForFieldId(value.FieldId);
                    if (fieldInfo != null)
                    {
                        UPCRMUndoField undoField = new UPCRMUndoField(fieldInfo.DatabaseFieldName, value.Value, null);
                        this.AddFieldValue(undoField);
                    }
                }
            }

            if (record.Links != null)
            {
                foreach (UPCRMLink link in record.Links)
                {
                    UPCRMLinkInfo linkInfo = tableInfo.LinkInfoForTargetInfoAreaIdLinkId(link.InfoAreaId, link.LinkId);
                    if (linkInfo?.HasColumn ?? false)
                    {
                        UPCRMUndoField undoField = new UPCRMUndoField(linkInfo.LinkFieldName, link.RecordId, null);
                        this.AddFieldValue(undoField);
                        string infoAreaColumnName = linkInfo.InfoAreaLinkFieldName;
                        if (!string.IsNullOrEmpty(infoAreaColumnName))
                        {
                            undoField = new UPCRMUndoField(infoAreaColumnName, link.InfoAreaId, null);
                            this.AddFieldValue(undoField);
                        }
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Checks the update before cache save with database.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <returns>0, if success, else error number</returns>
        private int CheckUpdateBeforeCacheSaveWithDatabase(DatabaseBase database)
        {
            UPCRMTableInfo tableInfo       = this.UndoRequest.DataStore.TableInfoForInfoArea(this.RecordIdentification.InfoAreaId());
            StringBuilder  selectStatement = new StringBuilder();

            selectStatement.Append("SELECT ");
            bool first = true;

            List <string> allColumns = this.FieldDictionary.Keys.ToList();

            foreach (string columnName in allColumns)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    selectStatement.Append(", ");
                }

                selectStatement.Append(columnName);
            }

            if (first)
            {
                selectStatement.Append("recid");
            }

            selectStatement.Append($" FROM {tableInfo.DatabaseTableName} WHERE recid = ?");
            CRMDatabase       db = this.UndoRequest.DataStore.DatabaseInstance;
            DatabaseRecordSet rs = new DatabaseRecordSet(db);

            if (!rs.Query.Prepare(selectStatement.ToString()))
            {
                return(-1);
            }

            rs.Query.Bind(1, this.RecordIdentification.RecordId());
            int ret = rs.Execute();

            if (ret != 0)
            {
                return(ret);
            }

            int rc = rs.GetRowCount();

            if (rc == 0)
            {
                this.Mode          = "UpdateNew";
                this.UndoOperation = "Delete";
                return(0);
            }

            if (rc > 1)
            {
                return(-1);
            }

            this.UndoOperation = "Update";
            int         colIndex = 0;
            DatabaseRow row      = rs.GetRow(0);

            foreach (string col in allColumns)
            {
                string v = row.GetColumn(colIndex++);
                if (v != null)
                {
                    UPCRMUndoField undoField = this.FieldDictionary[col];
                    undoField.OldValue = v;
                }
            }

            return(0);
        }