001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.tools.searchindex;
029
030import org.opencms.configuration.CmsSearchConfiguration;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsIllegalStateException;
034import org.opencms.main.OpenCms;
035import org.opencms.workplace.list.CmsListColumnAlignEnum;
036import org.opencms.workplace.list.CmsListColumnDefinition;
037import org.opencms.workplace.list.CmsListDirectAction;
038import org.opencms.workplace.list.CmsListItem;
039import org.opencms.workplace.list.CmsListMetadata;
040import org.opencms.workplace.list.CmsListOrderEnum;
041
042import java.util.ArrayList;
043import java.util.Iterator;
044import java.util.List;
045
046import javax.servlet.http.HttpServletRequest;
047import javax.servlet.http.HttpServletResponse;
048import javax.servlet.jsp.PageContext;
049
050/**
051 * A list that displays the search resources of a request parameter given
052 * <code>{@link org.opencms.search.CmsSearchIndexSource}</code> ("indexsource").
053 *
054 * This list is no stand-alone page but has to be embedded in another dialog
055 * (see <code> {@link org.opencms.workplace.tools.searchindex.A_CmsEmbeddedListDialog}</code>. <p>
056 *
057 * @since 6.0.0
058 */
059public class CmsSearchResourcesList extends A_CmsEmbeddedListDialog {
060
061    /** list action dummy id constant. */
062    public static final String LIST_ACTION_NONE = "an";
063
064    /** list column id constant. */
065    public static final String LIST_COLUMN_ICON = "ci";
066
067    /** list column id constant. */
068    public static final String LIST_COLUMN_PATH = "cp";
069
070    /** list id constant. */
071    public static final String LIST_ID = "lssr";
072
073    /** Stores the value of the request parameter for the search index source name. */
074    private String m_paramIndexsource;
075
076    /**
077     * Public constructor.<p>
078     *
079     * @param jsp an initialized JSP action element
080     */
081    public CmsSearchResourcesList(CmsJspActionElement jsp) {
082
083        this(jsp, LIST_ID, Messages.get().container(Messages.GUI_LIST_SEARCHRESOURCES_NAME_0));
084    }
085
086    /**
087     * Public constructor.<p>
088     *
089     * @param jsp an initialized JSP action element
090     * @param listId the id of the list
091     * @param listName the list name
092     */
093    public CmsSearchResourcesList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
094
095        this(jsp, listId, listName, LIST_COLUMN_PATH, CmsListOrderEnum.ORDER_ASCENDING, null);
096    }
097
098    /**
099     * Public constructor.<p>
100     *
101     * @param jsp an initialized JSP action element
102     * @param listId the id of the displayed list
103     * @param listName the name of the list
104     * @param sortedColId the a priory sorted column
105     * @param sortOrder the order of the sorted column
106     * @param searchableColId the column to search into
107     */
108    public CmsSearchResourcesList(
109        CmsJspActionElement jsp,
110        String listId,
111        CmsMessageContainer listName,
112        String sortedColId,
113        CmsListOrderEnum sortOrder,
114        String searchableColId) {
115
116        super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
117
118    }
119
120    /**
121     * Public constructor.<p>
122     *
123     * Public constructor with JSP variables.<p>
124     *
125     * @param context the JSP page context
126     * @param req the JSP request
127     * @param res the JSP response
128     */
129    public CmsSearchResourcesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
130
131        this(new CmsJspActionElement(context, req, res));
132    }
133
134    /**
135     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
136     */
137    @Override
138    public void executeListMultiActions() {
139
140        // view only
141    }
142
143    /**
144     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
145     */
146    @Override
147    public void executeListSingleActions() {
148
149        // view only
150    }
151
152    /**
153     * Returns the request parameter "indexsource".<p>
154     *
155     * @return the request parameter "indexsource"
156     */
157    public String getParamIndexsource() {
158
159        return m_paramIndexsource;
160    }
161
162    /**
163     * Sets the request parameter "indexsource". <p>
164     *
165     * Method intended for workplace-properietary automatic filling of
166     * request parameter values to dialogs, not for manual invocation. <p>
167     *
168     * @param indexsource the request parameter "indexsource" to set
169     */
170    public void setParamIndexsource(String indexsource) {
171
172        m_paramIndexsource = indexsource;
173    }
174
175    /**
176     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
177     */
178    @Override
179    protected void fillDetails(String detailId) {
180
181        // no details by now
182    }
183
184    /**
185     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
186     */
187    @Override
188    protected List<CmsListItem> getListItems() {
189
190        List<CmsListItem> result = new ArrayList<CmsListItem>();
191        // get content
192        List<String> resources = resources();
193        Iterator<String> itResources = resources.iterator();
194        String path;
195        while (itResources.hasNext()) {
196            path = itResources.next();
197            CmsListItem item = getList().newItem(path);
198            item.set(LIST_COLUMN_PATH, path);
199            result.add(item);
200        }
201        return result;
202    }
203
204    /**
205     * @see org.opencms.workplace.CmsWorkplace#initMessages()
206     */
207    @Override
208    protected void initMessages() {
209
210        // add specific dialog resource bundle
211        addMessages(Messages.get().getBundleName());
212        // add default resource bundles
213        super.initMessages();
214    }
215
216    /**
217     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
218     */
219    @Override
220    protected void setColumns(CmsListMetadata metadata) {
221
222        // create dummy column for corporate design reasons
223        CmsListColumnDefinition dummyCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
224        dummyCol.setName(Messages.get().container(Messages.GUI_LIST_RESOURCES_NAME_0));
225        dummyCol.setHelpText(Messages.get().container(Messages.GUI_LIST_RESOURCES_NAME_HELP_0));
226        dummyCol.setWidth("20");
227        dummyCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
228        dummyCol.setSorteable(false);
229        // add dummy icon
230        CmsListDirectAction dummyAction = new CmsListDirectAction(LIST_ACTION_NONE);
231        dummyAction.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_0));
232        dummyAction.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_HELP_0));
233        dummyAction.setIconPath(CmsSearchIndexSourceControlList.ICON_FOLDER);
234        dummyCol.addDirectAction(dummyAction);
235        // add it to the list definition
236        metadata.addColumn(dummyCol);
237
238        // add column for name
239        CmsListColumnDefinition pathCol = new CmsListColumnDefinition(LIST_COLUMN_PATH);
240        pathCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
241        pathCol.setName(Messages.get().container(Messages.GUI_LIST_SEARCHINDEX_COL_NAME_0));
242        pathCol.setWidth("100%");
243        metadata.addColumn(pathCol);
244
245    }
246
247    /**
248     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
249     */
250    @Override
251    protected void setIndependentActions(CmsListMetadata metadata) {
252
253        // no detail actions
254
255    }
256
257    /**
258     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
259     */
260    @Override
261    protected void setMultiActions(CmsListMetadata metadata) {
262
263        // view only
264    }
265
266    /**
267     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
268     */
269    @Override
270    protected void validateParamaters() throws Exception {
271
272        // test the needed parameters
273        if (getParamIndexsource() == null) {
274            throw new CmsIllegalStateException(Messages.get().container(
275                Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
276                A_CmsEditIndexSourceDialog.PARAM_INDEXSOURCE));
277        }
278    }
279
280    /**
281     * Writes the updated search configuration back to the XML
282     * configuration file and refreshes the complete list.<p>
283     *
284     * @param refresh if true, the list items are refreshed
285     */
286    protected void writeConfiguration(boolean refresh) {
287
288        // update the XML configuration
289        OpenCms.writeConfiguration(CmsSearchConfiguration.class);
290        if (refresh) {
291            refreshList();
292        }
293    }
294
295    /**
296     * Returns the configured resources of the current indexsource.
297     *
298     * @return the configured resources of the current indexsource
299     */
300    private List<String> resources() {
301
302        return OpenCms.getSearchManager().getIndexSource(getParamIndexsource()).getResourcesNames();
303    }
304
305}