001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.workplace.tools.modules;
033
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.widgets.CmsCheckboxWidget;
039import org.opencms.widgets.CmsInputWidget;
040import org.opencms.workplace.CmsDialog;
041import org.opencms.workplace.CmsWidgetDialog;
042import org.opencms.workplace.CmsWidgetDialogParameter;
043import org.opencms.workplace.tools.CmsToolDialog;
044
045import java.util.Collections;
046import java.util.HashMap;
047import java.util.Map;
048
049import javax.servlet.http.HttpServletRequest;
050import javax.servlet.http.HttpServletResponse;
051import javax.servlet.jsp.PageContext;
052
053import org.apache.commons.logging.Log;
054
055/**
056 * Clones a module.<p>
057 */
058public class CmsCloneModule extends CmsWidgetDialog {
059
060    /** The clone module information bean. */
061    public static final String ATTR_CLONE_MODULE_INFO = "cloneModuleInfo";
062
063    /** The message key prefix. */
064    public static final String KEY_PREFIX = "modules";
065
066    /** Path to the clone module report JSP. */
067    private static final String CLONE_MODULE_REPORT = "/system/workplace/admin/modules/module-clone-report.jsp";
068
069    /** The log object for this class. */
070    private static final Log LOG = CmsLog.getLog(CmsCloneModule.class);
071
072    /** The dialog pages. */
073    private static final String[] PAGES = {"page0"};
074
075    /** Module name. */
076    protected String m_paramModule;
077
078    /** The clone module information. */
079    private CmsCloneModuleInfo m_cloneInfo;
080
081    /**
082     * Constructor, with parameters.<p>
083     *
084     * @param context the JSP page context object
085     * @param req the JSP request
086     * @param res the JSP response
087     */
088    public CmsCloneModule(PageContext context, HttpServletRequest req, HttpServletResponse res) {
089
090        super(context, req, res);
091    }
092
093    /**
094     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
095     */
096    @Override
097    public void actionCommit() {
098
099        try {
100            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_cloneInfo.getName())) {
101                throw new CmsException(Messages.get().container(Messages.ERR_CLONEMODULE_EMPTY_PACKAGE_NAME_0));
102            }
103            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_cloneInfo.getNiceName())) {
104                throw new CmsException(Messages.get().container(Messages.ERR_CLONEMODULE_EMPTY_MODULE_NAME_0));
105            }
106            if (OpenCms.getModuleManager().hasModule(m_cloneInfo.getName())) {
107                throw new CmsException(
108                    Messages.get().container(Messages.ERR_CLONEMODULE_MODULE_ALREADY_EXISTS_1, m_cloneInfo.getName()));
109            }
110            getJsp().getRequest().setAttribute(ATTR_CLONE_MODULE_INFO, m_cloneInfo);
111            Map<String, String[]> params = new HashMap<String, String[]>();
112            params.put(PARAM_CLOSELINK, new String[] {getParamCloseLink()});
113            params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
114            getToolManager().jspForwardPage(this, CLONE_MODULE_REPORT, params);
115        } catch (Exception e) {
116            LOG.error(e.getLocalizedMessage(), e);
117            setCommitErrors(Collections.<Throwable> singletonList(e));
118        }
119    }
120
121    /**
122     * Gets the module parameter.<p>
123     *
124     * @return the module parameter
125     */
126    public String getParamModule() {
127
128        return m_paramModule;
129    }
130
131    /**
132     * Sets the module parameter.<p>
133     * @param paramModule the module parameter
134     */
135    public void setParamModule(String paramModule) {
136
137        m_paramModule = paramModule;
138    }
139
140    /**
141     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
142     */
143    @Override
144    protected String createDialogHtml(String page) {
145
146        StringBuffer result = new StringBuffer(1024);
147        if (PAGES[0].equals(page)) {
148
149            // create widget table
150            result.append(createWidgetTableStart());
151
152            // show error header once if there were validation errors
153            result.append(createWidgetErrorHeader());
154
155            result.append(createWidgetBlockStart(getMessages().key(Messages.GUI_CLONEMODULE_MODULE_INFORMATION_0)));
156            result.append(createDialogRowsHtml(0, 4));
157            result.append(createWidgetBlockEnd());
158            result.append(createWidgetBlockStart(getMessages().key(Messages.GUI_CLONEMODULE_AUTHOR_INFORMATION_0)));
159            result.append(createDialogRowsHtml(5, 6));
160            result.append(createWidgetBlockEnd());
161            result.append(createWidgetBlockStart(getMessages().key(Messages.GUI_CLONEMODULE_TRANSLATION_OPTIONS_0)));
162            result.append(createDialogRowsHtml(7, 13));
163            result.append(createWidgetBlockEnd());
164
165            // close widget table
166            result.append(createWidgetTableEnd());
167        } else {
168            result.append(page);
169
170        }
171
172        return result.toString();
173    }
174
175    /**
176     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
177     */
178    @Override
179    protected void defineWidgets() {
180
181        initCloneInfo();
182        setKeyPrefix(KEY_PREFIX);
183        // new module info
184        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "name", new CmsInputWidget()));
185        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "niceName", new CmsInputWidget()));
186        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "description", new CmsInputWidget()));
187        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "group", new CmsInputWidget()));
188        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "actionClass", new CmsInputWidget()));
189        // author info
190        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "authorName", new CmsInputWidget()));
191        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "authorEmail", new CmsInputWidget()));
192        // translation options
193        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "sourceNamePrefix", new CmsInputWidget()));
194        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "targetNamePrefix", new CmsInputWidget()));
195        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "formatterSourceModule", new CmsInputWidget()));
196        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "formatterTargetModule", new CmsInputWidget()));
197        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "changeResourceTypes", new CmsCheckboxWidget()));
198        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "rewriteContainerPages", new CmsCheckboxWidget()));
199        addWidget(new CmsWidgetDialogParameter(m_cloneInfo, "applyChangesEverywhere", new CmsCheckboxWidget()));
200    }
201
202    /**
203     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
204     */
205    @Override
206    protected String[] getPageArray() {
207
208        return PAGES;
209    }
210
211    /**
212     * @see org.opencms.workplace.CmsWorkplace#initMessages()
213     */
214    @Override
215    protected void initMessages() {
216
217        // add specific dialog resource bundle
218        addMessages(Messages.get().getBundleName());
219        // add default resource bundles
220        super.initMessages();
221    }
222
223    /**
224     * Initializes the resource type info bean.<p>
225     */
226    private void initCloneInfo() {
227
228        Object dialogObject = getDialogObject();
229        if ((dialogObject == null) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
230            m_cloneInfo = new CmsCloneModuleInfo();
231            m_cloneInfo.setSourceModuleName(getParamModule());
232        } else {
233            m_cloneInfo = (CmsCloneModuleInfo)dialogObject;
234        }
235    }
236}