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;
029
030import org.opencms.json.JSONArray;
031import org.opencms.json.JSONException;
032import org.opencms.json.JSONObject;
033import org.opencms.main.CmsLog;
034import org.opencms.ui.components.CmsResourceTableProperty;
035
036import java.io.Serializable;
037import java.util.ArrayList;
038import java.util.Collections;
039import java.util.List;
040import java.util.Map;
041
042import org.apache.commons.logging.Log;
043
044import com.google.common.collect.Lists;
045
046/**
047 * Stores the file explorer settings.<p>
048 */
049public class CmsFileExplorerSettings implements Serializable, I_CmsAppSettings {
050
051    /** JSON key. */
052    private static final String COLLAPSED_COLUMNS_KEY = "collapsed_collumns";
053
054    /** Log instance for this class. */
055    private static final Log LOG = CmsLog.getLog(CmsFileExplorerSettings.class);
056
057    /** The serial version id. */
058    private static final long serialVersionUID = 1L;
059
060    /** JSON key. */
061    private static final String SORT_COLUMN_KEY = "sort_column";
062
063    /** JSON key. */
064    private static final String SORT_ORDER_KEY = "sort_order";
065
066    /** The collapsed column ids. */
067    private List<CmsResourceTableProperty> m_collapsedColumns;
068
069    /** The sort order. */
070    private boolean m_sortAscending;
071
072    /** The sort column id. */
073    private CmsResourceTableProperty m_sortColumnId;
074
075    /**
076     * Constructor.<p>
077     * Will initialize the default settings.<p>
078     */
079    public CmsFileExplorerSettings() {
080
081        // initialize with the default settings
082        m_sortColumnId = CmsResourceTableProperty.PROPERTY_RESOURCE_NAME;
083        m_sortAscending = true;
084        m_collapsedColumns = new ArrayList<CmsResourceTableProperty>();
085        Collections.addAll(
086            m_collapsedColumns,
087            CmsResourceTableProperty.PROPERTY_NAVIGATION_TEXT,
088            CmsResourceTableProperty.PROPERTY_PERMISSIONS,
089            CmsResourceTableProperty.PROPERTY_USER_MODIFIED,
090            CmsResourceTableProperty.PROPERTY_DATE_CREATED,
091            CmsResourceTableProperty.PROPERTY_USER_CREATED,
092            CmsResourceTableProperty.PROPERTY_INTERNAL_RESOURCE_TYPE,
093            CmsResourceTableProperty.PROPERTY_STATE_NAME,
094            CmsResourceTableProperty.PROPERTY_USER_LOCKED);
095    }
096
097    /**
098     * Returns the collapsed column ids.<p>
099     *
100     * @return the collapsed column ids
101     */
102    public List<CmsResourceTableProperty> getCollapsedColumns() {
103
104        return m_collapsedColumns;
105    }
106
107    /**
108     * @see org.opencms.ui.apps.I_CmsAppSettings#getSettingsString()
109     */
110    public String getSettingsString() {
111
112        JSONObject json = new JSONObject();
113        try {
114            json.put(SORT_ORDER_KEY, m_sortAscending);
115            json.put(SORT_COLUMN_KEY, m_sortColumnId.getId());
116            List<String> collapsed = Lists.newArrayList();
117            for (CmsResourceTableProperty column : m_collapsedColumns) {
118                collapsed.add(column.getId());
119            }
120            json.put(COLLAPSED_COLUMNS_KEY, new JSONArray(collapsed));
121        } catch (JSONException e) {
122            LOG.error(e.getLocalizedMessage(), e);
123        }
124
125        return json.toString();
126    }
127
128    /**
129     * Gets the sort column id.<p>
130     *
131     * @return the sort column id
132     */
133    public CmsResourceTableProperty getSortColumnId() {
134
135        return m_sortColumnId;
136    }
137
138    /**
139     * Returns the sort order.<p>
140     *
141     * @return the sort order
142     */
143    public boolean isSortAscending() {
144
145        return m_sortAscending;
146    }
147
148    /**
149     * @see org.opencms.ui.apps.I_CmsAppSettings#restoreSettings(java.lang.String)
150     */
151    public void restoreSettings(String storedSettings) {
152
153        Map<String, CmsResourceTableProperty> columnMap = CmsResourceTableProperty.getDefaultColumnsByName();
154
155        try {
156            JSONObject json = new JSONObject(storedSettings);
157            if (json.has(SORT_ORDER_KEY)) {
158                m_sortAscending = json.getBoolean(SORT_ORDER_KEY);
159            }
160            if (json.has(SORT_COLUMN_KEY)) {
161                m_sortColumnId = columnMap.get(json.getString(SORT_COLUMN_KEY));
162            }
163            if (json.has(COLLAPSED_COLUMNS_KEY)) {
164                List<CmsResourceTableProperty> collapsed = new ArrayList<CmsResourceTableProperty>();
165                JSONArray array = json.getJSONArray(COLLAPSED_COLUMNS_KEY);
166
167                for (int i = 0; i < array.length(); i++) {
168                    collapsed.add(columnMap.get(array.getString(i)));
169                }
170                m_collapsedColumns = collapsed;
171            }
172
173        } catch (JSONException e) {
174            LOG.error("Failed to restore file explorer settings from '" + storedSettings + "'", e);
175        }
176    }
177
178    /**
179     * Sets the collapsed columns.<p>
180     *
181     * @param collapsedColumns the collapsed columns
182     */
183    public void setCollapsedColumns(List<CmsResourceTableProperty> collapsedColumns) {
184
185        m_collapsedColumns = collapsedColumns;
186    }
187
188    /**
189     * Sets the sort order.<p>
190     *
191     * @param sortAscending the sort order
192     */
193    public void setSortAscending(boolean sortAscending) {
194
195        m_sortAscending = sortAscending;
196    }
197
198    /**
199     * Sets the sort column.<p>
200     *
201     * @param sortColumnId the sort column
202     */
203    public void setSortColumnId(CmsResourceTableProperty sortColumnId) {
204
205        m_sortColumnId = sortColumnId;
206    }
207}