/// <summary>
 /// Initializes a new instance of the <see cref="LoadPackageRecordContext"/> class.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="packageRecord">The package record.</param>
 /// <param name="activeObjectSpaces">The active object spaces.</param>
 public LoadPackageRecordContext(LoadPackageSessionContext context, 
     PackageRecord packageRecord, Dictionary<Guid, IObjectSpace> activeObjectSpaces)
     : base(context, context.PackageSession)
 {
     PackageRecord = packageRecord;
     ActiveObjectSpaces = activeObjectSpaces;
 }
        private void loadTopSession(LoadPackageSessionContext context)
        {
            var objectSpace = context.ObjectSpace;
            var criteria = CriteriaOperator.Parse("SessionId = ?", context.PackageSession.SessionId);

            // avoid duplicate session loading
            if (objectSpace.FindObject<ProtocolSession>(criteria, true) == null)
            {
                var sessionTree = context.PackageSession.AllChildren;
                var allRecords = (from s in sessionTree from r in s.PackageRecords
                                  orderby r.ModifiedOn select r).ToList();

                allRecords = allRecords.Distinct().ToList();

                var args = new LoadSessionArgs(context, allRecords);
                var activeObjectSpaces = new Dictionary<Guid, IObjectSpace>();
                try
                {
                    if (!Owner.DoBeforeLoadSession(args))
                    {
                        allRecords.ForEach(y => loadPackageRecord(new LoadPackageRecordContext(context,
                            y, activeObjectSpaces)));
                    }
                    if (activeObjectSpaces.Count > 0)
                    {
                        commitObjectSpaceOnLoad(activeObjectSpaces.Values.ElementAt(0), activeObjectSpaces);
                        activeObjectSpaces.Clear();
                    }
                }
                finally
                {
                    if (activeObjectSpaces.Count > 0)
                    {
                        // first element in values is top session
                        activeObjectSpaces.Values.ElementAt(0).Rollback();
                        activeObjectSpaces.Values.ElementAt(0).Dispose();
                        activeObjectSpaces.Clear();
                    }
                }

                createProtocolRecords(allRecords, context);

                Owner.DoAfterLoadSession(args);
            }
            else
            {
                var errorText = string.Format(Localizer.SessionAlreadyLoaded,
                    context.PackageSession.SessionId);

                context.Worker.ReportProgress(Color.BlueViolet, errorText);
                context.Package.CreateLogRecord(PackageEventType.SessionAlreadyLoaded, errorText);
            }
        }