/// <summary>
        /// Sends a background request to Protect My Ministry
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="workflow">The Workflow initiating the request.</param>
        /// <param name="personAttribute">The person attribute.</param>
        /// <param name="ssnAttribute">The SSN attribute.</param>
        /// <param name="requestTypeAttribute">The request type attribute.</param>
        /// <param name="billingCodeAttribute">The billing code attribute.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns>
        /// True/False value of whether the request was successfully sent or not
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// Note: If the associated workflow type does not have attributes with the following keys, they
        /// will automatically be added to the workflow type configuration in order to store the results
        /// of the PMM background check request
        ///     ReportStatus:           The status returned by PMM
        ///     ReportLink:             The location of the background report on PMM server
        ///     ReportRecommendation:   PMM's recomendataion
        ///     Report (BinaryFile):    The downloaded background report
        /// </remarks>
        public override bool SendRequest( RockContext rockContext, Model.Workflow workflow,
            AttributeCache personAttribute, AttributeCache ssnAttribute, AttributeCache requestTypeAttribute,
            AttributeCache billingCodeAttribute, out List<string> errorMessages )
        {
            errorMessages = new List<string>();

            try
            {
                // Check to make sure workflow is not null
                if ( workflow == null )
                {
                    errorMessages.Add( "The 'ProtectMyMinistry' requires a valid workflow." );
                    return false;
                }

                // Get the person that the request is for
                Person person = null;
                if ( personAttribute != null )
                {
                    Guid? personAliasGuid = workflow.GetAttributeValue( personAttribute.Key ).AsGuidOrNull();
                    if ( personAliasGuid.HasValue )
                    {
                        person = new PersonAliasService( rockContext ).Queryable()
                            .Where( p => p.Guid.Equals( personAliasGuid.Value ) )
                            .Select( p => p.Person )
                            .FirstOrDefault();
                    }
                }

                if ( person == null )
                {
                    errorMessages.Add( "The 'ProtectMyMinistry' background check requires the workflow to have a 'Person' attribute that contains the person who the background check is for." );
                    return false;
                }

                string password = Encryption.DecryptString( GetAttributeValue( "Password" ) );

                XElement rootElement = new XElement( "OrderXML",
                    new XElement( "Method", "SEND ORDER" ),
                    new XElement( "Authentication",
                        new XElement( "Username", GetAttributeValue( "UserName" ) ),
                        new XElement( "Password", password )
                    )
                );

                if ( GetAttributeValue( "TestMode" ).AsBoolean() )
                {
                    rootElement.Add( new XElement( "TestMode", "YES" ) );
                }

                rootElement.Add( new XElement( "ReturnResultURL", GetAttributeValue( "ReturnURL" ) ) );

                XElement orderElement = new XElement( "Order" );
                rootElement.Add( orderElement );

                if ( billingCodeAttribute != null )
                {
                    Guid? campusGuid = workflow.GetAttributeValue( billingCodeAttribute.Key ).AsGuidOrNull();
                    if ( campusGuid.HasValue )
                    {
                        var campus = CampusCache.Read( campusGuid.Value );
                        if ( campus != null )
                        {
                            orderElement.Add( new XElement( "BillingReferenceCode", campus.Name ) );
                        }
                    }
                }

                XElement subjectElement = new XElement( "Subject",
                    new XElement( "FirstName", person.FirstName ),
                    new XElement( "MiddleName", person.MiddleName ),
                    new XElement( "LastName", person.LastName )
                );
                orderElement.Add( subjectElement );

                if ( person.BirthDate.HasValue )
                {
                    subjectElement.Add( new XElement( "DOB", person.BirthDate.Value.ToString( "MM/dd/yyyy" ) ) );
                }

                if ( ssnAttribute != null )
                {
                    string ssn = Encryption.DecryptString( workflow.GetAttributeValue( ssnAttribute.Key ) ).AsNumeric();
                    if ( !string.IsNullOrWhiteSpace( ssn ) && ssn.Length == 9 )
                    {
                        subjectElement.Add( new XElement( "SSN", ssn.Insert( 5, "-" ).Insert( 3, "-" ) ) );
                    }
                }

                if ( person.Gender == Gender.Male )
                {
                    subjectElement.Add( new XElement( "Gender", "Male" ) );
                }
                if ( person.Gender == Gender.Female )
                {
                    subjectElement.Add( new XElement( "Gender", "Female" ) );
                }

                var homelocation = person.GetHomeLocation();
                if ( homelocation != null)
                {
                    subjectElement.Add( new XElement( "CurrentAddress",
                        new XElement( "StreetAddress", homelocation.Street1 ),
                        new XElement( "City", homelocation.City ),
                        new XElement( "State", homelocation.State ),
                        new XElement( "Zipcode", homelocation.PostalCode )
                    ) );
                }

                XElement aliasesElement = new XElement( "Aliases" );
                if ( person.NickName != person.FirstName )
                {
                    aliasesElement.Add( new XElement( "Alias", new XElement( "FirstName", person.NickName ) ) );
                }
                // foreach ( string lastName in [previous last names] )
                //{
                //    aliasesElement.Add( new XElement( "Alias", new XElement( "LastName", lastName ) ) );
                //}
                if ( aliasesElement.HasElements )
                {
                    subjectElement.Add( aliasesElement );
                }

                string packageType = requestTypeAttribute != null ? workflow.GetAttributeValue( requestTypeAttribute.Key ) : string.Empty;
                if ( string.IsNullOrWhiteSpace(packageType) )
                {
                    packageType = "Basic";
                }
                orderElement.Add( new XElement( "PackageServiceCode", packageType,
                    new XAttribute( "OrderId", workflow.Id.ToString() ) ) );
                orderElement.Add( new XElement( "OrderDetail",
                    new XAttribute( "OrderId", workflow.Id.ToString() ),
                    new XAttribute( "ServiceCode", "combo" ) ) );

                XDocument xdoc = new XDocument( new XDeclaration( "1.0", "UTF-8", "yes" ), rootElement );

                XDocument xResult = PostToWebService( xdoc, GetAttributeValue("RequestURL") );

                if ( _HTTPStatusCode == HttpStatusCode.OK )
                {
                    if ( xResult.Root.Descendants().Count() > 0 )
                    {
                        SaveResults( xResult, workflow, rockContext );
                    }
                    return true;
                }
                else
                {
                    errorMessages.Add( "Invalid HttpStatusCode: " + _HTTPStatusCode.ToString() );
                }

                return false;

            }
            catch( Exception ex )
            {
                ExceptionLogService.LogException( ex, null );
                errorMessages.Add( ex.Message );
                return false;
            }
        }
Exemple #2
0
        /// <summary>
        /// Sends a background request to Protect My Ministry
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="workflow">The Workflow initiating the request.</param>
        /// <param name="personAttribute">The person attribute.</param>
        /// <param name="ssnAttribute">The SSN attribute.</param>
        /// <param name="requestTypeAttribute">The request type attribute.</param>
        /// <param name="billingCodeAttribute">The billing code attribute.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns>
        /// True/False value of whether the request was successfully sent or not
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// Note: If the associated workflow type does not have attributes with the following keys, they
        /// will automatically be added to the workflow type configuration in order to store the results
        /// of the PMM background check request
        ///     RequestStatus:          The request status returned by PMM request
        ///     RequestMessage:         Any error messages returned by PMM request
        ///     ReportStatus:           The report status returned by PMM
        ///     ReportLink:             The location of the background report on PMM server
        ///     ReportRecommendation:   PMM's recomendataion
        ///     Report (BinaryFile):    The downloaded background report
        /// </remarks>
        public override bool SendRequest( RockContext rockContext, Model.Workflow workflow,
            AttributeCache personAttribute, AttributeCache ssnAttribute, AttributeCache requestTypeAttribute,
            AttributeCache billingCodeAttribute, out List<string> errorMessages )
        {
            errorMessages = new List<string>();

            try
            {
                // Check to make sure workflow is not null
                if ( workflow == null )
                {
                    errorMessages.Add( "The 'Protect My Ministry' background check provider requires a valid workflow." );
                    return false;
                }

                // Get the person that the request is for
                Person person = null;
                if ( personAttribute != null )
                {
                    Guid? personAliasGuid = workflow.GetAttributeValue( personAttribute.Key ).AsGuidOrNull();
                    if ( personAliasGuid.HasValue )
                    {
                        person = new PersonAliasService( rockContext ).Queryable()
                            .Where( p => p.Guid.Equals( personAliasGuid.Value ) )
                            .Select( p => p.Person )
                            .FirstOrDefault();
                        person.LoadAttributes( rockContext );
                    }
                }

                if ( person == null )
                {
                    errorMessages.Add( "The 'Protect My Ministry' background check provider requires the workflow to have a 'Person' attribute that contains the person who the background check is for." );
                    return false;
                }

                string password = Encryption.DecryptString( GetAttributeValue( "Password" ) );

                XElement rootElement = new XElement( "OrderXML",
                    new XElement( "Method", "SEND ORDER" ),
                    new XElement( "Authentication",
                        new XElement( "Username", GetAttributeValue( "UserName" ) ),
                        new XElement( "Password", password )
                    )
                );

                if ( GetAttributeValue( "TestMode" ).AsBoolean() )
                {
                    rootElement.Add( new XElement( "TestMode", "YES" ) );
                }

                rootElement.Add( new XElement( "ReturnResultURL", GetAttributeValue( "ReturnURL" ) ) );

                XElement orderElement = new XElement( "Order" );
                rootElement.Add( orderElement );

                if ( billingCodeAttribute != null )
                {
                    string billingCode = workflow.GetAttributeValue( billingCodeAttribute.Key );
                    Guid? campusGuid = billingCode.AsGuidOrNull();
                    if ( campusGuid.HasValue )
                    {
                        var campus = CampusCache.Read( campusGuid.Value );
                        if ( campus != null )
                        {
                            billingCode = campus.Name;
                        }
                    }
                    orderElement.Add( new XElement( "BillingReferenceCode", billingCode ) );
                }

                XElement subjectElement = new XElement( "Subject",
                    new XElement( "FirstName", person.FirstName ),
                    new XElement( "MiddleName", person.MiddleName ),
                    new XElement( "LastName", person.LastName )
                );
                orderElement.Add( subjectElement );

                if ( person.SuffixValue != null )
                {
                    subjectElement.Add( new XElement( "Generation", person.SuffixValue.Value ) );
                }
                if ( person.BirthDate.HasValue )
                {
                    subjectElement.Add( new XElement( "DOB", person.BirthDate.Value.ToString( "MM/dd/yyyy" ) ) );
                }

                if ( ssnAttribute != null )
                {
                    string ssn = Encryption.DecryptString( workflow.GetAttributeValue( ssnAttribute.Key ) ).AsNumeric();
                    if ( !string.IsNullOrWhiteSpace( ssn ) && ssn.Length == 9 )
                    {
                        subjectElement.Add( new XElement( "SSN", ssn.Insert( 5, "-" ).Insert( 3, "-" ) ) );
                    }
                }

                if ( person.Gender == Gender.Male )
                {
                    subjectElement.Add( new XElement( "Gender", "Male" ) );
                }
                if ( person.Gender == Gender.Female )
                {
                    subjectElement.Add( new XElement( "Gender", "Female" ) );
                }

                string dlNumber = person.GetAttributeValue( "com.sparkdevnetwork.DLNumber" );
                if ( !string.IsNullOrWhiteSpace( dlNumber ) )
                {
                    subjectElement.Add( new XElement( "DLNumber", dlNumber ) );
                }

                var homelocation = person.GetHomeLocation();
                if ( homelocation != null)
                {
                    subjectElement.Add( new XElement( "CurrentAddress",
                        new XElement( "StreetAddress", homelocation.Street1 ),
                        new XElement( "City", homelocation.City ),
                        new XElement( "State", homelocation.State ),
                        new XElement( "Zipcode", homelocation.PostalCode )
                    ) );
                }

                XElement aliasesElement = new XElement( "Aliases" );
                if ( person.NickName != person.FirstName )
                {
                    aliasesElement.Add( new XElement( "Alias", new XElement( "FirstName", person.NickName ) ) );
                }

                foreach ( var previousName in person.GetPreviousNames() )
                {
                    aliasesElement.Add( new XElement( "Alias", new XElement( "LastName", previousName.LastName ) ) );
                }

                if ( aliasesElement.HasElements )
                {
                    subjectElement.Add( aliasesElement );
                }

                DefinedValueCache pkgTypeDefinedValue = null;
                string packageName = "BASIC";
                string county = string.Empty;
                string state = string.Empty;
                string mvrJurisdiction = string.Empty;
                string mvrState = string.Empty;

                if ( requestTypeAttribute != null )
                {
                    pkgTypeDefinedValue = DefinedValueCache.Read( workflow.GetAttributeValue( requestTypeAttribute.Key ).AsGuid() );
                    if ( pkgTypeDefinedValue != null )
                    {
                        if ( pkgTypeDefinedValue.Attributes == null )
                        {
                            pkgTypeDefinedValue.LoadAttributes( rockContext );
                        }

                        packageName = pkgTypeDefinedValue.GetAttributeValue("PMMPackageName");
                        county = pkgTypeDefinedValue.GetAttributeValue( "DefaultCounty" );
                        state = pkgTypeDefinedValue.GetAttributeValue( "DefaultState" );
                        Guid? mvrJurisdictionGuid = pkgTypeDefinedValue.GetAttributeValue( "MVRJurisdiction" ).AsGuidOrNull();
                        if ( mvrJurisdictionGuid.HasValue )
                        {
                            var mvrJurisdictionDv = DefinedValueCache.Read( mvrJurisdictionGuid.Value );
                            if ( mvrJurisdictionDv != null )
                            {
                                mvrJurisdiction = mvrJurisdictionDv.Value;
                                if ( mvrJurisdiction.Length >= 2 )
                                {
                                    mvrState = mvrJurisdiction.Left( 2 );
                                }
                            }
                        }

                        if ( homelocation != null )
                        {
                            if ( !string.IsNullOrWhiteSpace( homelocation.County ) &&
                                pkgTypeDefinedValue.GetAttributeValue("SendHomeCounty").AsBoolean() )
                            {
                                county = homelocation.County;
                            }

                            if ( !string.IsNullOrWhiteSpace( homelocation.State ) )
                            {
                                if ( pkgTypeDefinedValue.GetAttributeValue( "SendHomeState" ).AsBoolean() )
                                {
                                    state = homelocation.State;
                                }
                                if ( pkgTypeDefinedValue.GetAttributeValue( "SendHomeStateMVR" ).AsBoolean() )
                                {
                                    mvrState = homelocation.State;
                                }
                            }
                        }
                    }
                }

                if ( !string.IsNullOrWhiteSpace( packageName ) )
                {
                    orderElement.Add( new XElement( "PackageServiceCode", packageName,
                        new XAttribute( "OrderId", workflow.Id.ToString() ) ) );

                    if ( packageName.Trim().Equals( "BASIC", StringComparison.OrdinalIgnoreCase ) ||
                        packageName.Trim().Equals( "PLUS", StringComparison.OrdinalIgnoreCase ) )
                    {
                        orderElement.Add( new XElement( "OrderDetail",
                            new XAttribute( "OrderId", workflow.Id.ToString() ),
                            new XAttribute( "ServiceCode", "combo" ) ) );
                    }
                }

                if ( !string.IsNullOrWhiteSpace( county ) ||
                    !string.IsNullOrWhiteSpace( state ) )
                {
                    orderElement.Add( new XElement( "OrderDetail",
                        new XAttribute( "OrderId", workflow.Id.ToString() ),
                        new XAttribute( "ServiceCode", string.IsNullOrWhiteSpace(county) ? "StateCriminal" : "CountyCrim" ),
                        new XElement( "County", county ),
                        new XElement( "State", state ),
                        new XElement( "YearsToSearch", 7 ),
                        new XElement( "CourtDocsRequested", "NO" ),
                        new XElement( "RushRequested", "NO" ),
                        new XElement( "SpecialInstructions", "" ) )
                    );
                }

                if ( !string.IsNullOrWhiteSpace( mvrJurisdiction ) && !string.IsNullOrWhiteSpace( mvrState ) )
                {
                    orderElement.Add( new XElement( "OrderDetail",
                        new XAttribute( "OrderId", workflow.Id.ToString() ),
                        new XAttribute( "ServiceCode", "MVR" ),
                        new XElement( "JurisdictionCode", mvrJurisdiction ),
                        new XElement( "State", mvrState ) )
                    );
                }

                XDocument xdoc = new XDocument( new XDeclaration( "1.0", "UTF-8", "yes" ), rootElement );
                var requestDateTime = RockDateTime.Now;

                XDocument xResult = PostToWebService( xdoc, GetAttributeValue("RequestURL") );
                var responseDateTime = RockDateTime.Now;

                int? personAliasId = person.PrimaryAliasId;
                if ( personAliasId.HasValue )
                {
                    // Create a background check file
                    using ( var newRockContext = new RockContext() )
                    {
                        var backgroundCheckService = new BackgroundCheckService( newRockContext );
                        var backgroundCheck = backgroundCheckService.Queryable()
                            .Where( c =>
                                c.WorkflowId.HasValue &&
                                c.WorkflowId.Value == workflow.Id )
                            .FirstOrDefault();

                        if ( backgroundCheck == null )
                        {
                            backgroundCheck = new Rock.Model.BackgroundCheck();
                            backgroundCheck.PersonAliasId = personAliasId.Value;
                            backgroundCheck.WorkflowId = workflow.Id;
                            backgroundCheckService.Add( backgroundCheck );
                        }

                        backgroundCheck.RequestDate = RockDateTime.Now;

                        // Clear any SSN nodes before saving XML to record
                        foreach ( var xSSNElement in xdoc.Descendants( "SSN" ) )
                        {
                            xSSNElement.Value = "XXX-XX-XXXX";
                        }
                        foreach ( var xSSNElement in xResult.Descendants( "SSN" ) )
                        {
                            xSSNElement.Value = "XXX-XX-XXXX";
                        }

                        backgroundCheck.ResponseXml = string.Format( @"
            Request XML ({0}):
            ------------------------
            {1}

            Response XML ({2}):
            ------------------------
            {3}

            ", requestDateTime, xdoc.ToString(), responseDateTime, xResult.ToString() );
                        newRockContext.SaveChanges();
                    }
                }

                if ( _HTTPStatusCode == HttpStatusCode.OK )
                {
                    using ( var newRockContext = new RockContext() )
                    {
                        bool createdNewAttribute = false;
                        var xOrderXML = xResult.Elements( "OrderXML" ).FirstOrDefault();
                        if ( xOrderXML != null )
                        {
                            var xStatus = xOrderXML.Elements( "Status" ).FirstOrDefault();
                            if ( xStatus != null )
                            {
                                if ( SaveAttributeValue( workflow, "RequestStatus", xStatus.Value,
                                    FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ), newRockContext, null ) )
                                {
                                    createdNewAttribute = true;
                                }
                            }

                            var xErrors = xOrderXML.Elements( "Errors" ).FirstOrDefault();
                            if ( xErrors != null )
                            {
                                string errorMsg = xErrors.Elements( "Message" ).Select( x => x.Value ).ToList().AsDelimited( Environment.NewLine );
                                if ( SaveAttributeValue( workflow, "RequestMessage", errorMsg,
                                    FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ), newRockContext, null ) )
                                {
                                    createdNewAttribute = true;
                                }
                            }

                            if ( xResult.Root.Descendants().Count() > 0 )
                            {
                                SaveResults( xResult, workflow, rockContext, false );
                            }
                        }
                        else
                        {
                            if ( SaveAttributeValue( workflow, "RequestMessage", errorMessages.AsDelimited( Environment.NewLine ),
                                FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ), newRockContext, null ) )
                            {
                                createdNewAttribute = true;
                            }

                        }

                        newRockContext.SaveChanges();

                        if ( createdNewAttribute )
                        {
                            AttributeCache.FlushEntityAttributes();
                        }
                    }
                    return true;
                }
                else
                {
                    errorMessages.Add( "Invalid HttpStatusCode: " + _HTTPStatusCode.ToString() );
                    return false;
                }
            }

            catch ( Exception ex )
            {
                ExceptionLogService.LogException( ex, null );
                errorMessages.Add( ex.Message );
                return false;
            }
        }