コード例 #1
0
ファイル: MvcApplication.cs プロジェクト: tm011064/Luputa
        private void RefreshApplicationData()
        {
            // TODO (Roman): try/catch errorhandling?
            IApplicationContext context = InstanceContainer.ApplicationContext;

            #region context data
            context.Refresh(context.BaseDirectory);
            #endregion

            #region database content
            // TODO (Roman): this must be done at the windows service, not at a web client app

            Dictionary <string, List <IWikiLanguageThreadLookupItem> > wikiLanguageThreadIdLookup = new Dictionary <string, List <IWikiLanguageThreadLookupItem> >();

            Dictionary <string, IWikiLanguageThreadLookupItem> wikiLanguageThreadIds = new Dictionary <string, IWikiLanguageThreadLookupItem>();
            IApplicationManager        applicationManger         = InstanceContainer.ApplicationManager;
            IArticleGroupManager       articleGroupManager       = InstanceContainer.ArticleGroupManager;
            IArticleGroupThreadManager articleGroupThreadManager = InstanceContainer.ArticleGroupThreadManager;
            ISystemProfileImageManager systemProfileImageManager = InstanceContainer.SystemProfileImageManager;
            IApplication         application;
            IStaticContentLookup staticContentLookup;
            ArticleGroupThread   articleGroupThread;
            ArticleGroup         articleGroup;

            IApplicationThemeInfo[]        applicationThemeInfos = context.GetAllApplicationThemeInfos();
            Dictionary <int, IApplication> applicationLookup     = new Dictionary <int, IApplication>();

            #region applications
            foreach (IApplicationThemeInfo applicationThemeInfo in applicationThemeInfos)
            {
                application = applicationManger.GetApplication(applicationThemeInfo.ApplicationName);
                if (application == null)
                {
                    application = new Workmate.Components.Entities.Application(applicationThemeInfo.ApplicationName, "Generated on Application Data Refresh at " + DateTime.UtcNow.ToString() + " (UTC)");

                    #region do default settings - we really want this to be prepopulated by sql scripts
                    application.DefaultAdminSenderEmailAddress = "admin@" + applicationThemeInfo.ApplicationName + ".com";
                    #endregion

                    var report = applicationManger.Create(application);
                    if (report.Status != DataRepositoryActionStatus.Success)
                    {
                        // TODO (Roman): error handling?
                        continue;
                    }
                }
                // this was not set yet as the context refresh above did not have all data available, so we have to do it here
                applicationThemeInfo.ApplicationId = application.ApplicationId;
                applicationThemeInfo.Application   = application;

                applicationLookup[application.ApplicationId] = application;
            }
            #endregion

            #region Userroles
            string[] userRoles = Enum.GetNames(typeof(UserRole));
            foreach (int applicationId in applicationLookup.Keys)
            {
                InstanceContainer.WorkmateRoleProvider.CreateRolesIfNotExist(applicationId, userRoles);
            }
            #endregion

            IThemeFolderLookup themeFolderLookup;
            Dictionary <string, SystemProfileImage> systemProfileImageLookup;
            HashSet <int> usedApplicationIds = new HashSet <int>();
            foreach (IApplicationThemeInfo applicationThemeInfo in applicationThemeInfos.Where(c => c.ApplicationGroup == MagicStrings.APPLICATIONGROUP_NAME_WORKMATE))
            {
                themeFolderLookup = context.GetThemeFolderLookup(applicationThemeInfo.ApplicationGroup);

                #region profile images
                if (!usedApplicationIds.Contains(applicationThemeInfo.ApplicationId))
                {
                    systemProfileImageLookup = systemProfileImageManager.GetSystemProfileImages(applicationThemeInfo.ApplicationId);

                    if (!systemProfileImageLookup.ContainsKey(MagicStrings.PROFILE_IMAGE_MALE_FILENAME))
                    {
                        if (File.Exists(applicationThemeInfo.Images.DefaultThemeImageFolderServerPath + MagicStrings.PROFILE_IMAGE_MALE_FILENAME))
                        {
                            TryCreateSystemProfileImage(themeFolderLookup, applicationThemeInfo
                                                        , applicationThemeInfo.Images.DefaultThemeImageFolderServerPath + MagicStrings.PROFILE_IMAGE_MALE_FILENAME
                                                        , MagicStrings.PROFILE_IMAGE_MALE_FILENAME);
                        }
                    }
                    if (!systemProfileImageLookup.ContainsKey(MagicStrings.PROFILE_IMAGE_FEMALE_FILENAME))
                    {
                        if (File.Exists(applicationThemeInfo.Images.DefaultThemeImageFolderServerPath + MagicStrings.PROFILE_IMAGE_FEMALE_FILENAME))
                        {
                            TryCreateSystemProfileImage(themeFolderLookup, applicationThemeInfo
                                                        , applicationThemeInfo.Images.DefaultThemeImageFolderServerPath + MagicStrings.PROFILE_IMAGE_FEMALE_FILENAME
                                                        , MagicStrings.PROFILE_IMAGE_FEMALE_FILENAME);
                        }
                    }

                    // now reload in case we've created profile images for the first time
                    systemProfileImageLookup = systemProfileImageManager.GetSystemProfileImages(applicationThemeInfo.ApplicationId);
                    foreach (SystemProfileImage systemProfileImage in systemProfileImageLookup.Values)
                    {
                        TrySaveProfileImage(systemProfileImage, applicationThemeInfo, MagicStrings.FOLDER_IMAGES_PROFILE_NORMAL, MagicNumbers.PROFILEIMAGE_SIZE_NORMAL);
                        TrySaveProfileImage(systemProfileImage, applicationThemeInfo, MagicStrings.FOLDER_IMAGES_PROFILE_TINY, MagicNumbers.PROFILEIMAGE_SIZE_TINY);
                    }

                    usedApplicationIds.Add(applicationThemeInfo.ApplicationId);
                }

                systemProfileImageLookup = systemProfileImageManager.GetSystemProfileImages(applicationThemeInfo.ApplicationId);
                foreach (SystemProfileImage systemProfileImage in systemProfileImageLookup.Values)
                {
                    switch (systemProfileImage.FriendlyFileName)
                    {
                    case MagicStrings.PROFILE_IMAGE_MALE_FILENAME: applicationThemeInfo.Images.MaleSystemProfileImageId = systemProfileImage.ImageId; break;

                    case MagicStrings.PROFILE_IMAGE_FEMALE_FILENAME: applicationThemeInfo.Images.FemaleSystemProfileImageId = systemProfileImage.ImageId; break;
                    }
                }
                #endregion

                #region create wiki language threads
                articleGroup = articleGroupManager.GetArticleGroup(applicationThemeInfo.ApplicationId, MagicStrings.WIKI_LANGUAGE_SECTION);
                if (articleGroup == null)
                {
                    articleGroup             = new ArticleGroup(applicationThemeInfo.ApplicationId, MagicStrings.WIKI_LANGUAGE_SECTION, true);
                    articleGroup.Description = "Generated on Application Data Refresh at " + DateTime.UtcNow.ToString() + " (UTC)";

                    var report = articleGroupManager.Create(articleGroup);
                    if (report.Status != DataRepositoryActionStatus.Success)
                    {
                        // TODO (Roman): error handling?
                        continue;
                    }
                }

                wikiLanguageThreadIds = new Dictionary <string, IWikiLanguageThreadLookupItem>();
                staticContentLookup   = context.GetStaticContentLookup(applicationThemeInfo.ApplicationGroup);
                foreach (string theme in themeFolderLookup.ThemeNames)
                {
                    foreach (Language language in staticContentLookup.GetLanguages(theme))
                    {
                        if (wikiLanguageThreadIds.ContainsKey(language.ShortCode))
                        {
                            continue;
                        }

                        articleGroupThread = articleGroupThreadManager.GetArticleGroupThread(language.ShortCode);
                        if (articleGroupThread == null)
                        {
                            articleGroupThread            = new ArticleGroupThread(articleGroup, ArticleGroupThreadStatus.Enabled, language.ShortCode);
                            articleGroupThread.IsApproved = true;
                            articleGroupThread.IsLocked   = false;

                            var report = articleGroupThreadManager.Create(articleGroupThread);
                            if (report.Status != DataRepositoryActionStatus.Success)
                            {
                                // TODO (Roman): error handling?
                                continue;
                            }
                        }

                        wikiLanguageThreadIds[language.ShortCode] = new WikiLanguageThreadLookupItem(language.ShortCode, language.Name, articleGroupThread.ArticleGroupThreadId);
                    }
                }

                wikiLanguageThreadIdLookup[applicationThemeInfo.ApplicationName.ToLowerInvariant()] = wikiLanguageThreadIds.Values
                                                                                                      .ToList();

                #endregion
            }

            IApplicationDataCache applicationDataCache = InstanceContainer.ApplicationDataCache;
            applicationDataCache.Refresh(wikiLanguageThreadIdLookup);

            #endregion

            #region routes
            RouteCollection routes = RouteTable.Routes;

            // TODO (Roman): get read lock as well?
            using (IDisposable writeLock = routes.GetWriteLock())
            {
                routes.Clear();

                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                ISitemapLookup sitemapLookup;
                Menu           topMenu;
                MenuInfo       menuInfo;
                Dictionary <string, Breadcrumb> breadCrumbs = new Dictionary <string, Breadcrumb>();
                foreach (string applicationGroup in context.GetApplicationGroups())
                {
                    sitemapLookup = context.GetSitemapLookup(applicationGroup);

                    breadCrumbs.Clear();
                    CreateBreadcrumbs(breadCrumbs, sitemapLookup);

                    foreach (RouteTag routeTag in sitemapLookup.RouteTags)
                    {
                        topMenu = sitemapLookup.GetMenu(routeTag.TopMenuName);

                        #region data tokens extensions
                        routeTag.DataTokens[MagicStrings.DATATOKENS_BREADCRUMB] = breadCrumbs[routeTag.Name];
                        routeTag.DataTokens[MagicStrings.DATATOKENS_ROUTENAME]  = routeTag.Name;

                        menuInfo             = new MenuInfo();
                        menuInfo.TopMenuName = routeTag.TopMenuName;
                        if (topMenu != null)
                        {
                            foreach (MenuItem item in topMenu.MenuItems)
                            {
                                if (item.RouteName == routeTag.Name)
                                {
                                    menuInfo.TopMenuItemName = item.Name;
                                }

                                foreach (MenuItem subItem in item.Children)
                                {
                                    if (subItem.RouteName == routeTag.Name)
                                    {
                                        menuInfo.TopMenuItemName    = subItem.Parent.Name;
                                        menuInfo.TopMenuSubItemName = subItem.Name;
                                        break;
                                    }
                                }
                                foreach (MenuItem subItem in item.DropdownMenuItems)
                                {
                                    foreach (MenuItem dropdownSubItem in subItem.Children)
                                    {
                                        if (dropdownSubItem.RouteName == routeTag.Name)
                                        {
                                            menuInfo.TopMenuItemName    = dropdownSubItem.Parent.Name;
                                            menuInfo.TopMenuSubItemName = dropdownSubItem.Name;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        routeTag.DataTokens[MagicStrings.DATATOKENS_MENUINFO] = menuInfo;
                        #endregion

                        routes.Add(
                            MagicStrings.FormatRouteName(applicationGroup, routeTag.Name)
                            , new Route(routeTag.Url
                                        , routeTag.Defaults
                                        , routeTag.Constraints
                                        , routeTag.DataTokens
                                        , routeTag.RouteHandler));
                    }
                }
            }
            #endregion
        }