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.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.slf4j.Logger;
024
025import org.ametys.cms.search.model.SystemPropertyExtensionPoint;
026import org.ametys.cms.search.ui.model.SearchUIColumnHelper;
027import org.ametys.cms.search.ui.model.SearchUICriterion;
028import org.ametys.cms.search.ui.model.SearchUIModel;
029import org.ametys.runtime.model.View;
030import org.ametys.runtime.model.ViewHelper;
031import org.ametys.runtime.model.ViewItemContainer;
032
033/**
034 * Wrapper for a SearchUIModel to force LastValidationSystemProperty to be in result fields
035 */
036public class SearchModelWrapper implements SearchUIModel
037{
038    /** The logger */
039    protected Logger _logger;
040
041    /** The service manager */
042    protected ServiceManager _manager;
043
044    /** The SystemProperty extension point */
045    protected SystemPropertyExtensionPoint _systemPropertyExtensionPoint;
046    
047    private SearchUIModel _realModel;
048    
049    /**
050     * Constructor with the SearchUIModel to wrap
051     * @param model the SearchUIModel to wrap
052     * @param manager The service manager
053     * @param logger The logger
054     */
055    public SearchModelWrapper(SearchUIModel model, ServiceManager manager, Logger logger)
056    {
057        _realModel = model;
058        _manager = manager;
059        _logger = logger;
060    }
061
062    public ViewItemContainer getResultItems(Map<String, Object> contextualParameters)
063    {
064        ViewItemContainer resultItems = _realModel.getResultItems(contextualParameters);
065        
066        // If no item is already a LastValidationSearchField, add it
067        if (!resultItems.hasModelViewItem("lastValidation"))
068        {
069            try
070            {
071                View result = new View();
072                
073                result.addViewItems(ViewHelper.copyViewItems(resultItems.getViewItems()));
074                resultItems.addViewItem(SearchUIColumnHelper.createModelItemColumn(getSystemPropertyExtensionPoint().getExtension("lastValidation")));
075                
076                return result;
077            }
078            catch (ServiceException e)
079            {
080                getLogger().error("Error while adding 'lastValidation' in the set of result columns", e);
081            }
082        }
083        
084        return resultItems;
085    }
086    
087    /**
088     * Retrieves the {@link SystemPropertyExtensionPoint}
089     * @return The {@link SystemPropertyExtensionPoint}
090     * @throws ServiceException if an error occurs while looking to the extension point up
091     */
092    protected SystemPropertyExtensionPoint getSystemPropertyExtensionPoint() throws ServiceException
093    {
094        if (_systemPropertyExtensionPoint == null)
095        {
096            _systemPropertyExtensionPoint = (SystemPropertyExtensionPoint) _manager.lookup(SystemPropertyExtensionPoint.ROLE);
097        }
098        
099        return _systemPropertyExtensionPoint;
100    }
101    
102    public Set<String> getContentTypes(Map<String, Object> contextualParameters)
103    {
104        return _realModel.getContentTypes(contextualParameters);
105    }
106
107    public Set<String> getExcludedContentTypes(Map<String, Object> contextualParameters)
108    {
109        return _realModel.getExcludedContentTypes(contextualParameters);
110    }
111
112    public Map<String, SearchUICriterion> getCriteria(Map<String, Object> contextualParameters)
113    {
114        return _realModel.getCriteria(contextualParameters);
115    }
116
117    public Map<String, SearchUICriterion> getFacetedCriteria(Map<String, Object> contextualParameters)
118    {
119        return _realModel.getFacetedCriteria(contextualParameters);
120    }
121
122    public Map<String, SearchUICriterion> getAdvancedCriteria(Map<String, Object> contextualParameters)
123    {
124        return _realModel.getAdvancedCriteria(contextualParameters);
125    }
126
127    public int getPageSize(Map<String, Object> contextualParameters)
128    {
129        return _realModel.getPageSize(contextualParameters);
130    }
131
132    public String getWorkspace(Map<String, Object> contextualParameters)
133    {
134        return _realModel.getWorkspace(contextualParameters);
135    }
136
137    public String getSearchUrl(Map<String, Object> contextualParameters)
138    {
139        return _realModel.getSearchUrl(contextualParameters);
140    }
141
142    public String getSearchUrlPlugin(Map<String, Object> contextualParameters)
143    {
144        return _realModel.getSearchUrlPlugin(contextualParameters);
145    }
146
147    public String getExportCSVUrl(Map<String, Object> contextualParameters)
148    {
149        return _realModel.getExportCSVUrl(contextualParameters);
150    }
151
152    public String getExportCSVUrlPlugin(Map<String, Object> contextualParameters)
153    {
154        return _realModel.getExportCSVUrlPlugin(contextualParameters);
155    }
156
157    public String getExportDOCUrl(Map<String, Object> contextualParameters)
158    {
159        return _realModel.getExportDOCUrl(contextualParameters);
160    }
161
162    public String getExportDOCUrlPlugin(Map<String, Object> contextualParameters)
163    {
164        return _realModel.getExportDOCUrlPlugin(contextualParameters);
165    }
166
167    public String getExportXMLUrl(Map<String, Object> contextualParameters)
168    {
169        return _realModel.getExportXMLUrl(contextualParameters);
170    }
171
172    public String getExportXMLUrlPlugin(Map<String, Object> contextualParameters)
173    {
174        return _realModel.getExportXMLUrlPlugin(contextualParameters);
175    }
176
177    public String getExportPDFUrl(Map<String, Object> contextualParameters)
178    {
179        return _realModel.getExportPDFUrl(contextualParameters);
180    }
181
182    public String getExportPDFUrlPlugin(Map<String, Object> contextualParameters)
183    {
184        return _realModel.getExportPDFUrlPlugin(contextualParameters);
185    }
186
187    public String getPrintUrl(Map<String, Object> contextualParameters)
188    {
189        return _realModel.getPrintUrl(contextualParameters);
190    }
191
192    public String getPrintUrlPlugin(Map<String, Object> contextualParameters)
193    {
194        return _realModel.getPrintUrlPlugin(contextualParameters);
195    }
196
197    public String getSummaryView()
198    {
199        return _realModel.getSummaryView();
200    }
201
202    /**
203     * Get the logger.
204     * @return the logger.
205     */
206    protected final Logger getLogger()
207    {
208        return _logger;
209    }
210}