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.jsp;
029
030import org.opencms.ade.contenteditor.shared.CmsEditorConstants;
031import org.opencms.util.CmsStringUtil;
032import org.opencms.util.CmsUUID;
033
034/**
035 * A container to store information about a collector's result.<p>
036 *
037 * @since 6.0.0
038 */
039public class CmsContentInfoBean {
040
041    /** The name under which the collector info is saved in the page context. */
042    public static final String PAGE_CONTEXT_ATTRIBUTE_NAME = "CollectorInfo";
043
044    /** UUID identifying the content info instance. */
045    private String m_id = (new CmsUUID()).toString();
046
047    /** The default locale (as String) that is used. */
048    private String m_locale;
049
050    /** The number of pages of browse through the result list. */
051    private int m_pageCount;
052
053    /** The index of the current page that to be displayed. */
054    private int m_pageIndex;
055
056    /** The page index of the last element in the Google-like page navigation. */
057    private int m_pageNavEndIndex;
058
059    /** The number of page links in the Google-like page navigation. */
060    private int m_pageNavLength;
061
062    /** The page index of the first element in the Google-like page navigation. */
063    private int m_pageNavStartIndex;
064
065    /** The number of entries to be displayed on a page. */
066    private int m_pageSize;
067
068    /** The index of the current resource that gets iterated in the result list. */
069    private int m_resultIndex;
070
071    /** The total size of the collector's result list. */
072    private int m_resultSize;
073
074    /**
075     * Creates a new content info bean.<p>
076     */
077    public CmsContentInfoBean() {
078
079        super();
080
081        m_resultSize = -1;
082        m_resultIndex = -1;
083
084        m_pageCount = 1;
085        m_pageSize = -1;
086        m_pageIndex = 1;
087        m_pageNavStartIndex = 1;
088        m_pageNavEndIndex = 1;
089        m_pageNavLength = 10;
090    }
091
092    /**
093     * Gets an id which is common to all items in a collector list.<p>
094     *
095     * @return the id identifying the collector list
096     */
097    public String getId() {
098
099        return m_id;
100    }
101
102    /**
103     * Returns the locale used by the content loader.<p>
104     *
105     * @return the locale used by the content loader
106     */
107    public String getLocale() {
108
109        return m_locale;
110    }
111
112    /**
113     * Gets the javascript: link for creating and editing a new content.<p>
114     *
115     * @return the javascript: link for creating and editing a new content
116     */
117    public String getNewContentLink() {
118
119        return "javascript:" + getNewContentScript();
120    }
121
122    /**
123     * Gets the Javascript for creating and editing a new content.<p>
124     *
125     * @return the Javascript for creating and editing a new content
126     */
127    public String getNewContentScript() {
128
129        return CmsEditorConstants.FUNCTION_CREATE_NEW + "('" + m_id + "');";
130    }
131
132    /**
133     * Returns the number of pages of browse through the result list.<p>
134     *
135     * @return the number of pages of browse through the result list
136     */
137    public int getPageCount() {
138
139        return m_pageCount;
140    }
141
142    /**
143     * Returns the index of the current page that gets displayed.<p>
144     *
145     * @return the index of the current page that gets displayed
146     */
147    public int getPageIndex() {
148
149        return m_pageIndex;
150    }
151
152    /**
153     * Returns the page index of the first element in the Google-like page navigation.<p>
154     *
155     * @return the page index of the first element in the Google-like page navigation
156     */
157    public int getPageNavEndIndex() {
158
159        return m_pageNavEndIndex;
160    }
161
162    /**
163     * Returns the page index of the last element in the Google-like page navigation.<p>
164     *
165     * @return page index of the last element in the Google-like page navigation
166     */
167    public int getPageNavLength() {
168
169        return m_pageNavLength;
170    }
171
172    /**
173     * Returns the page index of the first element in the Google-like page navigation.<p>
174     *
175     * @return the page index of the first element in the Google-like page navigation
176     */
177    public int getPageNavStartIndex() {
178
179        return m_pageNavStartIndex;
180    }
181
182    /**
183     * Returns the size of a page.<p>
184     *
185     * @return the size of a page
186     */
187    public int getPageSize() {
188
189        return m_pageSize;
190    }
191
192    /**
193     * Returns the index of the current resource that gets iterated in the result list.<p>
194     *
195     * @return the index of the current resource that gets iterated in the result list
196     */
197    public int getResultIndex() {
198
199        return m_resultIndex;
200    }
201
202    /**
203     * Returns the total size of the collector's result list.<p>
204     *
205     * @return the total size of the collector's result list
206     */
207    public int getResultSize() {
208
209        return m_resultSize;
210    }
211
212    /**
213     * Returns true if there is no resource in the result list.<p>
214     *
215     * @return true if there is no resource in the result list
216     */
217    public boolean isEmptyResult() {
218
219        return m_resultSize <= 0;
220    }
221
222    /**
223     * Returns true if the current resource is the first resource on the current page.<p>
224     *
225     * @return true if the current resource is the first resource on the current page
226     */
227    public boolean isFirstOnPage() {
228
229        return m_resultIndex == (((m_pageIndex - 1) * m_pageSize) + 1);
230    }
231
232    /**
233     * Returns true if the current resource is the first resource in the result list.<p>
234     *
235     * @return true if the current resource is the first resource in the result list
236     */
237    public boolean isFirstResult() {
238
239        return m_resultIndex == 1;
240    }
241
242    /**
243     * Returns true if the current resource is the last resource on the current page.<p>
244     *
245     * @return true if the current resource is the last resource on the current page
246     */
247    public boolean isLastOnPage() {
248
249        return (m_resultIndex == (m_pageIndex * m_pageSize)) || isLastResult();
250    }
251
252    /**
253     * Returns true if the current resource is the last resource in the result list.<p>
254     *
255     * @return true if the current resource is the last resource in the result list
256     */
257    public boolean isLastResult() {
258
259        return m_resultIndex == m_resultSize;
260    }
261
262    /**
263     * Sets the current locale used by the content loader.<p>
264     *
265     * @param locale the locale to set
266     */
267    public void setLocale(String locale) {
268
269        m_locale = locale;
270    }
271
272    /**
273     * Increments the index of the current resource that gets iterated in the result list.<p>
274     */
275    void incResultIndex() {
276
277        m_resultIndex++;
278    }
279
280    /**
281     * Initializes the start and end indexes to build a Google-like page navigation.<p>
282     */
283    void initPageNavIndexes() {
284
285        if (m_pageIndex < m_pageNavLength) {
286
287            m_pageNavStartIndex = 1;
288            m_pageNavEndIndex = m_pageCount < m_pageNavLength ? m_pageCount : m_pageNavLength;
289
290        } else {
291
292            int middle = m_pageNavLength / 2;
293            m_pageNavStartIndex = m_pageIndex - middle;
294            m_pageNavEndIndex = (m_pageNavStartIndex + m_pageNavLength) - 1;
295
296            if (m_pageNavStartIndex < 1) {
297                m_pageNavStartIndex = 1;
298            } else if (m_pageNavEndIndex < 1) {
299                m_pageNavEndIndex = m_pageCount;
300            } else if (m_pageNavEndIndex > m_pageCount) {
301
302                // adjust end index
303                m_pageNavEndIndex = m_pageCount;
304                m_pageNavStartIndex = (m_pageNavEndIndex - m_pageNavLength) + 1;
305
306                if (m_pageNavStartIndex < 1) {
307                    // adjust the start index again
308                    m_pageNavStartIndex = 1;
309                }
310            }
311        }
312    }
313
314    /**
315     * Initializes the index of the current resource that gets iterated in the result list.<p>
316     */
317    void initResultIndex() {
318
319        int startIndex = 0;
320        if ((m_pageIndex > 0) && (m_pageSize > 0)) {
321            startIndex = (m_pageIndex - 1) * m_pageSize;
322        }
323
324        m_resultIndex = startIndex > m_resultSize ? m_resultSize : startIndex;
325    }
326
327    /**
328     * Sets the number of pages of browse through the result list.<p>
329     *
330     * @param pageCount the number of pages of browse through the result list
331     */
332    void setPageCount(int pageCount) {
333
334        m_pageCount = pageCount;
335    }
336
337    /**
338     * Sets the index of the current page that gets displayed as an int.<p>
339     *
340     * @param pageIndex the index of the current page that gets displayed as an int
341     */
342    void setPageIndex(int pageIndex) {
343
344        m_pageIndex = pageIndex;
345    }
346
347    /**
348     * Sets the index of the current page that gets displayed as a string.<p>
349     *
350     * The specified string gets parsed into an int.<p>
351     *
352     * @param pageIndex the index of the current page that gets displayed as a string
353     */
354    void setPageIndexAsString(String pageIndex) {
355
356        if (CmsStringUtil.isEmpty(pageIndex)) {
357            return;
358        }
359
360        try {
361            m_pageIndex = Integer.parseInt(pageIndex);
362        } catch (NumberFormatException e) {
363            // intentionally left blank
364        }
365    }
366
367    /**
368     * Sets the page index of the last element in the Google-like page navigation.<p>
369     *
370     * @param index the page index of the last element in the Google-like page navigation
371     */
372    void setPageNavEndIndex(int index) {
373
374        m_pageNavEndIndex = index;
375    }
376
377    /**
378     * Sets the number of page links in the Google-like page navigation.<p>
379     *
380     * @param length the number of page links in the Google-like page navigation
381     */
382    void setPageNavLength(int length) {
383
384        m_pageNavLength = length;
385    }
386
387    /**
388     * Sets number of page links in the Google-like page navigation as a string.<p>
389     *
390     * @param pageNavLength the number of page links in the Google-like page navigation
391     */
392    void setPageNavLengthAsString(String pageNavLength) {
393
394        if (CmsStringUtil.isEmpty(pageNavLength)) {
395            return;
396        }
397
398        try {
399            m_pageNavLength = Integer.parseInt(pageNavLength);
400        } catch (NumberFormatException e) {
401            // intentionally left blank
402        }
403    }
404
405    /**
406     * Sets the page index of the first element in the Google-like page navigation.<p>
407     *
408     * @param index the page index of the first element in the Google-like page navigation
409     */
410    void setPageNavStartIndex(int index) {
411
412        m_pageNavStartIndex = index;
413    }
414
415    /**
416     * Sets the size of a page as an int.<p>
417     *
418     * @param pageSize the size of a page as an int
419     */
420    void setPageSize(int pageSize) {
421
422        m_pageSize = pageSize;
423    }
424
425    /**
426     * Sets the size of a page as a string.<p>
427     *
428     * The specified string gets parsed into an int.<p>
429     *
430     * @param pageSize the size of a page as a string
431     */
432    void setPageSizeAsString(String pageSize) {
433
434        if (CmsStringUtil.isEmpty(pageSize)) {
435            return;
436        }
437
438        try {
439            m_pageSize = Integer.parseInt(pageSize);
440        } catch (NumberFormatException e) {
441            // intentionally left blank
442        }
443    }
444
445    /**
446     * Sets the total size of the collector's result list.<p>
447     *
448     * @param size the total size of the collector's result list
449     */
450    void setResultSize(int size) {
451
452        m_resultSize = size;
453    }
454}