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.widgets.serialdate;
029
030import org.opencms.acacia.shared.I_CmsSerialDateValue;
031import org.opencms.main.CmsLog;
032
033import org.apache.commons.logging.Log;
034
035/**
036 * Wrapper for the value stored by the {@link org.opencms.widgets.CmsSerialDateWidget}
037 * that provides easy access.
038 */
039public class CmsSerialDateBeanFactory {
040
041    /** Logger for the class. */
042    public static final Log LOG = CmsLog.getLog(CmsSerialDateBeanFactory.class);
043
044    /**
045     * Factory method for creating a serial date bean.
046     * @param value the value for the series
047     * @return the serial date bean.
048     */
049    public static I_CmsSerialDateBean createSerialDateBean(I_CmsSerialDateValue value) {
050
051        if ((null == value) || !value.isValid()) {
052            return null;
053        }
054        switch (value.getPatternType()) {
055            case DAILY:
056                if (value.isEveryWorkingDay()) {
057                    return new CmsSerialDateBeanWorkingDays(
058                        value.getStart(),
059                        value.getEnd(),
060                        value.isWholeDay(),
061                        value.getEndType(),
062                        value.getSeriesEndDate(),
063                        value.getOccurrences(),
064                        value.getExceptions());
065                } else {
066                    return new CmsSerialDateBeanDaily(
067                        value.getStart(),
068                        value.getEnd(),
069                        value.isWholeDay(),
070                        value.getEndType(),
071                        value.getSeriesEndDate(),
072                        value.getOccurrences(),
073                        value.getExceptions(),
074                        value.getInterval());
075                }
076            case WEEKLY:
077                return new CmsSerialDateBeanWeekly(
078                    value.getStart(),
079                    value.getEnd(),
080                    value.isWholeDay(),
081                    value.getEndType(),
082                    value.getSeriesEndDate(),
083                    value.getOccurrences(),
084                    value.getExceptions(),
085                    value.getInterval(),
086                    value.getWeekDays());
087            case MONTHLY:
088                if (null == value.getWeekDay()) {
089                    return new CmsSerialDateBeanMonthly(
090                        value.getStart(),
091                        value.getEnd(),
092                        value.isWholeDay(),
093                        value.getEndType(),
094                        value.getSeriesEndDate(),
095                        value.getOccurrences(),
096                        value.getExceptions(),
097                        value.getInterval(),
098                        value.getDayOfMonth());
099                } else {
100                    return new CmsSerialDateBeanMonthlyWeeks(
101                        value.getStart(),
102                        value.getEnd(),
103                        value.isWholeDay(),
104                        value.getEndType(),
105                        value.getSeriesEndDate(),
106                        value.getOccurrences(),
107                        value.getExceptions(),
108                        value.getInterval(),
109                        value.getWeeksOfMonth(),
110                        value.getWeekDay());
111                }
112            case YEARLY:
113                if (null == value.getWeekDay()) {
114                    return new CmsSerialDateBeanYearly(
115                        value.getStart(),
116                        value.getEnd(),
117                        value.isWholeDay(),
118                        value.getEndType(),
119                        value.getSeriesEndDate(),
120                        value.getOccurrences(),
121                        value.getExceptions(),
122                        value.getDayOfMonth(),
123                        value.getMonth());
124                } else {
125                    return new CmsSerialDateBeanYearlyWeekday(
126                        value.getStart(),
127                        value.getEnd(),
128                        value.isWholeDay(),
129                        value.getEndType(),
130                        value.getSeriesEndDate(),
131                        value.getOccurrences(),
132                        value.getExceptions(),
133                        value.getWeekOfMonth(),
134                        value.getMonth(),
135                        value.getWeekDay());
136                }
137            case INDIVIDUAL:
138                return new CmsSerialDateBeanIndividual(
139                    value.getStart(),
140                    value.getEnd(),
141                    value.isWholeDay(),
142                    value.getEndType(),
143                    value.getSeriesEndDate(),
144                    value.getOccurrences(),
145                    value.getExceptions(),
146                    value.getIndividualDates());
147            case NONE:
148                return new CmsSerialDateBeanSingle(value.getStart(), value.getEnd(), value.isWholeDay());
149            default:
150                throw new IllegalArgumentException();
151        }
152    }
153
154    /**
155     * Factory method for creating a serial date bean.
156     * @param widgetValue the value for the series as stored by the {@link org.opencms.widgets.CmsSerialDateWidget}
157     * @return the serial date bean.
158     */
159    public static I_CmsSerialDateBean createSerialDateBean(String widgetValue) {
160
161        I_CmsSerialDateValue value;
162        value = new CmsSerialDateValue(widgetValue);
163        return createSerialDateBean(value);
164    }
165}