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.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.DefaultConfiguration;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.contenttype.MetadataType;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.search.SearchField;
029import org.ametys.cms.search.model.SystemProperty;
030import org.ametys.cms.search.query.Query;
031import org.ametys.cms.search.query.Query.Operator;
032import org.ametys.cms.search.systemprop.AbstractSystemProperty;
033import org.ametys.web.repository.content.WebContent;
034import org.ametys.web.repository.site.Site;
035import org.ametys.web.search.misc.SiteQueryHelper;
036import org.ametys.web.search.solr.field.SiteSearchField;
037import org.ametys.web.site.SiteEnumerator;
038
039/**
040 * {@link SystemProperty} which represents the site of a Content.
041 */
042public class SiteSystemProperty extends AbstractSystemProperty
043{
044    private SiteQueryHelper _siteQueryHelper;
045
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _siteQueryHelper = (SiteQueryHelper) manager.lookup(SiteQueryHelper.ROLE);
051    }
052    
053    @Override
054    public MetadataType getType()
055    {
056        return MetadataType.STRING;
057    }
058    
059    @Override
060    public boolean isMultiple()
061    {
062        return false;
063    }
064    
065    @Override
066    public boolean isSortable()
067    {
068        return true;
069    }
070    
071    @Override
072    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
073    {
074        return _siteQueryHelper.getQuery(value, operator, contextualParameters, getId());
075    }
076    
077    @Override
078    public String getWidget()
079    {
080        return "edition.select-site";
081    }
082    
083    @Override
084    public String getRenderer()
085    {
086        return "Ametys.plugins.web.search.ContentSearchTool.renderSite";
087    }
088    
089    @Override
090    public String getConverter()
091    {
092        return "Ametys.plugins.web.search.ContentSearchTool.convertSite";
093    }
094    
095    @Override
096    public SearchField getSearchField()
097    {
098        return new SiteSearchField();
099    }
100    
101    @Override
102    public Object getValue(Content content)
103    {
104        if (content instanceof WebContent)
105        {
106            return ((WebContent) content).getSiteName();
107        }
108        
109        return null;
110    }
111    
112    @Override
113    public Object getJsonValue(Content content, boolean full)
114    {
115        String value = (String) getValue(content);
116        if (value != null && full)
117        {
118            Map<String, Object> info = new LinkedHashMap<>();
119            info.put("name", value);
120            
121            Site site = ((WebContent) content).getSite();
122            if (site != null)
123            {
124                info.put("title", site.getTitle());
125            }
126            return info;
127        }
128        
129        return value;
130    }
131    
132    @Override
133    public Object getSortValue(Content content)
134    {
135        if (content instanceof WebContent)
136        {
137            return ((WebContent) content).getSite().getTitle();
138        }
139        
140        return null;
141    }
142    
143    @Override
144    public EnumeratorDefinition getEnumeratorDefinition(Configuration configuration)
145    {
146        Configuration conf = new DefaultConfiguration("enumeration");
147        return new EnumeratorDefinition(SiteEnumerator.class, conf);
148    }
149
150}