001/*
002 *  Copyright 2017 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.plugins.contentio.synchronize.search;
017
018import java.util.HashMap;
019import java.util.LinkedHashMap;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.Map;
023
024import org.ametys.runtime.i18n.I18nizableText;
025
026import com.google.common.collect.ImmutableList;
027import com.google.common.collect.ImmutableMap;
028
029/**
030 * Search model configuration for SCC import/search tool
031 */
032public class SCCSearchModelConfiguration
033{
034    /** Criteria to mask imported elements in SCCSearchTool */
035    public static final String CRITERIA_MASK_IMPORTED = "maskImported";
036    
037    private static final Map<String, Object> __MASK_IMPORTED_CRITERION = ImmutableMap.of(
038            "id", CRITERIA_MASK_IMPORTED, 
039            "label", new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_UITOOL_SEARCH_MASK_IMPORTED"), 
040            "type", "BOOLEAN"
041        );
042    
043    private boolean _displayMaskImported = true;
044    private Map<String, Map<String, Object>> _criteria = new LinkedHashMap<>();
045    private List<Map<String, Object>> _columns = new LinkedList<>();
046
047    /**
048     * Set to <code>false</code> if you don't want to display the maskImported criteria.
049     * @param displayMaskImported <code>false</code> if you want to disable the maskImportedCriteria
050     */
051    public void displayMaskImported(boolean displayMaskImported)
052    {
053        _displayMaskImported = displayMaskImported;
054    }
055    
056    /**
057     * Add a criterion to the model.
058     * @param id ID
059     */
060    public void addCriterion(String id)
061    {
062        addCriterion(id, new I18nizableText(id));
063    }
064    
065    /**
066     * Add a criterion to the model.
067     * @param id ID
068     * @param label Internationalized label
069     */
070    public void addCriterion(String id, I18nizableText label)
071    {
072        addCriterion(id, label, "STRING");
073    }
074
075    /**
076     * Add a criterion to the model.
077     * @param id ID
078     * @param label Internationalized label
079     * @param type Type (string, boolean, etc.)
080     */
081    public void addCriterion(String id, I18nizableText label, String type)
082    {
083        addCriterion(id, label, type, null);
084    }
085
086    /**
087     * Add a criterion to the model.
088     * @param id ID
089     * @param label Internationalized label
090     * @param type Type (string, boolean, etc.)
091     * @param widget The widget to display
092     */
093    public void addCriterion(String id, I18nizableText label, String type, String widget)
094    {
095        Map<String, Object> criterion = new HashMap<>();
096        criterion.put("id", id);
097        criterion.put("label", label);
098        criterion.put("type", type);
099        criterion.put("widget", widget);
100        _criteria.put(id, criterion);
101    }
102
103    /**
104     * Add a column to the model.
105     * @param id ID
106     */
107    public void addColumn(String id)
108    {
109        addColumn(id, new I18nizableText(id));
110    }
111    
112    /**
113     * Add a column to the model.
114     * @param id ID
115     * @param label Internationalized label
116     */
117    public void addColumn(String id, I18nizableText label)
118    {
119        addColumn(id, label, 150);
120    }
121    
122    /**
123     * Add a column to the model.
124     * @param id ID
125     * @param label Internationalized label
126     * @param width Width of the column
127     */
128    public void addColumn(String id, I18nizableText label, int width)
129    {
130        addColumn(id, label, width, true);
131    }
132
133    /**
134     * Add a column to the model.
135     * @param id ID
136     * @param label Internationalized label
137     * @param sortable true if sortable
138     */
139    public void addColumn(String id, I18nizableText label, boolean sortable)
140    {
141        addColumn(id, label, 150, sortable);
142    }
143    
144    /**
145     * Add a column to the model.
146     * @param id ID
147     * @param label Internationalized label
148     * @param width Width of the column
149     * @param sortable true if sortable
150     */
151    public void addColumn(String id, I18nizableText label, int width, boolean sortable)
152    {
153        addColumn(id, label, width, sortable, "STRING");
154    }
155
156    /**
157     * Add a column to the model.
158     * @param id ID
159     * @param label Internationalized label
160     * @param width Width of the column
161     * @param sortable true if sortable
162     * @param type Type (string, boolean, etc.)
163     */
164    public void addColumn(String id, I18nizableText label, int width, boolean sortable, String type)
165    {
166        String renderer = null;
167        if (_displayMaskImported && _columns.isEmpty())
168        {
169            renderer = "Ametys.plugins.contentio.search.SCCSearchTool.renderImported";
170        }
171        addColumn(id, label, width, sortable, type, renderer);
172    }
173
174    /**
175     * Add a column to the model.
176     * @param id ID
177     * @param label Internationalized label
178     * @param width Width of the column
179     * @param sortable true if sortable
180     * @param type Type (string, boolean, etc.)
181     * @param renderer The render JS function
182     */
183    public void addColumn(String id, I18nizableText label, int width, boolean sortable, String type, String renderer)
184    {
185        Map<String, Object> column = new HashMap<>();
186        column.put("id", id);
187        column.put("label", label);
188        column.put("sortable", sortable);
189        column.put("type", type);
190        column.put("width", width);
191        column.put("renderer", renderer);
192        
193        _columns.add(column);
194    }
195
196    /**
197     * Convert criteria and columns to JSON Map and add searchUrlPlugin, searchUrl and pageSize to JSON results.
198     * @return A Map to be convert as JSON
199     */
200    public Map<String, Object> toJSON()
201    {
202        Map<String, Object> json = new HashMap<>();
203        Map<String, Map<String, Object>> displayedCriteria = new LinkedHashMap<>(_criteria);
204        if (_displayMaskImported)
205        {
206            displayedCriteria.put(CRITERIA_MASK_IMPORTED, __MASK_IMPORTED_CRITERION);
207        }
208        
209        json.put("criteria", ImmutableMap.of("fieldsets", ImmutableList.of(ImmutableMap.of("elements", displayedCriteria, "role", "fieldset"))));
210        json.put("columns", _columns);
211        json.put("searchUrlPlugin", "contentio");
212        json.put("searchUrl", "scc-search/list.json");
213        json.put("pageSize", 50);
214        return json;
215    }
216}