Example #1
0
        public IEnumerable <IMediaDetail> GetAllParentMediaDetails(long languageId)
        {
            if (_allParentMediaDetails.ContainsKey(languageId))
            {
                return(_allParentMediaDetails[languageId]);
            }

            var item = this;

            var items        = new List <IMediaDetail>();
            var absoluteRoot = MediasMapper.GetAbsoluteRoot();

            if ((item.Media.ParentMediaID != null) && (item.Media.ParentMediaID != absoluteRoot.ID))
            {
                items.Add(item);
            }

            while (true)
            {
                if (item == null)
                {
                    break;
                }

                if (item.Media.ParentMediaID == null)
                {
                    break;
                }

                var parentMedia = item.Media.ParentMedia;

                if (parentMedia == null)
                {
                    parentMedia = BaseMapper.GetDataModel().AllMedia.FirstOrDefault(i => i.ID == (long)item.Media.ParentMediaID);
                }

                if (parentMedia == null)
                {
                    break;
                }

                item = parentMedia.MediaDetails.FirstOrDefault(i => i.LanguageID == languageId && !i.IsHistory);

                if (item == null)
                {
                    break;
                }

                if (item.Media.ID != absoluteRoot.ID)
                {
                    items.Add(item);
                }
            }

            items.Reverse();

            _allParentMediaDetails[languageId] = items;

            return(items);
        }
Example #2
0
        public void AddRoles(List <Role> roles)
        {
            var mediaTypesRoles = MediaTypesRoles.ToList();

            foreach (var mediaTypesRole in mediaTypesRoles)
            {
                var mediaTypeRole = BaseMapper.GetDataModel().MediaTypeRoles.FirstOrDefault(i => i.ID == mediaTypesRole.ID);

                if (mediaTypeRole != null)
                {
                    BaseMapper.GetDataModel().MediaTypeRoles.Remove(mediaTypeRole);
                }
            }

            var savedChanges = BaseMapper.GetDataModel().SaveChanges();

            foreach (var role in roles)
            {
                var contextRole = BaseMapper.GetObjectFromContext(role);

                MediaTypesRoles.Add(new MediaTypeRole()
                {
                    Role = contextRole, DateCreated = DateTime.Now, DateLastModified = DateTime.Now
                });
            }
        }
        public static UrlRedirectRule GetRuleForUrl(string virtualPath)
        {
            if (virtualPath.StartsWith("~"))
            {
                virtualPath = virtualPath.Replace("~", "");
            }

            var enumName = MediaTypeEnum.UrlRedirectRule.ToString();

            virtualPath = virtualPath.Trim().ToLower();

            var rule = BaseMapper.GetDataModel().MediaDetails.FirstOrDefault(i => i.HistoryVersionNumber == 0 && !i.IsDeleted && i.PublishDate <= DateTime.Now && (i.ExpiryDate == null || i.ExpiryDate >= DateTime.Now) && i.MediaType.Name == enumName && ((i as UrlRedirectRule).VirtualPathToRedirect.Trim().ToLower() == virtualPath || (i as UrlRedirectRule).VirtualPathToRedirect.ToLower() == virtualPath + "/" || (i as UrlRedirectRule).VirtualPathToRedirect.ToLower() == virtualPath.Replace("/?", "?") || (i as UrlRedirectRule).VirtualPathToRedirect.ToLower() == virtualPath.Replace("/?", "?") + "/"));

            if (rule != null)
            {
                var urlredirectrule = rule as UrlRedirectRule;

                var toUrl = urlredirectrule.RedirectToUrl;

                if (toUrl.Contains("{"))
                {
                    toUrl = MediaDetailsMapper.ParseSpecialTags(rule, urlredirectrule.RedirectToUrl);
                }

                //if (!toUrl.EndsWith(virtualPath))
                return(urlredirectrule);
            }

            return(null);
        }
Example #4
0
        public static Return RestoreDatabase(string backUpFilePath)
        {
            Return returnObj = BaseMapper.GenerateReturn();

            if (!string.IsNullOrEmpty(backUpFilePath) && File.Exists(backUpFilePath))
            {
                var databaseName = BaseMapper.GetDataModel().Database.Connection.Database;

                var sqlCommand = $@"ALTER DATABASE {databaseName} SET Single_User WITH Rollback Immediate; USE master; RESTORE DATABASE {databaseName} FROM DISK = '{backUpFilePath}'; ALTER DATABASE {databaseName} SET Multi_User";

                try
                {
                    var result = BaseMapper.GetDataModel(true).Database.ExecuteSqlCommand(transactionalBehavior: System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sql: sqlCommand);
                    returnObj.SetRawData(result);

                    return(returnObj);
                }
                catch (Exception ex)
                {
                    ErrorHelper.LogException(ex);
                    returnObj.Error = ErrorHelper.CreateError(ex);

                    return(returnObj);
                }
            }

            return(returnObj);
        }
Example #5
0
        public static Role GetByEnum(RoleEnum roleEnum)
        {
            var strRoleEnum = roleEnum.ToString();

            return(BaseMapper.GetDataModel().Roles.FirstOrDefault(i => i.Name == strRoleEnum));
            //var allItems = GetAll();
            //return allItems.FirstOrDefault(item => item.Name == roleEnum.ToString());
        }
Example #6
0
        public static void ClearAllCached()
        {
            var webrequests = BaseMapper.GetDataModel().WebserviceRequests;

            foreach (var item in webrequests)
            {
                BaseMapper.GetDataModel().WebserviceRequests.Remove(item);
            }

            BaseMapper.GetDataModel().SaveChanges();

            ClearAllMemoryCache();
        }
        public static List <UrlRedirectRule> GetRulesFromUrl(string virtualPath)
        {
            if (virtualPath.StartsWith("~"))
            {
                virtualPath = virtualPath.Replace("~", "");
            }

            var enumName = MediaTypeEnum.UrlRedirectRule.ToString();

            var rules = BaseMapper.GetDataModel().MediaDetails.Where(i => i.HistoryVersionNumber == 0 && !i.IsDeleted && i.PublishDate <= DateTime.Now && (i.ExpiryDate == null || i.ExpiryDate >= DateTime.Now) && i.MediaType.Name == enumName && ((i as UrlRedirectRule).VirtualPathToRedirect.ToLower() == virtualPath || (i as UrlRedirectRule).VirtualPathToRedirect.ToLower() == virtualPath + "/")).ToList().Cast <UrlRedirectRule>().ToList();

            return(rules);
        }
        public static string ObjectToJson(object to, long depth = 1, Formatting indent = Formatting.None)
        {
            var settings = new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.None,
                ReferenceLoopHandling      = ReferenceLoopHandling.Ignore
            };

            if (to is IMediaDetail)
            {
                var mediaDetail = BaseMapper.GetDataModel(true, false).MediaDetails.FirstOrDefault(i => i.ID == ((IMediaDetail)to).ID);
                to = mediaDetail;
            }

            var json = JsonConvert.SerializeObject(to, indent, settings);

            return(json);
        }
Example #9
0
        /*public static IEnumerable<Field> GetAll()
         * {
         *  return GetDataModel().Fields.OrderByDescending(b => b.DateCreated);
         * }*/

        public static Field GetByID(long id)
        {
            //var allItems = GetAll();
            return(BaseMapper.GetDataModel().Fields.FirstOrDefault(item => item.ID == id));
        }
Example #10
0
        public Return PublishLive()
        {
            var returnObj    = new Return();
            var liveVersion  = BaseMapper.GetObjectFromContext((MediaDetail)this.Media.GetLiveMediaDetail());
            var selectedItem = BaseMapper.GetObjectFromContext((MediaDetail)this);

            selectedItem.HistoryVersionNumber  = 0;
            selectedItem.HistoryForMediaDetail = null;
            selectedItem.IsDraft     = false;
            selectedItem.PublishDate = DateTime.Now;
            //selectedItem.ShowInMenu = true;

            IEnumerable <MediaDetail> items = new List <MediaDetail>();

            if (liveVersion != null)
            {
                items = liveVersion.History.ToList();

                foreach (var item in items)
                {
                    if (item.ID != selectedItem.ID)
                    {
                        var tmpItem = BaseMapper.GetObjectFromContext(item);
                        item.HistoryForMediaDetailID = selectedItem.ID;
                    }
                }
            }

            foreach (var fieldAssociation in selectedItem.FieldAssociations)
            {
                var index = 1;
                foreach (var history in fieldAssociation.MediaDetail.History)
                {
                    history.HistoryForMediaDetail = fieldAssociation.MediaDetail;
                    history.HistoryVersionNumber  = 1;

                    index++;
                }

                fieldAssociation.MediaDetail.HistoryForMediaDetail = null;
                fieldAssociation.MediaDetail.HistoryVersionNumber  = 0;
            }

            foreach (var field in selectedItem.Fields)
            {
                foreach (var fieldAssociation in field.FieldAssociations)
                {
                    var index = 1;

                    foreach (var mediaDetail in fieldAssociation.MediaDetail.Media.MediaDetails)
                    {
                        mediaDetail.HistoryForMediaDetail = fieldAssociation.MediaDetail;
                        mediaDetail.HistoryVersionNumber  = 1;

                        index++;
                    }

                    fieldAssociation.MediaDetail.HistoryForMediaDetail = null;
                    fieldAssociation.MediaDetail.HistoryVersionNumber  = 0;
                }

                if (liveVersion != null)
                {
                    field.FrontEndSubmissions = liveVersion.LoadField(field.FieldCode)?.FrontEndSubmissions;
                }
            }

            foreach (var mediaTypeField in selectedItem.MediaType.Fields)
            {
                if (!selectedItem.Fields.Any(i => i.FieldCode == mediaTypeField.FieldCode))
                {
                    var mediaDetailField = new MediaDetailField();
                    mediaDetailField.CopyFrom(mediaTypeField);

                    mediaDetailField.UseMediaTypeFieldFrontEndLayout = true;
                    mediaDetailField.UseMediaTypeFieldDescription    = true;

                    mediaDetailField.MediaTypeField = mediaTypeField;

                    mediaDetailField.DateCreated = mediaDetailField.DateLastModified = DateTime.Now;

                    mediaDetailField.OrderIndex = selectedItem.Fields.Count;
                    selectedItem.Fields.Add(mediaDetailField);
                }
            }


            if (liveVersion != null)
            {
                if (items.Any())
                {
                    liveVersion.HistoryVersionNumber = items.OrderByDescending(i => i.HistoryVersionNumber).FirstOrDefault().HistoryVersionNumber + 1;
                }
                else
                {
                    liveVersion.HistoryVersionNumber = 1;
                }

                liveVersion.HistoryForMediaDetail = (MediaDetail)selectedItem;


                var associations = BaseMapper.GetDataModel().FieldAssociations.Where(i => i.AssociatedMediaDetailID == liveVersion.ID);

                foreach (var association in associations)
                {
                    association.MediaDetail = (MediaDetail)selectedItem;
                }
            }

            returnObj = MediaDetailsMapper.Update(selectedItem);

            if (!returnObj.IsError)
            {
                if (liveVersion != null)
                {
                    liveVersion.HistoryForMediaDetailID = selectedItem.ID;
                    returnObj = MediaDetailsMapper.Update(liveVersion);
                }

                if (!returnObj.IsError)
                {
                    ContextHelper.Clear(ContextType.Cache);
                    FileCacheHelper.ClearAllCache();

                    return(returnObj);
                }
                else
                {
                    return(returnObj);
                }
            }
            else
            {
                return(returnObj);
            }
        }
Example #11
0
        public static Return BackupDatabase()
        {
            Return returnObj = BaseMapper.GenerateReturn();

            var databaseName = BaseMapper.GetDataModel().Database.Connection.Database;

            var sqlCommand = $@"BACKUP DATABASE {databaseName} TO DISK = '{DbBackupPath}{DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss-tt")}-{databaseName}.bak'";

            try
            {
                var result = BaseMapper.GetDataModel(true).Database.ExecuteSqlCommand(transactionalBehavior: System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sql: sqlCommand);
                returnObj.SetRawData(result);

                return(returnObj);
            }
            catch (Exception ex)
            {
                ErrorHelper.LogException(ex);
                returnObj.Error = ErrorHelper.CreateError(ex);

                return(returnObj);
            }


            /*SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
             * string dbName = builder.InitialCatalog;
             * string backUpPath = URIHelper.BasePath + "App_Data/DBBackups/" + DateTime.Now.ToString("yyyy'-'MM'-'dd-HH'-'mm'-'ss'Z'") + "-" + dbName + ".bak";
             *
             * using (SqlConnection cnn = new SqlConnection(connectionString))
             * {
             *  cnn.Open();
             *  dbName = cnn.Database.ToString();
             *
             *  ServerConnection sc = new ServerConnection(cnn);
             *  Server sv = new Server(sc);
             *
             *  // Create backup device item for the backup
             *  BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);
             *
             *  // Create the backup informaton
             *  Microsoft.SqlServer.Management.Smo.Backup bk = new Backup();
             *
             *  //bk.PercentComplete += new PercentCompleteEventHandler(percentComplete);
             *  bk.Devices.Add(bdi);
             *  bk.Action = BackupActionType.Database;
             *  bk.PercentCompleteNotification = 1;
             *  bk.BackupSetDescription = dbName;
             *  bk.BackupSetName = dbName;
             *  bk.Database = dbName;
             *
             *  //bk.ExpirationDate = DateTime.Now.AddSeconds(10);
             *  bk.LogTruncation = BackupTruncateLogType.Truncate;
             *  bk.FormatMedia = false;
             *  bk.Initialize = true;
             *  bk.Checksum = true;
             *  bk.ContinueAfterError = true;
             *  bk.Incremental = false;
             *
             *  try
             *  {
             *      // Run the backup
             *      bk.SqlBackup(sv);//Exception
             *      return returnObj;
             *  }
             *  catch (Exception ex)
             *  {
             *      ErrorHelper.LogException(ex);
             *
             *      returnObj.Error = ErrorHelper.CreateError(ex);
             *
             *      return returnObj;
             *  }
             * }*/
        }