Example #1
0
        /// <summary>
        /// Archives a DE Message
        /// </summary>
        /// <param name="de">DE message to archive</param>
        /// <param name="sourceip">Source IP Address of the message</param>
        public bool ArchiveDE(DEv1_0 de, string sourceip)
        {
            bool              wasSuccessful     = false;
            NpgsqlCommand     command           = null;
            NpgsqlTransaction sqlTrans          = null;
            NpgsqlConnection  currentConnection = null;

            try
            {
                currentConnection = GetDatabaseConnection();
                if (OpenConnection(currentConnection) == false)
                {
                    return(wasSuccessful);
                }
                sqlTrans            = currentConnection.BeginTransaction();
                command             = currentConnection.CreateCommand();
                command.Transaction = sqlTrans;

                //add DE first
                //assumes calling function will wrap this in a try catch block
                //allows the error to be thrown to the calling function
                int iHash = DEUtilities.ComputeDELookupID(de);

                string sTable   = QualifiedTableName(TableNames.MessageArchive);
                string sColumns = MessageArchiveColumns.DELookupID + "," + MessageArchiveColumns.DistributionID + "," + MessageArchiveColumns.SenderID + "," +
                                  MessageArchiveColumns.DateTimeSent + "," + MessageArchiveColumns.SenderIP + "," + MessageArchiveColumns.DateTimeLogged + "," + MessageArchiveColumns.DE;
                command.CommandText = "INSERT INTO " + sTable + " (" + sColumns + ") VALUES (@DEHash, @DistributionID, @SenderID, @DateTimeSent, @SourceIP, @DateTimeLogged, @DEv1_0)";
                command.Parameters.Clear();
                AddParameter(command, NpgsqlDbType.Integer, "DEHash", iHash);
                AddParameter(command, NpgsqlDbType.Text, "DistributionID", de.DistributionID);
                AddParameter(command, NpgsqlDbType.Text, "SenderID", de.SenderID);
                AddParameter(command, NpgsqlDbType.TimestampTZ, "DateTimeSent", de.DateTimeSent);
                AddParameter(command, NpgsqlDbType.Text, "SourceIP", sourceip);
                AddParameter(command, NpgsqlDbType.TimestampTZ, "DateTimeLogged", DateTime.UtcNow);
                AddParameter(command, NpgsqlDbType.Xml, "DEv1_0", de.ToString());
                Log.Debug(command.CommandText);
                command.ExecuteNonQuery();
                sqlTrans.Commit();
                wasSuccessful = true;
            }
            catch (Exception Ex)
            {
                Log.Error("General Error in AddedDEToCache()", Ex);
                this.WasRolledBackTransaction(sqlTrans);
            }
            finally
            {
                CloseConnection(currentConnection);
            }

            return(wasSuccessful);
        }
Example #2
0
        public HttpResponseMessage Post([FromBody] DEv1_0 value)
        {
            // Not a Valid DE Message
            if (value == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The message was not a valid format.  The DE could not be created"));
            }

            string erMsg;

            if (DEUtilities.ValidateDE(value, out erMsg) == false)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, erMsg));
            }

            // Getting DE lookup ID
            int deID = DEUtilities.ComputeDELookupID(value);

            if (value.DistributionType == TypeValue.Update)
            {
                return(Put(deID, value));
            }
            else if (value.DistributionType == TypeValue.Cancel)
            {
                if (this.ArchiveEnabled)
                {
                    string clientAddress = HttpContext.Current.Request.UserHostAddress;
                    archiveDal.ArchiveDE(value, clientAddress);
                }

                return(Delete(deID));
            }
            else if (value.DistributionType == TypeValue.Report)
            {
                try
                {
                    if (dbDal.CreatedDE(value, out deID))
                    {
                        DELiteDTO retVal = new DELiteDTO();
                        retVal.LookupID       = deID;
                        retVal.DateTimeSent   = value.DateTimeSent;
                        retVal.DistributionID = value.DistributionID;
                        retVal.SenderID       = value.SenderID;
                        this.CreatedAtRoute("GetSingleDERule", new
                        {
                            lookupID = deID
                        }, value);
                        if (this.ArchiveEnabled)
                        {
                            string clientAddress = HttpContext.Current.Request.UserHostAddress;
                            archiveDal.ArchiveDE(value, clientAddress);
                        }
                        return(Request.CreateResponse(HttpStatusCode.Created, retVal));
                    }
                    else
                    {
                        // something went wrong
                        return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The DE could not be created"));
                    }
                }
                catch (ArgumentNullException e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The DE was invalid"));
                }
                catch (NpgsqlException e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Database error occurred"));
                }
                catch (FederationException e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occurred when federating the DE"));
                }
                catch (Exception e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occurred when adding the DE"));
                }
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unsupported DE Distribution Type Found"));
            }
        }