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.tools.accounts;
029
030import org.opencms.file.CmsResource;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsOrganizationalUnit;
034import org.opencms.security.CmsRole;
035import org.opencms.util.CmsFileUtil;
036import org.opencms.widgets.CmsCheckboxWidget;
037import org.opencms.widgets.CmsDisplayWidget;
038import org.opencms.widgets.CmsInputWidget;
039import org.opencms.widgets.CmsTextareaWidget;
040import org.opencms.widgets.CmsVfsFileWidget;
041import org.opencms.workplace.CmsWidgetDialogParameter;
042
043import java.util.ArrayList;
044import java.util.Iterator;
045import java.util.List;
046
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049import javax.servlet.jsp.PageContext;
050
051/**
052 * Dialog to edit new or existing organizational unit in the administration view.<p>
053 *
054 * @since 6.5.6
055 */
056public class CmsOrgUnitEditDialog extends A_CmsOrgUnitDialog {
057
058    /** Request parameter name for the sub organizational unit fqn. */
059    public static final String PARAM_SUBOUFQN = "suboufqn";
060
061    /**
062     * Public constructor with JSP action element.<p>
063     *
064     * @param jsp an initialized JSP action element
065     */
066    public CmsOrgUnitEditDialog(CmsJspActionElement jsp) {
067
068        super(jsp);
069    }
070
071    /**
072     * Public constructor with JSP variables.<p>
073     *
074     * @param context the JSP page context
075     * @param req the JSP request
076     * @param res the JSP response
077     */
078    public CmsOrgUnitEditDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
079
080        this(new CmsJspActionElement(context, req, res));
081    }
082
083    /**
084     * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
085     */
086    @Override
087    public void actionCommit() {
088
089        List<Throwable> errors = new ArrayList<Throwable>();
090
091        try {
092            // if new create it first
093            if (isNewOrgUnit()) {
094                List<String> resourceNames = CmsFileUtil.removeRedundancies(m_orgUnitBean.getResources());
095                CmsOrganizationalUnit newOrgUnit = OpenCms.getOrgUnitManager().createOrganizationalUnit(
096                    getCms(),
097                    m_orgUnitBean.getFqn(),
098                    m_orgUnitBean.getDescription(),
099                    m_orgUnitBean.getFlags(),
100                    resourceNames.isEmpty() ? null : (String)resourceNames.get(0));
101
102                if (!resourceNames.isEmpty()) {
103                    resourceNames.remove(0);
104                    Iterator<String> itResourceNames = CmsFileUtil.removeRedundancies(resourceNames).iterator();
105                    while (itResourceNames.hasNext()) {
106                        OpenCms.getOrgUnitManager().addResourceToOrgUnit(
107                            getCms(),
108                            newOrgUnit.getName(),
109                            itResourceNames.next());
110                    }
111                }
112            } else {
113                CmsOrganizationalUnit orgunit = OpenCms.getOrgUnitManager().readOrganizationalUnit(
114                    getCms(),
115                    m_orgUnitBean.getFqn());
116                orgunit.setDescription(m_orgUnitBean.getDescription());
117                orgunit.setFlags(m_orgUnitBean.getFlags());
118                List<String> resourceNamesNew = CmsFileUtil.removeRedundancies(m_orgUnitBean.getResources());
119                List<CmsResource> resourcesOld = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit(
120                    getCms(),
121                    orgunit.getName());
122                List<String> resourceNamesOld = new ArrayList<String>();
123                Iterator<CmsResource> itResourcesOld = resourcesOld.iterator();
124                while (itResourcesOld.hasNext()) {
125                    CmsResource resourceOld = itResourcesOld.next();
126                    resourceNamesOld.add(getCms().getSitePath(resourceOld));
127                }
128                Iterator<String> itResourceNamesNew = resourceNamesNew.iterator();
129                // add new resources to ou
130                while (itResourceNamesNew.hasNext()) {
131                    String resourceNameNew = itResourceNamesNew.next();
132                    if (!resourceNamesOld.contains(resourceNameNew)) {
133                        OpenCms.getOrgUnitManager().addResourceToOrgUnit(getCms(), orgunit.getName(), resourceNameNew);
134                    }
135                }
136                Iterator<String> itResourceNamesOld = resourceNamesOld.iterator();
137                // delete old resources from ou
138                while (itResourceNamesOld.hasNext()) {
139                    String resourceNameOld = itResourceNamesOld.next();
140                    if (!resourceNamesNew.contains(resourceNameOld)) {
141                        OpenCms.getOrgUnitManager().removeResourceFromOrgUnit(
142                            getCms(),
143                            orgunit.getName(),
144                            resourceNameOld);
145                    }
146                }
147                // write the edited organizational unit
148                OpenCms.getOrgUnitManager().writeOrganizationalUnit(getCms(), orgunit);
149            }
150        } catch (Throwable t) {
151            errors.add(t);
152        }
153
154        // set the list of errors to display when saving failed
155        setCommitErrors(errors);
156    }
157
158    /**
159     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
160     */
161    @Override
162    protected String createDialogHtml(String dialog) {
163
164        StringBuffer result = new StringBuffer(1024);
165
166        result.append(createWidgetTableStart());
167        // show error header once if there were validation errors
168        result.append(createWidgetErrorHeader());
169
170        if (dialog.equals(PAGES[0])) {
171            // create the widgets for the first dialog page
172            result.append(dialogBlockStart(key(Messages.GUI_ORGUNIT_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
173            result.append(createWidgetTableStart());
174            result.append(createDialogRowsHtml(0, 2));
175            result.append(createWidgetTableEnd());
176            result.append(dialogBlockEnd());
177            result.append(dialogBlockStart(key(Messages.GUI_ORGUNIT_EDITOR_LABEL_FLAGS_BLOCK_0)));
178            result.append(createWidgetTableStart());
179            result.append(createDialogRowsHtml(3, 4));
180            result.append(createWidgetTableEnd());
181            result.append(dialogBlockStart(key(Messages.GUI_ORGUNIT_EDITOR_LABEL_CONTENT_BLOCK_0)));
182            result.append(createWidgetTableStart());
183            result.append(createDialogRowsHtml(5, 5));
184            result.append(createWidgetTableEnd());
185            result.append(dialogBlockEnd());
186        }
187
188        result.append(createWidgetTableEnd());
189        return result.toString();
190    }
191
192    /**
193     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
194     */
195    @Override
196    protected void defineWidgets() {
197
198        super.defineWidgets();
199
200        // widgets to display
201        if (isNewOrgUnit()) {
202            addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "name", PAGES[0], new CmsInputWidget()));
203        } else {
204            addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "name", PAGES[0], new CmsDisplayWidget()));
205        }
206        addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "description", PAGES[0], new CmsTextareaWidget()));
207        addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "parentOuDesc", PAGES[0], new CmsDisplayWidget()));
208
209        if (isNewOrgUnit()) {
210            addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "nologin", PAGES[0], new CmsCheckboxWidget()));
211            addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "webusers", PAGES[0], new CmsCheckboxWidget()));
212        } else {
213            if (m_orgUnitBean.isWebusers()) {
214                addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "nologin", PAGES[0], new CmsDisplayWidget()));
215            } else {
216                addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "nologin", PAGES[0], new CmsCheckboxWidget()));
217            }
218            addWidget(new CmsWidgetDialogParameter(m_orgUnitBean, "webusers", PAGES[0], new CmsDisplayWidget()));
219        }
220        addWidget(
221            new CmsWidgetDialogParameter(
222                m_orgUnitBean,
223                "resources",
224                PAGES[0],
225                new CmsVfsFileWidget(false, getCms().getRequestContext().getSiteRoot(), false)));
226    }
227
228    /**
229     * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
230     */
231    @Override
232    protected void validateParamaters() throws Exception {
233
234        OpenCms.getRoleManager().checkRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(getParamOufqn()));
235        if (!isNewOrgUnit()) {
236            // test the needed parameters
237            OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn()).getName();
238        }
239    }
240}