001/*
002 *  Copyright 2011 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 */
016package org.ametys.plugins.translationflagging;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.core.ui.Callable;
028import org.ametys.core.ui.StaticClientSideElement;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034import org.ametys.web.repository.sitemap.Sitemap;
035
036/**
037 * This element creates a ribbon button to flag translation on a page.
038 * Provides callable methods to set and get translations.
039 */
040public class TranslationFlaggingClientSideElement extends StaticClientSideElement
041{
042    /** Constant for the translations metadata name */
043    public static final String TRANSLATIONS_META = "translations";
044    
045    private AmetysObjectResolver _resolver;
046    private SiteManager _siteManager;
047    private TranslationPageDAO _translationPageDAO;
048
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(smanager);
053        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
054        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
055        _translationPageDAO = (TranslationPageDAO) smanager.lookup(TranslationPageDAO.ROLE);
056    }
057    
058    /**
059     * Get all the translations associated with a page
060     * @param pageId The page Id
061     * @return The page and its translations data
062     */
063    @Callable
064    public Map<String, Object> getTranslations(String pageId)
065    {
066        Map<String, Object> result = new HashMap<>();
067
068        Page page = _resolver.resolveById(pageId);
069        result.put("page", _page2json(page));
070        
071        Map<String, Page> translatedPages = _translationPageDAO.getTranslations(page);
072        
073        List<Map<String, String>> translations = new ArrayList<>();
074        for (Page translatedPage : translatedPages.values())
075        {
076            translations.add(_page2json(translatedPage));
077        }
078        result.put("translations", translations);
079        
080        return result;
081    }
082    
083    private Map<String, String> _page2json (Page page)
084    {
085        Map<String, String> pageData = new HashMap<>();
086        
087        pageData.put("id", page.getId());
088        pageData.put("site", page.getSiteName());
089        pageData.put("lang", page.getSitemapName());
090        pageData.put("path", page.getPathInSitemap());
091        pageData.put("title", page.getTitle());
092        
093        return pageData;
094    }
095    
096    /**
097     * Set the translations for a specific page
098     * @param pageId The current page id
099     * @param translations an association language-pageId, to set as translations
100     * @return The list of updated pages.
101     */
102    @Callable
103    public List<String> setTranslations(String pageId, Map<String, String> translations)
104    {
105        // Get the translated pages from the request.
106        Map<String, Page> pages = _getTranslatedPages(translations, pageId);
107
108        return _translationPageDAO.setTranslations(pageId, pages);
109    }
110    
111    /**
112     * Get translated pages from the map.
113     * @param translations the associative list of translations.
114     * @param pageId the current page ID.
115     * @return the translated pages as a Map of pages, indexed by sitemap name.
116     */
117    protected Map<String, Page> _getTranslatedPages(Map<String, String> translations, String pageId)
118    {
119        Map<String, Page> pages = new HashMap<>();
120        
121        Page currentPage = _resolver.resolveById(pageId);
122        String siteName = currentPage.getSiteName();
123        
124        pages.put(currentPage.getSitemapName(), currentPage);
125        
126        Site site = _siteManager.getSite(siteName);
127        
128        AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps();
129        
130        for (Sitemap sitemap : sitemaps)
131        {
132            String name = sitemap.getSitemapName();
133            if (!name.equals(currentPage.getSitemapName()))
134            {
135                if (translations.containsKey(name) && StringUtils.isNotBlank(translations.get(name)))
136                {
137                    Page page = _resolver.resolveById(translations.get(name));
138                    if (name.equals(page.getSitemapName()))
139                    {
140                        pages.put(name, page);
141                    }
142                }
143                else
144                {
145                    pages.put(name, null);
146                }
147            }
148        }
149        
150        return pages;
151    }
152}