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.jsp.CmsJspActionElement;
032import org.opencms.main.CmsIllegalStateException;
033import org.opencms.main.OpenCms;
034import org.opencms.search.CmsSearchIndexSource;
035import org.opencms.search.CmsSearchManager;
036import org.opencms.workplace.CmsWidgetDialog;
037import org.opencms.workplace.CmsWorkplaceSettings;
038
039import java.util.ArrayList;
040import java.util.HashMap;
041import java.util.List;
042import java.util.Map;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import javax.servlet.jsp.PageContext;
047
048/**
049 * Abstract dialog class for editing or creating a <code>{@link org.opencms.search.CmsSearchIndexSource}</code>.<p>
050 *
051 * The <code>{@link #PARAM_INDEXSOURCE}</code> ("indexsource") is supported
052 * by means of widget technology (setter / getter).<p>
053 *
054 * Also - for accessing search functionality a member <code>{@link #m_searchManager}</code>
055 * is accessible for implementations. <p>
056 *
057 * If the property "admintoolhandler-args" contains the sequence "path:/searchindex/new-indexsource",
058 * a new indexsource will be created at first visit of this page
059 * (<code>{@link #initUserObject()}</code>). Else this dialog will be for editing an existing
060 * indexsource and the request parameter "indexsource" is mandatory. <p>
061 *
062 * @since 6.0.0
063 */
064public abstract class A_CmsEditIndexSourceDialog extends CmsWidgetDialog {
065
066    /** localized messages Keys prefix. */
067    public static final String KEY_PREFIX = "indexsource";
068
069    /** Defines which pages are valid for this dialog. */
070    protected static final String[] PAGES = {"page1"};
071
072    /**
073     * The request parameter for the indexsource to work with when contacting
074     * this dialog from another. <p>
075     *
076     * It may be empty if we are on the new indexsource dialog (/searchindex/new-indexsource.jsp).<p>
077     *
078     **/
079    public static final String PARAM_INDEXSOURCE = "indexsource";
080
081    /** The user object that is edited on this dialog. */
082    protected CmsSearchIndexSource m_indexsource;
083
084    /** The search manager singleton for convenient access. **/
085    protected CmsSearchManager m_searchManager;
086
087    /** Stores the value of the request parameter for the search index Name. */
088    private String m_paramIndexSource;
089
090    /**
091     * Public constructor with JSP action element.<p>
092     *
093     * @param jsp an initialized JSP action element
094     */
095    public A_CmsEditIndexSourceDialog(CmsJspActionElement jsp) {
096
097        super(jsp);
098    }
099
100    /**
101     * Public constructor with JSP variables.<p>
102     *
103     * @param context the JSP page context
104     * @param req the JSP request
105     * @param res the JSP response
106     */
107    public A_CmsEditIndexSourceDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
108
109        this(new CmsJspActionElement(context, req, res));
110    }
111
112    /**
113     * Writes the updated search configuration back to the XML
114     * configuration file and refreshes the complete list.<p>
115     */
116    protected static void writeConfiguration() {
117
118        // update the XML configuration
119        OpenCms.writeConfiguration(CmsSearchConfiguration.class);
120    }
121
122    /**
123     * Commits the edited search index to the search manager.<p>
124     */
125    @Override
126    public void actionCommit() {
127
128        List<Throwable> errors = new ArrayList<Throwable>();
129
130        try {
131
132            // if new create it first
133            if (!m_searchManager.getSearchIndexSources().keySet().contains(m_indexsource.getName())) {
134                m_searchManager.addSearchIndexSource(m_indexsource);
135            }
136            writeConfiguration();
137
138        } catch (Throwable t) {
139            errors.add(t);
140        }
141
142        // set the list of errors to display when saving failed
143        setCommitErrors(errors);
144    }
145
146    /**
147     * Returns the request parameter value for parameter indexsource. <p>
148     *
149     * @return the request parameter value for parameter indexsource
150     */
151    public String getParamIndexsource() {
152
153        return m_paramIndexSource;
154    }
155
156    /**
157     * Sets the request parameter value for parameter indexsource. <p>
158     *
159     * @param indexsource the request parameter value for parameter indexsource
160     */
161    public void setParamIndexsource(String indexsource) {
162
163        m_paramIndexSource = indexsource;
164    }
165
166    /**
167     * Initializes the user object (a <code>{@link org.opencms.search.I_CmsSearchIndex}</code> instance.<p>
168     *
169     * Implementation always have to call <code>"super.defineWidgets()"</code> first as
170     * this action may only be done here (relies on filled request parameters, the next
171     * following operation <code>{@link CmsWidgetDialog#createDialogHtml()}</code> will
172     * rely on this. <p>
173     *
174     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
175     */
176    @Override
177    protected void defineWidgets() {
178
179        initUserObject();
180        setKeyPrefix(KEY_PREFIX);
181
182    }
183
184    /**
185     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
186     */
187    @Override
188    protected String[] getPageArray() {
189
190        return PAGES;
191    }
192
193    /**
194     * Returns the root path of this dialog (path relative to "/system/workplace/admin").<p>
195     *
196     * @return the root path of this dialog (path relative to "/system/workplace/admin")
197     */
198    protected String getToolPath() {
199
200        return "/searchindex/indexsources/indexsource";
201    }
202
203    /**
204     * @see org.opencms.workplace.CmsWorkplace#initMessages()
205     */
206    @Override
207    protected void initMessages() {
208
209        // add specific dialog resource bundle
210        addMessages(Messages.get().getBundleName());
211        // add default resource bundles
212        super.initMessages();
213    }
214
215    /**
216     * Initializes the user object to work with depending on the dialog state and request parameters.<p>
217     *
218     * Two initializations of the user object on first dialog call are possible:
219     * <ul>
220     * <li>edit an existing search index</li>
221     * <li>create a new search index with default initialization</li>
222     * </ul>
223     */
224    protected void initUserObject() {
225
226        if (m_indexsource == null) {
227            try {
228                m_indexsource = m_searchManager.getIndexSource(getParamIndexsource());
229                if (m_indexsource == null) {
230                    m_indexsource = new CmsSearchIndexSource();
231                }
232            } catch (Exception e) {
233                m_indexsource = new CmsSearchIndexSource();
234            }
235        }
236    }
237
238    /**
239     * Overridden to initialize the internal <code>CmsSearchManager</code> before initWorkplaceRequestValues ->
240     * defineWidgets ->  will access it (NPE). <p>
241     *
242     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
243     */
244    @Override
245    protected void initWorkplaceMembers(CmsJspActionElement jsp) {
246
247        m_searchManager = OpenCms.getSearchManager();
248        super.initWorkplaceMembers(jsp);
249    }
250
251    /**
252     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
253     */
254    @Override
255    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
256
257        // initialize parameters and dialog actions in super implementation
258        super.initWorkplaceRequestValues(settings, request);
259
260        // save the current search index source
261        @SuppressWarnings("unchecked")
262        Map<String, CmsSearchIndexSource> dialogObject = (Map<String, CmsSearchIndexSource>)getDialogObject();
263        if (dialogObject == null) {
264            dialogObject = new HashMap<String, CmsSearchIndexSource>();
265            dialogObject.put(PARAM_INDEXSOURCE, m_indexsource);
266            setDialogObject(dialogObject);
267        }
268
269    }
270
271    /**
272     * Checks if the new search index dialog has to be displayed.<p>
273     *
274     * @return <code>true</code> if the new search index dialog has to be displayed
275     */
276    protected boolean isNewIndexSource() {
277
278        return DIALOG_INITIAL.equals(getParamAction());
279    }
280
281    /**
282     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
283     */
284    @Override
285    protected void validateParamaters() throws Exception {
286
287        if (!isNewIndexSource()) {
288            // test the needed parameters
289            if ((getParamIndexsource() == null) && (getJsp().getRequest().getParameter("name.0") == null)) {
290                throw new CmsIllegalStateException(
291                    Messages.get().container(Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1, PARAM_INDEXSOURCE));
292            }
293        }
294    }
295}