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