/// <summary>
            /// Checks whether the row version for a sales transaction is valid or not.
            /// </summary>
            /// <param name="context">The database context.</param>
            /// <param name="transactionId">The sales transaction identifier.</param>
            /// <param name="rowversion">The row version value.</param>
            /// <remarks>An exception is thrown for a invalid row version.</remarks>
            /// <returns>Transaction's created date time.</returns>
            private static DateTime RetrieveTransactionCreatedDateOrThrowOnInvalidRowVersion(SqliteDatabaseContext context, string transactionId, byte[] rowversion)
            {
                const string GetRowVersionQueryString =
                    "SELECT {0}, {1} FROM [crt].SALESTRANSACTION WHERE TRANSACTIONID = @TRANSACTIONID;";

                var query = new SqlQuery(GetRowVersionQueryString, RowversionColumnName, CreatedDateTimeColumName);

                query.Parameters["@TRANSACTIONID"] = transactionId;

                byte[] savedRowverion = null;

                // default created time as utc now
                DateTime createdDateTime = DateTime.UtcNow;

                SalesTransactionData transactionData = context.ReadEntity <SalesTransactionData>(query).FirstOrDefault();

                if (transactionData != null)
                {
                    savedRowverion = transactionData.Version;

                    // update created date time value in case it exists in the db
                    createdDateTime = transactionData.CreatedDateTime.DateTime;
                }

                // null is fine since the record does not exist yet, otherwise compare to see if they match
                if (savedRowverion != null && !RowVersionHelper.AreEquals(rowversion, savedRowverion))
                {
                    throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectVersionMismatchError, "Version mismatch while saving transaction.");
                }

                return(createdDateTime);
            }
Beispiel #2
0
        static MachineDtoMapper()
        {
            Mapper.CreateMap <Machine, MachineDto>()
            .ForMember(d => d.Version, x => x.MapFrom(y => RowVersionHelper.ConvertToString(y.RowVersion)));

            Mapper.CreateMap <Credentials, CredentialsDto>();
        }
            /// <summary>
            /// Creates and adds a data row based on the sales transaction data contents.
            /// </summary>
            /// <param name="table">The data table to be populated.</param>
            /// <param name="salesTransactionData">The sales transaction data.</param>
            /// <param name="createdDateTime">The transaction's created date time.</param>
            private static void AddSalesTransactionDataRow(DataTable table, SalesTransactionData salesTransactionData, DateTime createdDateTime)
            {
                // no out of the box rowversion support in sqlite, increment value here
                salesTransactionData.Version = RowVersionHelper.Increment(salesTransactionData.Version);

                DataRow row = salesTransactionData.CreateDataRow(table);

                // all fields must be set otherwise the insert or update query will clear them out
                row[CreatedDateTimeColumName]   = createdDateTime;
                row[ModifiedDateTimeColumnName] = DateTime.UtcNow;

                table.Rows.Add(row);
            }
Beispiel #4
0
        public void Can_convert_between_string_and_byte_array()
        {
            var bytes = new byte[] { 255, 24 };

            var str = RowVersionHelper.ConvertToString(bytes);

            var resultBytes = RowVersionHelper.ConvertToBytes(str);

            var resultStr = RowVersionHelper.ConvertToString(resultBytes);

            Assert.AreEqual(bytes, resultBytes);
            Assert.AreEqual(str, resultStr);
        }
        // PUT /api/machines/5
        public HttpResponseMessage Put(int id, [FromBody] MachineDto machineDto)
        {
            //zero is passed back if the top 'null' option is selected inthe web form drop-down
            //   if (machineDto.CredentialsId == 0) machineDto.CredentialsId = null;

            //validate
            if (!ModelState.IsValid)
            {
                _logger.Error("MachinesController.Put failed: model state not valid");


                throw new HttpResponseException(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content    = new StringContent("Model state not valid")
                });
            }


            var machine = _unitOfWork.Machines.GetById(id);


            //check a machine exists
            if (machine == null)
            {
                throw new HttpResponseException(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.NotFound,
                    Content    = new StringContent("Machine not found")
                });
            }

            //all's well - so update the machine

            //but first, validate that the data hasn't been changed by another user since we loaded it
            var machineRV = RowVersionHelper.ConvertToString(machine.RowVersion);

            if (machineRV != machineDto.Version)
            {
                _logger.Error("Concurrency exception - mismatched rowversions : " + machineRV + " | " + machineDto.Version);

                throw new HttpResponseException(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content    = new StringContent("Concurrency exception")
                });
            }

            machine.Update(machineDto);

            _unitOfWork.Commit();


            var response = Request.CreateResponse(HttpStatusCode.NoContent);

            //pass the updated rowversion info back in an eTag
            var version = RowVersionHelper.ConvertToString(machine.RowVersion);

            response.Headers.ETag = new EntityTagHeaderValue((QuotedString)version, true);

            return(response);
        }