001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.web.languages; 018 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.cms.languages.Language; 028import org.ametys.cms.languages.LanguagesManager; 029import org.ametys.plugins.repository.AmetysObjectIterable; 030import org.ametys.runtime.i18n.I18nizableText; 031import org.ametys.web.repository.site.Site; 032import org.ametys.web.repository.site.SiteManager; 033import org.ametys.web.repository.sitemap.Sitemap; 034 035/** 036 * Mangage existing languages 037 */ 038public class WebLanguagesManager implements Serviceable, Component 039{ 040 /** The avalon role */ 041 public static final String ROLE = WebLanguagesManager.class.getName(); 042 043 private LanguagesManager _languagesManager; 044 private SiteManager _siteManager; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 _languagesManager = (LanguagesManager) manager.lookup(LanguagesManager.ROLE); 050 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 051 } 052 053 /** 054 * Get the list of possible languages (delegate to org.ametys.cms.languages.LanguagesManager) 055 * @return the association language-code language-object 056 */ 057 public Map<String, Language> getAvailableLanguages() 058 { 059 return _languagesManager.getAvailableLanguages(); 060 } 061 062 /** 063 * Get the available languages limited to thoses available in the given Site 064 * @param site The site to look at 065 * @return the association language-code language-object 066 */ 067 public Map<String, Language> getLanguages(Site site) 068 { 069 Map<String, Language> siteLanguages = new LinkedHashMap<>(); 070 071 AmetysObjectIterable< ? extends Sitemap> sitemaps = site.getSitemaps(); 072 for (Sitemap sitemap : sitemaps) 073 { 074 Language language = getAvailableLanguages().get(sitemap.getName()); 075 if (language == null) 076 { 077 language = new Language(sitemap.getName(), new I18nizableText(sitemap.getName())); 078 } 079 siteLanguages.put(sitemap.getName(), language); 080 } 081 082 return siteLanguages; 083 } 084 085 086 /** 087 * Get the available languages limited to thoses available in the given Site 088 * @param siteName The site to look at 089 * @return the association language-code language-object 090 */ 091 public Map<String, Language> getLanguages(String siteName) 092 { 093 Site site = _siteManager.getSite(siteName); 094 return getLanguages(site); 095 } 096}