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.CmsUser;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.main.OpenCms;
035import org.opencms.workplace.list.CmsListColumnAlignEnum;
036import org.opencms.workplace.list.CmsListColumnDefinition;
037import org.opencms.workplace.list.CmsListDefaultAction;
038import org.opencms.workplace.list.CmsListDirectAction;
039import org.opencms.workplace.list.CmsListItem;
040import org.opencms.workplace.list.CmsListMetadata;
041import org.opencms.workplace.list.CmsListMultiAction;
042
043import java.io.IOException;
044import java.util.ArrayList;
045import java.util.HashSet;
046import java.util.Iterator;
047import java.util.List;
048import java.util.Set;
049
050import javax.servlet.ServletException;
051import javax.servlet.http.HttpServletRequest;
052import javax.servlet.http.HttpServletResponse;
053import javax.servlet.jsp.JspException;
054import javax.servlet.jsp.PageContext;
055
056/**
057 * Organizational unit users view.<p>
058 *
059 * @since 6.5.6
060 */
061public class CmsOrgUnitUsersList extends A_CmsOrgUnitUsersList {
062
063    /** list action id constant. */
064    public static final String LIST_ACTION_REMOVE = "ar";
065
066    /** list action id constant. */
067    public static final String LIST_DEFACTION_REMOVE = "dr";
068
069    /** list id constant. */
070    public static final String LIST_ID = "louu";
071
072    /** list action id constant. */
073    public static final String LIST_MACTION_REMOVE = "mr";
074
075    /** a set of action id's to use for removing. */
076    protected static Set<String> m_removeActionIds = new HashSet<String>();
077
078    /**
079     * Public constructor.<p>
080     *
081     * @param jsp an initialized JSP action element
082     */
083    public CmsOrgUnitUsersList(CmsJspActionElement jsp) {
084
085        this(jsp, LIST_ID);
086    }
087
088    /**
089     * Public constructor with JSP variables.<p>
090     *
091     * @param context the JSP page context
092     * @param req the JSP request
093     * @param res the JSP response
094     */
095    public CmsOrgUnitUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
096
097        this(new CmsJspActionElement(context, req, res));
098    }
099
100    /**
101     * Protected constructor.<p>
102     * @param jsp an initialized JSP action element
103     * @param listId the id of the specialized list
104     */
105    protected CmsOrgUnitUsersList(CmsJspActionElement jsp, String listId) {
106
107        super(jsp, listId, Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_NAME_0), true);
108    }
109
110    /**
111     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
112     */
113    @Override
114    public void actionDialog() throws JspException, ServletException, IOException {
115
116        if (getAction() == ACTION_CANCEL) {
117            // ACTION: cancel button pressed
118            actionCloseDialog();
119            return;
120        }
121
122        if (getAction() == ACTION_DEFAULT) {
123
124            @SuppressWarnings("unchecked")
125            List<CmsUser> ouUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
126                A_CmsOrgUnitUsersList.ORGUNIT_USERS);
127            Iterator<CmsUser> itOuUsers = ouUsers.iterator();
128            while (itOuUsers.hasNext()) {
129                CmsUser user = itOuUsers.next();
130
131                try {
132                    OpenCms.getOrgUnitManager().setUsersOrganizationalUnit(getCms(), getParamOufqn(), user.getName());
133                } catch (CmsException e) {
134                    throw new JspException(e);
135                }
136            }
137            actionCloseDialog();
138            return;
139        }
140        super.actionDialog();
141        refreshList();
142    }
143
144    /**
145     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
146     */
147    @Override
148    public void executeListMultiActions() throws CmsRuntimeException {
149
150        if (getParamListAction().equals(LIST_MACTION_REMOVE)) {
151            // execute the remove multiaction
152            Iterator<CmsListItem> itItems = getSelectedItems().iterator();
153            while (itItems.hasNext()) {
154                CmsListItem listItem = itItems.next();
155                String userName = (String)listItem.get(LIST_COLUMN_LOGIN);
156                try {
157                    CmsUser user = getCms().readUser(userName);
158                    @SuppressWarnings("unchecked")
159                    List<CmsUser> ouUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
160                        A_CmsOrgUnitUsersList.ORGUNIT_USERS);
161                    if (ouUsers == null) {
162                        ouUsers = new ArrayList<CmsUser>();
163                    }
164                    ouUsers.remove(user);
165                    setOuUsers(ouUsers);
166
167                    @SuppressWarnings("unchecked")
168                    List<CmsUser> notOuUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
169                        A_CmsOrgUnitUsersList.NOT_ORGUNIT_USERS);
170                    notOuUsers.add(user);
171                    setNotOuUsers(notOuUsers);
172                } catch (CmsException e) {
173                    // noop
174                }
175            }
176        } else {
177            throwListUnsupportedActionException();
178        }
179        listSave();
180    }
181
182    /**
183     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
184     */
185    @Override
186    public void executeListSingleActions() throws CmsRuntimeException {
187
188        if (m_removeActionIds.contains(getParamListAction())) {
189            CmsListItem listItem = getSelectedItem();
190            try {
191
192                CmsUser user = getCms().readUser((String)listItem.get(LIST_COLUMN_LOGIN));
193                @SuppressWarnings("unchecked")
194                List<CmsUser> ouUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
195                    A_CmsOrgUnitUsersList.ORGUNIT_USERS);
196                if (ouUsers == null) {
197                    ouUsers = new ArrayList<CmsUser>();
198                }
199                ouUsers.remove(user);
200                setOuUsers(ouUsers);
201
202                @SuppressWarnings("unchecked")
203                List<CmsUser> notOuUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
204                    A_CmsOrgUnitUsersList.NOT_ORGUNIT_USERS);
205                notOuUsers.add(user);
206                setNotOuUsers(notOuUsers);
207            } catch (CmsException e) {
208                // should never happen
209                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_REMOVE_SELECTED_ORGUNITUSER_0), e);
210            }
211        } else {
212            throwListUnsupportedActionException();
213        }
214        listSave();
215    }
216
217    /**
218     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#getUsers()
219     */
220    @Override
221    protected List<CmsUser> getUsers() {
222
223        @SuppressWarnings("unchecked")
224        List<CmsUser> ouUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
225            A_CmsOrgUnitUsersList.ORGUNIT_USERS);
226
227        if (ouUsers == null) {
228            ouUsers = new ArrayList<CmsUser>();
229            setOuUsers(ouUsers);
230        } else {
231            setOuUsers(ouUsers);
232        }
233
234        return getOuUsers();
235    }
236
237    /**
238     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#setDefaultAction(org.opencms.workplace.list.CmsListColumnDefinition)
239     */
240    @Override
241    protected void setDefaultAction(CmsListColumnDefinition loginCol) {
242
243        // add default remove action
244        CmsListDefaultAction removeAction = new CmsListDefaultAction(LIST_DEFACTION_REMOVE);
245        removeAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_REMOVE_NAME_0));
246        removeAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_REMOVE_HELP_0));
247        loginCol.addDefaultAction(removeAction);
248        // keep the id
249        m_removeActionIds.add(removeAction.getId());
250    }
251
252    /**
253     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#setIconAction(org.opencms.workplace.list.CmsListColumnDefinition)
254     */
255    @Override
256    protected void setIconAction(CmsListColumnDefinition iconCol) {
257
258        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON) {
259
260            /**
261             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
262             */
263            @Override
264            public String getIconPath() {
265
266                return ((A_CmsOrgUnitUsersList)getWp()).getIconPath(getItem());
267            }
268        };
269        iconAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_INORGUNIT_NAME_0));
270        iconAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_INORGUNIT_HELP_0));
271        iconAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "user.png");
272        iconAction.setEnabled(false);
273        iconCol.addDirectAction(iconAction);
274    }
275
276    /**
277     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
278     */
279    @Override
280    protected void setMultiActions(CmsListMetadata metadata) {
281
282        // add remove multi action
283        CmsListMultiAction removeMultiAction = new CmsListMultiAction(LIST_MACTION_REMOVE);
284        removeMultiAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_MACTION_REMOVE_NAME_0));
285        removeMultiAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_MACTION_REMOVE_HELP_0));
286        removeMultiAction.setIconPath(ICON_MULTI_MINUS);
287        metadata.addMultiAction(removeMultiAction);
288    }
289
290    /**
291     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
292     */
293    @Override
294    protected void setStateActionCol(CmsListMetadata metadata) {
295
296        // create column for state change
297        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
298        stateCol.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_COLS_STATE_0));
299        stateCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_COLS_STATE_HELP_0));
300        stateCol.setWidth("20");
301        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
302        stateCol.setSorteable(false);
303        // add remove action
304        CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_REMOVE);
305        stateAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_REMOVE_NAME_0));
306        stateAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_REMOVE_HELP_0));
307        stateAction.setIconPath(ICON_MINUS);
308        stateCol.addDirectAction(stateAction);
309        // add it to the list definition
310        metadata.addColumn(stateCol);
311        // keep the id
312        m_removeActionIds.add(stateAction.getId());
313    }
314
315}