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 */
016
017package org.ametys.plugins.blog.repository;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.cms.tag.Tag;
029import org.ametys.cms.tag.TagHelper;
030import org.ametys.cms.tag.TagProviderExtensionPoint;
031import org.ametys.core.util.I18nUtils;
032import org.ametys.plugins.blog.BlogCacheManager;
033import org.ametys.plugins.repository.AmetysObjectFactory;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.AmetysRepositoryException;
036import org.ametys.plugins.repository.UnknownAmetysObjectException;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.plugin.component.PluginAware;
039import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
040import org.ametys.web.repository.page.PagesContainer;
041import org.ametys.web.service.ServiceExtensionPoint;
042import org.ametys.web.skin.SkinsManager;
043
044/**
045 * {@link AmetysObjectFactory} handling {@link VirtualTagPage}.
046 */
047public class VirtualTagPageFactory implements AmetysObjectFactory<VirtualTagPage>, Serviceable, PluginAware
048{
049    /** The i18n cache. */
050    protected I18nUtils _i18nUtils;
051    
052    /** The plugin name. */
053    protected String _pluginName;
054    
055    /** The i18n catalogue. */
056    protected String _i18nCatalogue;
057    
058    private AmetysObjectResolver _resolver;
059    private BlogCacheManager _cacheManager;
060    private TagProviderExtensionPoint _tagProviderEP;
061    private SkinsManager _skinsManager;
062    
063    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
064    private ServiceExtensionPoint _serviceExtensionPoint;
065    
066    @Override
067    public void service(ServiceManager manager) throws ServiceException
068    {
069        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
070        _cacheManager = (BlogCacheManager) manager.lookup(BlogCacheManager.ROLE);
071        _tagProviderEP = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE);
072        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
073        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
074        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
075        _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
076    }
077    
078    @Override
079    public void setPluginInfo(String pluginName, String featureName, String id)
080    {
081        _pluginName = pluginName;
082        _i18nCatalogue = "plugin." + pluginName;
083    }
084    
085    @Override
086    public String getScheme()
087    {
088        return "blog-tag";
089    }
090    
091    @Override
092    public VirtualTagPage getAmetysObjectById(String id) throws AmetysRepositoryException
093    {
094        // id is like blog-tag://tagName?rootId=...
095        int rootPos = id.indexOf('?');
096        String tagName = id.substring(getScheme().length() + 3, rootPos);
097        String rootId = id.substring(rootPos + "?rootId=".length());
098        
099        PagesContainer root = _resolver.resolveById(rootId);
100        
101        Map<String, Object> contextualParams = new HashMap<>();
102        contextualParams.put("siteName", root.getSiteName());
103        
104        Tag tag = _tagProviderEP.getTag(tagName, contextualParams);
105
106        if (tag == null)
107        {
108            throw new UnknownAmetysObjectException("There's no virtual child page named " + tagName + " for parent " + rootId);
109        }
110        
111        Set<String> descendantOrItSelfNames = TagHelper.getDescendantNames(tag, true);
112        Set<String> tagNames = _cacheManager.getTags(root.getSiteName(), root.getSitemapName());
113
114        if (Collections.disjoint(tagNames, descendantOrItSelfNames))
115        {
116            throw new UnknownAmetysObjectException("There's no virtual child page named " + tagName + " for parent " + rootId); 
117        }
118
119        String title = _i18nUtils.translate(tag.getTitle(), root.getSitemapName());
120        
121        return new VirtualTagPage(_resolver, _cacheManager, _skinsManager, _tagProviderEP, _i18nUtils, tagName, title, root, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
122    }
123
124    @Override
125    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
126    {
127        // id is like blog-tag://tagName?rootId=...
128        int rootPos = id.indexOf('?');
129        String tagName = id.substring(getScheme().length() + 3, rootPos);
130        String rootId = id.substring(rootPos + "?rootId=".length());
131        
132        PagesContainer root = _resolver.resolveById(rootId);
133        try
134        {
135            root = _resolver.resolveById(rootId);
136
137            Map<String, Object> contextualParams = new HashMap<>();
138            contextualParams.put("siteName", root.getSiteName());
139            
140            Tag tag = _tagProviderEP.getTag(tagName, contextualParams);
141            if (tag == null)
142            {
143                return false;
144            }
145
146            Set<String> descendantOrItSelfNames = TagHelper.getDescendantNames(tag, true);
147            Set<String> tagNames = _cacheManager.getTags(root.getSiteName(), root.getSitemapName());
148
149            return !Collections.disjoint(tagNames, descendantOrItSelfNames);
150        }         
151        catch (UnknownAmetysObjectException e)
152        {
153            // Ignore.
154        }
155        
156        return false;
157    }
158    
159    /**
160     * Get a translated i18n message from the cache.
161     * @param language the language.
162     * @param key the i18n message key.
163     * @return the translated text.
164     */
165    public String translate(String language, String key)
166    {
167        return _i18nUtils.translate(new I18nizableText(_i18nCatalogue, key), language);
168    }
169    
170}