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.db.CmsExportPoint;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.OpenCms;
033import org.opencms.module.CmsModule;
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 exportpoint overview.<p>
050 *
051 * @since 6.0.0
052 */
053public class CmsExportpointsOverview extends CmsWidgetDialog {
054
055    /** The dialog type. */
056    public static final String DIALOG_TYPE = "ExportpointsOverview";
057
058    /** Defines which pages are valid for this dialog. */
059    public static final String[] PAGES = {"page1"};
060
061    /** The module exportpoints object that is shown on this dialog. */
062    private CmsExportPoint m_exportpoint;
063
064    /** Exportpoint name. */
065    private String m_paramExportpoint;
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 CmsExportpointsOverview(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 CmsExportpointsOverview(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 exportpoint parameter.<p>
136     *
137     * @return the module exportpoint parameter
138     */
139    public String getParamExportpoint() {
140
141        return m_paramExportpoint;
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 exportpoint parameter.<p>
156     * @param paramExportpoint the module exportpoint parameter
157     */
158    public void setParamExportpoint(String paramExportpoint) {
159
160        m_paramExportpoint = paramExportpoint;
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.exportpointinformation")));
191            result.append(createWidgetTableStart());
192            result.append(createDialogRowsHtml(0, 2));
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_exportpoint, "uri", PAGES[0], new CmsDisplayWidget()));
212        addWidget(
213            new CmsWidgetDialogParameter(m_exportpoint, "configuredDestination", PAGES[0], new CmsDisplayWidget()));
214        addWidget(new CmsWidgetDialogParameter(m_exportpoint, "destinationPath", PAGES[0], new CmsDisplayWidget()));
215
216    }
217
218    /**
219     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
220     */
221    @Override
222    protected String[] getPageArray() {
223
224        return PAGES;
225    }
226
227    /**
228     * @see org.opencms.workplace.CmsWorkplace#initMessages()
229     */
230    @Override
231    protected void initMessages() {
232
233        // add specific dialog resource bundle
234        addMessages(Messages.get().getBundleName());
235        // add default resource bundles
236        super.initMessages();
237    }
238
239    /**
240     * Initializes the module  to work with depending on the dialog state and request parameters.<p>
241     */
242    protected void initModule() {
243
244        Object o;
245        CmsModule module;
246
247        if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
248            // this is the initial dialog call
249            if (CmsStringUtil.isNotEmpty(m_paramModule)) {
250                // edit an existing module, get it from manager
251                o = OpenCms.getModuleManager().getModule(m_paramModule);
252            } else {
253                // create a new module
254                o = null;
255            }
256        } else {
257            // this is not the initial call, get module from session
258            o = getDialogObject();
259        }
260
261        if (!(o instanceof CmsModule)) {
262            // create a new module
263            module = new CmsModule();
264
265        } else {
266            // reuse module stored in session
267            module = (CmsModule)((CmsModule)o).clone();
268        }
269
270        List exportpoints = module.getExportPoints();
271        m_exportpoint = new CmsExportPoint();
272        if ((exportpoints != null) && (exportpoints.size() > 0)) {
273            Iterator i = exportpoints.iterator();
274            while (i.hasNext()) {
275                CmsExportPoint exportpoint = (CmsExportPoint)i.next();
276                if (exportpoint.getUri().equals(m_paramExportpoint)) {
277                    m_exportpoint = exportpoint;
278                }
279            }
280        }
281
282    }
283
284    /**
285     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
286     */
287    @Override
288    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
289
290        // set the dialog type
291        setParamDialogtype(DIALOG_TYPE);
292
293        super.initWorkplaceRequestValues(settings, request);
294
295        // save the current state of the module (may be changed because of the widget values)
296        setDialogObject(m_exportpoint);
297
298    }
299
300    /**
301     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
302     */
303    @Override
304    protected void validateParamaters() throws Exception {
305
306        String moduleName = getParamModule();
307        // check module
308        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
309        if (module == null) {
310            throw new Exception();
311        }
312        // check export point
313        Iterator it = module.getExportPoints().iterator();
314        while (it.hasNext()) {
315            CmsExportPoint ep = (CmsExportPoint)it.next();
316            if (ep.getUri().equals(getParamExportpoint())) {
317                // export point found
318                return;
319            }
320        }
321        throw new Exception();
322    }
323}