Data Access/service class for Rock.Model.PersonViewed entities.
Пример #1
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        public void BindGrid()
        {
            int targetId = int.Parse( PageParameter( "targetId" ) );
            int viewerId = int.Parse( PageParameter( "viewerId" ) );
            bool viewedBy = Convert.ToBoolean( PageParameter( "viewedBy" ) );
            var personViewedService = new PersonViewedService( new RockContext() );
            var personViewedList = personViewedService.Queryable()
                .Where( p =>
                    p.ViewerPersonAlias != null &&
                    p.ViewerPersonAlias.PersonId == viewerId &&
                    p.TargetPersonAlias != null &&
                    p.TargetPersonAlias.PersonId == targetId )
                .Select( p => new
                {
                    Id = p.TargetPersonAlias.PersonId,
                    Source = p.Source,
                    TargetPerson = p.TargetPersonAlias.Person,
                    ViewerPerson = p.ViewerPersonAlias.Person,
                    ViewDateTime = p.ViewDateTime,
                    IpAddress = p.IpAddress
                } ).ToList();

            if ( viewedBy )
            {
                gridTitle.InnerText = string.Format(
                    "{0} Viewed By {1}",
                    personViewedList.Select( p => p.TargetPerson.FullName ).FirstOrDefault(),
                    personViewedList.Select( p => p.ViewerPerson.FullName ).FirstOrDefault() );
            }
            else
            {
                gridTitle.InnerText = string.Format(
                    "{0} Viewed {1}",
                    personViewedList.Select( p => p.ViewerPerson.FullName ).FirstOrDefault(),
                    personViewedList.Select( p => p.TargetPerson.FullName ).FirstOrDefault() );
            }

            SortProperty sortProperty = gViewDetails.SortProperty;
            if ( sortProperty != null )
            {
                personViewedList = personViewedList.AsQueryable().Sort( sortProperty ).ToList();
            }
            else
            {
                personViewedList = personViewedList.OrderByDescending( p => p.ViewDateTime ).ToList();
            }

            gViewDetails.EntityTypeId = EntityTypeCache.Read<PersonViewed>().Id;
            gViewDetails.DataSource = personViewedList;
            gViewDetails.DataBind();
        }
Пример #2
0
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            // store the view to the database if the viewer is NOT the target (don't track looking at your own record)
            if ( ViewerPersonId != TargetPersonId )
            {
                using ( new Rock.Data.UnitOfWorkScope() )
                {

                    PersonViewedService pvService = new PersonViewedService();

                    PersonViewed pvRecord = new PersonViewed();
                    pvService.Add( pvRecord, null );

                    pvRecord.IpAddress = IPAddress;
                    pvRecord.TargetPersonId = TargetPersonId;
                    pvRecord.ViewerPersonId = ViewerPersonId;
                    pvRecord.ViewDateTime = DateViewed;
                    pvRecord.Source = Source;

                    pvService.Save( pvRecord, null );
                }
            }
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        protected void BindGrid()
        {
            var showProfilesViewed = GetAttributeValue( "SeeProfilesViewed" ).AsBoolean();
            var rockContext = new RockContext();

            var personViewedService = new PersonViewedService( rockContext );
            var personService = new PersonService( rockContext );
            if ( showProfilesViewed )
            {
                // This grid should show the profiles viewed by this person.
                pnlViewed.Visible = true;
                pnlViewedBy.Visible = false;

                if ( personId.HasValue )
                {
                    var viewedList = personViewedService.Queryable()
                        .Where( p => p.ViewerPersonAlias != null && p.ViewerPersonAlias.PersonId == personId )
                        .GroupBy( p => p.TargetPersonAlias.PersonId )
                        .Select( p => new
                        {
                            TargetPersonId = p.Key,
                            FirstViewed = p.Min( g => g.ViewDateTime ),
                            LastViewed = p.Max( g => g.ViewDateTime ),
                            ViewedCount = p.Count()
                        } );

                    var pQry = personService.Queryable();

                    var qry = viewedList
                        .Join( pQry, v => v.TargetPersonId, p => p.Id, ( v, p ) => new
                        {
                            p.Id,
                            FullName = p.NickName + " " + p.LastName,
                            p.BirthDate,
                            p.Gender,
                            FirstViewedDate = v.FirstViewed,
                            LastViewedDate = v.LastViewed,
                            ViewedCount = v.ViewedCount
                        } );

                    SortProperty sortProperty = gViewed.SortProperty;
                    if ( sortProperty != null )
                    {
                        qry = qry.Sort( sortProperty );
                    }
                    else
                    {
                        qry = qry.OrderByDescending( q => q.LastViewedDate );
                    }

                    gViewed.DataSource = qry.ToList();
                    gViewed.DataBind();
                }
            }
            else
            {
                // This grid should show the profiles that have viewed this person.
                pnlViewed.Visible = false;
                pnlViewedBy.Visible = true;

                if ( personId.HasValue )
                {
                    var viewedList = personViewedService.Queryable()
                        .Where( p => p.TargetPersonAlias != null && p.TargetPersonAlias.PersonId == personId )
                        .GroupBy( p => p.ViewerPersonAlias.PersonId )
                        .Select( p => new
                        {
                            ViewerPersonId = p.Key,
                            FirstViewed = p.Min( g => g.ViewDateTime ),
                            LastViewed = p.Max( g => g.ViewDateTime ),
                            ViewedCount = p.Count()
                        } );

                    var pQry = personService.Queryable();

                    var qry = viewedList
                        .Join( pQry, v => v.ViewerPersonId, p => p.Id, ( v, p ) => new
                        {
                            p.Id,
                            FullName = p.NickName + " " + p.LastName,
                            p.BirthDate,
                            p.Gender,
                            FirstViewedDate = v.FirstViewed,
                            LastViewedDate = v.LastViewed,
                            ViewedCount = v.ViewedCount
                        } );

                    SortProperty sortProperty = gViewedBy.SortProperty;
                    if ( sortProperty != null )
                    {
                        qry = qry.Sort( sortProperty );
                    }
                    else
                    {
                        qry = qry.OrderByDescending( q => q.LastViewedDate );
                    }

                    gViewedBy.DataSource = qry.ToList();
                    gViewedBy.DataBind();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Deletes the family's addresses, phone numbers, photos, viewed records, and people.
        /// TODO: delete attendance codes for attendance data that's about to be deleted when
        /// we delete the person record.
        /// </summary>
        /// <param name="families">The families.</param>
        /// <param name="rockContext">The rock context.</param>
        private void DeleteExistingFamilyData( XElement families, RockContext rockContext )
        {
            PersonService personService = new PersonService( rockContext );
            PhoneNumberService phoneNumberService = new PhoneNumberService( rockContext );
            PersonViewedService personViewedService = new PersonViewedService( rockContext );
            PageViewService pageViewService = new PageViewService( rockContext );
            BinaryFileService binaryFileService = new BinaryFileService( rockContext );
            PersonAliasService personAliasService = new PersonAliasService( rockContext );
            PersonDuplicateService personDuplicateService = new PersonDuplicateService( rockContext );
            NoteService noteService = new NoteService( rockContext );
            AuthService authService = new AuthService( rockContext );
            CommunicationService communicationService = new CommunicationService( rockContext );
            CommunicationRecipientService communicationRecipientService = new CommunicationRecipientService( rockContext );
            FinancialBatchService financialBatchService = new FinancialBatchService( rockContext );
            FinancialTransactionService financialTransactionService = new FinancialTransactionService( rockContext );
            PersonPreviousNameService personPreviousNameService = new PersonPreviousNameService( rockContext );
            ConnectionRequestService connectionRequestService = new ConnectionRequestService( rockContext );
            ConnectionRequestActivityService connectionRequestActivityService = new ConnectionRequestActivityService( rockContext );

            // delete the batch data
            List<int> imageIds = new List<int>();
            foreach ( var batch in financialBatchService.Queryable().Where( b => b.Name.StartsWith( "SampleData" ) ) )
            {
                imageIds.AddRange( batch.Transactions.SelectMany( t => t.Images ).Select( i => i.BinaryFileId ).ToList() );
                financialTransactionService.DeleteRange( batch.Transactions );
                financialBatchService.Delete( batch );
            }

            // delete all transaction images
            foreach ( var image in binaryFileService.GetByIds( imageIds ) )
            {
                binaryFileService.Delete( image );
            }

            foreach ( var elemFamily in families.Elements( "family" ) )
            {
                Guid guid = elemFamily.Attribute( "guid" ).Value.Trim().AsGuid();

                GroupService groupService = new GroupService( rockContext );
                Group family = groupService.Get( guid );
                if ( family != null )
                {
                    var groupMemberService = new GroupMemberService( rockContext );
                    var members = groupMemberService.GetByGroupId( family.Id, true );

                    // delete the people records
                    string errorMessage;
                    List<int> photoIds = members.Select( m => m.Person ).Where( p => p.PhotoId != null ).Select( a => (int)a.PhotoId ).ToList();

                    foreach ( var person in members.Select( m => m.Person ) )
                    {
                        person.GivingGroup = null;
                        person.GivingGroupId = null;
                        person.PhotoId = null;

                        // delete phone numbers
                        foreach ( var phone in phoneNumberService.GetByPersonId( person.Id ) )
                        {
                            if ( phone != null )
                            {
                                phoneNumberService.Delete( phone );
                            }
                        }

                        // delete communication recipient
                        foreach ( var recipient in communicationRecipientService.Queryable().Where( r => r.PersonAlias.PersonId == person.Id ) )
                        {
                            communicationRecipientService.Delete( recipient );
                        }

                        // delete communication
                        foreach ( var communication in communicationService.Queryable().Where( c => c.SenderPersonAliasId == person.PrimaryAlias.Id ) )
                        {
                            communicationService.Delete( communication );
                        }

                        // delete person viewed records
                        foreach ( var view in personViewedService.GetByTargetPersonId( person.Id ) )
                        {
                            personViewedService.Delete( view );
                        }

                        // delete page viewed records
                        foreach ( var view in pageViewService.GetByPersonId( person.Id ) )
                        {
                            pageViewService.Delete( view );
                        }

                        // delete notes created by them or on their record.
                        foreach ( var note in noteService.Queryable().Where ( n => n.CreatedByPersonAlias.PersonId == person.Id
                            || (n.NoteType.EntityTypeId == _personEntityTypeId && n.EntityId == person.Id ) ) )
                        {
                            noteService.Delete( note );
                        }

                        // delete previous names on their records
                        foreach ( var previousName in personPreviousNameService.Queryable().Where( r => r.PersonAlias.PersonId == person.Id ) )
                        {
                            personPreviousNameService.Delete( previousName );
                        }

                        // delete any GroupMember records they have
                        foreach ( var groupMember in groupMemberService.Queryable().Where( gm => gm.PersonId == person.Id ) )
                        {
                            groupMemberService.Delete( groupMember );
                        }

                        //// delete any Authorization data
                        //foreach ( var auth in authService.Queryable().Where( a => a.PersonId == person.Id ) )
                        //{
                        //    authService.Delete( auth );
                        //}

                        // delete their aliases
                        foreach ( var alias in personAliasService.Queryable().Where( a => a.PersonId == person.Id ) )
                        {
                            foreach ( var duplicate in personDuplicateService.Queryable().Where( d => d.DuplicatePersonAliasId == alias.Id ) )
                            {
                                personDuplicateService.Delete( duplicate );
                            }

                            personAliasService.Delete( alias );
                        }

                        // delete any connection requests tied to them
                        foreach ( var request in connectionRequestService.Queryable().Where( r => r.PersonAlias.PersonId == person.Id || r.ConnectorPersonAlias.PersonId == person.Id ) )
                        {
                            connectionRequestActivityService.DeleteRange( request.ConnectionRequestActivities );
                            connectionRequestService.Delete( request );
                        }

                        // Save these changes so the CanDelete passes the check...
                        //rockContext.ChangeTracker.DetectChanges();
                        rockContext.SaveChanges( disablePrePostProcessing: true );

                        if ( personService.CanDelete( person, out errorMessage ) )
                        {
                            personService.Delete( person );
                            //rockContext.ChangeTracker.DetectChanges();
                            //rockContext.SaveChanges( disablePrePostProcessing: true );
                        }
                        else
                        {
                            throw new Exception( string.Format( "Trying to delete {0}, but: {1}", person.FullName, errorMessage ) );
                        }
                    }

                    //rockContext.ChangeTracker.DetectChanges();
                    rockContext.SaveChanges( disablePrePostProcessing: true );

                    // delete all member photos
                    foreach ( var photo in binaryFileService.GetByIds( photoIds ) )
                    {
                        binaryFileService.Delete( photo );
                    }

                    DeleteGroupAndMemberData( family, rockContext );
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Execute method to write transaction to the database.
        /// </summary>
        public void Execute()
        {
            // store the view to the database if the viewer is NOT the target (don't track looking at your own record)
            if ( ViewerPersonAliasId != TargetPersonAliasId )
            {

                var pvRecord = new PersonViewed();
                pvRecord.TargetPersonAliasId = TargetPersonAliasId;
                pvRecord.ViewerPersonAliasId = ViewerPersonAliasId;
                pvRecord.ViewDateTime = DateTimeViewed;
                pvRecord.IpAddress = IPAddress;
                pvRecord.Source = Source;

                var rockContext = new Rock.Data.RockContext();
                var pvService = new PersonViewedService( rockContext );
                pvService.Add( pvRecord );
                rockContext.SaveChanges();
            }
        }
Пример #6
0
        /// <summary>
        /// Deletes the family's addresses, phone numbers, photos, viewed records, and people.
        /// TODO: delete attendance codes for attendance data that's about to be deleted when
        /// we delete the person record.
        /// </summary>
        /// <param name="families">The families.</param>
        /// <param name="rockContext">The rock context.</param>
        private void DeleteExistingFamilyData( XElement families, RockContext rockContext )
        {
            PersonService personService = new PersonService( rockContext );
            PhoneNumberService phoneNumberService = new PhoneNumberService( rockContext );
            PersonViewedService personViewedService = new PersonViewedService( rockContext );
            PageViewService pageViewService = new PageViewService( rockContext );
            BinaryFileService binaryFileService = new BinaryFileService( rockContext );
            PersonAliasService personAliasService = new PersonAliasService( rockContext );
            NoteService noteService = new NoteService( rockContext );
            AuthService authService = new AuthService( rockContext );

            foreach ( var elemFamily in families.Elements( "family" ) )
            {
                Guid guid = elemFamily.Attribute( "guid" ).Value.Trim().AsGuid();

                GroupService groupService = new GroupService( rockContext );
                Group family = groupService.Get( guid );
                if ( family != null )
                {
                    var groupMemberService = new GroupMemberService( rockContext );
                    var members = groupMemberService.GetByGroupId( family.Id );

                    // delete the people records
                    string errorMessage;
                    List<int> photoIds = members.Select( m => m.Person ).Where( p => p.PhotoId != null ).Select( a => (int)a.PhotoId ).ToList();

                    foreach ( var person in members.Select( m => m.Person ) )
                    {
                        person.GivingGroup = null;
                        person.GivingGroupId = null;
                        person.PhotoId = null;

                        // delete phone numbers
                        foreach ( var phone in phoneNumberService.GetByPersonId( person.Id ) )
                        {
                            if ( phone != null )
                            {
                                phoneNumberService.Delete( phone );
                            }
                        }

                        // delete person viewed records
                        foreach ( var view in personViewedService.GetByTargetPersonId( person.Id ) )
                        {
                            personViewedService.Delete( view );
                        }

                        // delete page viewed records
                        foreach ( var view in pageViewService.GetByPersonId( person.Id ) )
                        {
                            pageViewService.Delete( view );
                        }

                        // delete notes created by them or on their record.
                        foreach ( var note in noteService.Queryable().Where ( n => n.CreatedByPersonAlias.PersonId == person.Id
                            || (n.NoteType.EntityTypeId == _personEntityTypeId && n.EntityId == person.Id ) ) )
                        {
                            noteService.Delete( note );
                        }

                        //// delete any GroupMember records they have
                        //foreach ( var groupMember in groupMemberService.Queryable().Where( gm => gm.PersonId == person.Id ) )
                        //{
                        //    groupMemberService.Delete( groupMember );
                        //}

                        //// delete any Authorization data
                        //foreach ( var auth in authService.Queryable().Where( a => a.PersonId == person.Id ) )
                        //{
                        //    authService.Delete( auth );
                        //}

                        // delete their aliases
                        foreach ( var alias in personAliasService.Queryable().Where( a => a.PersonId == person.Id ) )
                        {
                            personAliasService.Delete( alias );
                        }

                        //foreach ( var relationship in person.Gro)

                        // Save these changes so the CanDelete passes the check...
                        //rockContext.ChangeTracker.DetectChanges();
                        rockContext.SaveChanges( disablePrePostProcessing: true );

                        if ( personService.CanDelete( person, out errorMessage ) )
                        {
                            personService.Delete( person );
                            //rockContext.ChangeTracker.DetectChanges();
                            //rockContext.SaveChanges( disablePrePostProcessing: true );
                        }
                    }
                    //rockContext.ChangeTracker.DetectChanges();
                    rockContext.SaveChanges( disablePrePostProcessing: true );

                    // delete all member photos
                    foreach ( var photo in binaryFileService.GetByIds( photoIds ) )
                    {
                        binaryFileService.Delete( photo );
                    }

                    DeleteGroupAndMemberData( family, rockContext );
                }
            }
        }