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.editors;
029
030import org.opencms.file.CmsResource;
031import org.opencms.file.types.CmsResourceTypeXmlContent;
032import org.opencms.file.types.I_CmsResourceType;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.CmsWorkplace;
037
038import java.util.HashMap;
039import java.util.Map;
040
041/**
042 * Pre editor action for XML content resource types, checks if model files are available for the XML content
043 * to create in direct edit mode and shows the selection before opening the editor.<p>
044 *
045 * @since 6.5.4
046 */
047public class CmsPreEditorActionDefinitionXmlContent extends A_CmsPreEditorActionDefinition {
048
049    /**
050     * Constructor, without parameters.<p>
051     */
052    public CmsPreEditorActionDefinitionXmlContent() {
053
054        // empty constructor, needed for initialization
055    }
056
057    /**
058     * @see org.opencms.workplace.editors.I_CmsPreEditorActionDefinition#doPreAction(org.opencms.file.CmsResource, org.opencms.workplace.CmsDialog, java.lang.String)
059     */
060    @Override
061    public boolean doPreAction(CmsResource resource, CmsDialog dialog, String originalParams) throws Exception {
062
063        String newlink = dialog.getJsp().getRequest().getParameter(CmsXmlContentEditor.PARAM_NEWLINK);
064        if (CmsStringUtil.isNotEmpty(newlink)) {
065            // pre editor action not executed yet and new link is provided, now check model files for resource type
066            I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource.getTypeId());
067            String folderPath = dialog.getSettings().getExplorerResource();
068            // get the name of the currently edited resource
069            String resName = dialog.getJsp().getRequest().getParameter(CmsDialog.PARAM_RESOURCE);
070            if (CmsStringUtil.isNotEmpty(resName)) {
071                // get the folder path from the currently edited resource
072                folderPath = CmsResource.getFolderPath(resName);
073            }
074            if (CmsResourceTypeXmlContent.getModelFiles(dialog.getCms(), folderPath, type.getTypeName()).size() > 0) {
075                // model files present, display model file selection dialog before opening editor
076                Map<String, String[]> params = new HashMap<String, String[]>(4);
077                // put the original request parameters to a new parameter value
078                params.put(CmsDialog.PARAM_ORIGINALPARAMS, new String[] {originalParams});
079                // set action for dialog to open
080                params.put(CmsDialog.PARAM_ACTION, new String[] {CmsResourceTypeXmlContent.DIALOG_CHOOSEMODEL});
081                // set the title for the dialog
082                params.put(
083                    CmsDialog.PARAM_TITLE,
084                    new String[] {dialog.getJsp().getRequest().getParameter("editortitle")});
085                // set the resource type to create for the dialog
086                params.put(CmsWorkplace.PARAM_NEWRESOURCETYPE, new String[] {type.getTypeName()});
087                // set the back link URL to return to if pressing the cancel button
088                String paramBackLink = dialog.getJsp().getRequest().getParameter(CmsEditor.PARAM_BACKLINK);
089                if (CmsStringUtil.isNotEmpty(paramBackLink)) {
090                    params.put(CmsEditor.PARAM_BACKLINK, new String[] {paramBackLink});
091                }
092                // set the resource name
093                if (CmsStringUtil.isNotEmpty(resName)) {
094                    params.put(CmsDialog.PARAM_RESOURCE, new String[] {resName});
095                }
096                // forward to model file selection dialog
097                dialog.sendForward(CmsWorkplace.VFS_PATH_MODELDIALOG, params);
098                return true;
099            }
100        }
101        return false;
102    }
103
104}