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.workplace;
029
030import org.opencms.file.CmsUser;
031import org.opencms.main.CmsLog;
032
033import java.lang.reflect.InvocationTargetException;
034
035import org.apache.commons.beanutils.PropertyUtilsBean;
036import org.apache.commons.logging.Log;
037
038/**
039 * Account info bean.<p>
040 */
041public class CmsAccountInfo {
042
043    /** Account info fields. */
044    public enum Field {
045        /** An additional info field. */
046        addinfo,
047
048        /** The address field. */
049        address,
050
051        /** The city field. */
052        city,
053
054        /** The country field. */
055        country,
056
057        /** The email field. */
058        email,
059
060        /** The first name field. */
061        firstname,
062
063        /** The institution field. */
064        institution,
065
066        /** The last name field. */
067        lastname,
068
069        /** The zip code field. */
070        zipcode
071    }
072
073    /** Logger instance for this class. */
074    private static final Log LOG = CmsLog.getLog(CmsAccountInfo.class);
075
076    /** The additional info key. */
077    private String m_addInfoKey;
078
079    /** The editable flag. */
080    private boolean m_editable;
081
082    /** The field. */
083    private Field m_field;
084
085    /**
086     * Constructor.<p>
087     *
088     * @param field the field
089     * @param addInfoKey the additional info key
090     * @param editable the editable flag
091     */
092    public CmsAccountInfo(Field field, String addInfoKey, boolean editable) {
093        m_field = field;
094        m_addInfoKey = addInfoKey;
095        m_editable = editable;
096    }
097
098    /**
099     * Constructor.<p>
100     *
101     * @param field the field
102     * @param addInfoKey the additional info key
103     * @param editable the editable flag
104     */
105    public CmsAccountInfo(String field, String addInfoKey, String editable) {
106        m_field = Field.valueOf(field);
107        m_addInfoKey = addInfoKey;
108        m_editable = Boolean.parseBoolean(editable);
109    }
110
111    /**
112     * Returns the additional info key.<p>
113     *
114     * @return the additional info key
115     */
116    public String getAddInfoKey() {
117
118        return m_addInfoKey;
119    }
120
121    /**
122     * Returns the field.<p>
123     *
124     * @return the field
125     */
126    public Field getField() {
127
128        return m_field;
129    }
130
131    /**
132     * Returns the account info value for the given user.<p>
133     *
134     * @param user the user
135     *
136     * @return the value
137     */
138    public String getValue(CmsUser user) {
139
140        String value = null;
141        if (isAdditionalInfo()) {
142            value = (String)user.getAdditionalInfo(getAddInfoKey());
143        } else {
144            try {
145                PropertyUtilsBean propUtils = new PropertyUtilsBean();
146                value = (String)propUtils.getProperty(user, getField().name());
147            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
148                LOG.error("Error reading account info field.", e);
149            }
150        }
151        return value;
152    }
153
154    /**
155     * Returns whether this is an additional info field.<p>
156     * @return <code>true</code> in case of an additional info field
157     */
158    public boolean isAdditionalInfo() {
159
160        return Field.addinfo.equals(m_field);
161    }
162
163    /**
164     * Returns if the field is editable.<p>
165     *
166     * @return if the field is editable
167     */
168    public boolean isEditable() {
169
170        return m_editable;
171    }
172
173}