001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.layouts.admin.kernel.util.SitemapURLProvider;
018    import com.liferay.layouts.admin.kernel.util.SitemapUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.model.Layout;
022    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
023    import com.liferay.portal.kernel.theme.ThemeDisplay;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.PortalUtil;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.kernel.xml.Element;
029    
030    import java.util.Locale;
031    import java.util.Set;
032    
033    /**
034     * @author Eduardo Garcia
035     */
036    @OSGiBeanProperties
037    public class LayoutSitemapURLProvider implements SitemapURLProvider {
038    
039            @Override
040            public String getClassName() {
041                    return Layout.class.getName();
042            }
043    
044            @Override
045            public void visitLayout(
046                            Element element, Layout layout, ThemeDisplay themeDisplay)
047                    throws PortalException {
048    
049                    UnicodeProperties typeSettingsProperties =
050                            layout.getTypeSettingsProperties();
051    
052                    if (!PortalUtil.isLayoutSitemapable(layout) ||
053                            !GetterUtil.getBoolean(
054                                    typeSettingsProperties.getProperty("sitemap-include"), true)) {
055    
056                            return;
057                    }
058    
059                    String layoutFullURL = PortalUtil.getLayoutFullURL(
060                            layout, themeDisplay);
061    
062                    layoutFullURL = PortalUtil.getCanonicalURL(
063                            layoutFullURL, themeDisplay, layout);
064    
065                    SitemapUtil.addURLElement(
066                            element, layoutFullURL, typeSettingsProperties,
067                            layout.getModifiedDate(), layoutFullURL,
068                            SitemapUtil.getAlternateURLs(layoutFullURL, themeDisplay, layout));
069    
070                    Set<Locale> availableLocales = LanguageUtil.getAvailableLocales(
071                            layout.getGroupId());
072    
073                    if (availableLocales.size() > 1) {
074                            Locale defaultLocale = LocaleUtil.getSiteDefault();
075    
076                            for (Locale availableLocale : availableLocales) {
077                                    if (availableLocale.equals(defaultLocale)) {
078                                            continue;
079                                    }
080    
081                                    String alternateURL = PortalUtil.getAlternateURL(
082                                            layoutFullURL, themeDisplay, availableLocale, layout);
083    
084                                    SitemapUtil.addURLElement(
085                                            element, alternateURL, typeSettingsProperties,
086                                            layout.getModifiedDate(), layoutFullURL,
087                                            SitemapUtil.getAlternateURLs(
088                                                    layoutFullURL, themeDisplay, layout));
089                            }
090                    }
091            }
092    
093    }