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.HashMap;
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.cms.search.model.SystemProperty;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.Query.Operator;
026import org.ametys.cms.search.systemprop.AbstractIndexableSystemProperty;
027import org.ametys.runtime.model.type.DataContext;
028import org.ametys.runtime.model.type.ModelItemTypeConstants;
029import org.ametys.web.repository.content.SharedContent;
030import org.ametys.web.repository.content.WebContent;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.search.query.SharedQuery;
033
034/**
035 * {@link SystemProperty} which represents the shared status of a Content.
036 */
037public class SharedSystemProperty extends AbstractIndexableSystemProperty<Boolean, Boolean, Content>
038{
039    /** Solr field name. */
040    public static final String SOLR_FIELD_NAME = "shared";
041    
042    @Override
043    public String getConverter()
044    {
045        return "Ametys.plugins.web.search.ContentSearchTool.convertSharedContent";
046    }
047    
048    @Override
049    public String getRenderer()
050    {
051        return "Ametys.plugins.web.search.ContentSearchTool.renderSharedContent";
052    }
053    
054    @Override
055    public Integer getColumnWidth()
056    {
057        return 60;
058    }
059    
060    @Override
061    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
062    {
063        if (value == null)
064        {
065            return new SharedQuery();
066        }
067        return new SharedQuery((Boolean) value, operator);
068    }
069    
070    @Override
071    public String getSolrFieldName()
072    {
073        return SOLR_FIELD_NAME;
074    }
075    
076    @Override
077    public String getSolrSortFieldName()
078    {
079        return SOLR_FIELD_NAME + "_sort";
080    }
081    
082    @Override
083    public String getSolrFacetFieldName()
084    {
085        return SOLR_FIELD_NAME;
086    }
087    
088    @Override
089    public Object getValue(Content content)
090    {
091        return content instanceof SharedContent;
092    }
093    
094    @Override
095    public Object valueToJSON(Content content, DataContext context)
096    {
097        boolean value = (boolean) getValue(content);
098        Map<String, Object> infos = new LinkedHashMap<>();
099        
100        infos.put("isShared", value);
101        
102        if (value)
103        {
104            Content sharedContent = ((SharedContent) content).getInitialContent();
105            if (sharedContent != null && sharedContent instanceof WebContent)
106            {
107                Map<String, String> orginalSite = new HashMap<>();
108                orginalSite.put("name", ((WebContent) sharedContent).getSiteName());
109                
110                Site site = ((WebContent) sharedContent).getSite();
111                if (site != null)
112                {
113                    orginalSite.put("title", site.getTitle());
114                }
115                
116                infos.put("originalSite", orginalSite);
117            }
118        }
119        
120        return infos;
121    }
122    
123    @Override
124    protected String getTypeId()
125    {
126        return ModelItemTypeConstants.BOOLEAN_TYPE_ID;
127    }
128}