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.ui.dialogs;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsUser;
032import org.opencms.i18n.CmsResourceBundleLoader;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.CmsRuntimeException;
036import org.opencms.main.OpenCms;
037import org.opencms.ui.A_CmsUI;
038import org.opencms.ui.CmsVaadinUtils;
039import org.opencms.ui.I_CmsDialogContext;
040import org.opencms.ui.Messages;
041import org.opencms.ui.apps.user.CmsAccountsApp;
042import org.opencms.ui.components.CmsBasicDialog;
043import org.opencms.ui.components.CmsOkCancelActionHandler;
044import org.opencms.ui.components.CmsUserDataFormLayout;
045import org.opencms.util.CmsUUID;
046
047import java.util.Collections;
048import java.util.Locale;
049import java.util.MissingResourceException;
050import java.util.ResourceBundle;
051
052import org.apache.commons.logging.Log;
053
054import com.vaadin.ui.Button;
055import com.vaadin.ui.Button.ClickEvent;
056import com.vaadin.ui.Button.ClickListener;
057import com.vaadin.v7.ui.Label;
058
059/**
060 * Dialog to edit the user data.<p>
061 */
062public class CmsUserDataDialog extends CmsBasicDialog implements I_CmsHasTitle {
063
064    /** The embedded dialog id. */
065    public static final String DIALOG_ID = "edituserdata";
066
067    /** The serial version id. */
068    private static final long serialVersionUID = 8907786853232656944L;
069
070    /** Logger instance for this class. */
071    private static final Log LOG = CmsLog.getLog(CmsUserDataDialog.class);
072
073    /** The Cancel button. */
074    private Button m_cancelButton;
075
076    /** The dialog context. */
077    I_CmsDialogContext m_context;
078
079    /** The form layout. */
080    private CmsUserDataFormLayout m_form;
081
082    /** The OK  button. */
083    private Button m_okButton;
084
085    /** The edited user. */
086    CmsUser m_user;
087
088    /**
089     * Creates a new instance.<p>
090     *
091     * @param context the dialog context
092     */
093    public CmsUserDataDialog(I_CmsDialogContext context) {
094
095        m_context = context;
096        CmsObject cms = context.getCms();
097        m_user = cms.getRequestContext().getCurrentUser();
098        if (m_user.isManaged()) {
099            throw new CmsRuntimeException(
100                Messages.get().container(Messages.ERR_USER_NOT_SELF_MANAGED_1, m_user.getName()));
101        }
102
103        CmsVaadinUtils.readAndLocalizeDesign(
104            this,
105            OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale()),
106            null);
107
108        displayResourceInfoDirectly(Collections.singletonList(CmsAccountsApp.getPrincipalInfo(m_user)));
109
110        m_form.initFields(m_user);
111
112        m_cancelButton.addClickListener(new ClickListener() {
113
114            private static final long serialVersionUID = 1L;
115
116            public void buttonClick(ClickEvent event) {
117
118                cancel();
119            }
120
121        });
122
123        m_okButton.addClickListener(new ClickListener() {
124
125            private static final long serialVersionUID = 1L;
126
127            public void buttonClick(ClickEvent event) {
128
129                submit();
130            }
131        });
132
133        setActionHandler(new CmsOkCancelActionHandler() {
134
135            private static final long serialVersionUID = 1L;
136
137            @Override
138            protected void cancel() {
139
140                CmsUserDataDialog.this.cancel();
141            }
142
143            @Override
144            protected void ok() {
145
146                submit();
147            }
148        });
149    }
150
151    /**
152     * Creates a new instance.<p>
153     *
154     * @param context the dialog context
155     * @param forcedCheck <code>true</code> in case of a forced user data check after login
156     */
157    public CmsUserDataDialog(I_CmsDialogContext context, boolean forcedCheck) {
158
159        this(context);
160        if (forcedCheck) {
161            addComponent(new Label(getUserDataCheckMessage()), 0);
162
163            m_cancelButton.setVisible(false);
164        }
165    }
166
167    /**
168     * @see org.opencms.ui.dialogs.I_CmsHasTitle#getTitle(java.util.Locale)
169     */
170    public String getTitle(Locale locale) {
171
172        return org.opencms.ui.components.Messages.get().getBundle(locale).key(
173            org.opencms.ui.components.Messages.GUI_USER_EDIT_0);
174    }
175
176    /**
177     * Returns the message to be displayed for the user data check dialog.<p>
178     *
179     * @return the message to display
180     */
181    protected String getUserDataCheckMessage() {
182
183        ResourceBundle bundle = null;
184        try {
185            bundle = CmsResourceBundleLoader.getBundle("org.opencms.userdatacheck.custom", A_CmsUI.get().getLocale());
186            return bundle.getString("userdatacheck.text");
187        } catch (MissingResourceException e) {
188            return CmsVaadinUtils.getMessageText(org.opencms.ui.dialogs.Messages.GUI_USER_DATA_CHECK_INFO_0);
189        }
190    }
191
192    /**
193     * Cancels the dialog.<p>
194     */
195    void cancel() {
196
197        m_context.finish(Collections.<CmsUUID> emptyList());
198        m_context.updateUserInfo();
199    }
200
201    /**
202     * Submits the dialog.<p>
203     */
204    void submit() {
205
206        try {
207            // Special user info attributes may have been set since the time the dialog was instantiated,
208            // and we don't want to overwrite them, so we read the user again.
209            m_user = m_context.getCms().readUser(m_user.getId());
210            m_form.submit(m_user, m_context.getCms(), new Runnable() {
211
212                public void run() {
213
214                    try {
215                        m_context.getCms().writeUser(m_user);
216                        m_context.finish(Collections.<CmsUUID> emptyList());
217                        m_context.updateUserInfo();
218                    } catch (CmsException e) {
219                        //
220                    }
221                }
222            });
223        } catch (CmsException e) {
224            LOG.error("Unable to read user", e);
225        }
226    }
227
228    /**
229     * Updates the user info.<p>
230     */
231    void updateUserInfo() {
232
233        try {
234            m_user = m_context.getCms().readUser(m_user.getId());
235            displayResourceInfoDirectly(Collections.singletonList(CmsAccountsApp.getPrincipalInfo(m_user)));
236        } catch (CmsException e) {
237            LOG.error("Error updating user info.", e);
238        }
239    }
240}