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.components;
029
030import org.opencms.file.types.I_CmsResourceType;
031import org.opencms.i18n.CmsLocaleManager;
032import org.opencms.loader.CmsLoaderException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.apps.A_CmsWorkplaceApp;
036
037import java.util.Date;
038import java.util.Locale;
039
040import org.apache.commons.logging.Log;
041
042/**
043 * Component state bean.
044 */
045public class CmsComponentState {
046
047    /** The log object for this class. */
048    static final Log LOG = CmsLog.getLog(CmsComponentState.class);
049
050    /** Hash parameter. */
051    private static final String SITE = "s";
052
053    /** Hash parameter. */
054    private static final String FOLDER = "f";
055
056    /** Hash parameter. */
057    private static final String RESOURCE_TYPE = "t";
058
059    /** Hash parameter. */
060    private static final String DATE_FROM = "d1";
061
062    /** Hash parameter. */
063    private static final String DATE_TO = "d2";
064
065    /** Hash parameter. */
066    private static final String AVAILABILITY = "a";
067
068    /** Hash parameter. */
069    private static final String LOCALE = "l";
070
071    /** Hash parameter. */
072    private static final String QUERY = "q";
073
074    /** Hash parameter. */
075    private static final String CATEGORY = "c";
076
077    /** The site. */
078    String m_site;
079
080    /** The folder. */
081    String m_folder;
082
083    /** The resource type. */
084    I_CmsResourceType m_resourceType;
085
086    /** The date from. */
087    Date m_dateFrom;
088
089    /** The date to. */
090    Date m_dateTo;
091
092    /** The availability. */
093    String m_availability;
094
095    /** The locale. */
096    Locale m_locale;
097
098    /** The text search query. */
099    String m_query;
100
101    /** The category. */
102    String m_category;
103
104    /**
105     * Creates a new state bean.
106     */
107    public CmsComponentState() {
108
109    }
110
111    /**
112     * Creates a new state bean for a given state string.
113     * @param state the state string
114     */
115    public CmsComponentState(String state) {
116
117        parseStateString(state);
118    }
119
120    /**
121     * Generates a state string for this state bean.
122     * @return the state string
123     */
124    public String generateStateString() {
125
126        String state = "";
127        if (m_site != null) {
128            state = A_CmsWorkplaceApp.addParamToState(state, SITE, m_site);
129        }
130        if (m_folder != null) {
131            state = A_CmsWorkplaceApp.addParamToState(state, FOLDER, m_folder);
132        }
133        if (m_resourceType != null) {
134            state = A_CmsWorkplaceApp.addParamToState(state, RESOURCE_TYPE, m_resourceType.getTypeName());
135        }
136        if (m_dateFrom != null) {
137            state = A_CmsWorkplaceApp.addParamToState(state, DATE_FROM, String.valueOf(m_dateFrom.getTime()));
138        }
139        if (m_dateTo != null) {
140            state = A_CmsWorkplaceApp.addParamToState(state, DATE_TO, String.valueOf(m_dateTo.getTime()));
141        }
142        if (m_availability != null) {
143            state = A_CmsWorkplaceApp.addParamToState(state, AVAILABILITY, m_availability);
144        }
145        if (m_locale != null) {
146            state = A_CmsWorkplaceApp.addParamToState(state, LOCALE, m_locale.toString());
147        }
148        if (m_query != null) {
149            state = A_CmsWorkplaceApp.addParamToState(state, QUERY, m_query);
150        }
151        if (m_category != null) {
152            state = A_CmsWorkplaceApp.addParamToState(state, CATEGORY, m_category);
153        }
154        return state;
155    }
156
157    /**
158     * Returns the availability.
159     * @return the availability
160     */
161    public String getAvailability() {
162
163        return m_availability;
164    }
165
166    /**
167     * Returns the category.
168     * @return the category
169     */
170    public String getCategory() {
171
172        return m_category;
173    }
174
175    /**
176     * Returns the date from.
177     * @return the date from
178     */
179    public Date getDateFrom() {
180
181        return m_dateFrom;
182    }
183
184    /**
185     * Returns the date to.
186     * @return the date to
187     */
188    public Date getDateTo() {
189
190        return m_dateTo;
191    }
192
193    /**
194     * Returns the folder.
195     * @return the folder
196     */
197    public String getFolder() {
198
199        return m_folder;
200    }
201
202    /**
203     * Returns the locale.
204     * @return the locale
205     */
206    public Locale getLocale() {
207
208        return m_locale;
209    }
210
211    /**
212     * Returns the query.
213     * @return the query
214     */
215    public String getQuery() {
216
217        return m_query;
218    }
219
220    /**
221     * Returns the resource type.
222     * @return the resource type
223     */
224    public I_CmsResourceType getResourceType() {
225
226        return m_resourceType;
227    }
228
229    /**
230     * Returns the site.
231     * @return the site
232     */
233    public String getSite() {
234
235        return m_site;
236    }
237
238    /**
239     * Parses a state string.
240     * @param state the state string
241     */
242    public void parseStateString(String state) {
243
244        if (state != null) {
245            String site = A_CmsWorkplaceApp.getParamFromState(state, SITE);
246            if (site != null) {
247                m_site = site.replace("%2F", "/");
248            }
249            String folder = A_CmsWorkplaceApp.getParamFromState(state, FOLDER);
250            if (folder != null) {
251                m_folder = folder.replace("%2F", "/");
252            }
253            try {
254                String typeName = A_CmsWorkplaceApp.getParamFromState(state, RESOURCE_TYPE);
255                if (typeName != null) {
256                    I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(typeName);
257                    m_resourceType = type;
258                }
259            } catch (CmsLoaderException e) {
260                LOG.error(e.getLocalizedMessage(), e);
261            }
262            String dateFrom = A_CmsWorkplaceApp.getParamFromState(state, DATE_FROM);
263            if (dateFrom != null) {
264                try {
265                    m_dateFrom = new Date(Long.parseLong(dateFrom));
266                } catch (NumberFormatException e) {
267                    m_dateFrom = null;
268                }
269            }
270            String dateTo = A_CmsWorkplaceApp.getParamFromState(state, DATE_TO);
271            if (dateTo != null) {
272                try {
273                    m_dateTo = new Date(Long.parseLong(dateTo));
274                } catch (NumberFormatException e) {
275                    m_dateTo = null;
276                }
277            }
278            String availability = A_CmsWorkplaceApp.getParamFromState(state, AVAILABILITY);
279            if (availability != null) {
280                m_availability = availability;
281            }
282            String locale = A_CmsWorkplaceApp.getParamFromState(state, LOCALE);
283            if (locale != null) {
284                m_locale = CmsLocaleManager.getLocale(locale);
285            }
286            String query = A_CmsWorkplaceApp.getParamFromState(state, QUERY);
287            if (query != null) {
288                m_query = query.replace("%2F", "/");
289            }
290            String category = A_CmsWorkplaceApp.getParamFromState(state, CATEGORY);
291            if (category != null) {
292                m_category = category.replace("%2F", "/");
293            }
294        }
295    }
296
297    /**
298     * Sets the availability.
299     * @param availability the availability
300     */
301    public void setAvailability(String availability) {
302
303        m_availability = availability;
304    }
305
306    /**
307     * Sets the category.
308     * @param category the category
309     */
310    public void setCategory(String category) {
311
312        m_category = category;
313    }
314
315    /**
316     * Sets the from date.
317     * @param dateFrom the dateFrom
318     */
319    public void setDateFrom(Date dateFrom) {
320
321        m_dateFrom = dateFrom;
322    }
323
324    /**
325     * Sets the to date.
326     * @param dateTo the dateTo
327     */
328    public void setDateTo(Date dateTo) {
329
330        m_dateTo = dateTo;
331    }
332
333    /**
334     * Sets the folder.
335     * @param folder the folder
336     */
337    public void setFolder(String folder) {
338
339        m_folder = folder;
340    }
341
342    /**
343     * Sets the locale.
344     * @param locale the locale
345     */
346    public void setLocale(Locale locale) {
347
348        m_locale = locale;
349    }
350
351    /**
352     * Sets the query.
353     * @param query the query
354     */
355    public void setQuery(String query) {
356
357        m_query = query;
358    }
359
360    /**
361     * Sets the resource type.
362     * @param resourceType the resource type
363     */
364    public void setResourceType(I_CmsResourceType resourceType) {
365
366        m_resourceType = resourceType;
367    }
368
369    /**
370     * Sets the site.
371     * @param site the site
372     */
373    public void setSite(String site) {
374
375        m_site = site;
376    }
377}