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.lists;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.search.CmsSearchResource;
033import org.opencms.search.fields.CmsSearchField;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.I_CmsContextProvider;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.CmsFileTable;
038import org.opencms.ui.components.CmsResourceTableProperty;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.util.CmsUUID;
041
042import java.text.DateFormat;
043import java.text.SimpleDateFormat;
044import java.util.ArrayList;
045import java.util.Collection;
046import java.util.List;
047import java.util.Locale;
048import java.util.Map;
049
050import com.vaadin.v7.data.Item;
051import com.vaadin.v7.data.util.converter.StringToDateConverter;
052import com.vaadin.v7.ui.AbstractSelect.ItemDescriptionGenerator;
053
054/**
055 * Table to display the list manager search results.<p>
056 */
057public class CmsResultTable extends CmsFileTable {
058
059    /** Separator string used in item ids. */
060    protected static final String ID_SEPARATOR = ":";
061
062    /** The serial version id. */
063    private static final long serialVersionUID = 5680421086123351830L;
064
065    /** The content locale. */
066    private Locale m_contentLocale;
067
068    /** The date field key. */
069    private String m_dateFieldKey;
070
071    /**
072     * Constructor.<p>
073     *
074     * @param contextProvider the context provider
075     * @param tableColumns the table columns
076     */
077    public CmsResultTable(I_CmsContextProvider contextProvider, Map<CmsResourceTableProperty, Integer> tableColumns) {
078        super(contextProvider, tableColumns);
079        m_fileTable.setConverter(CmsListManager.INSTANCEDATE_PROPERTY, new StringToDateConverter() {
080
081            private static final long serialVersionUID = 1L;
082
083            @Override
084            protected DateFormat getFormat(Locale locale) {
085
086                return new SimpleDateFormat(CmsVaadinUtils.getMessageText(Messages.GUI_LISTMANAGER_DATE_FORMAT_0));
087            }
088        });
089    }
090
091    /**
092     * Returns the first visible item id.<p>
093     *
094     * @return the first visible item id
095     */
096    public String getCurrentPageFirstItemId() {
097
098        return (String)m_fileTable.getCurrentPageFirstItemId();
099    }
100
101    /**
102     * Returns the currently selected items.<p>
103     *
104     * @return the selected items
105     */
106    public List<Item> getSelectedItems() {
107
108        Collection<?> ids = (Collection<?>)m_fileTable.getValue();
109        List<Item> items = new ArrayList<>();
110        for (Object id : ids) {
111            if (m_container.containsId(id)) {
112                items.add(m_container.getItem(id));
113            }
114        }
115        return items;
116    }
117
118    /**
119     * @see org.opencms.ui.components.CmsResourceTable#getUUIDFromItemID(java.lang.String)
120     */
121    @Override
122    public CmsUUID getUUIDFromItemID(String itemId) {
123
124        if (itemId.contains(ID_SEPARATOR)) {
125            return super.getUUIDFromItemID(itemId.substring(0, itemId.indexOf(ID_SEPARATOR)));
126        } else {
127            return super.getUUIDFromItemID(itemId);
128        }
129    }
130
131    /**
132     * Sets the content locale.<p>
133     *
134     * @param locale the content locale
135     */
136    public void setContentLocale(Locale locale) {
137
138        m_contentLocale = locale;
139        m_dateFieldKey = CmsSearchField.FIELD_INSTANCEDATE + "_" + m_contentLocale.toString() + "_dt";
140    }
141
142    /**
143     * Sets the first visible item id.<p>
144     *
145     * @param itemId the item id
146     */
147    public void setCurrentPageFirstItemId(String itemId) {
148
149        m_fileTable.setCurrentPageFirstItemId(itemId);
150    }
151
152    /**
153     * Set the item description generator which generates tooltips for cells and rows in the Table.<p>
154     *
155     * @param generator the generator to use
156     */
157    public void setsetItemDescriptionGenerator(ItemDescriptionGenerator generator) {
158
159        m_fileTable.setItemDescriptionGenerator(generator);
160    }
161
162    /**
163     * @see org.opencms.ui.components.CmsFileTable#update(java.util.Collection, boolean)
164     */
165    @Override
166    public void update(Collection<CmsUUID> ids, boolean remove) {
167
168        // not supported, requires a complete refresh of the search result
169    }
170
171    /**
172     * @see org.opencms.ui.components.CmsResourceTable#fillItem(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.util.Locale)
173     */
174    @Override
175    protected void fillItem(CmsObject cms, CmsResource resource, Locale locale) {
176
177        if (resource instanceof CmsSearchResource) {
178            String instanceDate = ((CmsSearchResource)resource).getField(m_dateFieldKey);
179            String itemId = resource.getStructureId().toString();
180            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(instanceDate)) {
181                itemId += ID_SEPARATOR + instanceDate;
182            }
183            Item resourceItem = m_container.getItem(itemId);
184            if (resourceItem == null) {
185                resourceItem = m_container.addItem(itemId);
186            }
187            fillItemDefault(resourceItem, cms, resource, locale);
188            for (I_ResourcePropertyProvider provider : m_propertyProviders) {
189                provider.addItemProperties(resourceItem, cms, resource, locale);
190            }
191        } else {
192            super.fillItem(cms, resource, locale);
193        }
194    }
195
196    /**
197     * Returns the date field key.<p>
198     *
199     * @return the date field key
200     */
201    protected String getDateFieldKey() {
202
203        return m_dateFieldKey;
204    }
205
206}