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, 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.content.languagecopy;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.OpenCms;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.widgets.CmsCheckboxWidget;
034import org.opencms.widgets.CmsDisplayWidget;
035import org.opencms.widgets.CmsRadioSelectWidget;
036import org.opencms.widgets.CmsSelectWidgetOption;
037import org.opencms.widgets.CmsVfsFileWidget;
038import org.opencms.workplace.CmsWidgetDialog;
039import org.opencms.workplace.CmsWidgetDialogParameter;
040import org.opencms.workplace.CmsWorkplace;
041import org.opencms.workplace.CmsWorkplaceSettings;
042import org.opencms.workplace.tools.CmsToolDialog;
043import org.opencms.workplace.tools.CmsToolManager;
044
045import java.io.IOException;
046import java.util.ArrayList;
047import java.util.HashMap;
048import java.util.LinkedList;
049import java.util.List;
050import java.util.Locale;
051import java.util.Map;
052
053import javax.servlet.ServletException;
054import javax.servlet.http.HttpServletRequest;
055import javax.servlet.http.HttpServletResponse;
056import javax.servlet.jsp.PageContext;
057
058/**
059 * Widget dialog that collects the folders and the languages for XML content language node copy operation.
060 * <p>
061 *
062 * @since 7.5.1
063 */
064public class CmsLanguageCopyFolderAndLanguageSelectDialog extends CmsWidgetDialog {
065
066    /**
067     * Settings bean for the dialog.
068     * <p>
069     *
070     */
071    public class CmsLanguageCopyFolderAndLanguageSelectDialogSettings {
072
073        /** Signals whether to delete the original language node or not. */
074        private boolean m_delete;
075
076        /** Display message. */
077        private String m_message;
078
079        /** The paths to collect resources. */
080        private List<String> m_paths = new LinkedList<String>();
081
082        /** The list of resource paths to process: all should be files. */
083        private String[] m_resources;
084
085        /** The source language. */
086        private String m_sourcelanguage;
087
088        /** The source language. */
089        private String m_targetlanguage;
090
091        /**
092         * Constructor from the inner class.
093         * <p>
094         */
095        public CmsLanguageCopyFolderAndLanguageSelectDialogSettings() {
096
097            m_paths.add("/");
098        }
099
100        /**
101         * @return the message
102         */
103        public String getMessage() {
104
105            return m_message;
106        }
107
108        /**
109         * @return the paths
110         */
111        public List<String> getPaths() {
112
113            return m_paths;
114        }
115
116        /**
117         * @return the resources
118         */
119        public String getResources() {
120
121            return CmsStringUtil.arrayAsString(m_resources, ",");
122        }
123
124        /**
125         * @return the sourceLanguage
126         */
127        public String getSourcelanguage() {
128
129            return m_sourcelanguage;
130        }
131
132        /**
133         * @return the targetLanguage
134         */
135        public String getTargetlanguage() {
136
137            return m_targetlanguage;
138        }
139
140        /**
141         * Returns the delete.<p>
142         *
143         * @return the delete
144         */
145        public boolean isDelete() {
146
147            return m_delete;
148        }
149
150        /**
151         * Sets the delete.<p>
152         *
153         * @param delete the delete to set
154         */
155        public void setDelete(boolean delete) {
156
157            m_delete = delete;
158        }
159
160        /**
161         * @param message
162         *            the message to set
163         */
164        public void setMessage(final String message) {
165
166            // nop, this is hardcoded... just has to be here for "bean - convention".
167        }
168
169        /**
170         * @param paths
171         *            the paths to set
172         */
173        public void setPaths(final List<String> paths) {
174
175            m_paths = paths;
176        }
177
178        /**
179         * @param resources
180         *            the resources to set
181         */
182        public void setResources(final String resources) {
183
184            m_resources = CmsStringUtil.splitAsArray(resources, ",");
185
186        }
187
188        /**
189         * Sets the resources.<p>
190         *
191         * @param resources the resources to set
192         */
193        public void setResources(String[] resources) {
194
195            m_resources = resources;
196        }
197
198        /**
199         * @param sourceLanguage
200         *            the sourceLanguage to set
201         */
202        public void setSourcelanguage(final String sourceLanguage) {
203
204            m_sourcelanguage = sourceLanguage;
205        }
206
207        /**
208         * @param targetLanguage
209         *            the targetLanguage to set
210         */
211        public void setTargetlanguage(final String targetLanguage) {
212
213            m_targetlanguage = targetLanguage;
214        }
215    }
216
217    /** localized messages Keys prefix. */
218    public static final String KEY_PREFIX = "languagecopy";
219
220    /** Defines which pages are valid for this dialog. */
221    public static final String[] PAGES = {"page1"};
222
223    /** The request parameter for the resources to process from the previous dialog. */
224    public static final String PARAM_COPYRESOURCES = "copyresources";
225
226    /** The request parameter signaling whether to delete the original language node or not. */
227    public static final String PARAM_DELETE = "delete";
228
229    /** The widget mapped data container. */
230    private CmsLanguageCopyFolderAndLanguageSelectDialogSettings m_dialogSettings = new CmsLanguageCopyFolderAndLanguageSelectDialogSettings();
231
232    /**
233     * Public constructor with JSP action element.
234     * <p>
235     *
236     * @param jsp
237     *            an initialized JSP action element
238     */
239    public CmsLanguageCopyFolderAndLanguageSelectDialog(final CmsJspActionElement jsp) {
240
241        super(jsp);
242    }
243
244    /**
245     * Public constructor with JSP variables.
246     * <p>
247     *
248     * @param context the JSP page context
249     * @param req the JSP request
250     * @param res the JSP response
251     */
252    public CmsLanguageCopyFolderAndLanguageSelectDialog(
253        final PageContext context,
254        final HttpServletRequest req,
255        final HttpServletResponse res) {
256
257        this(new CmsJspActionElement(context, req, res));
258
259    }
260
261    /**
262     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
263     */
264    @Override
265    public void actionCommit() throws IOException, ServletException {
266
267        initDialogObject();
268        List<Throwable> errors = new ArrayList<Throwable>();
269        // create absolute RFS path and store it in dialog object
270
271        Map<String, String[]> params = new HashMap<String, String[]>();
272        List<String> paths = m_dialogSettings.getPaths();
273        params.put(CmsLanguageCopySelectionList.PARAM_PATHS, paths.toArray(new String[paths.size()]));
274        String sourceLanguage = m_dialogSettings.getSourcelanguage();
275        params.put(CmsLanguageCopySelectionList.PARAM_SOURCE_LANGUAGE, new String[] {sourceLanguage});
276        String targetLanguage = m_dialogSettings.getTargetlanguage();
277        params.put(CmsLanguageCopySelectionList.PARAM_TARGET_LANGUAGE, new String[] {targetLanguage});
278        String toDelete = Boolean.toString(m_dialogSettings.isDelete());
279        params.put(CmsLanguageCopySelectionList.PARAM_DELETE, new String[] {toDelete});
280        // set style to display report in correct layout
281        params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
282        // set close link to get back to overview after finishing the import
283        params.put(PARAM_CLOSELINK, new String[] {CmsToolManager.linkForToolPath(getJsp(), "/languagecopy")});
284        // redirect to the report output JSP
285        getToolManager().jspForwardPage(
286            this,
287            CmsWorkplace.PATH_WORKPLACE + "admin/contenttools/languagecopy/selectresources.jsp",
288            params);
289        // set the list of errors to display when saving failed
290        setCommitErrors(errors);
291    }
292
293    /**
294     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
295     */
296    @Override
297    protected String createDialogHtml(final String dialog) {
298
299        StringBuffer result = new StringBuffer(1024);
300
301        // create table
302        result.append(createWidgetTableStart());
303
304        // show error header once if there were validation errors
305        result.append(createWidgetErrorHeader());
306
307        // create export file name block
308        result.append(createWidgetBlockStart(null));
309        result.append(createDialogRowsHtml(0, 2));
310        result.append(createWidgetBlockEnd());
311
312        // create source language block
313        result.append(createWidgetBlockStart(key(Messages.GUI_LANGUAGECOPY_SELECTLANGUAGE_SOURCE_BLOCK_0)));
314        result.append(createDialogRowsHtml(3, 3));
315        result.append(createWidgetBlockEnd());
316
317        // create target language block
318        result.append(createWidgetBlockStart(key(Messages.GUI_LANGUAGECOPY_SELECTLANGUAGE_TARGET_BLOCK_0)));
319        result.append(createDialogRowsHtml(4, 4));
320        result.append(createWidgetBlockEnd());
321
322        // close table
323        result.append(createWidgetTableEnd());
324
325        return result.toString();
326    }
327
328    /**
329     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
330     */
331    @Override
332    protected void defineWidgets() {
333
334        setKeyPrefix(KEY_PREFIX);
335        List<CmsSelectWidgetOption> options = getLanguageSelections();
336
337        addWidget(new CmsWidgetDialogParameter(
338            m_dialogSettings,
339            "message",
340            key(Messages.GUI_LANGUAGECOPY_SELECTLANGUAGE_DIALOG_MESSAGE_0),
341            PAGES[0],
342            new CmsDisplayWidget(),
343            1,
344            1));
345        addWidget(new CmsWidgetDialogParameter(
346            m_dialogSettings,
347            "paths",
348            "/",
349            PAGES[0],
350            new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
351            1,
352            CmsWidgetDialogParameter.MAX_OCCURENCES));
353        addWidget(
354            new CmsWidgetDialogParameter(m_dialogSettings, "delete", "false", PAGES[0], new CmsCheckboxWidget(), 1, 1));
355        addWidget(
356            new CmsWidgetDialogParameter(
357                m_dialogSettings,
358                "sourcelanguage",
359                "/",
360                PAGES[0],
361                new CmsRadioSelectWidget(options),
362                1,
363                1));
364        addWidget(
365            new CmsWidgetDialogParameter(
366                m_dialogSettings,
367                "targetlanguage",
368                "/",
369                PAGES[0],
370                new CmsRadioSelectWidget(options),
371                1,
372                1));
373    }
374
375    /**
376     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
377     */
378    @Override
379    protected String[] getPageArray() {
380
381        return PAGES;
382    }
383
384    /**
385     * @see org.opencms.workplace.CmsWorkplace#initMessages()
386     */
387    @Override
388    protected void initMessages() {
389
390        // add specific dialog resource bundle
391        addMessages(Messages.get().getBundleName());
392        // add default resource bundles
393        super.initMessages();
394    }
395
396    /**
397     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
398     *      javax.servlet.http.HttpServletRequest)
399     */
400    @Override
401    protected void initWorkplaceRequestValues(final CmsWorkplaceSettings settings, final HttpServletRequest request) {
402
403        initDialogObject();
404        // initialize parameters and dialog actions in super implementation
405        super.initWorkplaceRequestValues(settings, request);
406    }
407
408    /**
409     * Returns a list with the possible <code>{@link Locale}</code> selections based on the OpenCms configuration.
410     * <p>
411     *
412     * @return a list with the possible <code>{@link Locale}</code> selections based on the OpenCms configuration.
413     */
414    private List<CmsSelectWidgetOption> getLanguageSelections() {
415
416        List<CmsSelectWidgetOption> result = new LinkedList<CmsSelectWidgetOption>();
417        List<Locale> sysLocales = OpenCms.getLocaleManager().getAvailableLocales();
418        CmsSelectWidgetOption option;
419        boolean first = true;
420        for (Locale locale : sysLocales) {
421            option = new CmsSelectWidgetOption(locale.toString(), first, locale.getDisplayName(getLocale()));
422            first = false;
423            result.add(option);
424        }
425        return result;
426    }
427
428    /**
429     * Initializes the dialog object.
430     * <p>
431     */
432    private void initDialogObject() {
433
434        Object o = getDialogObject();
435        if (o != null) {
436            m_dialogSettings = (CmsLanguageCopyFolderAndLanguageSelectDialogSettings)o;
437        } else {
438            m_dialogSettings = new CmsLanguageCopyFolderAndLanguageSelectDialogSettings();
439            setDialogObject(m_dialogSettings);
440        }
441    }
442}