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.modules;
029
030import org.opencms.configuration.CmsConfigurationException;
031import org.opencms.db.CmsExportPoint;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.OpenCms;
034import org.opencms.module.CmsModule;
035import org.opencms.security.CmsSecurityException;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.widgets.CmsComboWidget;
038import org.opencms.widgets.CmsDisplayWidget;
039import org.opencms.widgets.CmsSelectWidgetOption;
040import org.opencms.widgets.CmsVfsFileWidget;
041import org.opencms.workplace.CmsDialog;
042import org.opencms.workplace.CmsWidgetDialog;
043import org.opencms.workplace.CmsWidgetDialogParameter;
044import org.opencms.workplace.CmsWorkplaceSettings;
045
046import java.util.ArrayList;
047import java.util.Iterator;
048import java.util.List;
049import java.util.Map;
050
051import javax.servlet.http.HttpServletRequest;
052import javax.servlet.http.HttpServletResponse;
053import javax.servlet.jsp.PageContext;
054
055/**
056 * Class to edit a module dependencies.<p>
057 *
058 * @since 6.0.0
059 */
060public class CmsExportpointsEdit extends CmsWidgetDialog {
061
062    /** The dialog type. */
063    public static final String DIALOG_TYPE = "ExportpointsOverview";
064
065    /** localized messages Keys prefix. */
066    public static final String KEY_PREFIX = "modules";
067
068    /** Defines which pages are valid for this dialog. */
069    public static final String[] PAGES = {"page1"};
070
071    /** The module exportpoints object that is shown on this dialog. */
072    private CmsExportPoint m_exportpoint;
073
074    /** Exportpoint name. */
075    private String m_paramExportpoint;
076
077    /** Modulename. */
078    private String m_paramModule;
079
080    /**
081     * Public constructor with JSP action element.<p>
082     *
083     * @param jsp an initialized JSP action element
084     */
085    public CmsExportpointsEdit(CmsJspActionElement jsp) {
086
087        super(jsp);
088    }
089
090    /**
091     * Public constructor with JSP variables.<p>
092     *
093     * @param context the JSP page context
094     * @param req the JSP request
095     * @param res the JSP response
096     */
097    public CmsExportpointsEdit(PageContext context, HttpServletRequest req, HttpServletResponse res) {
098
099        this(new CmsJspActionElement(context, req, res));
100    }
101
102    /**
103     * Commits the edited module.<p>
104     */
105    @Override
106    public void actionCommit() {
107
108        List errors = new ArrayList();
109
110        try {
111            // get the correct module
112            String moduleName = getParamModule();
113            CmsModule module = (CmsModule)OpenCms.getModuleManager().getModule(moduleName).clone();
114            // get the current exportpoints from the module
115            List oldExportpoints = module.getExportPoints();
116            // now loop through the exportpoints and create the new list of exportpoints
117            List newExportpoints = new ArrayList();
118            Iterator i = oldExportpoints.iterator();
119            while (i.hasNext()) {
120                CmsExportPoint exp = (CmsExportPoint)i.next();
121                if (!exp.getUri().equals(m_exportpoint.getUri())) {
122                    newExportpoints.add(exp);
123                }
124            }
125            // update the exportpoints
126            newExportpoints.add(m_exportpoint);
127            module.setExportPoints(newExportpoints);
128            // update the module
129            OpenCms.getModuleManager().updateModule(getCms(), module);
130            // refresh the list
131            Map objects = (Map)getSettings().getListObject();
132            if (objects != null) {
133                objects.remove(CmsModulesList.class.getName());
134                objects.remove(CmsExportpointsList.class.getName());
135            }
136        } catch (CmsConfigurationException ce) {
137            errors.add(ce);
138        } catch (CmsSecurityException se) {
139            errors.add(se);
140        }
141
142        // set the list of errors to display when saving failed
143        setCommitErrors(errors);
144    }
145
146    /**
147     * Builds the HTML for the dialog form.<p>
148     *
149     * @return the HTML for the dialog form
150     */
151    @Override
152    public String buildDialogForm() {
153
154        StringBuffer result = new StringBuffer(1024);
155
156        try {
157
158            // create the dialog HTML
159            result.append(createDialogHtml(getParamPage()));
160
161        } catch (Throwable t) {
162            // TODO: Error handling
163        }
164        return result.toString();
165    }
166
167    /**
168     * @see org.opencms.workplace.CmsDialog#getCancelAction()
169     */
170    @Override
171    public String getCancelAction() {
172
173        // set the default action
174        setParamPage(getPages().get(0));
175
176        return DIALOG_SET;
177    }
178
179    /**
180     * Gets the module exportpoint parameter.<p>
181     *
182     * @return the module exportpoint parameter
183     */
184    public String getParamExportpoint() {
185
186        return m_paramExportpoint;
187    }
188
189    /**
190     * Gets the module parameter.<p>
191     *
192     * @return the module parameter
193     */
194    public String getParamModule() {
195
196        return m_paramModule;
197    }
198
199    /**
200     * Sets the module exportpoint parameter.<p>
201     * @param  paramExportpoint the module exportpoint parameter
202     */
203    public void setParamExportpoint(String paramExportpoint) {
204
205        m_paramExportpoint = paramExportpoint;
206    }
207
208    /**
209     * Sets the module parameter.<p>
210     * @param paramModule the module parameter
211     */
212    public void setParamModule(String paramModule) {
213
214        m_paramModule = paramModule;
215    }
216
217    /**
218     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
219     *
220     * @param dialog the dialog (page) to get the HTML for
221     * @return the dialog HTML for all defined widgets of the named dialog (page)
222     */
223    @Override
224    protected String createDialogHtml(String dialog) {
225
226        StringBuffer result = new StringBuffer(1024);
227
228        // create table
229        result.append(createWidgetTableStart());
230
231        // show error header once if there were validation errors
232        result.append(createWidgetErrorHeader());
233
234        if (dialog.equals(PAGES[0])) {
235            result.append(dialogBlockStart(key("label.exportpointinformation")));
236            result.append(createWidgetTableStart());
237            result.append(createDialogRowsHtml(0, 2));
238            result.append(createWidgetTableEnd());
239            result.append(dialogBlockEnd());
240        }
241
242        // close table
243        result.append(createWidgetTableEnd());
244
245        return result.toString();
246    }
247
248    /**
249     * Creates the list of widgets for this dialog.<p>
250     */
251    @Override
252    protected void defineWidgets() {
253
254        initModule();
255
256        setKeyPrefix(KEY_PREFIX);
257
258        List destinations = getDestinations();
259
260        addWidget(new CmsWidgetDialogParameter(m_exportpoint, "uri", PAGES[0], new CmsVfsFileWidget()));
261        addWidget(
262            new CmsWidgetDialogParameter(
263                m_exportpoint,
264                "configuredDestination",
265                PAGES[0],
266                new CmsComboWidget(destinations)));
267        addWidget(new CmsWidgetDialogParameter(m_exportpoint, "destinationPath", PAGES[0], new CmsDisplayWidget()));
268
269    }
270
271    /**
272     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
273     */
274    @Override
275    protected String[] getPageArray() {
276
277        return PAGES;
278    }
279
280    /**
281     * @see org.opencms.workplace.CmsWorkplace#initMessages()
282     */
283    @Override
284    protected void initMessages() {
285
286        // add specific dialog resource bundle
287        addMessages(Messages.get().getBundleName());
288        // add default resource bundles
289        super.initMessages();
290    }
291
292    /**
293     * Initializes the module  to work with depending on the dialog state and request parameters.<p>
294     */
295    protected void initModule() {
296
297        Object o;
298        CmsModule module;
299
300        if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
301            // this is the initial dialog call
302            if (CmsStringUtil.isNotEmpty(m_paramModule)) {
303                // edit an existing module, get it from manager
304                o = OpenCms.getModuleManager().getModule(m_paramModule);
305            } else {
306                // create a new module
307                o = null;
308            }
309        } else {
310            // this is not the initial call, get module from session
311            o = getDialogObject();
312        }
313
314        if (!(o instanceof CmsModule)) {
315            // create a new module
316            module = new CmsModule();
317
318        } else {
319            // reuse module stored in session
320            module = (CmsModule)((CmsModule)o).clone();
321        }
322
323        List exportpoints = module.getExportPoints();
324        m_exportpoint = new CmsExportPoint();
325        if ((exportpoints != null) && (exportpoints.size() > 0)) {
326            Iterator i = exportpoints.iterator();
327            while (i.hasNext()) {
328                CmsExportPoint exportpoint = (CmsExportPoint)i.next();
329                if (exportpoint.getUri().equals(m_paramExportpoint)) {
330                    m_exportpoint = exportpoint;
331                }
332            }
333        }
334
335    }
336
337    /**
338     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
339     */
340    @Override
341    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
342
343        // set the dialog type
344        setParamDialogtype(DIALOG_TYPE);
345
346        super.initWorkplaceRequestValues(settings, request);
347
348        // save the current state of the module (may be changed because of the widget values)
349        setDialogObject(m_exportpoint);
350
351    }
352
353    /**
354     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
355     */
356    @Override
357    protected void validateParamaters() throws Exception {
358
359        String moduleName = getParamModule();
360        // check module
361        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
362        if (module == null) {
363            throw new Exception();
364        }
365        // check export point
366        if (!isNewExportPoint()) {
367            Iterator it = module.getExportPoints().iterator();
368            while (it.hasNext()) {
369                CmsExportPoint ep = (CmsExportPoint)it.next();
370                if (ep.getUri().equals(getParamExportpoint())) {
371                    // export point found
372                    return;
373                }
374            }
375            throw new Exception();
376        }
377    }
378
379    /**
380     * Returns the list of default destinations for export points.<p>
381     *
382     * The result list elements are of type <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code>.<p>
383     *
384     * @return the list of default destinations for export points
385     */
386    private List getDestinations() {
387
388        List result = new ArrayList();
389        result.add(new CmsSelectWidgetOption("WEB-INF/classes/"));
390        result.add(new CmsSelectWidgetOption("WEB-INF/lib/"));
391        return result;
392    }
393
394    /**
395     * Checks if the new export point dialog has to be displayed.<p>
396     *
397     * @return <code>true</code> if the new export point dialog has to be displayed
398     */
399    private boolean isNewExportPoint() {
400
401        return getCurrentToolPath().equals("/modules/edit/exportpoints/new");
402    }
403}