001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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 jakarta.servlet.ServletException;
054import jakarta.servlet.http.HttpServletRequest;
055import jakarta.servlet.http.HttpServletResponse;
056import jakarta.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(
338            new CmsWidgetDialogParameter(
339                m_dialogSettings,
340                "message",
341                key(Messages.GUI_LANGUAGECOPY_SELECTLANGUAGE_DIALOG_MESSAGE_0),
342                PAGES[0],
343                new CmsDisplayWidget(),
344                1,
345                1));
346        addWidget(
347            new CmsWidgetDialogParameter(
348                m_dialogSettings,
349                "paths",
350                "/",
351                PAGES[0],
352                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
353                1,
354                CmsWidgetDialogParameter.MAX_OCCURENCES));
355        addWidget(
356            new CmsWidgetDialogParameter(m_dialogSettings, "delete", "false", PAGES[0], new CmsCheckboxWidget(), 1, 1));
357        addWidget(
358            new CmsWidgetDialogParameter(
359                m_dialogSettings,
360                "sourcelanguage",
361                "/",
362                PAGES[0],
363                new CmsRadioSelectWidget(options),
364                1,
365                1));
366        addWidget(
367            new CmsWidgetDialogParameter(
368                m_dialogSettings,
369                "targetlanguage",
370                "/",
371                PAGES[0],
372                new CmsRadioSelectWidget(options),
373                1,
374                1));
375    }
376
377    /**
378     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
379     */
380    @Override
381    protected String[] getPageArray() {
382
383        return PAGES;
384    }
385
386    /**
387     * @see org.opencms.workplace.CmsWorkplace#initMessages()
388     */
389    @Override
390    protected void initMessages() {
391
392        // add specific dialog resource bundle
393        addMessages(Messages.get().getBundleName());
394        // add default resource bundles
395        super.initMessages();
396    }
397
398    /**
399     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings,
400     *      jakarta.servlet.http.HttpServletRequest)
401     */
402    @Override
403    protected void initWorkplaceRequestValues(final CmsWorkplaceSettings settings, final HttpServletRequest request) {
404
405        initDialogObject();
406        // initialize parameters and dialog actions in super implementation
407        super.initWorkplaceRequestValues(settings, request);
408    }
409
410    /**
411     * Returns a list with the possible <code>{@link Locale}</code> selections based on the OpenCms configuration.
412     * <p>
413     *
414     * @return a list with the possible <code>{@link Locale}</code> selections based on the OpenCms configuration.
415     */
416    private List<CmsSelectWidgetOption> getLanguageSelections() {
417
418        List<CmsSelectWidgetOption> result = new LinkedList<CmsSelectWidgetOption>();
419        List<Locale> sysLocales = OpenCms.getLocaleManager().getAvailableLocales();
420        CmsSelectWidgetOption option;
421        boolean first = true;
422        for (Locale locale : sysLocales) {
423            option = new CmsSelectWidgetOption(locale.toString(), first, locale.getDisplayName(getLocale()));
424            first = false;
425            result.add(option);
426        }
427        return result;
428    }
429
430    /**
431     * Initializes the dialog object.
432     * <p>
433     */
434    private void initDialogObject() {
435
436        Object o = getDialogObject();
437        if (o != null) {
438            m_dialogSettings = (CmsLanguageCopyFolderAndLanguageSelectDialogSettings)o;
439        } else {
440            m_dialogSettings = new CmsLanguageCopyFolderAndLanguageSelectDialogSettings();
441            setDialogObject(m_dialogSettings);
442        }
443    }
444}