001/*
002 *  Copyright 2015 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.web.repository.tag;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.cms.tag.CMSTag;
029import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
030import org.ametys.plugins.repository.TraversableAmetysObject;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * {@link WEBJCRTagProvider} for sites.
035 *
036 */
037public class WEBJCRTagProvider extends org.ametys.cms.tag.jcr.CMSJCRTagProvider
038{
039    /** Constant for plugin node name */ 
040    public static final String PLUGIN_WEB_NODE_NAME = "web";
041    
042    private SiteManager _siteManager;
043
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
049    }
050    
051    @Override
052    protected Map<String, CMSTag> _getCache(Map<String, Object> contextualParameters) throws RepositoryException
053    {
054        Request request = ContextHelper.getRequest(_context);
055        if (request == null)
056        {
057            return new HashMap<>();
058        }
059        
060        String siteName = (String) contextualParameters.get("siteName");
061        
062        @SuppressWarnings("unchecked")
063        Map<String, Map<String, CMSTag>> cache = (Map<String, Map<String, CMSTag>>) request.getAttribute(CACHE_REQUEST_ATTRIBUTE + "$" + getId());
064        if (cache == null)
065        {
066            cache = new HashMap<>();
067            request.setAttribute(CACHE_REQUEST_ATTRIBUTE + "$" + getId(), cache);
068        }
069        
070        Map<String, CMSTag> siteCache = cache.get(siteName);
071        if (siteCache == null)
072        {
073            siteCache = new HashMap<>();
074            
075            TraversableAmetysObject rootNode = getRootNode(contextualParameters);
076            _fillCache(rootNode, null, siteCache);
077            
078            cache.put(siteName, siteCache);
079        }
080        
081        return siteCache;
082    }
083    
084    @Override
085    public ModifiableTraversableAmetysObject getRootNode(Map<String, Object> contextualParameters) throws RepositoryException
086    {
087        String siteName = (String) contextualParameters.get("siteName");
088        if (siteName == null)
089        {
090            return super.getRootNode(contextualParameters);
091        }
092        else
093        {
094            ModifiableTraversableAmetysObject pluginsNode = _siteManager.getSite(siteName).getRootPlugins();
095            
096            ModifiableTraversableAmetysObject pluginNode = _getOrCreateNode(pluginsNode, PLUGIN_WEB_NODE_NAME, "ametys:unstructured");
097            
098            return _getOrCreateNode(pluginNode, _tagProviderEP.getTagsNodeName(), _tagProviderEP.getTagsNodeType());
099        }
100        
101    }
102}