001/*
002 *  Copyright 2019 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.repository.page;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.component.Component;
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030import org.apache.commons.lang3.StringUtils;
031
032import org.ametys.cms.tag.CMSTag;
033import org.ametys.cms.tag.CMSTag.TagVisibility;
034import org.ametys.cms.tag.Tag;
035import org.ametys.cms.tag.TagProvider;
036import org.ametys.cms.tag.TagProviderExtensionPoint;
037import org.ametys.cms.tag.TagsDAO;
038import org.ametys.core.right.RightManager;
039import org.ametys.core.ui.Callable;
040import org.ametys.core.user.CurrentUserProvider;
041import org.ametys.core.user.UserIdentity;
042import org.ametys.core.util.I18nUtils;
043import org.ametys.plugins.repository.AmetysObject;
044import org.ametys.plugins.repository.AmetysObjectResolver;
045import org.ametys.web.repository.sitemap.Sitemap;
046
047/**
048 * Component to help AddPageWizard to get all it's infos without calling {@link PageDAO} or {@link TagsDAO} multiple times
049 */
050public class AddPageWizardHelper extends AbstractLogEnabled implements Serviceable, Component
051{
052    /** Avalon Role */
053    public static final String ROLE = AddPageWizardHelper.class.getName();
054    
055    /** Ametys Object Resolver */
056    protected AmetysObjectResolver _resolver;
057    
058    /** Right Manager */
059    protected RightManager _rightManager;
060    
061    /** Current User Provider */
062    protected CurrentUserProvider _currentUserProvider;
063    
064    /** Tags DAO */
065    protected TagsDAO _tagsDAO;
066    
067    /** The tags provider */
068    protected TagProviderExtensionPoint _tagExtPt;
069    
070    /** The I18n utils */
071    protected I18nUtils _i18nUtils;
072    
073    public void service(ServiceManager manager) throws ServiceException
074    {
075        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
076        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
077        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
078        _tagsDAO = (TagsDAO) manager.lookup(TagsDAO.ROLE);
079        _tagExtPt = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE);
080        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
081    }
082
083    /**
084     * Get the page's properties
085     * @param pageId the page ID
086     * @param depth number of parents to fetch to generate the title (0 to display only the page name)
087     * @param separator separator between the parents (e.g. " > " to have "grandParent > parent > page")
088     * @return the properties
089     */
090    @Callable
091    public Map<String, Object> getPageInfos (String pageId, int depth, String separator)
092    {
093        AmetysObject page = _resolver.resolveById(pageId);
094        UserIdentity user = _currentUserProvider.getUser();
095        return getPageDetails(page, user, depth, separator);
096    }
097    
098    /**
099     * Get the page's properties
100     * @param pageIds the page IDs
101     * @param depth number of parents to fetch to generate the title (0 to display only the page name)
102     * @param separator separator between the parents (e.g. " &gt; " to have "grandParent &gt; parent &gt; page")
103     * @return the properties
104     */
105    @Callable
106    public List<Map<String, Object>> getPagesInfos (List<String> pageIds, int depth, String separator)
107    {
108        UserIdentity user = _currentUserProvider.getUser();
109        List<Map<String, Object>> result = new ArrayList<>();
110        for (String pageId : pageIds)
111        {
112            AmetysObject page = _resolver.resolveById(pageId);
113            Map<String, Object> pageInfos = getPageDetails(page, user, depth, separator);
114            result.add(pageInfos);
115        }
116        return result;
117    }
118    
119    /**
120     * get the id, rights and formatted title of a page
121     * @param page page/sitemap to work on
122     * @param user user to check rights
123     * @param depth number of parents to fetch to generate the title (0 to display only the page name)
124     * @param separator separator between the parents (e.g. " &gt; " to have "grandParent &gt; parent &gt; page")
125     * @return a map with id (String), rights (Set&lt;String&gt;) and title (String)
126     */
127    protected Map<String, Object> getPageDetails(AmetysObject page, UserIdentity user, int depth, String separator)
128    {
129        Map<String, Object> result = new HashMap<>();
130        result.put("id", page.getId());
131        Set<String> userRights = _rightManager.getUserRights(user, page);
132        result.put("rights", userRights);
133        
134        String title = "";
135        AmetysObject parent = page;
136        for (int i = 0; i <= depth; i++)
137        {
138            // We stop if we have no parent
139            // OR parent is not a page 
140            //   AND parent is not a Sitemap
141            // OR parent is a Sitemap and i != 0 (we display the sitemap only if it is specifically requested)
142            if (parent == null 
143                    || !(parent instanceof Page || parent instanceof Sitemap)
144                    || parent instanceof Sitemap && i != 0)
145            {
146                // If we have reached the sitemap, we stop to try to get a parent
147                break;
148            }
149            
150            if (i != 0)
151            {
152                title = separator + title;
153            }
154            
155            if (parent instanceof Page)
156            {
157                title = ((Page) parent).getTitle() + title;
158            }
159            else if (parent instanceof Sitemap)
160            {
161                Sitemap sitemap = (Sitemap) parent;
162                title = sitemap.getSiteName() + " (" + sitemap.getSitemapName() + ")" + title;
163            }
164            parent = parent.getParent();
165        }
166        
167        result.put("title", title);
168        
169        return result;
170    }
171    
172    /**
173     * Get a the of tags from a list of root tags or tag providers
174     * @param tagNames name of root tags or provider where we want to get the list of contained tags
175     * @param filters list of filters to disable, can contain PAGE_PRIVATE, CONTENT_PRIVATE, PAGE, CONTENT
176     * @param targetType tag target type
177     * @param showCategories true to display tags containing other tags. If false, only the non-parents tags will be listed
178     * @param separator separator between the parents (e.g. " &gt; " to have "grandParent &gt; parent &gt; page")
179     * @param contextualParameters the contextual parameters
180     * @return a list with one item per requested tagName, each one containing name (String), title (String) and tags (List of Map&lt;String, String&gt;, each map containing a name and title)
181     */
182    @Callable(rights = Callable.SKIP_BUILTIN_CHECK)
183    public List<Map<String, Object>> getTags(List<String> tagNames, List<String> filters, String targetType, boolean showCategories, String separator, Map<String, Object> contextualParameters)
184    {
185        List<Map<String, Object>> result = new ArrayList<>();
186        for (String tagName : tagNames)
187        {
188            if (tagName.startsWith("provider_"))
189            {
190                TagProvider<CMSTag> provider = _tagExtPt.getExtension(tagName.substring("provider_".length()));
191                if (provider != null)
192                {
193                    Map<String, Object> providerMap = new HashMap<>();
194                    
195                    providerMap.put("name", provider.getId());
196                    providerMap.put("title", provider.getLabel());
197                    
198                    List<Map<String, Object>> tags = new ArrayList<>();
199                    
200                    for (Tag childTag : provider.getTags(contextualParameters).values())
201                    {
202                        if (isTagAvailable(childTag, filters, targetType))
203                        {
204                            if (showCategories || childTag.getTags().isEmpty())
205                            {
206                                // add tag to the list
207                                Map<String, Object> subTagMap = parseTag(childTag, "");
208                                tags.add(subTagMap);
209                            }
210                            
211                            String title = _i18nUtils.translate(childTag.getTitle());
212                            tags.addAll(getSubTags(childTag, title + separator, filters, targetType, showCategories, separator));
213                        }
214                    }
215                    
216                    providerMap.put("tags", tags);
217                    result.add(providerMap);
218                }
219                else
220                {
221                    getLogger().warn("The tag provider '" + tagName + "' can not be found.");
222                }
223            }
224            else
225            {
226                Tag tag = _tagsDAO.getTag(tagName, contextualParameters);
227                if (tag != null)
228                {
229                    if (isTagAvailable(tag, filters, targetType))
230                    {
231                        Map<String, Object> tagMap = parseTag(tag, "");
232                        List<Map<String, Object>> subTags = getSubTags(tag, "", filters, targetType, showCategories, separator);
233                        tagMap.put("tags", subTags);
234                        result.add(tagMap);
235                    }
236                }
237                else
238                {
239                    getLogger().warn("The tag '" + tagName + "' can not be found.");
240                }
241            }
242        }
243        
244        return result;
245    }
246    
247    /**
248     * Check if we can use this tag
249     * @param tag tag to check
250     * @param filters list of filters to disable, can contain PAGE_PRIVATE, CONTENT_PRIVATE, PAGE, CONTENT
251     * @param targetType tag target type
252     * @return true if this tag is not impacted by the input filters
253     */
254    protected boolean isTagAvailable(Tag tag, List<String> filters, String targetType)
255    {
256        // If we can find the visibility/target of the tag, we filter it
257        if (tag instanceof CMSTag)
258        {
259            CMSTag cmsTag = (CMSTag) tag;
260
261            //PAGE_PRIVATE, CONTENT_PRIVATE, PAGE, CONTENT
262            boolean filterContent = filters.contains("CONTENT");
263            boolean filterPage = filters.contains("PAGE");
264            boolean filterPrivateContent = filters.contains("CONTENT_PRIVATE");
265            boolean filterPrivatePage = filters.contains("PAGE_PRIVATE");
266            
267            boolean isContent = cmsTag.getTarget().getName().equals("CONTENT");
268            boolean isPage = cmsTag.getTarget().getName().equals("PAGE");
269            boolean isPrivate = cmsTag.getVisibility() == TagVisibility.PRIVATE;
270            
271            boolean targetTypeOK = StringUtils.isEmpty(targetType) || cmsTag.getTarget().getName().equals(targetType);
272            boolean typeContentOK = targetTypeOK
273                                    || isContent && !(filterContent || filterPrivateContent);
274            boolean typePageOK = targetTypeOK
275                                 || isPage && !(filterPage || filterPrivatePage);
276            boolean visibilityOK = !(isPrivate && (filterPrivateContent || filterPrivatePage));
277            
278            return typeContentOK && typePageOK && visibilityOK;
279//            return !((filterContent && isContent)
280//                    || (filterPage && isPage)
281//                    || (filterPrivateContent && isContent && isPrivate)
282//                    || (filterPrivatePage && isPage && isPrivate));
283        }
284        return true;
285    }
286    
287    /**
288     * Iterate over all sub-tags for a tag and list them with their full name
289     * @param tag tag to iterate over
290     * @param parentPath The parent path to display the hierarchy in the full name
291     * @param filters list of filters to disable, can contain PAGE_PRIVATE, CONTENT_PRIVATE, PAGE, CONTENT
292     * @param targetType tag target type
293     * @param showCategories true to display tags containing other tags. If false, only the non-parents tags will be listed
294     * @param separator separator between the parents (e.g. " &gt; " to have "grandParent &gt; parent &gt; page")
295     * @return a list with one item per requested tagName, each one containing name (String), title (String) and tags (List of Map&lt;String, String&gt;, each map containing a name and title)
296     */
297    private List<Map<String, Object>> getSubTags(Tag tag, String parentPath, List<String> filters, String targetType, boolean showCategories, String separator)
298    {
299        List<Map<String, Object>> subTags = new ArrayList<>();
300        
301        for (Tag childTag : tag.getTags().values())
302        {
303            if (isTagAvailable(childTag, filters, targetType))
304            {
305                if (showCategories || childTag.getTags().isEmpty())
306                {
307                    // add tag to the list
308                    Map<String, Object> subTagMap = parseTag(childTag, parentPath);
309                    subTags.add(subTagMap);
310                }
311                
312                String title = _i18nUtils.translate(childTag.getTitle());
313                
314                subTags.addAll(getSubTags(childTag, parentPath + title + separator, filters, targetType, showCategories, separator));
315            }
316        }
317        return subTags;
318    }
319    
320    private Map<String, Object> parseTag(Tag tag, String parentPath)
321    {
322        Map<String, Object> result = new HashMap<>();
323        
324        result.put("name", tag.getName());
325        
326        String title = _i18nUtils.translate(tag.getTitle());
327        result.put("title", parentPath + title);
328        
329        return result;
330    }
331    
332}