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.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.OpenCms;
036import org.opencms.workplace.list.CmsListColumnAlignEnum;
037import org.opencms.workplace.list.CmsListColumnDefinition;
038import org.opencms.workplace.list.CmsListDefaultAction;
039import org.opencms.workplace.list.CmsListDirectAction;
040import org.opencms.workplace.list.CmsListItem;
041import org.opencms.workplace.list.CmsListMetadata;
042import org.opencms.workplace.list.CmsListMultiAction;
043
044import java.util.ArrayList;
045import java.util.HashSet;
046import java.util.Iterator;
047import java.util.List;
048import java.util.Set;
049
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.servlet.jsp.PageContext;
053
054/**
055 * Not organizational unit users view.<p>
056 *
057 * @since 6.5.6
058 */
059public class CmsNotOrgUnitUsersList extends A_CmsOrgUnitUsersList {
060
061    /** list action id constant. */
062    public static final String LIST_ACTION_ADD = "aa";
063
064    /** list action id constant. */
065    public static final String LIST_DEFACTION_ADD = "da";
066
067    /** list id constant. */
068    public static final String LIST_ID = "lnouu";
069
070    /** list action id constant. */
071    public static final String LIST_MACTION_ADD = "ma";
072
073    /** a set of action id's to use for adding. */
074    protected static Set<String> m_addActionIds = new HashSet<String>();
075
076    /**
077     * Public constructor.<p>
078     *
079     * @param jsp an initialized JSP action element
080     */
081    public CmsNotOrgUnitUsersList(CmsJspActionElement jsp) {
082
083        this(jsp, LIST_ID);
084    }
085
086    /**
087     * Public constructor with JSP variables.<p>
088     *
089     * @param context the JSP page context
090     * @param req the JSP request
091     * @param res the JSP response
092     */
093    public CmsNotOrgUnitUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
094
095        this(new CmsJspActionElement(context, req, res));
096    }
097
098    /**
099     * Protected constructor.<p>
100     * @param jsp an initialized JSP action element
101     * @param listId the id of the specialized list
102     */
103    protected CmsNotOrgUnitUsersList(CmsJspActionElement jsp, String listId) {
104
105        super(jsp, listId, Messages.get().container(Messages.GUI_NOTORGUNITUSERS_LIST_NAME_0), true);
106    }
107
108    /**
109     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
110     */
111    @Override
112    public void executeListMultiActions() throws CmsRuntimeException {
113
114        if (getParamListAction().equals(LIST_MACTION_ADD)) {
115            // execute the remove multiaction
116            try {
117                Iterator<CmsListItem> itItems = getSelectedItems().iterator();
118                while (itItems.hasNext()) {
119                    CmsListItem listItem = itItems.next();
120
121                    CmsUser user = getCms().readUser((String)listItem.get(LIST_COLUMN_LOGIN));
122                    List<CmsUser> currentUsers = OpenCms.getOrgUnitManager().getUsers(getCms(), getParamOufqn(), false);
123
124                    boolean inOrgUnit = false;
125                    Iterator<CmsUser> itCurrentUsers = currentUsers.iterator();
126                    while (itCurrentUsers.hasNext()) {
127                        CmsUser currentUser = itCurrentUsers.next();
128                        if (currentUser.getSimpleName().equals(user.getSimpleName())) {
129                            inOrgUnit = true;
130                        }
131                    }
132                    if (!inOrgUnit) {
133                        @SuppressWarnings("unchecked")
134                        List<CmsUser> ouUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
135                            A_CmsOrgUnitUsersList.ORGUNIT_USERS);
136                        if (ouUsers == null) {
137                            ouUsers = new ArrayList<CmsUser>();
138                        }
139                        ouUsers.add(user);
140                        setOuUsers(ouUsers);
141
142                        @SuppressWarnings("unchecked")
143                        List<CmsUser> notOuUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
144                            A_CmsOrgUnitUsersList.NOT_ORGUNIT_USERS);
145                        notOuUsers.remove(user);
146                        setNotOuUsers(notOuUsers);
147                    }
148                }
149            } catch (CmsException e) {
150                // noop
151            }
152        } else {
153            throwListUnsupportedActionException();
154        }
155        listSave();
156    }
157
158    /**
159     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
160     */
161    @Override
162    public void executeListSingleActions() throws CmsRuntimeException {
163
164        if (m_addActionIds.contains(getParamListAction())) {
165            CmsListItem listItem = getSelectedItem();
166            try {
167                CmsUser user = getCms().readUser((String)listItem.get(LIST_COLUMN_LOGIN));
168                @SuppressWarnings("unchecked")
169                List<CmsUser> ouUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
170                    A_CmsOrgUnitUsersList.ORGUNIT_USERS);
171                if (ouUsers == null) {
172                    ouUsers = new ArrayList<CmsUser>();
173                }
174                ouUsers.add(user);
175                setOuUsers(ouUsers);
176
177                @SuppressWarnings("unchecked")
178                List<CmsUser> notOuUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
179                    A_CmsOrgUnitUsersList.NOT_ORGUNIT_USERS);
180                notOuUsers.remove(user);
181                setNotOuUsers(notOuUsers);
182            } catch (CmsException e) {
183                // should never happen
184                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ADD_SELECTED_ORGUNITUSER_0), e);
185            }
186        } else {
187            throwListUnsupportedActionException();
188        }
189        listSave();
190    }
191
192    /**
193     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#getUsers()
194     */
195    @Override
196    protected List<CmsUser> getUsers() throws CmsException {
197
198        @SuppressWarnings("unchecked")
199        List<CmsUser> notOuUsers = (ArrayList<CmsUser>)getJsp().getRequest().getSession().getAttribute(
200            A_CmsOrgUnitUsersList.NOT_ORGUNIT_USERS);
201
202        if (notOuUsers == null) {
203            List<CmsUser> orgUnitsUser = OpenCms.getOrgUnitManager().getUsers(getCms(), getParamOufqn(), false);
204            List<CmsUser> notOrgUnitUsers = OpenCms.getRoleManager().getManageableUsers(getCms(), "", true);
205
206            notOrgUnitUsers.removeAll(orgUnitsUser);
207            setNotOuUsers(notOrgUnitUsers);
208        } else {
209            setNotOuUsers(notOuUsers);
210        }
211
212        return getNotOuUsers();
213    }
214
215    /**
216     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#setDefaultAction(org.opencms.workplace.list.CmsListColumnDefinition)
217     */
218    @Override
219    protected void setDefaultAction(CmsListColumnDefinition loginCol) {
220
221        // add add action
222        CmsListDefaultAction addAction = new CmsListDefaultAction(LIST_DEFACTION_ADD) {
223
224            /**
225             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getHelpText()
226             */
227            @Override
228            public CmsMessageContainer getHelpText() {
229
230                if (!isEnabled()) {
231                    return Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DISABLED_DELETE_HELP_0);
232                }
233                return super.getHelpText();
234            }
235
236            /**
237             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
238             */
239            @Override
240            public boolean isEnabled() {
241
242                if (getItem() != null) {
243                    try {
244                        String userName = getItem().get(LIST_COLUMN_NAME).toString();
245                        List<CmsUser> currentUsers = OpenCms.getOrgUnitManager().getUsers(
246                            getWp().getCms(),
247                            ((A_CmsOrgUnitUsersList)getWp()).getParamOufqn(),
248                            false);
249                        Iterator<CmsUser> itCurrentUsers = currentUsers.iterator();
250                        while (itCurrentUsers.hasNext()) {
251                            CmsUser user = itCurrentUsers.next();
252                            if (user.getSimpleName().equals(userName)) {
253                                return false;
254                            }
255                            if (((A_CmsOrgUnitUsersList)getWp()).getCms().getGroupsOfUser(
256                                getItem().get(LIST_COLUMN_LOGIN).toString(),
257                                false).size() > 0) {
258                                return false;
259                            }
260                        }
261                        return true;
262                    } catch (CmsException e) {
263                        return super.isVisible();
264                    }
265                }
266                return super.isVisible();
267            }
268        };
269        addAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_ADD_NAME_0));
270        addAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_ADD_HELP_0));
271        loginCol.addDefaultAction(addAction);
272        // keep the id
273        m_addActionIds.add(addAction.getId());
274    }
275
276    /**
277     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#setIconAction(org.opencms.workplace.list.CmsListColumnDefinition)
278     */
279    @Override
280    protected void setIconAction(CmsListColumnDefinition iconCol) {
281
282        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON) {
283
284            /**
285             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
286             */
287            @Override
288            public String getIconPath() {
289
290                return ((A_CmsOrgUnitUsersList)getWp()).getIconPath(getItem());
291            }
292        };
293        iconAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_AVAILABLE_NAME_0));
294        iconAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_AVAILABLE_HELP_0));
295        iconAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "user.png");
296        iconAction.setEnabled(false);
297        iconCol.addDirectAction(iconAction);
298    }
299
300    /**
301     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
302     */
303    @Override
304    protected void setMultiActions(CmsListMetadata metadata) {
305
306        // add add multi action
307        CmsListMultiAction addMultiAction = new CmsListMultiAction(LIST_MACTION_ADD);
308        addMultiAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_MACTION_ADD_NAME_0));
309        addMultiAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_MACTION_ADD_HELP_0));
310        addMultiAction.setIconPath(ICON_MULTI_ADD);
311        metadata.addMultiAction(addMultiAction);
312    }
313
314    /**
315     * @see org.opencms.workplace.tools.accounts.A_CmsOrgUnitUsersList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
316     */
317    @Override
318    protected void setStateActionCol(CmsListMetadata metadata) {
319
320        // create column for state change
321        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
322        stateCol.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_COLS_STATE_0));
323        stateCol.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_COLS_STATE_HELP_0));
324        stateCol.setWidth("20");
325        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
326        stateCol.setSorteable(false);
327        // add add action
328        CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_ADD) {
329
330            /**
331             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getHelpText()
332             */
333            @Override
334            public CmsMessageContainer getHelpText() {
335
336                if (!isEnabled()) {
337                    return Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DISABLED_DELETE_HELP_0);
338                }
339                return super.getHelpText();
340            }
341
342            /**
343             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
344             */
345            @Override
346            public boolean isEnabled() {
347
348                if (getItem() != null) {
349                    try {
350                        String userName = getItem().get(LIST_COLUMN_NAME).toString();
351                        List<CmsUser> currentUsers = OpenCms.getOrgUnitManager().getUsers(
352                            getWp().getCms(),
353                            ((A_CmsOrgUnitUsersList)getWp()).getParamOufqn(),
354                            false);
355                        Iterator<CmsUser> itCurrentUsers = currentUsers.iterator();
356                        while (itCurrentUsers.hasNext()) {
357                            CmsUser user = itCurrentUsers.next();
358                            if (user.getSimpleName().equals(userName)) {
359                                return false;
360                            }
361                            if (((A_CmsOrgUnitUsersList)getWp()).getCms().getGroupsOfUser(
362                                getItem().get(LIST_COLUMN_LOGIN).toString(),
363                                false).size() > 0) {
364                                return false;
365                            }
366                        }
367                        return true;
368                    } catch (CmsException e) {
369                        return super.isVisible();
370                    }
371                }
372                return super.isVisible();
373            }
374        };
375        stateAction.setName(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_ADD_NAME_0));
376        stateAction.setHelpText(Messages.get().container(Messages.GUI_ORGUNITUSERS_LIST_DEFACTION_ADD_HELP_0));
377        stateAction.setIconPath(ICON_ADD);
378        stateCol.addDirectAction(stateAction);
379        // add it to the list definition
380        metadata.addColumn(stateCol);
381        // keep the id
382        m_addActionIds.add(stateAction.getId());
383    }
384
385}