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.util.CmsStringUtil;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.CmsWidgetDialog;
037import org.opencms.workplace.CmsWorkplaceSettings;
038
039import java.util.ArrayList;
040import java.util.Iterator;
041import java.util.List;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045import javax.servlet.jsp.PageContext;
046
047/**
048 * Dialog to edit new or existing organizational unit in the administration view.<p>
049 *
050 * @since 6.5.6
051 */
052public abstract class A_CmsOrgUnitDialog extends CmsWidgetDialog {
053
054    /** localized messages Keys prefix. */
055    public static final String KEY_PREFIX = "orgunit";
056
057    /** Defines which pages are valid for this dialog. */
058    public static final String[] PAGES = {"page1"};
059
060    /** Request parameter name for the organizational unit fqn. */
061    public static final String PARAM_OUFQN = "oufqn";
062
063    /** The organizational unit bean object to work with in this dialog. */
064    protected CmsOrgUnitBean m_orgUnitBean;
065
066    /** Stores the value of the request parameter for the organizational unit fqn. */
067    private String m_paramOufqn;
068
069    /**
070     * Public constructor with JSP action element.<p>
071     *
072     * @param jsp an initialized JSP action element
073     */
074    public A_CmsOrgUnitDialog(CmsJspActionElement jsp) {
075
076        super(jsp);
077    }
078
079    /**
080     * Public constructor with JSP variables.<p>
081     *
082     * @param context the JSP page context
083     * @param req the JSP request
084     * @param res the JSP response
085     */
086    public A_CmsOrgUnitDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
087
088        this(new CmsJspActionElement(context, req, res));
089    }
090
091    /**
092     * Returns the organizational unit fqn parameter value.<p>
093     *
094     * @return the organizational unit fqn parameter value
095     */
096    public String getParamOufqn() {
097
098        return m_paramOufqn;
099    }
100
101    /**
102     * Sets the organizational unit fqn parameter value.<p>
103     *
104     * @param ouFqn the organizational unit fqn parameter value
105     */
106    public void setParamOufqn(String ouFqn) {
107
108        if (ouFqn == null) {
109            ouFqn = "";
110        }
111        m_paramOufqn = ouFqn;
112    }
113
114    /**
115     * Sets the resources for the given orgUnitBean.<p>
116     *
117     * @param orgUnitBean the <code>CmsOrgUnitBean</code> object
118     * @param resources the list of resources
119     */
120    public void setResourcesInBean(CmsOrgUnitBean orgUnitBean, List<CmsResource> resources) {
121
122        List<String> resourceNames = new ArrayList<String>();
123        Iterator<CmsResource> itResources = resources.iterator();
124        while (itResources.hasNext()) {
125            CmsResource resource = itResources.next();
126            resourceNames.add(getCms().getSitePath(resource));
127        }
128        orgUnitBean.setResources(resourceNames);
129    }
130
131    /**
132     *
133     * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
134     */
135    @Override
136    protected void defineWidgets() {
137
138        initOrgUnitObject();
139        setKeyPrefix(KEY_PREFIX);
140    }
141
142    /**
143     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
144     */
145    @Override
146    protected String[] getPageArray() {
147
148        return PAGES;
149    }
150
151    /**
152     * Initializes the organizational unit object to work with depending
153     * on the dialog state and request parameters.<p>
154     */
155    protected void initOrgUnitObject() {
156
157        try {
158            if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
159                // edit an existing ou, get the ou object from database
160                CmsOrganizationalUnit orgunit = OpenCms.getOrgUnitManager().readOrganizationalUnit(
161                    getCms(),
162                    getParamOufqn());
163                m_orgUnitBean = new CmsOrgUnitBean();
164                if (!isNewOrgUnit()) {
165                    m_orgUnitBean.setName(orgunit.getName());
166                    m_orgUnitBean.setDescription(orgunit.getDescription(getLocale()));
167                    m_orgUnitBean.setParentOu(orgunit.getParentFqn());
168                    m_orgUnitBean.setFqn(orgunit.getName());
169                    m_orgUnitBean.setNologin(orgunit.hasFlagHideLogin());
170                    m_orgUnitBean.setWebusers(orgunit.hasFlagWebuser());
171                    if (orgunit.getParentFqn() != null) {
172                        m_orgUnitBean.setParentOuDesc(
173                            OpenCms.getOrgUnitManager().readOrganizationalUnit(
174                                getCms(),
175                                orgunit.getParentFqn()).getDescription(getLocale())
176                                + " ("
177                                + CmsOrganizationalUnit.SEPARATOR
178                                + orgunit.getParentFqn()
179                                + ")");
180                    }
181                } else {
182                    m_orgUnitBean.setParentOu(orgunit.getName());
183                    m_orgUnitBean.setParentOuDesc(
184                        orgunit.getDescription(getLocale())
185                            + " ("
186                            + CmsOrganizationalUnit.SEPARATOR
187                            + orgunit.getName()
188                            + ")");
189                }
190                List<CmsResource> resources = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit(
191                    getCms(),
192                    orgunit.getName());
193                setResourcesInBean(m_orgUnitBean, resources);
194            } else {
195                // this is not the initial call, get the ou object from session
196                m_orgUnitBean = (CmsOrgUnitBean)getDialogObject();
197                // test
198                m_orgUnitBean.getName();
199            }
200        } catch (Exception e) {
201            // create a new ou object
202            m_orgUnitBean = new CmsOrgUnitBean();
203            m_orgUnitBean.setParentOu(getParamOufqn());
204        }
205    }
206
207    /**
208     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
209     */
210    @Override
211    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
212
213        // initialize parameters and dialog actions in super implementation
214        super.initWorkplaceRequestValues(settings, request);
215
216        // save the current state of the ou (may be changed because of the widget values)
217        setDialogObject(m_orgUnitBean);
218    }
219
220    /**
221     * Checks if the new organizational unit dialog has to be displayed.<p>
222     *
223     * @return <code>true</code> if the new organizational unit dialog has to be displayed
224     */
225    protected boolean isNewOrgUnit() {
226
227        return getCurrentToolPath().endsWith("/orgunit/mgmt/new");
228    }
229}