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 GmbH & Co. KG, 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.widgets.CmsInputWidget;
031import org.opencms.widgets.I_CmsWidget;
032
033import java.lang.reflect.Constructor;
034
035/**
036 * Represents an user additional information entry.<p>
037 *
038 * @since 6.5.6
039 */
040public final class CmsWorkplaceUserInfoEntry {
041
042    /** The entry's key. */
043    private final String m_key;
044
045    /** If entry is optional. */
046    private final String m_optional;
047
048    /** The widget. */
049    private final String m_widget;
050
051    /** The class name of the stored data type. */
052    private String m_type;
053
054    /** The widget parameters. */
055    private String m_params;
056
057    /**
058     * Default constructor.<p>
059     *
060     * @param key the key for this entry
061     * @param type the class name of the stored data type
062     * @param widget the widget to use
063     * @param params the widget parameters
064     * @param optional if this entry is optional
065     */
066    public CmsWorkplaceUserInfoEntry(String key, String type, String widget, String params, String optional) {
067
068        m_key = key;
069        m_type = type;
070        m_widget = widget;
071        m_params = params;
072        m_optional = optional;
073    }
074
075    /**
076     * Returns the key.<p>
077     *
078     * @return the key
079     */
080    public String getKey() {
081
082        return m_key;
083    }
084
085    /**
086     * Returns the widget class name.<p>
087     *
088     * @return the widget class name
089     */
090    public String getWidget() {
091
092        return m_widget;
093    }
094
095    /**
096     * Returns a new widget object.<p>
097     *
098     * @return a new widget object
099     */
100    public I_CmsWidget getWidgetObject() {
101
102        if (getWidget() == null) {
103            if (m_params == null) {
104                return new CmsInputWidget();
105            } else {
106                return new CmsInputWidget(m_params);
107            }
108        }
109        try {
110            if (m_params == null) {
111                return (I_CmsWidget)Class.forName(getWidget()).newInstance();
112            }
113            Class<?> clazz = Class.forName(getWidget());
114            Constructor<?> ctor = clazz.getConstructor(new Class[] {String.class});
115            return (I_CmsWidget)ctor.newInstance(new Object[] {m_params});
116        } catch (Exception e) {
117            return new CmsInputWidget();
118        }
119    }
120
121    /**
122     * Returns the optional flag.<p>
123     *
124     * @return the optional flag
125     */
126    public String getOptional() {
127
128        return m_optional;
129    }
130
131    /**
132     * Returns the optional flag.<p>
133     *
134     * @return the optional flag
135     */
136    public boolean isOptional() {
137
138        return Boolean.valueOf(m_optional).booleanValue();
139    }
140
141    /**
142     * Returns the configured widget parameters.<p>
143     *
144     * @return the configured widget parameters
145     */
146    public String getParams() {
147
148        return m_params;
149    }
150
151    /**
152     * Returns the configured class name type.<p>
153     *
154     * @return the configured class name type
155     */
156    public String getType() {
157
158        return m_type;
159    }
160
161    /**
162     * Returns the class type.<p>
163     *
164     * @return the class type
165     */
166    public Class<?> getClassType() {
167
168        if (m_type == null) {
169            return String.class;
170        }
171        try {
172            return Class.forName(m_type);
173        } catch (ClassNotFoundException e) {
174            return String.class;
175        }
176    }
177}