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, 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.ui.apps.dbmanager;
029
030import org.opencms.main.OpenCms;
031import org.opencms.util.CmsStringUtil;
032
033import java.io.File;
034import java.io.FileNotFoundException;
035import java.io.FileOutputStream;
036import java.io.OutputStream;
037
038import com.vaadin.v7.ui.Label;
039import com.vaadin.v7.ui.Upload;
040import com.vaadin.v7.ui.Upload.ChangeEvent;
041import com.vaadin.v7.ui.Upload.ChangeListener;
042import com.vaadin.v7.ui.Upload.StartedEvent;
043import com.vaadin.v7.ui.Upload.StartedListener;
044import com.vaadin.v7.ui.Upload.SucceededEvent;
045import com.vaadin.v7.ui.Upload.SucceededListener;
046
047/**
048 *Abstract class for HTTP imports.<p>
049 */
050public abstract class A_CmsHTTPImportForm extends A_CmsImportForm {
051
052    /**vaadin serial id.*/
053    private static final long serialVersionUID = 8268966029442189695L;
054
055    /**
056     * public constructor.<p>
057     *
058     * @param app calling app instance
059     * @param pathToServer path to server to save uploaded file
060     * @param validate indicates if import should be validated (only possible for modules)
061     */
062    @SuppressWarnings("deprecation")
063    public A_CmsHTTPImportForm(I_CmsReportApp app, final String pathToServer, final boolean validate) {
064
065        super(app);
066
067        getUpload().setImmediate(true);
068        getUpload().addStartedListener(new StartedListener() {
069
070            private static final long serialVersionUID = -1167851886739855757L;
071
072            public void uploadStarted(StartedEvent event) {
073
074                getOkButton().setEnabled(false);
075                getSiteSelector().setEnabled(true);
076
077                String name = event.getFilename();
078                name = processFileName(name);
079                getUploadLabel().setValue(name);
080            }
081        });
082
083        getUpload().addChangeListener(new ChangeListener() {
084
085            private static final long serialVersionUID = -8531203923548531981L;
086
087            public void filenameChanged(ChangeEvent event) {
088
089                getOkButton().setEnabled(false);
090                getSiteSelector().setEnabled(true);
091
092                String name = processFileName(event.getFilename());
093                getUploadLabel().setValue(name);
094            }
095        });
096
097        getUpload().setReceiver(new Upload.Receiver() {
098
099            private static final long serialVersionUID = 5860617055589937645L;
100
101            public OutputStream receiveUpload(String filename, String mimeType) {
102
103                String path = CmsStringUtil.joinPaths(
104                    OpenCms.getSystemInfo().getWebInfRfsPath(),
105                    pathToServer,
106                    processFileName(filename));
107                // make sure parent folders exist
108                File rfsFile = new File(path);
109                rfsFile.getParentFile().mkdirs();
110                m_importFile = new CmsImportFile(path);
111                try {
112                    return new FileOutputStream(m_importFile.getPath());
113                } catch (FileNotFoundException e) {
114                    throw new RuntimeException(e); // shouldn't happen, but if it does, there is no point in continuing
115                }
116            }
117        });
118        getUpload().addSucceededListener(new SucceededListener() {
119
120            private static final long serialVersionUID = 3430913281578577509L;
121
122            public void uploadSucceeded(SucceededEvent event) {
123
124                if (validate) {
125                    validateModuleFile();
126                } else {
127                    getOkButton().setEnabled(true);
128                }
129            }
130        });
131    }
132
133    /**
134     *Gets the upload button.<p>
135     *
136     * @return a vaadin upload button
137     */
138    @SuppressWarnings("deprecation")
139    protected abstract Upload getUpload();
140
141    /**
142     * Gets the upload label, which shows the name of the uploaded file.<p>
143     *
144     * @return a vaadin label
145     */
146    @SuppressWarnings("deprecation")
147    protected abstract Label getUploadLabel();
148}