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.modules;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.OpenCms;
032import org.opencms.module.CmsModule;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.widgets.CmsDisplayWidget;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.CmsWidgetDialog;
037import org.opencms.workplace.CmsWidgetDialogParameter;
038import org.opencms.workplace.CmsWorkplaceSettings;
039
040import java.util.Map;
041
042import javax.servlet.http.HttpServletRequest;
043import javax.servlet.http.HttpServletResponse;
044import javax.servlet.jsp.PageContext;
045
046/**
047 * Class to show the module overview.<p>
048 *
049 * @since 6.0.0
050 */
051public class CmsModulesOverview extends CmsWidgetDialog {
052
053    /** The dialog type. */
054    public static final String DIALOG_TYPE = "ModulesOverview";
055
056    /** Defines which pages are valid for this dialog. */
057    public static final String[] PAGES = {"page1"};
058
059    /** The module object that is edited on this dialog. */
060    private CmsModule m_module;
061
062    /** Modulename. */
063    private String m_paramModule;
064
065    /**
066     * Public constructor with JSP action element.<p>
067     *
068     * @param jsp an initialized JSP action element
069     */
070    public CmsModulesOverview(CmsJspActionElement jsp) {
071
072        super(jsp);
073    }
074
075    /**
076     * Public constructor with JSP variables.<p>
077     *
078     * @param context the JSP page context
079     * @param req the JSP request
080     * @param res the JSP response
081     */
082    public CmsModulesOverview(PageContext context, HttpServletRequest req, HttpServletResponse res) {
083
084        this(new CmsJspActionElement(context, req, res));
085    }
086
087    /**
088     * Commits the edited module.<p>
089     */
090    @Override
091    public void actionCommit() {
092
093        // noop
094    }
095
096    /**
097     * Builds the HTML for the dialog form.<p>
098     *
099     * @return the HTML for the dialog form
100     */
101    @Override
102    public String buildDialogForm() {
103
104        StringBuffer result = new StringBuffer(1024);
105
106        try {
107
108            // create the dialog HTML
109            result.append(createDialogHtml(getParamPage()));
110
111        } catch (Throwable t) {
112            // TODO: Error handling
113        }
114        return result.toString();
115    }
116
117    /**
118     * @see org.opencms.workplace.CmsDialog#getCancelAction()
119     */
120    @Override
121    public String getCancelAction() {
122
123        // set the default action
124        setParamPage(getPages().get(0));
125
126        return DIALOG_SET;
127    }
128
129    /**
130     * Gets the module parameter.<p>
131     *
132     * @return the module parameter
133     */
134    public String getParamModule() {
135
136        return m_paramModule;
137    }
138
139    /**
140     * Sets the module parameter.<p>
141     * @param paramModule the module parameter
142     */
143    public void setParamModule(String paramModule) {
144
145        m_paramModule = paramModule;
146    }
147
148    /**
149     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
150     *
151     * @param dialog the dialog (page) to get the HTML for
152     * @return the dialog HTML for all defined widgets of the named dialog (page)
153     */
154    @Override
155    protected String createDialogHtml(String dialog) {
156
157        StringBuffer result = new StringBuffer(1024);
158
159        // create table
160        result.append(createWidgetTableStart());
161
162        // show error header once if there were validation errors
163        result.append(createWidgetErrorHeader());
164
165        if (dialog.equals(PAGES[0])) {
166            result.append(dialogBlockStart(key("label.moduleinformation")));
167            result.append(createWidgetTableStart());
168            result.append(createDialogRowsHtml(0, 5));
169            result.append(createWidgetTableEnd());
170            result.append(dialogBlockEnd());
171            result.append(dialogBlockStart(key("label.modulecreator")));
172            result.append(createWidgetTableStart());
173            result.append(createDialogRowsHtml(6, 7));
174            result.append(createWidgetTableEnd());
175            result.append(dialogBlockEnd());
176        }
177
178        // close table
179        result.append(createWidgetTableEnd());
180
181        return result.toString();
182    }
183
184    /**
185     * Creates the list of widgets for this dialog.<p>
186     */
187    @Override
188    protected void defineWidgets() {
189
190        initModule();
191        setKeyPrefix(CmsModulesEditBase.KEY_PREFIX);
192        addWidget(new CmsWidgetDialogParameter(m_module, "name", PAGES[0], new CmsDisplayWidget()));
193        addWidget(new CmsWidgetDialogParameter(m_module, "niceName", PAGES[0], new CmsDisplayWidget()));
194        addWidget(new CmsWidgetDialogParameter(m_module, "description", PAGES[0], new CmsDisplayWidget()));
195        addWidget(new CmsWidgetDialogParameter(m_module, "version.version", PAGES[0], new CmsDisplayWidget()));
196        addWidget(new CmsWidgetDialogParameter(m_module, "group", PAGES[0], new CmsDisplayWidget()));
197        addWidget(new CmsWidgetDialogParameter(m_module, "actionClass", PAGES[0], new CmsDisplayWidget()));
198        addWidget(new CmsWidgetDialogParameter(m_module, "authorName", PAGES[0], new CmsDisplayWidget()));
199        addWidget(new CmsWidgetDialogParameter(m_module, "authorEmail", PAGES[0], new CmsDisplayWidget()));
200
201    }
202
203    /**
204     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
205     */
206    @Override
207    protected String[] getPageArray() {
208
209        return PAGES;
210    }
211
212    /**
213     * @see org.opencms.workplace.CmsWorkplace#initMessages()
214     */
215    @Override
216    protected void initMessages() {
217
218        // add specific dialog resource bundle
219        addMessages(Messages.get().getBundleName());
220        // add default resource bundles
221        super.initMessages();
222    }
223
224    /**
225     * Initializes the module  to work with depending on the dialog state and request parameters.<p>
226     */
227    protected void initModule() {
228
229        Object o;
230
231        if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
232            // this is the initial dialog call
233            if (CmsStringUtil.isNotEmpty(m_paramModule)) {
234                // edit an existing module, get it from manager
235                o = OpenCms.getModuleManager().getModule(m_paramModule);
236            } else {
237                // create a new module
238                o = null;
239            }
240        } else {
241            // this is not the initial call, get module from session
242            o = getDialogObject();
243        }
244
245        if (!(o instanceof CmsModule)) {
246            // create a new module
247            m_module = new CmsModule();
248
249        } else {
250            // reuse module stored in session
251            m_module = (CmsModule)((CmsModule)o).clone();
252        }
253    }
254
255    /**
256     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
257     */
258    @Override
259    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
260
261        // set the dialog type
262        setParamDialogtype(DIALOG_TYPE);
263
264        super.initWorkplaceRequestValues(settings, request);
265
266        // refresh the list
267        Map<?, ?> objects = (Map<?, ?>)getSettings().getListObject();
268        if (objects != null) {
269            objects.remove(CmsExportpointsList.class.getName());
270            objects.remove(CmsModulesDependenciesList.class.getName());
271        }
272
273        // save the current state of the module (may be changed because of the widget values)
274        setDialogObject(m_module);
275    }
276
277    /**
278     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
279     */
280    @Override
281    protected void validateParamaters() throws Exception {
282
283        String moduleName = getParamModule();
284        // check module
285        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
286        if (module == null) {
287            throw new Exception();
288        }
289    }
290}