001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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
094        m_field = field;
095        m_addInfoKey = addInfoKey;
096        m_editable = editable;
097    }
098
099    /**
100     * Constructor.<p>
101     *
102     * @param field the field
103     * @param addInfoKey the additional info key
104     * @param editable the editable flag
105     */
106    public CmsAccountInfo(String field, String addInfoKey, String editable) {
107
108        m_field = Field.valueOf(field);
109        m_addInfoKey = addInfoKey;
110        m_editable = Boolean.parseBoolean(editable);
111    }
112
113    /**
114     * Returns the additional info key.<p>
115     *
116     * @return the additional info key
117     */
118    public String getAddInfoKey() {
119
120        return m_addInfoKey;
121    }
122
123    /**
124     * Returns the field.<p>
125     *
126     * @return the field
127     */
128    public Field getField() {
129
130        return m_field;
131    }
132
133    /**
134     * Returns the account info value for the given user.<p>
135     *
136     * @param user the user
137     *
138     * @return the value
139     */
140    public String getValue(CmsUser user) {
141
142        String value = null;
143        if (isAdditionalInfo()) {
144            value = (String)user.getAdditionalInfo(getAddInfoKey());
145        } else {
146            try {
147                PropertyUtilsBean propUtils = new PropertyUtilsBean();
148                value = (String)propUtils.getProperty(user, getField().name());
149            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
150                LOG.error("Error reading account info field.", e);
151            }
152        }
153        return value;
154    }
155
156    /**
157     * Returns whether this is an additional info field.<p>
158     * @return <code>true</code> in case of an additional info field
159     */
160    public boolean isAdditionalInfo() {
161
162        return Field.addinfo.equals(m_field);
163    }
164
165    /**
166     * Returns if the field is editable.<p>
167     *
168     * @return if the field is editable
169     */
170    public boolean isEditable() {
171
172        return m_editable;
173    }
174
175}