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        return new SharedQuery(parseBoolean(value));
081    }
082    
083    @Override
084    public SearchField getSearchField()
085    {
086        return new SharedSeachField();
087    }
088    
089    @Override
090    public Object getValue(Content content)
091    {
092        return content instanceof SharedContent;
093    }
094    
095    @Override
096    public Object getJsonValue(Content content, boolean full)
097    {
098        boolean value = (boolean) getValue(content);
099        if (full)
100        {
101            Map<String, Object> infos = new LinkedHashMap<>();
102            
103            infos.put("isShared", value);
104            
105            if (value)
106            {
107                Content sharedContent = ((SharedContent) content).getInitialContent();
108                if (sharedContent != null && sharedContent instanceof WebContent)
109                {
110                    Map<String, String> orginalSite = new HashMap<>();
111                    orginalSite.put("name", ((WebContent) sharedContent).getSiteName());
112                    
113                    Site site = ((WebContent) sharedContent).getSite();
114                    if (site != null)
115                    {
116                        orginalSite.put("title", site.getTitle());
117                    }
118                    
119                    infos.put("originalSite", orginalSite);
120                }
121            }
122            
123            return infos;
124        }
125        else
126        {
127            return value;
128        }
129        
130    }
131}