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