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.database;
029
030import org.opencms.importexport.CmsExportParameters;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.widgets.CmsCalendarWidget;
035import org.opencms.widgets.CmsCheckboxWidget;
036import org.opencms.widgets.CmsComboWidget;
037import org.opencms.widgets.CmsInputWidget;
038import org.opencms.widgets.CmsSelectWidgetOption;
039import org.opencms.widgets.CmsVfsFileWidget;
040import org.opencms.workplace.CmsWidgetDialog;
041import org.opencms.workplace.CmsWidgetDialogParameter;
042import org.opencms.workplace.CmsWorkplaceSettings;
043import org.opencms.workplace.tools.CmsToolDialog;
044import org.opencms.workplace.tools.CmsToolManager;
045
046import java.io.File;
047import java.io.IOException;
048import java.util.ArrayList;
049import java.util.HashMap;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Map;
053
054import javax.servlet.ServletException;
055import javax.servlet.http.HttpServletRequest;
056import javax.servlet.http.HttpServletResponse;
057import javax.servlet.jsp.PageContext;
058
059/**
060 * Widget dialog that sets the export options to export VFS resources to the OpenCms server.<p>
061 *
062 * @since 6.0.0
063 */
064public class CmsDatabaseExportDialog extends CmsWidgetDialog {
065
066    /** Defines which pages are valid for this dialog. */
067    public static final String[] PAGES = {"page1"};
068
069    /** The import JSP report workplace URI. */
070    protected static final String EXPORT_ACTION_REPORT = PATH_WORKPLACE + "admin/database/reports/export.jsp";
071
072    /** The export parameters object that is edited on this dialog. */
073    private CmsExportParameters m_exportParams;
074
075    /**
076     * Public constructor with JSP action element.<p>
077     *
078     * @param jsp an initialized JSP action element
079     */
080    public CmsDatabaseExportDialog(CmsJspActionElement jsp) {
081
082        super(jsp);
083    }
084
085    /**
086     * Public constructor with JSP variables.<p>
087     *
088     * @param context the JSP page context
089     * @param req the JSP request
090     * @param res the JSP response
091     */
092    public CmsDatabaseExportDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
093
094        this(new CmsJspActionElement(context, req, res));
095    }
096
097    /**
098     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
099     */
100    @Override
101    public void actionCommit() throws IOException, ServletException {
102
103        List errors = new ArrayList();
104        // create absolute RFS path and store it in dialog object
105        String exportFileName = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
106            OpenCms.getSystemInfo().getPackagesRfsPath() + File.separator + m_exportParams.getPath());
107        m_exportParams.setPath(exportFileName);
108        setDialogObject(m_exportParams);
109        Map params = new HashMap();
110        // set the name of this class to get dialog object in report
111        params.put(CmsDatabaseExportReport.PARAM_CLASSNAME, this.getClass().getName());
112        // set style to display report in correct layout
113        params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
114        // set close link to get back to overview after finishing the import
115        params.put(PARAM_CLOSELINK, CmsToolManager.linkForToolPath(getJsp(), "/database"));
116        // redirect to the report output JSP
117        getToolManager().jspForwardPage(this, EXPORT_ACTION_REPORT, params);
118        // set the list of errors to display when saving failed
119        setCommitErrors(errors);
120    }
121
122    /**
123     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
124     */
125    @Override
126    protected String createDialogHtml(String dialog) {
127
128        StringBuffer result = new StringBuffer(1024);
129
130        // create table
131        result.append(createWidgetTableStart());
132
133        // show error header once if there were validation errors
134        result.append(createWidgetErrorHeader());
135
136        // create export file name block
137        result.append(createWidgetBlockStart(key(Messages.GUI_DATABASE_EXPORT_FILE_BLOCK_0)));
138        result.append(createDialogRowsHtml(0, 0));
139        result.append(createWidgetBlockEnd());
140
141        // create export data type block
142        result.append(createWidgetBlockStart(key(Messages.GUI_DATABASE_EXPORT_TYPES_BLOCK_0)));
143        result.append(createDialogRowsHtml(1, 3));
144        result.append(createWidgetBlockEnd());
145
146        // create export settings block
147        result.append(createWidgetBlockStart(key(Messages.GUI_DATABASE_EXPORT_SETTINGS_BLOCK_0)));
148        result.append(createDialogRowsHtml(4, 9));
149        result.append(createWidgetBlockEnd());
150
151        // create export resource(s) block
152        result.append(createWidgetBlockStart(key(Messages.GUI_DATABASE_EXPORT_RESOURCES_BLOCK_0)));
153        result.append(createDialogRowsHtml(10, 10));
154        result.append(createWidgetBlockEnd());
155
156        // close table
157        result.append(createWidgetTableEnd());
158
159        return result.toString();
160    }
161
162    /**
163     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
164     */
165    @Override
166    protected void defineWidgets() {
167
168        // initialize the sexport object to use for the dialog
169        initDatabaseExportObject();
170
171        List exportFiles = getComboExportFiles();
172        if (exportFiles.isEmpty()) {
173            // no export files available, display text input field
174            addWidget(new CmsWidgetDialogParameter(m_exportParams, "path", PAGES[0], new CmsInputWidget()));
175        } else {
176            // one or more export files present, create combo widget
177            addWidget(new CmsWidgetDialogParameter(m_exportParams, "path", PAGES[0], new CmsComboWidget(exportFiles)));
178        }
179
180        addWidget(
181            new CmsWidgetDialogParameter(m_exportParams, "exportResourceData", PAGES[0], new CmsCheckboxWidget()));
182        addWidget(new CmsWidgetDialogParameter(m_exportParams, "exportAccountData", PAGES[0], new CmsCheckboxWidget()));
183        addWidget(new CmsWidgetDialogParameter(m_exportParams, "exportProjectData", PAGES[0], new CmsCheckboxWidget()));
184
185        addWidget(
186            new CmsWidgetDialogParameter(
187                m_exportParams,
188                "includeUnchangedResources",
189                PAGES[0],
190                new CmsCheckboxWidget()));
191        addWidget(
192            new CmsWidgetDialogParameter(m_exportParams, "includeSystemFolder", PAGES[0], new CmsCheckboxWidget()));
193        addWidget(
194            new CmsWidgetDialogParameter(m_exportParams, "contentAge", "0", PAGES[0], new CmsCalendarWidget(), 0, 1));
195        addWidget(new CmsWidgetDialogParameter(m_exportParams, "recursive", PAGES[0], new CmsCheckboxWidget()));
196        addWidget(new CmsWidgetDialogParameter(m_exportParams, "inProject", PAGES[0], new CmsCheckboxWidget()));
197        addWidget(new CmsWidgetDialogParameter(m_exportParams, "exportAsFiles", PAGES[0], new CmsCheckboxWidget()));
198
199        addWidget(
200            new CmsWidgetDialogParameter(
201                m_exportParams,
202                "resources",
203                "/",
204                PAGES[0],
205                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot()),
206                1,
207                CmsWidgetDialogParameter.MAX_OCCURENCES));
208
209    }
210
211    /**
212     * Returns the present export files on the server to show in the combo box.<p>
213     *
214     * The result list elements are of type <code>{@link org.opencms.widgets.CmsSelectWidgetOption}</code>.<p>
215     *
216     * @return the present export files on the server to show in the combo box
217     */
218    protected List getComboExportFiles() {
219
220        List result = new ArrayList(8);
221        Iterator i = CmsDatabaseImportFromServer.getFileListFromServer(true).iterator();
222        while (i.hasNext()) {
223            String fileName = (String)i.next();
224            String helpText = key(Messages.GUI_EDITOR_HELP_EXPORTFILE_1, new String[] {fileName});
225            result.add(new CmsSelectWidgetOption(fileName, false, null, helpText));
226        }
227        return result;
228    }
229
230    /**
231     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
232     */
233    @Override
234    protected String[] getPageArray() {
235
236        return PAGES;
237    }
238
239    /**
240     * Initializes the import/export object to work with depending on the dialog state and request parameters.<p>
241     */
242    protected void initDatabaseExportObject() {
243
244        Object o;
245
246        if (CmsStringUtil.isEmpty(getParamAction())) {
247            o = new CmsExportParameters();
248        } else {
249            // this is not the initial call, get the job object from session
250            o = getDialogObject();
251        }
252
253        if (!(o instanceof CmsExportParameters)) {
254            // create a new export parameters object
255            m_exportParams = new CmsExportParameters();
256        } else {
257            // reuse export parameters object stored in session
258            m_exportParams = (CmsExportParameters)o;
259        }
260
261        if (CmsStringUtil.isEmpty(getParamAction()) && (m_exportParams.getResources().size() < 1)) {
262            // on initial call, at least on resource input field has to be present
263            List initialPaths = new ArrayList(1);
264            initialPaths.add("/");
265            m_exportParams.setResources(initialPaths);
266        }
267    }
268
269    /**
270     * @see org.opencms.workplace.CmsWorkplace#initMessages()
271     */
272    @Override
273    protected void initMessages() {
274
275        // add specific dialog resource bundle
276        addMessages(Messages.get().getBundleName());
277        // add default resource bundles
278        super.initMessages();
279    }
280
281    /**
282     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
283     */
284    @Override
285    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
286
287        // initialize parameters and dialog actions in super implementation
288        super.initWorkplaceRequestValues(settings, request);
289
290        // save the current state of the export parameters (may be changed because of the widget values)
291        setDialogObject(m_exportParams);
292    }
293}