001/*
002 *  Copyright 2023 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.cms.search.ui.model.impl;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.apache.cocoon.ProcessingException;
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.search.model.SystemProperty;
026import org.ametys.cms.search.ui.model.SearchUIColumn;
027import org.ametys.cms.search.ui.model.SearchUIColumnHelper;
028import org.ametys.runtime.model.DefinitionContext;
029import org.ametys.runtime.model.ElementDefinition;
030import org.ametys.runtime.model.ViewElement;
031import org.ametys.runtime.model.ViewHelper;
032import org.ametys.runtime.model.ViewItem;
033
034/**
035 * This class represents a result column
036 */
037public class ViewElementSearchUIColumn extends ViewElement implements SearchUIColumn<ElementDefinition>
038{
039    private int _width = -1;
040    private boolean _hidden;
041    private boolean _editable = true;
042    private boolean _sortable = true;
043    private boolean _allowSortOnMultipleJoin;
044    private Optional<String> _defaultSorter = Optional.empty();
045    private Optional<String> _renderer = Optional.empty();
046    private Optional<String> _converter = Optional.empty();
047    
048    public int getWidth()
049    {
050        return _width > 0
051                ? _width
052                : SearchUIColumnHelper.getDefaultColumnWidth(this);
053    }
054    
055    public void setWidth (int width)
056    {
057        _width = width;
058    }
059    
060    public boolean isHidden()
061    {
062        return _hidden;
063    }
064    
065    public void setHidden (boolean hidden)
066    {
067        _hidden = hidden;
068    }
069    
070    public boolean isEditable()
071    {
072        return _editable && SearchUIColumnHelper.isEditionAllowed(this);
073    }
074    
075    public void setEditable (boolean editable)
076    {
077        _editable = editable;
078    }
079    
080    public boolean isSortable()
081    {
082        return _sortable && SearchUIColumnHelper.isSortAllowed(this);
083    }
084    
085    public void setSortable (boolean sortable)
086    {
087        _sortable = sortable;
088    }
089    
090    public boolean allowSortOnMultipleJoin()
091    {
092        return _allowSortOnMultipleJoin;
093    }
094    
095    public void setAllowSortOnMultipleJoin(boolean allowSortOnMultipleJoin)
096    {
097        _allowSortOnMultipleJoin = allowSortOnMultipleJoin;
098    }
099    
100    public String getDefaultSorter()
101    {
102        return isSortable()
103                ? _defaultSorter.orElse(null)
104                : null;
105    }
106    
107    public void setDefaultSorter(Optional<String> defaultSorter)
108    {
109        _defaultSorter = defaultSorter;
110    }
111    
112    public String getRenderer()
113    {
114        return _renderer
115                .or(this::_getDefaultRenderer)
116                .orElse(null);
117    }
118    
119    private Optional<String> _getDefaultRenderer()
120    {
121        String defaultRenderer = null;
122        if (getDefinition() instanceof SystemProperty systemProperty)
123        {
124            defaultRenderer = systemProperty.getRenderer();
125        }
126        else if (Content.ATTRIBUTE_TITLE.equals(ViewHelper.getModelViewItemPath(this)))
127        {
128            defaultRenderer = "Ametys.plugins.cms.search.SearchGridHelper.renderTitle";
129        }
130        
131        return Optional.ofNullable(defaultRenderer)
132                       .filter(StringUtils::isNotEmpty);
133    }
134    
135    public void setRenderer(Optional<String> renderer)
136    {
137        this._renderer = renderer;
138    }
139    
140    public String getConverter()
141    {
142        return _converter
143                .or(() -> SearchUIColumnHelper.getElementDefinitionDefaultConverter(getDefinition()))
144                .orElse(null);
145    }
146    
147    public void setConverter(Optional<String> converter)
148    {
149        this._converter = converter;
150    }
151    
152    @Override
153    public ViewElementSearchUIColumn createInstance()
154    {
155        return new ViewElementSearchUIColumn();
156    }
157
158    @Override
159    public Map<String, Object> toJSON(DefinitionContext context) throws ProcessingException
160    {
161        Map<String, Object> json = super.toJSON(context);
162
163        if (!json.isEmpty())
164        {
165            json.putAll(SearchUIColumnHelper.columnPropertiesToJSON(this));
166        }
167        return json;
168    }
169    
170    @Override
171    public void copyTo(ViewItem item)
172    {
173        super.copyTo(item);
174        
175        assert item instanceof ViewElementSearchUIColumn;
176        ViewElementSearchUIColumn column = (ViewElementSearchUIColumn) item;
177        
178        column.setWidth(_width);
179        column.setHidden(_hidden);
180        
181        column.setEditable(_editable);
182        column.setSortable(_sortable);
183        column.setAllowSortOnMultipleJoin(_allowSortOnMultipleJoin);
184        column.setDefaultSorter(_defaultSorter);
185        
186        column.setRenderer(_renderer);
187        column.setConverter(_converter);
188    }
189}