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