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.search.systemprop;
017
018import java.util.Collections;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.web.repository.content.WebContent;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.site.SiteConfigurationExtensionPoint;
030
031/**
032 * Web-specific TagsSystemProperty which provides the current site to the TagProvider.
033 */
034public class TagsSystemProperty extends org.ametys.cms.search.systemprop.TagsSystemProperty
035{
036    
037    /** The site configuration extension point. */
038    protected SiteConfigurationExtensionPoint _siteConfEP;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _siteConfEP = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE);
045    }
046    
047    @Override
048    public Map<String, I18nizableText> getWidgetParameters(Configuration configuration)
049    {
050        Map<String, I18nizableText> params = super.getWidgetParameters(configuration);
051        
052        // TODO Find a system to provide a simple "site" property in the search criterion definition.
053        // The search criterion definition in the model file declares "site" and the search model provides the real ID?
054        params.put("sitesField", new I18nizableText("property-site-eq"));
055        
056        return params;
057    }
058    
059    @Override
060    protected Map<String, Object> getTagContextualParameters(Content content)
061    {
062        String siteName = getSiteName(content);
063        
064        if (siteName != null)
065        {
066            return Collections.singletonMap("siteName", siteName);
067        }
068        else
069        {
070            return super.getTagContextualParameters(content);
071        }
072    }
073    
074    @Override
075    protected boolean isAutopostingEnabled(Content content)
076    {
077        Site site = getSite(content);
078        
079        if (site != null)
080        {
081            return site.getValue("tags-autoposting", false, false);
082        }
083        else
084        {
085            return super.isAutopostingEnabled(content);
086        }
087    }
088    
089    /**
090     * Get the content site name.
091     * @param content The content.
092     * @return the content site name or null if not found.
093     */
094    protected String getSiteName(Content content)
095    {
096        if (content != null && content instanceof WebContent)
097        {
098            return ((WebContent) content).getSiteName();
099        }
100        
101        return null;
102    }
103    
104    /**
105     * Get the content site name.
106     * @param content The content.
107     * @return the content site name or null if not found.
108     */
109    protected Site getSite(Content content)
110    {
111        if (content != null && content instanceof WebContent)
112        {
113            return ((WebContent) content).getSite();
114        }
115        
116        return null;
117    }
118
119}