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.sqlconsole;
029
030import org.opencms.ui.CmsVaadinUtils;
031import org.opencms.ui.components.CmsBasicDialog;
032
033import java.io.ByteArrayInputStream;
034import java.io.InputStream;
035import java.io.UnsupportedEncodingException;
036import java.util.List;
037
038import com.vaadin.server.FileDownloader;
039import com.vaadin.server.StreamResource;
040import com.vaadin.server.StreamResource.StreamSource;
041import com.vaadin.shared.ui.ContentMode;
042import com.vaadin.ui.Button;
043import com.vaadin.ui.Label;
044import com.vaadin.ui.VerticalLayout;
045import com.vaadin.v7.data.Item;
046import com.vaadin.v7.data.util.IndexedContainer;
047import com.vaadin.v7.ui.Table;
048
049/**
050 * Displays results from an SQL query.<p>
051 */
052public class CmsSqlConsoleResultsForm extends CmsBasicDialog {
053
054    /**
055     * CSV generator for the download button.
056     */
057    public class CsvSource implements StreamSource {
058
059        /** Serial version id. */
060        private static final long serialVersionUID = 1L;
061
062        /** The results. */
063        private CmsSqlConsoleResults m_results;
064
065        /**
066         * Creates a new instance.
067         *
068         * @param results the results
069         */
070        public CsvSource(CmsSqlConsoleResults results) {
071
072            m_results = results;
073
074        }
075
076        /**
077         * @see com.vaadin.server.StreamResource.StreamSource#getStream()
078         */
079        public InputStream getStream() {
080
081            try {
082                return new ByteArrayInputStream(m_results.getCsv().getBytes("UTF-8"));
083            } catch (UnsupportedEncodingException e) {
084                return null;
085            }
086        }
087
088    }
089
090    /** Serial version id. */
091    private static final long serialVersionUID = 1L;
092
093    /** The CSV download button. */
094    protected Button m_csv;
095
096    /** The OK button. */
097    protected Button m_ok;
098
099    /** The table container. */
100    protected VerticalLayout m_tableContainer;
101
102    /** The label for displaying the report output. */
103    private Label m_reportOutput;
104
105    /**
106     * Creates a new instance.<p>
107     *
108     * @param results the database results
109     * @param reportOutput the report output
110     */
111    public CmsSqlConsoleResultsForm(CmsSqlConsoleResults results, String reportOutput) {
112
113        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
114        m_ok.addClickListener(evt -> CmsVaadinUtils.getWindow(CmsSqlConsoleResultsForm.this).close());
115        if (results != null) {
116            Table table = buildTable(results);
117            m_tableContainer.addComponent(table);
118            StreamResource res = new StreamResource(new CsvSource(results), "data.csv");
119            res.setMIMEType("text/plain; charset=utf-8");
120            FileDownloader downloader = new FileDownloader(res);
121            downloader.extend(m_csv);
122        } else {
123            m_csv.setVisible(false);
124        }
125        m_reportOutput.setContentMode(ContentMode.PREFORMATTED);
126        m_reportOutput.setValue(reportOutput);
127
128    }
129
130    /**
131     * Builds the table for the database results.
132     *
133     * @param results the database results
134     * @return the table
135     */
136    private Table buildTable(CmsSqlConsoleResults results) {
137
138        IndexedContainer container = new IndexedContainer();
139        int numCols = results.getColumns().size();
140        for (int c = 0; c < numCols; c++) {
141            container.addContainerProperty(Integer.valueOf(c), results.getColumnType(c), null);
142        }
143        int r = 0;
144        for (List<Object> row : results.getData()) {
145            Item item = container.addItem(Integer.valueOf(r));
146            for (int c = 0; c < numCols; c++) {
147                item.getItemProperty(Integer.valueOf(c)).setValue(row.get(c));
148            }
149            r += 1;
150        }
151        Table table = new Table();
152        table.setContainerDataSource(container);
153        for (int c = 0; c < numCols; c++) {
154            String col = (results.getColumns().get(c));
155            table.setColumnHeader(Integer.valueOf(c), col);
156        }
157        table.setWidth("100%");
158        table.setHeight("100%");
159        table.setColumnCollapsingAllowed(true);
160        return table;
161    }
162
163}