001/* 002 * Copyright 2020 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.mobileapp; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.avalon.framework.configuration.DefaultConfiguration; 023import org.apache.avalon.framework.context.Context; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.slf4j.Logger; 026 027import org.ametys.cms.search.solr.field.LastValidationSearchField; 028import org.ametys.cms.search.ui.model.SearchUIColumn; 029import org.ametys.cms.search.ui.model.SearchUICriterion; 030import org.ametys.cms.search.ui.model.SearchUIModel; 031import org.ametys.cms.search.ui.model.SearchUIModelHelper; 032import org.ametys.cms.search.ui.model.impl.SystemSearchUIColumn; 033import org.ametys.runtime.plugin.component.ThreadSafeComponentManager; 034 035/** 036 * Wrapper for a SearchUIModel to force LastValidationSystemProperty to be in result fields 037 */ 038public class SearchModelWrapper implements SearchUIModel 039{ 040 /** The logger */ 041 protected Logger _logger; 042 043 /** The service manager */ 044 protected ServiceManager _manager; 045 046 /** The context */ 047 protected Context _context; 048 049 /** The search model helper. */ 050 protected SearchUIModelHelper _searchModelHelper; 051 052 private SearchUIModel _realModel; 053 054 /** 055 * Constructor with the SearchUIModel to wrap 056 * @param model the SearchUIModel to wrap 057 * @param manager The service manager 058 * @param context The context 059 * @param logger The logger 060 */ 061 public SearchModelWrapper(SearchUIModel model, ServiceManager manager, Context context, Logger logger) 062 { 063 _realModel = model; 064 _manager = manager; 065 _context = context; 066 _logger = logger; 067 } 068 069 public Map<String, SearchUIColumn> getResultFields(Map<String, Object> contextualParameters) 070 { 071 Map<String, SearchUIColumn> resultFields = _realModel.getResultFields(contextualParameters); 072 // If no field is already a LastValidationSearchField, add it 073 if (!resultFields.values().stream().anyMatch(searchUiColumn -> searchUiColumn.getSearchField() instanceof LastValidationSearchField)) 074 { 075 try 076 { 077 SearchUIColumn lastValidation = getLastValidationColumn(contextualParameters); 078 String id = lastValidation.getId(); 079 Map<String, SearchUIColumn> result = new HashMap<>(); 080 result.putAll(resultFields); 081 result.put(id, lastValidation); 082 083 return result; 084 } 085 catch (Exception e) 086 { 087 getLogger().error("Error while adding 'lastValidation' in the set of result columns"); 088 } 089 } 090 return resultFields; 091 } 092 093 /** 094 * Create a new SearchUIColumn for the last validation date 095 * @param contextualParameters the contextual parameters. Can be null. 096 * @return a new SearchUIColumn for the last validation date 097 * @throws Exception something went wrong 098 */ 099 protected SearchUIColumn getLastValidationColumn(Map<String, Object> contextualParameters) throws Exception 100 { 101 ThreadSafeComponentManager<SearchUIColumn> searchColumnManager = new ThreadSafeComponentManager<>(); 102 searchColumnManager.setLogger(getLogger()); 103 searchColumnManager.contextualize(_context); 104 searchColumnManager.service(_manager); 105 String property = "lastValidation"; 106 DefaultConfiguration conf = new DefaultConfiguration("column"); 107 DefaultConfiguration propConf = new DefaultConfiguration("systemProperty"); 108 propConf.setAttribute("name", property); 109 conf.addChild(propConf); 110 111 searchColumnManager.addComponent("cms", null, "lastValidation", SystemSearchUIColumn.class, conf); 112 searchColumnManager.initialize(); 113 return searchColumnManager.lookup("lastValidation"); 114 } 115 116 117 public Set<String> getContentTypes(Map<String, Object> contextualParameters) 118 { 119 return _realModel.getContentTypes(contextualParameters); 120 } 121 122 public Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters) 123 { 124 return _realModel.getExcludedContentTypes(contextualParameters); 125 } 126 127 public Map<String, SearchUICriterion> getCriteria(Map<String, Object> contextualParameters) 128 { 129 return _realModel.getCriteria(contextualParameters); 130 } 131 132 public Map<String, SearchUICriterion> getFacetedCriteria(Map<String, Object> contextualParameters) 133 { 134 return _realModel.getFacetedCriteria(contextualParameters); 135 } 136 137 public Map<String, SearchUICriterion> getAdvancedCriteria(Map<String, Object> contextualParameters) 138 { 139 return _realModel.getAdvancedCriteria(contextualParameters); 140 } 141 142 public int getPageSize(Map<String, Object> contextualParameters) 143 { 144 return _realModel.getPageSize(contextualParameters); 145 } 146 147 public String getWorkspace(Map<String, Object> contextualParameters) 148 { 149 return _realModel.getWorkspace(contextualParameters); 150 } 151 152 public String getSearchUrl(Map<String, Object> contextualParameters) 153 { 154 return _realModel.getSearchUrl(contextualParameters); 155 } 156 157 public String getSearchUrlPlugin(Map<String, Object> contextualParameters) 158 { 159 return _realModel.getSearchUrlPlugin(contextualParameters); 160 } 161 162 public String getExportCSVUrl(Map<String, Object> contextualParameters) 163 { 164 return _realModel.getExportCSVUrl(contextualParameters); 165 } 166 167 public String getExportCSVUrlPlugin(Map<String, Object> contextualParameters) 168 { 169 return _realModel.getExportCSVUrlPlugin(contextualParameters); 170 } 171 172 public String getExportDOCUrl(Map<String, Object> contextualParameters) 173 { 174 return _realModel.getExportDOCUrl(contextualParameters); 175 } 176 177 public String getExportDOCUrlPlugin(Map<String, Object> contextualParameters) 178 { 179 return _realModel.getExportDOCUrlPlugin(contextualParameters); 180 } 181 182 public String getExportXMLUrl(Map<String, Object> contextualParameters) 183 { 184 return _realModel.getExportXMLUrl(contextualParameters); 185 } 186 187 public String getExportXMLUrlPlugin(Map<String, Object> contextualParameters) 188 { 189 return _realModel.getExportXMLUrlPlugin(contextualParameters); 190 } 191 192 public String getPrintUrl(Map<String, Object> contextualParameters) 193 { 194 return _realModel.getPrintUrl(contextualParameters); 195 } 196 197 public String getPrintUrlPlugin(Map<String, Object> contextualParameters) 198 { 199 return _realModel.getPrintUrlPlugin(contextualParameters); 200 } 201 202 public String getSummaryView() 203 { 204 return _realModel.getSummaryView(); 205 } 206 207 /** 208 * Get the logger. 209 * @return the logger. 210 */ 211 protected final Logger getLogger() 212 { 213 return _logger; 214 } 215}