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 GmbH & Co. KG, 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.workplace.list;
029
030import org.opencms.main.CmsIllegalArgumentException;
031
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.Map;
035
036/**
037 * Generic list item.<p>
038 *
039 * @since 6.0.0
040 */
041public class CmsListItem {
042
043    /** Unique id for later recovery. */
044    private final String m_id;
045
046    /** Associated list definition. */
047    private final CmsListMetadata m_metadata;
048
049    /** Item values. */
050    private final Map<String, Object> m_values = new HashMap<String, Object>();
051
052    /**
053     * Default Constructor.<p>
054     *
055     * @param id the id of the item has to be unique
056     * @param metadata the corresponding list definition
057     */
058    public CmsListItem(CmsListMetadata metadata, String id) {
059
060        if (id == null) {
061            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LIST_INVALID_NULL_ARG_1, "id"));
062        }
063        if (metadata == null) {
064            throw new CmsIllegalArgumentException(
065                Messages.get().container(Messages.ERR_LIST_INVALID_NULL_ARG_1, "metadata"));
066        }
067        m_metadata = metadata;
068        m_id = id;
069    }
070
071    /**
072     * Returns the value of the column for this item.<p>
073     *
074     * @param columnId the column id
075     *
076     * @return the content, may be <code>null</code>
077     *
078     * @throws CmsIllegalArgumentException if the given <code>columnId</code> is invalid
079     */
080    public Object get(String columnId) throws CmsIllegalArgumentException {
081
082        if ((getMetadata().getColumnDefinition(columnId) == null)
083            && (getMetadata().getItemDetailDefinition(columnId) == null)) {
084            throw new CmsIllegalArgumentException(
085                Messages.get().container(Messages.ERR_LIST_INVALID_COLUMN_1, columnId));
086        }
087        return m_values.get(columnId);
088    }
089
090    /**
091     * Returns the id of the item.<p>
092     *
093     * @return the id
094     *
095     * @see CmsHtmlList#getItem(String)
096     */
097    public String getId() {
098
099        return m_id;
100    }
101
102    /**
103     * Returns the meta data.<p>
104     *
105     * @return the meta data
106     */
107    public CmsListMetadata getMetadata() {
108
109        return m_metadata;
110    }
111
112    /**
113     * Returns the value map of the list item.<p>
114     *
115     * @return the value map of the list item
116     */
117    public Map<String, Object> getValues() {
118
119        return Collections.unmodifiableMap(m_values);
120    }
121
122    /**
123     * Sets the object to display at the given column.<p>
124     *
125     * @param columnId the column id
126     * @param value the value to display
127     *
128     * @return the previous value, or <code>null</code> if not set
129     * @throws CmsIllegalArgumentException if the given <code>columnId</code> is invalid
130     *
131     */
132    public Object set(String columnId, Object value) throws CmsIllegalArgumentException {
133
134        if ((getMetadata().getColumnDefinition(columnId) == null)
135            && (getMetadata().getItemDetailDefinition(columnId) == null)) {
136            throw new CmsIllegalArgumentException(
137                Messages.get().container(Messages.ERR_LIST_INVALID_COLUMN_1, columnId));
138        }
139        return m_values.put(columnId, value);
140    }
141
142    /**
143     * @see java.lang.Object#toString()
144     */
145    @Override
146    public String toString() {
147
148        return m_id + ":" + m_values.toString();
149    }
150}