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    @Override
059    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
060    {
061        String siteName = (String) contextParameters.get("siteName");
062        if (siteName != null)
063        {
064            Site site = _siteManager.getSite(siteName);
065            if (site.getSitemaps().getSize() > 1)
066            {
067                return super.getScripts(ignoreRights, contextParameters);
068            }
069        }
070        
071        return new ArrayList<>();
072    }
073    
074    /**
075     * Get all the translations associated with a page
076     * @param pageId The page Id
077     * @return The page and its translations data
078     */
079    @Callable
080    public Map<String, Object> getTranslations(String pageId)
081    {
082        Map<String, Object> result = new HashMap<>();
083
084        Page page = _resolver.resolveById(pageId);
085        result.put("page", _page2json(page));
086        
087        Map<String, Page> translatedPages = _translationPageDAO.getTranslations(page);
088        
089        List<Map<String, String>> translations = new ArrayList<>();
090        for (Page translatedPage : translatedPages.values())
091        {
092            translations.add(_page2json(translatedPage));
093        }
094        result.put("translations", translations);
095        
096        return result;
097    }
098    
099    private Map<String, String> _page2json (Page page)
100    {
101        Map<String, String> pageData = new HashMap<>();
102        
103        pageData.put("id", page.getId());
104        pageData.put("site", page.getSiteName());
105        pageData.put("lang", page.getSitemapName());
106        pageData.put("path", page.getPathInSitemap());
107        pageData.put("title", page.getTitle());
108        
109        return pageData;
110    }
111    
112    /**
113     * Set the translations for a specific page
114     * @param pageId The current page id
115     * @param translations an association language-pageId, to set as translations
116     * @return The list of updated pages.
117     */
118    @Callable
119    public List<String> setTranslations(String pageId, Map<String, String> translations)
120    {
121        // Get the translated pages from the request.
122        Map<String, Page> pages = _getTranslatedPages(translations, pageId);
123
124        return _translationPageDAO.setTranslations(pageId, pages);
125    }
126    
127    /**
128     * Get translated pages from the map.
129     * @param translations the associative list of translations.
130     * @param pageId the current page ID.
131     * @return the translated pages as a Map of pages, indexed by sitemap name.
132     */
133    protected Map<String, Page> _getTranslatedPages(Map<String, String> translations, String pageId)
134    {
135        Map<String, Page> pages = new HashMap<>();
136        
137        Page currentPage = _resolver.resolveById(pageId);
138        String siteName = currentPage.getSiteName();
139        
140        pages.put(currentPage.getSitemapName(), currentPage);
141        
142        Site site = _siteManager.getSite(siteName);
143        
144        AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps();
145        
146        for (Sitemap sitemap : sitemaps)
147        {
148            String name = sitemap.getSitemapName();
149            if (!name.equals(currentPage.getSitemapName()))
150            {
151                if (translations.containsKey(name) && StringUtils.isNotBlank(translations.get(name)))
152                {
153                    Page page = _resolver.resolveById(translations.get(name));
154                    if (name.equals(page.getSitemapName()))
155                    {
156                        pages.put(name, page);
157                    }
158                }
159                else
160                {
161                    pages.put(name, null);
162                }
163            }
164        }
165        
166        return pages;
167    }
168}