public FdpDerivativeMapping FdpDerivativeMappingDelete(FdpDerivativeMapping derivativeMapping)
        {
            FdpDerivativeMapping retVal = new EmptyFdpDerivativeMapping();
            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@FdpDerivativeMappingId", derivativeMapping.FdpDerivativeMappingId, DbType.Int32);
                    para.Add("@CDSId", CurrentCDSID, DbType.String);

                    var results = conn.Query<FdpDerivativeMapping>("Fdp_DerivativeMapping_Delete", para, commandType: CommandType.StoredProcedure);
                    if (results.Any())
                    {
                        retVal = results.First();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
 public async Task<FdpDerivativeMapping> CopyFdpDerivativeMappingToDocument(FdpDerivativeMapping fdpDerivativeMapping, int targetDocumentId)
 {
     return await Task.FromResult(_derivativeDataStore.FdpDerivativeMappingCopy(fdpDerivativeMapping, targetDocumentId));
 }
 public async Task<FdpDerivativeMapping> DeleteFdpDerivativeMapping(FdpDerivativeMapping derivativeMappingToDelete)
 {
     return await Task.FromResult(_derivativeDataStore.FdpDerivativeMappingDelete(derivativeMappingToDelete));
 }
 public async Task<ImportError> MapDerivative(ImportQueueFilter filter, FdpDerivativeMapping derivativeMapping)
 {
     var task = await Task.FromResult(_derivativeDataStore.FdpDerivativeMappingSave(derivativeMapping));
     return await Task.FromResult(_importDataStore.ImportErrorGet(filter));
 }
        public async Task<ActionResult> MapOxoDerivative(ImportExceptionParameters parameters)
        {
            var filter = ImportQueueFilter.FromExceptionId(parameters.ExceptionId.GetValueOrDefault());

            var derivative = Derivative.FromIdentifier(parameters.DerivativeCode);
            var importView = await GetModelFromParameters(parameters);

            var importDerivatives = (IEnumerable<string>) TempData["MapOxoDerivative"];
            
            foreach (var importDerivative in importDerivatives)
            {
                var derivativeMapping = new FdpDerivativeMapping()
                {
                    ImportDerivativeCode = importDerivative,
                    DocumentId = parameters.DocumentId.GetValueOrDefault(),
                    ProgrammeId = parameters.ProgrammeId.GetValueOrDefault(),
                    Gateway = parameters.Gateway,
                    DerivativeCode = derivative.DerivativeCode,
                    BodyId = derivative.BodyId.GetValueOrDefault(),
                    EngineId = derivative.EngineId.GetValueOrDefault(),
                    TransmissionId = derivative.TransmissionId.GetValueOrDefault()
                };

                await DataContext.Import.MapDerivative(filter, derivativeMapping);
            }
            await DeactivateException(importView.CurrentException);
            await ReProcessException(importView.CurrentException);

            return Json(JsonActionResult.GetSuccess(), JsonRequestBehavior.AllowGet);
        }
        public async Task<ActionResult> MapMissingDerivative(ImportExceptionParameters parameters)
        {
            var filter = ImportQueueFilter.FromExceptionId(parameters.ExceptionId.GetValueOrDefault());
            var derivative = Derivative.FromIdentifier(parameters.DerivativeCode);
            var importView = await GetModelFromParameters(parameters);

            //var derivative = importView.AvailableDerivatives
            //    .First(d => d.DerivativeCode.Equals(parameters.DerivativeCode, StringComparison.InvariantCultureIgnoreCase));

            var derivativeMapping = new FdpDerivativeMapping()
            {
                ImportDerivativeCode = parameters.ImportDerivativeCode,
                
                DocumentId = parameters.DocumentId.GetValueOrDefault(),
                ProgrammeId = parameters.ProgrammeId.GetValueOrDefault(),
                Gateway = parameters.Gateway,
                DerivativeCode = derivative.DerivativeCode,
                BodyId = derivative.BodyId.GetValueOrDefault(),
                EngineId = derivative.EngineId.GetValueOrDefault(),
                TransmissionId = derivative.TransmissionId.GetValueOrDefault()
            };
            importView.CurrentException = await DataContext.Import.MapDerivative(filter, derivativeMapping);
            await DeactivateException(importView.CurrentException);
            await ReProcessException(importView.CurrentException);

            return Json(JsonActionResult.GetSuccess(), JsonRequestBehavior.AllowGet);
        }
        public FdpDerivativeMapping FdpDerivativeMappingCopy(FdpDerivativeMapping derivativeMappingToCopy, int toDocumentId)
        {
            FdpDerivativeMapping retVal = new EmptyFdpDerivativeMapping();
            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();

                    para.Add("@FdpDerivativeMappingId", derivativeMappingToCopy.FdpDerivativeMappingId, DbType.Int32);
                    para.Add("@DocumentId", toDocumentId, DbType.Int32);
                    para.Add("@CDSId", CurrentCDSID, DbType.String);

                    var rows = conn.Execute("Fdp_DerivativeMapping_Copy", para, commandType: CommandType.StoredProcedure);

                    retVal = FdpDerivativeMappingGet(new DerivativeMappingFilter { DerivativeMappingId = derivativeMappingToCopy.FdpDerivativeMappingId });
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public FdpDerivativeMapping FdpDerivativeMappingSave(FdpDerivativeMapping derivativeMapping)
        {
            FdpDerivativeMapping retVal = new EmptyFdpDerivativeMapping();
            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();

                    para.Add("@ImportDerivativeCode", derivativeMapping.ImportDerivativeCode, DbType.String);
                    para.Add("@DocumentId", derivativeMapping.DocumentId, DbType.Int32);
                    //para.Add("@ProgrammeId", derivativeMapping.ProgrammeId, DbType.Int32);
                    //para.Add("@Gateway", derivativeMapping.Gateway, DbType.String);
                    if (!string.IsNullOrEmpty(derivativeMapping.DerivativeCode))
                    {
                        para.Add("@DerivativeCode", derivativeMapping.DerivativeCode, DbType.String);
                    }
                    para.Add("@BodyId", derivativeMapping.BodyId, DbType.Int32);
                    para.Add("@EngineId", derivativeMapping.EngineId, DbType.Int32);
                    para.Add("@TransmissionId", derivativeMapping.TransmissionId, DbType.Int32);
                    para.Add("@CDSId", CurrentCDSID, DbType.String);

                    var results = conn.Query<FdpDerivativeMapping>("Fdp_DerivativeMapping_Save", para, commandType: CommandType.StoredProcedure);
                    var fdpDerivativeMappings = results as IList<FdpDerivativeMapping> ?? results.ToList();
                    if (fdpDerivativeMappings.Any())
                    {
                        retVal = fdpDerivativeMappings.First();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }