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
079        super(contextProvider, tableColumns);
080        m_fileTable.setConverter(CmsListManager.INSTANCEDATE_PROPERTY, new StringToDateConverter() {
081
082            private static final long serialVersionUID = 1L;
083
084            @Override
085            protected DateFormat getFormat(Locale locale) {
086
087                return new SimpleDateFormat(CmsVaadinUtils.getMessageText(Messages.GUI_LISTMANAGER_DATE_FORMAT_0));
088            }
089        });
090    }
091
092    /**
093     * Returns the first visible item id.<p>
094     *
095     * @return the first visible item id
096     */
097    public String getCurrentPageFirstItemId() {
098
099        return (String)m_fileTable.getCurrentPageFirstItemId();
100    }
101
102    /**
103     * Returns the date field key.<p>
104     *
105     * @return the date field key
106     */
107    public String getDateFieldKey() {
108
109        return m_dateFieldKey;
110    }
111
112    /**
113     * Returns the currently selected items.<p>
114     *
115     * @return the selected items
116     */
117    public List<Item> getSelectedItems() {
118
119        Collection<?> ids = (Collection<?>)m_fileTable.getValue();
120        List<Item> items = new ArrayList<>();
121        for (Object id : ids) {
122            if (m_container.containsId(id)) {
123                items.add(m_container.getItem(id));
124            }
125        }
126        return items;
127    }
128
129    /**
130     * @see org.opencms.ui.components.CmsResourceTable#getUUIDFromItemID(java.lang.String)
131     */
132    @Override
133    public CmsUUID getUUIDFromItemID(String itemId) {
134
135        if (itemId.contains(ID_SEPARATOR)) {
136            return super.getUUIDFromItemID(itemId.substring(0, itemId.indexOf(ID_SEPARATOR)));
137        } else {
138            return super.getUUIDFromItemID(itemId);
139        }
140    }
141
142    /**
143     * Sets the content locale.<p>
144     *
145     * @param locale the content locale
146     */
147    public void setContentLocale(Locale locale) {
148
149        m_contentLocale = locale;
150        m_dateFieldKey = CmsSearchField.FIELD_INSTANCEDATE + "_" + m_contentLocale.toString() + "_dt";
151    }
152
153    /**
154     * Sets the first visible item id.<p>
155     *
156     * @param itemId the item id
157     */
158    public void setCurrentPageFirstItemId(String itemId) {
159
160        m_fileTable.setCurrentPageFirstItemId(itemId);
161    }
162
163    /**
164     * Set the item description generator which generates tooltips for cells and rows in the Table.<p>
165     *
166     * @param generator the generator to use
167     */
168    public void setsetItemDescriptionGenerator(ItemDescriptionGenerator generator) {
169
170        m_fileTable.setItemDescriptionGenerator(generator);
171    }
172
173    /**
174     * @see org.opencms.ui.components.CmsFileTable#update(java.util.Collection, boolean)
175     */
176    @Override
177    public void update(Collection<CmsUUID> ids, boolean remove) {
178
179        // not supported, requires a complete refresh of the search result
180    }
181
182    /**
183     * @see org.opencms.ui.components.CmsResourceTable#fillItem(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.util.Locale)
184     */
185    @Override
186    protected void fillItem(CmsObject cms, CmsResource resource, Locale locale) {
187
188        if (resource instanceof CmsSearchResource) {
189            String instanceDate = ((CmsSearchResource)resource).getField(m_dateFieldKey);
190            String itemId = resource.getStructureId().toString();
191            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(instanceDate)) {
192                itemId += ID_SEPARATOR + instanceDate;
193            }
194            Item resourceItem = m_container.getItem(itemId);
195            if (resourceItem == null) {
196                resourceItem = m_container.addItem(itemId);
197            }
198            fillItemDefault(resourceItem, cms, resource, locale);
199            for (I_ResourcePropertyProvider provider : m_propertyProviders) {
200                provider.addItemProperties(resourceItem, cms, resource, locale);
201            }
202        } else {
203            super.fillItem(cms, resource, locale);
204        }
205    }
206
207}