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