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