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.jsp.userdata;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsLog;
032import org.opencms.main.OpenCms;
033import org.opencms.xml.content.CmsXmlContent;
034
035import java.util.Locale;
036
037import org.apache.commons.logging.Log;
038
039/**
040 * Configuration for user data requests.<p>
041 *
042 * Mostly used to access texts to show to the user. Currently, a thin wrapper around an XML content.
043 */
044public class CmsUserDataRequestConfig {
045
046    /** Logger instance for this class. */
047    private static final Log LOG = CmsLog.getLog(CmsUserDataRequestConfig.class);
048
049    /** The CMS context used to load the configuration. */
050    private CmsObject m_cms;
051
052    /** The XML content with the configuration. */
053    private CmsXmlContent m_content;
054
055    /** The locale for which the data should be read from the XML content. .*/
056    private Locale m_locale;
057
058    /**
059     * Creates a new instance.
060     *
061     * @param cms the CMS context
062     * @param content the content with the configuration
063     * @param locale the locale
064     */
065    public CmsUserDataRequestConfig(CmsObject cms, CmsXmlContent content, Locale locale) {
066
067        m_content = content;
068        m_cms = cms;
069        m_locale = OpenCms.getLocaleManager().getBestMatchingLocale(locale, content.getLocales(), content.getLocales());
070    }
071
072    /**
073     * Gets the email subject.
074     *
075     * @return the email subject
076     */
077    public String getMailSubject() {
078
079        return getText("MailSubject");
080    }
081
082    /**
083     * Gets the email text.
084     *
085     * @return the email text
086     */
087    public String getMailText() {
088
089        return getText("MailText");
090
091    }
092
093    /**
094     * Gets the time for which user data requests are valid (in milliseconds).
095     *
096     * @return the time for which user data requests are valid
097     */
098    public long getRequestLifetime() {
099
100        String lifetimeStr = getText("LinkHoursValid");
101        long hours = 24;
102        try {
103            hours = Long.parseLong(lifetimeStr.trim());
104        } catch (NumberFormatException e) {
105            LOG.error(e.getLocalizedMessage(), e);
106        }
107        return hours * 60 * 60 * 1000;
108    }
109
110    /**
111     * Gets the text for the given key.
112     *
113     * @param key the text key
114     *
115     * @return the text for that key
116     */
117    public String getText(String key) {
118
119        if (m_content.hasValue(key, m_locale)) {
120            try {
121                return m_content.getValue(key, m_locale).getStringValue(m_cms);
122            } catch (Exception e) {
123                LOG.warn(e.getLocalizedMessage(), e);
124            }
125        }
126        return "???" + key + "???";
127
128    }
129
130}