internal static BibDSModel.Preservation Convert(this Preservation pre, BibDSModel.BiblosDS2010Entities db, BibDSModel.Preservation original = null, int level = 0, int deeplevel = 5)
        {
            if (pre == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new BibDSModel.Preservation() : original;

            // retval.Archive = pre.Archive.ConvertPreservation(db);
            // retval.Archive = pre.Archive;
            retval.CloseContent       = pre.CloseContent;
            retval.CloseDate          = pre.CloseDate;
            retval.IndexHash          = pre.IndexHash;
            retval.Label              = pre.Label;
            retval.LastVerifiedDate   = pre.LastVerifiedDate;
            retval.Path               = pre.Path;
            retval.PreservationDate   = pre.PreservationDate;
            retval.IdPreservationUser = pre.IdPreservationUser;
            retval.IdPreservation     = pre.IdPreservation;

            //var journaling = pre.PreservationJournalings.Convert(db, (original != null) ? original.PreservationJournaling : null, level, deeplevel);
            //foreach (var journal in journaling)
            //{
            //    retval.PreservationJournaling.Add(journal);
            //}

            return(retval);
        }
        /// <summary>
        /// Verifica la coerenza delle date dei tasks.
        /// </summary>
        /// <returns></returns>
        private static bool CheckTasksDates(Model.BiblosDS2010Entities db, IEnumerable <Obj.PreservationTask> tasks)
        {
            var retval = false;

            if (db == null || tasks == null || tasks.Count() < 1)
            {
                return(true);
            }

            try
            {
                if (tasks.Any(x => x.Archive == null || x.TaskType == null ||
                              !x.StartDocumentDate.HasValue || !x.EndDocumentDate.HasValue ||
                              x.StartDocumentDate.Value >= x.EndDocumentDate.Value))
                {
                    throw new Exception();
                }

                DateTime dataInizio, dataFine;
                Guid     idArchivio;
                string   keyCode;
                foreach (var t in tasks)
                {
                    idArchivio = t.Archive.IdArchive;
                    keyCode    = ((int)t.TaskType.Type).ToString();
                    dataInizio = t.StartDocumentDate.Value.Date;
                    dataFine   = t.EndDocumentDate.Value.Date;

                    retval = db.PreservationTask
                             .Include(x => x.PreservationTaskType)
                             .Where(x => x.IdArchive == idArchivio && x.StartDocumentDate.HasValue && x.EndDocumentDate.HasValue)
                             .Any(x => dataInizio <= x.EndDocumentDate.Value && dataFine >= x.StartDocumentDate.Value && x.PreservationTaskType.KeyCode == keyCode);
                    if (retval)
                    {
                        throw new Exception();
                    }

                    if (t.EstimatedDate < dataFine)
                    {
                        throw new Exception();
                    }
                }

                retval = true;
            }
            catch { retval = false; }

            return(retval);
        }
        internal static BibDSModel.Archive ConvertPreservation(this DocumentArchive arch, BibDSModel.BiblosDS2010Entities db)
        {
            if (arch == null)
            {
                return(null);
            }

            //var retval = (original == null) ? new BibDSModel.Archive() : original;

            //if (original == null)
            //{

            //    if (arch.Storage != null)
            //        retval.ArchiveStorage = db.ArchiveStorage.Where(x => x.IdStorage == arch.Storage.IdStorage);

            //    var attrs = db.Attributes.Where(x => x.IdArchive == arch.IdArchive);

            //    if (retval.Attributes == null && attrs.Count() > 0)
            //        retval.Attributes = new EntityCollection<BibDSModel.Attributes>();

            //    foreach (var i in attrs)
            //    {
            //        if (!retval.Document.Contains(i))
            //            retval.Attributes.Add(i);
            //    }

            //    var docs=db.Document.Where(x => x.IdArchive == arch.IdArchive);
            //    if (retval.Document == null && docs.Count() > 0)
            //        retval.Document = new EntityCollection<BibDSModel.Document>();

            //    foreach (var doc in docs)
            //    {
            //        if (!retval.Document.Contains(doc))
            //            retval.Document.Add(doc);
            //    }

            //    var pres = db.Preservation.Where(x => x.IdArchive == arch.IdArchive);

            //    if (retval.Preservation == null && pres.Count() > 0)
            //        retval.Preservation = new EntityCollection<BibDSModel.Preservation>();

            //    foreach (var p in pres)
            //    {
            //        if (!retval.Preservation.Contains(p))
            //            retval.Preservation.Add(p);
            //    }

            //    retval.IdArchive = arch.IdArchive;
            //}

            //retval.AuthorizationAssembly = arch.AuthorizationAssembly;
            //retval.AuthorizationClassName = arch.AuthorizationClassName;
            //retval.AutoVersion = arch.AutoVersion;
            //retval.EnableSecurity = arch.EnableSecurity;
            //retval.IsLegal = arch.IsLegal;
            ////retval.LastAutoIncValue = arch.las
            //retval.LastIdBiblos = arch.LastIdBiblos;
            //retval.LowerCache = arch.LowerCache;
            //retval.MaxCache = arch.MaxCache;
            //retval.Name = arch.Name;
            //retval.PathCache = arch.PathCache;
            //retval.PathPreservation = arch.PathPreservation;
            //retval.PathTransito = arch.PathTransito;
            //retval.UpperCache = arch.UpperCache;

            var retval = db.Archive.Where(x => x.IdArchive == arch.IdArchive).FirstOrDefault();

            return(retval);
        }
        internal static EntityCollection <BibDSModel.PreservationJournaling> Convert(this BindingList <PreservationJournaling> jou, BibDSModel.BiblosDS2010Entities db, EntityCollection <BibDSModel.PreservationJournaling> original = null, int level = 0, int deeplevel = 5)
        {
            if (jou == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new EntityCollection <BibDSModel.PreservationJournaling>() : original;

            BibDSModel.PreservationJournaling journal = null;

            foreach (var entity in jou)
            {
                if (original != null)
                {
                    journal = original
                              .Where(x => x.IdPreservationJournaling == entity.IdPreservationJournaling)
                              .SingleOrDefault();
                }

                retval.Add(entity.Convert(db, journal, level + 1, deeplevel));
            }

            return(retval);
        }
        internal static BibDSModel.PreservationJournaling Convert(this PreservationJournaling jou, BibDSModel.BiblosDS2010Entities db, BibDSModel.PreservationJournaling original = null, int level = 0, int deeplevel = 5)
        {
            if (jou == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new BibDSModel.PreservationJournaling() : original;

            retval.IdPreservationJournaling         = jou.IdPreservationJournaling;
            retval.IdPreservation                   = jou.IdPreservation;
            retval.IdPreservationJournalingActivity = jou.IdPreservationJournalingActivity;
            retval.IdPreservationUser               = jou.User.IdPreservationUser;
            retval.Notes        = jou.Notes;
            retval.DomainUser   = jou.DomainUser;
            retval.DateActivity = jou.DateActivity;
            retval.DateCreated  = jou.DateCreated;

            return(retval);
        }
        internal static EntityCollection <BibDSModel.PreservationTaskType> Convert(this BindingList <PreservationTaskType> typ, BibDSModel.BiblosDS2010Entities db, EntityCollection <BibDSModel.PreservationTaskType> original = null, int level = 0, int deeplevel = 5)
        {
            if (typ == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new EntityCollection <BibDSModel.PreservationTaskType>() : original;

            foreach (var entity in typ)
            {
                retval.Add(entity.Convert(db, null, level + 1, deeplevel));
            }

            return(retval);
        }
        internal static BibDSModel.PreservationTaskType Convert(this PreservationTaskType typ, BibDSModel.BiblosDS2010Entities db, BibDSModel.PreservationTaskType original = null, int level = 0, int deeplevel = 5)
        {
            if (typ == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new BibDSModel.PreservationTaskType() : original;

            retval.IdPreservationTaskType = typ.IdPreservationTaskType;
            retval.Description            = typ.Description;
            retval.Period  = typ.Period;
            retval.KeyCode = ((int)typ.Type).ToString();

            return(retval);
        }
        internal static BibDSModel.PreservationRole Convert(this PreservationRole rol, BibDSModel.BiblosDS2010Entities db, BibDSModel.PreservationRole original = null, int level = 0, int deeplevel = 5)
        {
            if (rol == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new BibDSModel.PreservationRole() : original;

            retval.KeyCode     = (short)rol.KeyCode;
            retval.AlertEnable = rol.AlertEnabled;
            retval.Enable      = rol.Enabled;
            retval.Name        = rol.Name;

            if (rol.IdPreservationRole != Guid.Empty)
            {
                retval.IdPreservationRole = rol.IdPreservationRole;
            }

            return(retval);
        }
        internal static BibDSModel.PreservationUserRole Convert(this PreservationUserRole rol, BibDSModel.BiblosDS2010Entities db, BibDSModel.PreservationUserRole original = null, int level = 0, int deeplevel = 5)
        {
            if (rol == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new BibDSModel.PreservationUserRole() : original;

            retval.Archive = rol.Archive.ConvertPreservation(db);

            retval.PreservationRole = rol.PreservationRole.Convert(db, retval.PreservationRole, level + 1, deeplevel);

            if (rol.PreservationUser != null && rol.PreservationUser.IdPreservationUser != Guid.Empty)
            {
                retval.IdPreservationUser = rol.PreservationUser.IdPreservationUser;
            }
            else if (rol.IdPreservationUser != Guid.Empty)
            {
                retval.IdPreservationUser = rol.IdPreservationUser;
            }

            if (rol.PreservationRole != null && rol.PreservationRole.IdPreservationRole != Guid.Empty)
            {
                retval.IdPreservationRole = rol.PreservationRole.IdPreservationRole;
            }
            else if (rol.IdPreservationRole != Guid.Empty)
            {
                retval.IdPreservationRole = rol.IdPreservationRole;
            }

            if (retval.IdPreservationUserRole == Guid.Empty)
            {
                retval.IdPreservationUserRole = rol.IdPreservationUserRole;
            }

            return(retval);
        }
        internal static BibDSModel.PreservationUser Convert(this PreservationUser usr, BibDSModel.BiblosDS2010Entities db, BibDSModel.PreservationUser original = null, int level = 0, int deeplevel = 5)
        {
            if (usr == null || level > deeplevel)
            {
                return(null);
            }

            var retval = (original == null) ? new BibDSModel.PreservationUser() : original;

            retval.Address              = usr.Address;
            retval.DomainUser           = usr.DomainUser;
            retval.Email                = usr.EMail;
            retval.Enable               = usr.Enabled;
            retval.FiscalId             = usr.FiscalId;
            retval.Name                 = usr.Name;
            retval.Surname              = usr.Surname;
            retval.PreservationUserRole = usr.UserRoles.Convert(db, null, level, deeplevel);

            return(retval);
        }