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.commons;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsRuntimeException;
033import org.opencms.relations.CmsCategory;
034import org.opencms.workplace.list.CmsListColumnAlignEnum;
035import org.opencms.workplace.list.CmsListColumnDefinition;
036import org.opencms.workplace.list.CmsListDirectAction;
037import org.opencms.workplace.list.CmsListItem;
038import org.opencms.workplace.list.CmsListMetadata;
039import org.opencms.workplace.list.I_CmsListFormatter;
040
041import java.util.Iterator;
042import java.util.List;
043import java.util.Locale;
044
045import javax.servlet.http.HttpServletRequest;
046import javax.servlet.http.HttpServletResponse;
047import javax.servlet.jsp.PageContext;
048
049/**
050 * Not resource categories view.<p>
051 *
052 * @since 6.9.2
053 */
054public class CmsNotResourceCategoriesList extends A_CmsResourceCategoriesList {
055
056    /** list action id constant. */
057    public static final String LIST_ACTION_ADD = "aa";
058
059    /** list id constant. */
060    public static final String LIST_ID = "lnrc";
061
062    /**
063     * Public constructor.<p>
064     *
065     * @param jsp an initialized JSP action element
066     */
067    public CmsNotResourceCategoriesList(CmsJspActionElement jsp) {
068
069        this(jsp, LIST_ID);
070    }
071
072    /**
073     * Public constructor with JSP variables.<p>
074     *
075     * @param context the JSP page context
076     * @param req the JSP request
077     * @param res the JSP response
078     */
079    public CmsNotResourceCategoriesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
080
081        this(new CmsJspActionElement(context, req, res));
082    }
083
084    /**
085     * Protected constructor.<p>
086     * @param jsp an initialized JSP action element
087     * @param listId the id of the specialized list
088     */
089    protected CmsNotResourceCategoriesList(CmsJspActionElement jsp, String listId) {
090
091        super(jsp, listId, Messages.get().container(Messages.GUI_NOTRESOURCECATEGORIES_LIST_NAME_0), true);
092    }
093
094    /**
095     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
096     */
097    @Override
098    public void executeListSingleActions() throws CmsRuntimeException {
099
100        if (getParamListAction().equals(LIST_ACTION_ADD)) {
101            try {
102                // lock resource if autolock is enabled
103                checkLock(getParamResource());
104
105                CmsListItem listItem = getSelectedItem();
106                getCategoryService().addResourceToCategory(getCms(), getParamResource(), listItem.getId());
107                getCategoryService().repairRelations(getCms(), getParamResource());
108            } catch (CmsException e) {
109                throw new CmsRuntimeException(e.getMessageContainer(), e);
110            }
111        } else {
112            throwListUnsupportedActionException();
113        }
114        listSave();
115    }
116
117    /**
118     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
119     */
120    @Override
121    protected String defaultActionHtmlStart() {
122
123        return getList().listJs() + dialogContentStart(getParamTitle());
124    }
125
126    /**
127     * @see org.opencms.workplace.commons.A_CmsResourceCategoriesList#getCategories()
128     */
129    @Override
130    protected List<CmsCategory> getCategories() throws CmsException {
131
132        List<CmsCategory> resourceRelations = getResourceCategories();
133        List<CmsCategory> result = getCategoryService().readCategories(
134            getJsp().getCmsObject(),
135            null,
136            true,
137            getParamResource());
138        Iterator<CmsCategory> itResourceRelations = resourceRelations.iterator();
139        while (itResourceRelations.hasNext()) {
140            CmsCategory category = itResourceRelations.next();
141            if (result.contains(category)
142                && resourceRelations.containsAll(
143                    getCategoryService().readCategories(
144                        getJsp().getCmsObject(),
145                        category.getPath(),
146                        true,
147                        getParamResource()))) {
148                result.remove(category);
149            }
150        }
151        return result;
152    }
153
154    /**
155     * @see org.opencms.workplace.commons.A_CmsResourceCategoriesList#setColumns(org.opencms.workplace.list.CmsListMetadata)
156     */
157    @Override
158    protected void setColumns(CmsListMetadata metadata) {
159
160        super.setColumns(metadata);
161
162        metadata.getColumnDefinition(LIST_COLUMN_NAME).setFormatter(new I_CmsListFormatter() {
163
164            /**
165             * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
166             */
167            @Override
168            public String format(Object data, Locale locale) {
169
170                CmsListColumnDefinition listColumnDefinition = getList().getMetadata().getColumnDefinition(
171                    LIST_COLUMN_STATE);
172                CmsListDirectAction stateAction = (CmsListDirectAction)listColumnDefinition.getDirectAction(
173                    LIST_ACTION_ADD);
174                if (!stateAction.isEnabled()) {
175                    StringBuffer ret = new StringBuffer();
176                    ret.append("<span style=\"color: graytext;\">");
177                    ret.append(data);
178                    ret.append("</span>");
179                    return ret.toString();
180                }
181                return (String)data;
182            }
183
184        });
185    }
186
187    /**
188     * @see org.opencms.workplace.commons.A_CmsResourceCategoriesList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
189     */
190    @Override
191    protected void setStateActionCol(CmsListMetadata metadata) {
192
193        // create column for state change
194        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
195        stateCol.setName(Messages.get().container(Messages.GUI_CATEGORIES_LIST_COLS_STATE_0));
196        stateCol.setHelpText(Messages.get().container(Messages.GUI_CATEGORIES_LIST_COLS_STATE_HELP_0));
197        stateCol.setWidth("20");
198        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
199        stateCol.setSorteable(false);
200        // add remove action
201        CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_ADD) {
202
203            /**
204             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isEnabled()
205             */
206            @Override
207            public boolean isEnabled() {
208
209                try {
210                    A_CmsResourceCategoriesList wp = (A_CmsResourceCategoriesList)getWp();
211                    if (wp.getResourceCategories().contains(
212                        wp.getCategoryService().readCategory(wp.getCms(), getItem().getId(), wp.getParamResource()))) {
213                        return false;
214                    }
215                } catch (CmsException e) {
216                    // ignore
217                }
218                return true;
219            }
220        };
221        stateAction.setName(Messages.get().container(Messages.GUI_CATEGORIES_LIST_DEFACTION_ADD_NAME_0));
222        stateAction.setHelpText(Messages.get().container(Messages.GUI_CATEGORIES_LIST_DEFACTION_ADD_HELP_0));
223        stateAction.setIconPath(ICON_ADD);
224        stateCol.addDirectAction(stateAction);
225        // add it to the list definition
226        metadata.addColumn(stateCol);
227    }
228}