001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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, 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.ade.sitemap.client.alias;
029
030import org.opencms.ade.sitemap.client.CmsSitemapView;
031import org.opencms.ade.sitemap.client.alias.CmsImportResultList.I_Css;
032import org.opencms.ade.sitemap.shared.I_CmsAliasConstants;
033import org.opencms.ade.sitemap.shared.rpc.I_CmsSitemapServiceAsync;
034import org.opencms.gwt.client.CmsCoreProvider;
035import org.opencms.gwt.client.rpc.CmsRpcAction;
036import org.opencms.gwt.client.ui.CmsPopup;
037import org.opencms.gwt.client.ui.CmsPushButton;
038import org.opencms.gwt.client.ui.CmsScrollPanel;
039import org.opencms.gwt.client.ui.I_CmsButton;
040import org.opencms.gwt.client.ui.input.upload.CmsFileInfo;
041import org.opencms.gwt.client.ui.input.upload.CmsFileInput;
042import org.opencms.gwt.client.ui.input.upload.CmsUploadButton;
043import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton;
044import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler;
045import org.opencms.gwt.shared.alias.CmsAliasImportResult;
046import org.opencms.util.CmsStringUtil;
047
048import java.util.List;
049
050import com.google.gwt.core.client.GWT;
051import com.google.gwt.dom.client.Style.Unit;
052import com.google.gwt.event.dom.client.ClickEvent;
053import com.google.gwt.uibinder.client.UiBinder;
054import com.google.gwt.uibinder.client.UiField;
055import com.google.gwt.uibinder.client.UiHandler;
056import com.google.gwt.user.client.Timer;
057import com.google.gwt.user.client.ui.Composite;
058import com.google.gwt.user.client.ui.FlowPanel;
059import com.google.gwt.user.client.ui.FormPanel;
060import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
061import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
062import com.google.gwt.user.client.ui.HasText;
063import com.google.gwt.user.client.ui.Hidden;
064import com.google.gwt.user.client.ui.Label;
065import com.google.gwt.user.client.ui.Widget;
066
067/**
068 * This widget is used for importing aliases by uploading a CSV file.<p>
069 *
070 * It contains buttons for uploading a file, a form used to submit the file to the server, and an area
071 * used for displaying the result of the import operation on the server.<p>
072 */
073public class CmsImportView extends Composite {
074
075    /**
076     * The UiBinder interface for this widget.<p>
077     */
078    protected interface I_CmsImportViewUiBinder extends UiBinder<Widget, CmsImportView> {
079        // empty
080    }
081
082    /** The actual form. */
083    @UiField
084    protected FormPanel m_formPanel;
085
086    /** The panel containing the form elements. */
087    @UiField
088    protected FlowPanel m_formPanelContents;
089
090    /** The label containing the path or name of the CSV file to import. */
091    @UiField
092    protected Label m_pathLabel;
093
094    /** The panel containing the results of the server-side import operation. */
095    @UiField
096    protected CmsImportResultList m_results;
097
098    /** The scroll panel containing the import results. */
099    @UiField
100    protected CmsScrollPanel m_scrollPanel;
101
102    /** Text box for setting the separator. */
103    @UiField
104    protected HasText m_separator;
105
106    /** The button used to submit the file which should be imported to the server. */
107    @UiField
108    protected CmsPushButton m_submitButton;
109
110    /** The upload button. */
111    protected CmsUploadButton m_uploadButton;
112
113    /** The hidden form field for the separator parameter. */
114    private Hidden m_separatorField = new Hidden(I_CmsAliasConstants.PARAM_SEPARATOR);
115
116    /**
117     * Creates a new widget instance.<p>
118     */
119    public CmsImportView() {
120
121        I_CmsImportViewUiBinder binder = GWT.create(I_CmsImportViewUiBinder.class);
122        Widget content = binder.createAndBindUi(this);
123        initWidget(content);
124        I_CmsUploadButtonHandler handler = new I_CmsUploadButtonHandler() {
125
126            public void initializeFileInput(CmsFileInput fileInput) {
127
128                fileInput.setAllowMultipleFiles(false);
129                fileInput.getElement().getStyle().setFontSize(200, Unit.PX);
130                fileInput.getElement().getStyle().setProperty("minHeight", "200px");
131                fileInput.setName(I_CmsAliasConstants.PARAM_IMPORTFILE);
132                fileInput.addStyleName(
133                    org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.uploadButton().uploadFileInput());
134
135            }
136
137            public void onChange(CmsFileInput fileInput) {
138
139                CmsFileInfo[] files = fileInput.getFiles();
140                if (files.length == 1) {
141                    CmsFileInfo fileInfo = files[0];
142                    updatePath(fileInfo.getFileName());
143                }
144
145            }
146
147            public void setButton(I_CmsUploadButton button) {
148
149                // do nothing
150
151            }
152
153        };
154        m_uploadButton = new CmsUploadButton(handler);
155        m_formPanelContents.add(m_uploadButton);
156        m_submitButton.setText(CmsAliasMessages.messageButtonSubmit());
157        m_uploadButton.setText(CmsAliasMessages.messageButtonSelectFile());
158        m_uploadButton.setSize(I_CmsButton.Size.big);
159        m_submitButton.setSize(I_CmsButton.Size.big);
160
161        m_submitButton.setEnabled(false);
162        m_separator.setText(",");
163
164        initializeForm();
165    }
166
167    /**
168     * Shows a popup containing the import view.<p>
169     */
170    public static void showPopup() {
171
172        final CmsPopup popup = new CmsPopup(CmsAliasMessages.messageTitleImport());
173        popup.setModal(true);
174        popup.setGlassEnabled(true);
175        CmsImportView importView = new CmsImportView();
176        popup.setMainContent(importView);
177        popup.setWidth(800);
178        popup.addDialogClose(null);
179        popup.centerHorizontally(100);
180    }
181
182    /**
183     * The event handler for the submit button.<p>
184     *
185     * @param event the click event
186     */
187    @UiHandler("m_submitButton")
188    public void onClickSubmit(ClickEvent event) {
189
190        String separator = ",";
191        String selectedValue = m_separator.getText().trim();
192        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(selectedValue)) {
193            separator = selectedValue;
194        }
195        m_separatorField.setValue(separator);
196        m_formPanel.submit();
197    }
198
199    /**
200     * Adds an import result to the displayed list of import results.<p>
201     *
202     * @param result the result to add
203     */
204    protected void addImportResult(CmsAliasImportResult result) {
205
206        String cssClass;
207        I_Css css = CmsImportResultList.RESOURCES.css();
208        switch (result.getStatus()) {
209            case aliasChanged:
210                cssClass = css.aliasImportOverwrite();
211                break;
212            case aliasImportError:
213            case aliasParseError:
214                cssClass = css.aliasImportError();
215                break;
216            case aliasNew:
217            default:
218                cssClass = css.aliasImportOk();
219                break;
220        }
221        m_results.addRow(CmsAliasMessages.messageAliasImportLine(result), result.getMessage(), cssClass);
222    }
223
224    /**
225     * Clears the panel used to display the import results.<p>
226     */
227    protected void clearResults() {
228
229        m_results.clear();
230    }
231
232    /**
233     * Processes the result of the import operation from the server.<p>
234     *
235     * @param results the string containing the results of the import sent by the server
236     */
237    protected void handleImportResults(List<CmsAliasImportResult> results) {
238
239        clearResults();
240        for (CmsAliasImportResult singleResult : results) {
241            addImportResult(singleResult);
242        }
243    }
244
245    /**
246     * Initializes the form used for submitting the alias CSV file to the server.<p>
247     */
248    protected void initializeForm() {
249
250        String target = CmsSitemapView.getInstance().getController().getData().getAliasImportUrl();
251        m_formPanel.setAction(target);
252        m_formPanel.setMethod(FormPanel.METHOD_POST);
253        m_formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
254        Hidden siteRootField = new Hidden(I_CmsAliasConstants.PARAM_SITEROOT);
255        siteRootField.setValue(CmsCoreProvider.get().getSiteRoot());
256        m_formPanelContents.add(siteRootField);
257        m_formPanelContents.add(m_separatorField);
258        m_formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
259
260            public void onSubmitComplete(SubmitCompleteEvent event) {
261
262                final String formPostResults = event.getResults();
263                final I_CmsSitemapServiceAsync service = CmsSitemapView.getInstance().getController().getService();
264                CmsRpcAction<List<CmsAliasImportResult>> action = new CmsRpcAction<List<CmsAliasImportResult>>() {
265
266                    @Override
267                    public void execute() {
268
269                        start(200, false);
270                        service.getAliasImportResult(formPostResults.trim(), this);
271                    }
272
273                    @Override
274                    public void onResponse(List<CmsAliasImportResult> result) {
275
276                        stop(false);
277                        handleImportResults(result);
278                        Timer resizeTimer = new Timer() {
279
280                            @Override
281                            public void run() {
282
283                                m_scrollPanel.onResizeDescendant();
284                            }
285                        };
286                        resizeTimer.schedule(100);
287                    }
288                };
289                action.execute();
290            }
291        });
292    }
293
294    /**
295     * Updates the path of the file to import.<p>
296     *
297     * @param path the new path
298     */
299    protected void updatePath(String path) {
300
301        m_pathLabel.setText(path);
302        m_submitButton.setEnabled(true);
303    }
304
305}