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.projects;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.history.CmsHistoryProject;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.apps.Messages;
038import org.opencms.ui.apps.projects.CmsProjectsTable.ProjectResources;
039import org.opencms.util.CmsUUID;
040
041import java.util.Date;
042import java.util.List;
043import java.util.Locale;
044
045import org.apache.commons.logging.Log;
046
047import com.vaadin.shared.MouseEventDetails.MouseButton;
048import com.vaadin.ui.UI;
049import com.vaadin.v7.data.Item;
050import com.vaadin.v7.data.util.IndexedContainer;
051import com.vaadin.v7.event.ItemClickEvent;
052import com.vaadin.v7.ui.Table;
053
054/**
055 * The project history table.<p>
056 */
057public class CmsProjectHistoryTable extends Table {
058
059    /** Logger instance for this class. */
060    private static final Log LOG = CmsLog.getLog(CmsProjectHistoryTable.class);
061
062    /** The serial version id. */
063    private static final long serialVersionUID = 7343623156086839992L;
064
065    /** Publish date property. */
066    public static final String PROP_PUBLISH_DATE = "publishDate";
067
068    /** Publish user property. */
069    public static final String PROP_PUBLISH_USER = "publishUser";
070
071    /** The data container. */
072    IndexedContainer m_container;
073
074    /** The project manager instance. */
075    CmsProjectManager m_manager;
076
077    /**
078     * Constructor.<p>
079     */
080    public CmsProjectHistoryTable() {
081
082        setSizeFull();
083        m_container = new IndexedContainer();
084        m_container.addContainerProperty(CmsProjectsTable.PROP_ID, CmsUUID.class, null);
085        m_container.addContainerProperty(CmsProjectsTable.PROP_NAME, String.class, "");
086        m_container.addContainerProperty(CmsProjectsTable.PROP_DESCRIPTION, String.class, "");
087        m_container.addContainerProperty(PROP_PUBLISH_DATE, Date.class, "");
088        m_container.addContainerProperty(PROP_PUBLISH_USER, String.class, "");
089        m_container.addContainerProperty(CmsProjectsTable.PROP_ORG_UNIT, String.class, "");
090        m_container.addContainerProperty(CmsProjectsTable.PROP_OWNER, String.class, "");
091        m_container.addContainerProperty(CmsProjectsTable.PROP_MANAGER, String.class, "");
092        m_container.addContainerProperty(CmsProjectsTable.PROP_USER, String.class, "");
093        m_container.addContainerProperty(CmsProjectsTable.PROP_DATE_CREATED, Date.class, "");
094        m_container.addContainerProperty(CmsProjectsTable.PROP_RESOURCES, CmsProjectsTable.ProjectResources.class, "");
095
096        setContainerDataSource(m_container);
097        setColumnHeader(CmsProjectsTable.PROP_NAME, CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_NAME_0));
098        setColumnHeader(
099            CmsProjectsTable.PROP_DESCRIPTION,
100            CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_DESCRIPTION_0));
101        setColumnHeader(PROP_PUBLISH_DATE, CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_PUBLISH_DATE_0));
102        setColumnHeader(PROP_PUBLISH_USER, CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_PUBLISHED_BY_0));
103        setColumnHeader(
104            CmsProjectsTable.PROP_ORG_UNIT,
105            CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_ORG_UNIT_0));
106        setColumnHeader(CmsProjectsTable.PROP_OWNER, CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_OWNER_0));
107        setColumnHeader(
108            CmsProjectsTable.PROP_MANAGER,
109            CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_MANAGER_GROUP_0));
110        setColumnHeader(CmsProjectsTable.PROP_USER, CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_USER_GROUP_0));
111        setColumnHeader(
112            CmsProjectsTable.PROP_DATE_CREATED,
113            CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_DATE_CREATED_0));
114        setColumnHeader(
115            CmsProjectsTable.PROP_RESOURCES,
116            CmsVaadinUtils.getMessageText(Messages.GUI_PROJECTS_RESOURCES_0));
117
118        setSelectable(true);
119        setMultiSelect(true);
120        addItemClickListener(event -> handleItemClick(event));
121        loadProjects();
122    }
123
124    /**
125     * Loads the projects table.<p>
126     */
127    public void loadProjects() {
128
129        CmsObject cms = A_CmsUI.getCmsObject();
130        Locale locale = UI.getCurrent().getLocale();
131        m_container.removeAllItems();
132        boolean isMultiOU = false;
133        // hide ou column if only one ou exists
134        try {
135            isMultiOU = !OpenCms.getOrgUnitManager().getOrganizationalUnits(cms, "", true).isEmpty();
136        } catch (CmsException e) {
137            // noop
138        }
139        if (isMultiOU) {
140            setVisibleColumns(
141                CmsProjectsTable.PROP_NAME,
142                CmsProjectsTable.PROP_DESCRIPTION,
143                PROP_PUBLISH_DATE,
144                PROP_PUBLISH_USER,
145                CmsProjectsTable.PROP_ORG_UNIT,
146                CmsProjectsTable.PROP_OWNER,
147                CmsProjectsTable.PROP_MANAGER,
148                CmsProjectsTable.PROP_USER,
149                CmsProjectsTable.PROP_DATE_CREATED,
150                CmsProjectsTable.PROP_RESOURCES);
151        } else {
152            setVisibleColumns(
153                CmsProjectsTable.PROP_NAME,
154                CmsProjectsTable.PROP_DESCRIPTION,
155                PROP_PUBLISH_DATE,
156                PROP_PUBLISH_USER,
157                CmsProjectsTable.PROP_OWNER,
158                CmsProjectsTable.PROP_MANAGER,
159                CmsProjectsTable.PROP_USER,
160                CmsProjectsTable.PROP_DATE_CREATED,
161                CmsProjectsTable.PROP_RESOURCES);
162        }
163
164        // get content
165        try {
166            List<CmsHistoryProject> projects = cms.getAllHistoricalProjects();
167            for (CmsHistoryProject project : projects) {
168                Item item = m_container.addItem(Integer.valueOf(project.getPublishTag()));
169                if (item != null) {
170                    item.getItemProperty(CmsProjectsTable.PROP_ID).setValue(project.getUuid());
171                    item.getItemProperty(CmsProjectsTable.PROP_NAME).setValue(project.getSimpleName());
172                    item.getItemProperty(CmsProjectsTable.PROP_DESCRIPTION).setValue(project.getDescription());
173                    item.getItemProperty(PROP_PUBLISH_DATE).setValue(new Date(project.getPublishingDate()));
174                    item.getItemProperty(PROP_PUBLISH_USER).setValue(project.getPublishedByName(cms));
175                    try {
176                        item.getItemProperty(CmsProjectsTable.PROP_ORG_UNIT).setValue(
177                            OpenCms.getOrgUnitManager().readOrganizationalUnit(cms, project.getOuFqn()).getDisplayName(
178                                locale));
179                        item.getItemProperty(CmsProjectsTable.PROP_OWNER).setValue(
180                            cms.readUser(project.getOwnerId()).getName());
181                        item.getItemProperty(CmsProjectsTable.PROP_MANAGER).setValue(
182                            cms.readGroup(project.getManagerGroupId()).getSimpleName());
183                        item.getItemProperty(CmsProjectsTable.PROP_USER).setValue(
184                            cms.readGroup(project.getGroupId()).getSimpleName());
185                    } catch (CmsException e) {
186                        LOG.warn(e.getLocalizedMessage(), e);
187                    }
188                    item.getItemProperty(CmsProjectsTable.PROP_DATE_CREATED).setValue(
189                        new Date(project.getDateCreated()));
190
191                    StringBuffer html = new StringBuffer(512);
192                    ProjectResources resourceList = new ProjectResources(cms.readProjectResources(project));
193                    item.getItemProperty(CmsProjectsTable.PROP_RESOURCES).setValue(resourceList);
194                }
195            }
196            m_container.sort(new Object[] {PROP_PUBLISH_DATE}, new boolean[] {false});
197        } catch (CmsException e) {
198            LOG.error(e.getLocalizedMessage(), e);
199        }
200    }
201
202    /**
203     * Handles item clicks.
204     *
205     * @param event the click event
206     */
207    private void handleItemClick(ItemClickEvent event) {
208
209        if (event.getButton().equals(MouseButton.LEFT)
210            && CmsProjectsTable.PROP_RESOURCES.equals(event.getPropertyId())) {
211            CmsProjectsTable.showProjectResources(event.getItem());
212
213        }
214    }
215}