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.apps.user;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.components.CmsBasicDialog;
036import org.opencms.util.CmsUUID;
037
038import java.util.Collections;
039
040import org.apache.commons.logging.Log;
041
042import com.vaadin.ui.Button;
043import com.vaadin.ui.Button.ClickEvent;
044import com.vaadin.ui.Button.ClickListener;
045import com.vaadin.ui.Window;
046import com.vaadin.v7.data.Property.ValueChangeEvent;
047import com.vaadin.v7.data.Property.ValueChangeListener;
048import com.vaadin.v7.ui.CheckBox;
049import com.vaadin.v7.ui.Label;
050import com.vaadin.v7.ui.TextArea;
051import com.vaadin.v7.ui.TextField;
052
053/**
054 * Class for the dialog to edit or create a CmsGroup.<p>
055 */
056public class CmsGroupEditDialog extends CmsBasicDialog {
057
058    /** The logger for this class. */
059    static Log LOG = CmsLog.getLog(CmsGroupEditDialog.class.getName());
060
061    /**vaadin serial id.*/
062    private static final long serialVersionUID = 6633733627052633351L;
063
064    /**vaadin component.*/
065    Button m_ok;
066
067    /** The app instance. */
068    private CmsAccountsApp m_app;
069
070    /**vaadin component.*/
071    private Button m_cancel;
072
073    /**CmsObject.*/
074    private CmsObject m_cms;
075
076    /**vaadin component.*/
077    private TextArea m_description;
078
079    /**vaadin component.*/
080    private CheckBox m_enabled;
081
082    /**CmsGroup.*/
083    private CmsGroup m_group;
084
085    /** The group edit parameters. */
086    private CmsGroupEditParameters m_groupEditParameters = new CmsGroupEditParameters();
087
088    /**vaadin component.*/
089    private TextField m_name;
090
091    /**vaadin component.*/
092    private Label m_ou;
093
094    /**
095     * public constructor.<p>
096     *
097     * @param cms CmsObject
098     * @param groupId id of group edit, null if groud should be created
099     * @param window window holding the dialog
100     * @param app the app instance
101     */
102    public CmsGroupEditDialog(CmsObject cms, CmsUUID groupId, final Window window, final CmsAccountsApp app) {
103
104        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
105        m_cms = cms;
106        m_app = app;
107
108        try {
109            if (groupId != null) {
110                m_group = m_cms.readGroup(groupId);
111                m_groupEditParameters = m_app.getGroupEditParameters(m_group);
112                displayResourceInfoDirectly(Collections.singletonList(CmsAccountsApp.getPrincipalInfo(m_group)));
113                m_ou.setValue(m_group.getOuFqn());
114                m_name.setValue(m_group.getSimpleName());
115                m_name.setEnabled(false);
116
117                m_description.setValue(m_group.getDescription());
118
119                m_enabled.setValue(Boolean.valueOf(m_group.isEnabled()));
120
121            }
122
123        } catch (CmsException e) {
124            LOG.error("unable to read group", e);
125        }
126        m_ok.setEnabled(false);
127        m_ok.addClickListener(new ClickListener() {
128
129            private static final long serialVersionUID = 2337532424806798793L;
130
131            public void buttonClick(ClickEvent event) {
132
133                saveGroup();
134                window.close();
135                app.reload();
136
137            }
138        });
139
140        m_cancel.addClickListener(new ClickListener() {
141
142            private static final long serialVersionUID = -6389260624197980323L;
143
144            public void buttonClick(ClickEvent event) {
145
146                window.close();
147
148            }
149        });
150
151        ValueChangeListener listener = new ValueChangeListener() {
152
153            private static final long serialVersionUID = -7480617292190495288L;
154
155            public void valueChange(ValueChangeEvent event) {
156
157                m_ok.setEnabled(true);
158
159            }
160
161        };
162
163        m_enabled.addValueChangeListener(listener);
164        m_description.addValueChangeListener(listener);
165        m_name.addValueChangeListener(listener);
166        if (!m_groupEditParameters.isEditable()) {
167            m_description.setEnabled(false);
168            m_enabled.setEnabled(false);
169        }
170
171    }
172
173    /**
174     * Constructor for dialog for new groups.
175     *
176     * @param cms CmsObject
177     * @param window window holding dialog
178     * @param ou to create group in
179     * @param app the app instance
180     */
181    public CmsGroupEditDialog(CmsObject cms, Window window, String ou, CmsAccountsApp app) {
182
183        this(cms, null, window, app);
184        m_ou.setValue(ou);
185        m_enabled.setValue(Boolean.valueOf(true));
186    }
187
188    /**
189     * Save group.<p>
190     */
191    protected void saveGroup() {
192
193        if (m_group == null) {
194            m_group = new CmsGroup();
195            String ou = m_ou.getValue();
196            if (!ou.endsWith("/")) {
197                ou += "/";
198            }
199            m_group.setName(m_name.getValue());
200            try {
201                m_cms.createGroup(ou + m_name.getValue(), m_description.getValue(), 0, null);
202            } catch (CmsException e) {
203                //
204            }
205        }
206
207        m_group.setDescription(m_description.getValue());
208        m_group.setEnabled(m_enabled.getValue().booleanValue());
209
210        try {
211            m_cms.writeGroup(m_group);
212        } catch (CmsException e) {
213            //
214        }
215    }
216}