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.file.CmsObject;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.file.types.I_CmsResourceType;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.CmsVaadinUtils.PropertyId;
039
040import org.apache.commons.logging.Log;
041
042import com.vaadin.v7.data.util.IndexedContainer;
043import com.vaadin.ui.Button;
044import com.vaadin.v7.ui.ComboBox;
045import com.vaadin.ui.Panel;
046import com.vaadin.v7.ui.VerticalLayout;
047
048/**
049 * Class for the database statistic view.<p>
050 */
051public class CmsResourceTypeStatsView extends VerticalLayout {
052
053    /** The logger for this class. */
054    static Log LOG = CmsLog.getLog(CmsResourceTypeStatsView.class.getName());
055
056    /**Result list.*/
057    static CmsResourceTypeStatResultList results;
058
059    /**vaadin serial id.*/
060    private static final long serialVersionUID = -5884776533727327584L;
061
062    /**CmsObject.*/
063    CmsObject m_cms;
064
065    /**Vaadin component.*/
066    Button m_ok;
067
068    /**Vaadin component.*/
069    VerticalLayout m_resLayout;
070
071    /**Vaadin component.*/
072    ComboBox m_resType;
073
074    /**Vaadin component.*/
075    Panel m_results;
076
077    /**Vaadin component.*/
078    ComboBox m_siteSelect;
079
080    /**
081     * public constructor.<p>
082     */
083    public CmsResourceTypeStatsView() {
084        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
085        results = CmsResourceTypeStatResultList.init(results);
086        results.setVerticalLayout(m_resLayout, true);
087        if (results.isEmpty()) {
088            m_results.setVisible(false);
089        }
090        try {
091            m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
092
093            setClickListener();
094
095            setupResourceType();
096
097            final IndexedContainer availableSites = CmsVaadinUtils.getAvailableSitesContainer(m_cms, "caption");
098
099            m_siteSelect.setContainerDataSource(availableSites);
100            if (availableSites.getItem(m_cms.getRequestContext().getSiteRoot()) != null) {
101                m_siteSelect.setValue(m_cms.getRequestContext().getSiteRoot());
102            }
103            m_siteSelect.setNullSelectionAllowed(false);
104            m_siteSelect.setItemCaptionPropertyId("caption");
105        } catch (CmsException e1) {
106            LOG.error("Unable to get CmsObject", e1);
107        }
108    }
109
110    /**
111     * Sets the cms object to selected site and returns it.<p>
112     *
113     * @return CmsObject
114     */
115    protected CmsObject getCmsObject() {
116
117        m_cms.getRequestContext().setSiteRoot((String)m_siteSelect.getValue());
118        return m_cms;
119    }
120
121    /**
122     * Returns the type which is selected by UI.<p>
123     *
124     * @return I_CmsResourceType
125     */
126    protected I_CmsResourceType getType() {
127
128        return (I_CmsResourceType)m_resType.getValue();
129    }
130
131    /**
132     * Sets the click listener for the ok button.<p>
133     */
134    private void setClickListener() {
135
136        m_ok.addClickListener(new Button.ClickListener() {
137
138            private static final long serialVersionUID = 6621475980210242125L;
139
140            public void buttonClick(com.vaadin.ui.Button.ClickEvent event) {
141
142                try {
143                    CmsResourceFilter filter = getType() == null
144                    ? CmsResourceFilter.ALL
145                    : CmsResourceFilter.requireType(getType());
146                    results.addResult(
147                        new CmsResourceTypeStatResult(
148                            getType(),
149                            (String)m_siteSelect.getValue(),
150                            getCmsObject().readResources("/", filter, true).size()));
151                    m_results.setVisible(true);
152                    results.setVerticalLayout(m_resLayout, false);
153                } catch (CmsException e) {
154                    LOG.error("Unable to read resource tree", e);
155                }
156            }
157        });
158    }
159
160    /**
161     * Sets up the combo box for choosing the resource type.<p>
162     */
163    private void setupResourceType() {
164
165        IndexedContainer resTypes = CmsVaadinUtils.getResourceTypesContainer();
166        resTypes.addContainerFilter(CmsVaadinUtils.FILTER_NO_FOLDERS);
167        m_resType.setContainerDataSource(resTypes);
168        m_resType.setItemCaptionPropertyId(PropertyId.caption);
169        m_resType.setItemIconPropertyId(PropertyId.icon);
170    }
171}