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