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.jsp.CmsJspActionElement;
031import org.opencms.main.CmsException;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsOrganizationalUnit;
034import org.opencms.security.CmsRole;
035import org.opencms.workplace.CmsDialog;
036import org.opencms.workplace.list.CmsListItem;
037
038import java.util.ArrayList;
039import java.util.HashMap;
040import java.util.Iterator;
041import java.util.List;
042import java.util.Map;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import javax.servlet.jsp.PageContext;
047
048/**
049 * Sub organization units list.<p>
050 *
051 * @since 6.5.6
052 */
053public class CmsOrgUnitsSubList extends A_CmsOrgUnitsList {
054
055    /** list id constant. */
056    public static final String LIST_ID = "lsous";
057
058    /** Stores the value of the request parameter for the user id. */
059    private String m_paramOufqn;
060
061    /**
062     * Public constructor.<p>
063     *
064     * @param jsp an initialized JSP action element
065     */
066    public CmsOrgUnitsSubList(CmsJspActionElement jsp) {
067
068        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_SUBORGUNITS_LIST_NAME_0));
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 CmsOrgUnitsSubList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
079
080        this(new CmsJspActionElement(context, req, res));
081    }
082
083    /**
084     * Deletes the given organizational unit.<p>
085     *
086     * @throws Exception if something goes wrong
087     */
088    public void actionDelete() throws Exception {
089
090        OpenCms.getOrgUnitManager().deleteOrganizationalUnit(getCms(), getParamOufqn());
091        actionCloseDialog();
092    }
093
094    /**
095     * Deletes the given organizational unit.<p>
096     *
097     * @throws Exception if something goes wrong
098     */
099    public void actionParent() throws Exception {
100
101        String ouFqn = CmsOrganizationalUnit.getParentFqn(getParamOufqn());
102
103        Map<String, String[]> params = new HashMap<String, String[]>();
104        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, new String[] {ouFqn});
105        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
106        String toolPath = getCurrentToolPath().substring(0, getCurrentToolPath().lastIndexOf("/"));
107        getToolManager().jspForwardTool(this, toolPath, params);
108        actionCloseDialog();
109    }
110
111    /**
112     *
113     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtml()
114     */
115    @Override
116    public String defaultActionHtml() {
117
118        if ((getList() != null) && getList().getAllContent().isEmpty()) {
119            // TODO: check the need for this
120            refreshList();
121        }
122        StringBuffer result = new StringBuffer(2048);
123        result.append(defaultActionHtmlStart());
124        result.append(customHtmlStart());
125        try {
126            if (hasSubOUs()) {
127                result.append(defaultActionHtmlContent());
128            }
129        } catch (CmsException e) {
130            // noop
131        }
132        result.append(customHtmlEnd());
133        result.append(defaultActionHtmlEnd());
134        return result.toString();
135    }
136
137    /**
138     * Returns the organizational unit fqn parameter value.<p>
139     *
140     * @return the organizational unit fqn parameter value
141     */
142    public String getParamOufqn() {
143
144        return m_paramOufqn;
145    }
146
147    /**
148     * Checks if the user has more then one organizational unit to administrate.<p>
149     *
150     * @return true if the user has more then then one organizational unit to administrate
151     *         otherwise false
152     * @throws CmsException if the organizational units can not be read
153     */
154    public boolean hasSubOUs() throws CmsException {
155
156        List<CmsOrganizationalUnit> orgUnits = OpenCms.getOrgUnitManager().getOrganizationalUnits(
157            getCms(),
158            m_paramOufqn,
159            true);
160        if (orgUnits == null) {
161            return false;
162        }
163        if (orgUnits.size() < 1) {
164            return false;
165        }
166        return true;
167    }
168
169    /**
170     * Sets the organizational unit fqn parameter value.<p>
171     *
172     * @param ouFqn the organizational unit fqn parameter value
173     */
174    public void setParamOufqn(String ouFqn) {
175
176        if (ouFqn == null) {
177            ouFqn = "";
178        }
179        m_paramOufqn = ouFqn;
180    }
181
182    /**
183     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
184     */
185    @Override
186    protected List<CmsListItem> getListItems() throws CmsException {
187
188        List<CmsListItem> ret = new ArrayList<CmsListItem>();
189        List<CmsOrganizationalUnit> orgUnits = OpenCms.getOrgUnitManager().getOrganizationalUnits(
190            getCms(),
191            m_paramOufqn,
192            true);
193        Iterator<CmsOrganizationalUnit> itOrgUnits = orgUnits.iterator();
194        while (itOrgUnits.hasNext()) {
195            CmsOrganizationalUnit childOrgUnit = itOrgUnits.next();
196            CmsListItem item = getList().newItem(childOrgUnit.getName());
197            item.set(LIST_COLUMN_NAME, CmsOrganizationalUnit.SEPARATOR + childOrgUnit.getName());
198            item.set(LIST_COLUMN_DESCRIPTION, childOrgUnit.getDescription(getLocale()));
199            item.set(LIST_COLUMN_ADMIN, Boolean.valueOf(
200                OpenCms.getRoleManager().hasRole(getCms(), CmsRole.ADMINISTRATOR.forOrgUnit(childOrgUnit.getName()))));
201            item.set(LIST_COLUMN_WEBUSER, Boolean.valueOf(childOrgUnit.hasFlagWebuser()));
202            ret.add(item);
203        }
204        return ret;
205    }
206
207    /**
208     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
209     */
210    @Override
211    protected void validateParamaters() throws Exception {
212
213        // test the needed parameters
214        OpenCms.getRoleManager().checkRole(getCms(), CmsRole.ACCOUNT_MANAGER.forOrgUnit(getParamOufqn()));
215        OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), m_paramOufqn).getName();
216    }
217}