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.ade.contenteditor;
029
030import org.opencms.acacia.shared.CmsSerialDateUtil;
031import org.opencms.acacia.shared.I_CmsSerialDateValue;
032import org.opencms.acacia.shared.rpc.I_CmsSerialDateService;
033import org.opencms.gwt.CmsGwtService;
034import org.opencms.i18n.CmsMessages;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsPair;
037import org.opencms.widgets.serialdate.CmsSerialDateBeanFactory;
038import org.opencms.widgets.serialdate.CmsSerialDateValue;
039import org.opencms.widgets.serialdate.I_CmsSerialDateBean;
040
041import java.text.DateFormat;
042import java.util.ArrayList;
043import java.util.Collection;
044import java.util.Collections;
045import java.util.Date;
046import java.util.Locale;
047import java.util.SortedSet;
048
049/** Implementation of the serial date RPC service. */
050public class CmsSerialDateService extends CmsGwtService implements I_CmsSerialDateService {
051
052    /** Serialization id. */
053    private static final long serialVersionUID = -5078405766510438917L;
054
055    /** Date formatter for status messages. */
056    private DateFormat m_dateFormat;
057
058    /**
059     * @see org.opencms.acacia.shared.rpc.I_CmsSerialDateService#getDates(java.lang.String)
060     */
061    public Collection<CmsPair<Date, Boolean>> getDates(String config) {
062
063        I_CmsSerialDateBean bean = CmsSerialDateBeanFactory.createSerialDateBean(config);
064        if (null != bean) {
065            Collection<Date> dates = bean.getDates();
066            Collection<Date> exceptions = bean.getExceptions();
067            Collection<CmsPair<Date, Boolean>> result = new ArrayList<>(dates.size() + exceptions.size());
068            for (Date d : dates) {
069                result.add(new CmsPair<Date, Boolean>(d, Boolean.TRUE));
070            }
071            for (Date d : exceptions) {
072                result.add(new CmsPair<Date, Boolean>(d, Boolean.FALSE));
073            }
074            return result;
075        }
076        return Collections.EMPTY_LIST;
077    }
078
079    /**
080     * @see org.opencms.acacia.shared.rpc.I_CmsSerialDateService#getStatus(java.lang.String)
081     */
082    public CmsPair<Boolean, String> getStatus(String config) {
083
084        I_CmsSerialDateValue value = new CmsSerialDateValue(config);
085        Locale l = OpenCms.getWorkplaceManager().getWorkplaceLocale(getCmsObject());
086        CmsMessages messages = Messages.get().getBundle(l);
087        if (value.isValid()) {
088            I_CmsSerialDateBean bean = CmsSerialDateBeanFactory.createSerialDateBean(value);
089            if (bean.hasTooManyDates()) {
090                return new CmsPair<Boolean, String>(
091                    Boolean.FALSE,
092                    messages.key(
093                        Messages.GUI_SERIALDATE_STATUS_TOO_MANY_DATES_2,
094                        Integer.valueOf(CmsSerialDateUtil.getMaxEvents()),
095                        formatDate(bean.getDates().last())));
096            } else {
097                SortedSet<Date> dates = bean.getDates();
098                String message;
099                if (dates.isEmpty()) {
100                    message = messages.key(Messages.GUI_SERIALDATE_EMPTY_EVENT_SERIES_0);
101                } else if (dates.size() == 1) {
102                    message = messages.key(Messages.GUI_SERIALDATE_SINGLE_EVENT_1, formatDate(dates.first()));
103                } else {
104                    message = messages.key(
105                        Messages.GUI_SERIALDATE_MULTIPLE_EVENTS_3,
106                        Integer.valueOf(dates.size()),
107                        formatDate(dates.first()),
108                        formatDate(dates.last()));
109                }
110                return new CmsPair<Boolean, String>(Boolean.TRUE, message);
111            }
112
113        }
114        return new CmsPair<Boolean, String>(
115            Boolean.FALSE,
116            messages.key(Messages.GUI_SERIALDATE_INVALID_SERIES_SPECIFICATION_0));
117    }
118
119    /**
120     * Format the date for the status messages.
121     *
122     * @param date the date to format.
123     *
124     * @return the formatted date.
125     */
126    private String formatDate(Date date) {
127
128        if (null == m_dateFormat) {
129            m_dateFormat = DateFormat.getDateInstance(
130                0,
131                OpenCms.getWorkplaceManager().getWorkplaceLocale(getCmsObject()));
132        }
133        return m_dateFormat.format(date);
134    }
135
136}