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;
020import java.util.Optional;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
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.runtime.model.Enumerator;
034import org.ametys.runtime.model.ViewItem;
035import org.ametys.runtime.model.type.DataContext;
036import org.ametys.runtime.model.type.ModelItemTypeConstants;
037import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
038import org.ametys.web.repository.content.WebContent;
039import org.ametys.web.repository.site.Site;
040import org.ametys.web.search.misc.SiteQueryHelper;
041import org.ametys.web.search.solr.field.SiteSearchField;
042import org.ametys.web.site.SiteEnumerator;
043
044/**
045 * {@link SystemProperty} which represents the site of a Content.
046 */
047public class SiteSystemProperty extends AbstractSystemProperty<String, Content>
048{
049    /** The site query helper */
050    protected SiteQueryHelper _siteQueryHelper;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _siteQueryHelper = (SiteQueryHelper) manager.lookup(SiteQueryHelper.ROLE);
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, getName());
075    }
076    
077    @Override
078    public String getCriterionWidget()
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 webContent)
105        {
106            return webContent.getSiteName();
107        }
108        
109        return null;
110    }
111    
112    @Override
113    public Object valueToJSON(Content content, Optional<ViewItem> viewItem, DataContext context)
114    {
115        String value = (String) getValue(content);
116        if (value != null)
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 webContent)
136        {
137            return webContent.getSite().getTitle();
138        }
139        
140        return null;
141    }
142    
143    public Enumerator<String> getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
144    {
145        String role = "enumerator";
146        enumeratorManager.addComponent(getPluginName(), null, role, SiteEnumerator.class, configuration);
147        
148        try
149        {
150            enumeratorManager.initialize();
151            return enumeratorManager.lookup(role);
152        }
153        catch (Exception e)
154        {
155            throw new ConfigurationException("Unable to initialize the site enumerator for system property '" + getName() + "'.", configuration, e);
156        }
157    }
158    
159    @Override
160    protected String _getTypeId()
161    {
162        return ModelItemTypeConstants.STRING_TYPE_ID;
163    }
164}