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.modules;
029
030import org.opencms.configuration.CmsConfigurationException;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsRuntimeException;
033import org.opencms.main.OpenCms;
034import org.opencms.module.CmsModule;
035import org.opencms.module.CmsModuleDependency;
036import org.opencms.security.CmsRoleViolationException;
037import org.opencms.workplace.list.A_CmsListDialog;
038import org.opencms.workplace.list.CmsListColumnAlignEnum;
039import org.opencms.workplace.list.CmsListColumnDefinition;
040import org.opencms.workplace.list.CmsListDefaultAction;
041import org.opencms.workplace.list.CmsListDirectAction;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.CmsListMetadata;
044import org.opencms.workplace.list.CmsListMultiAction;
045import org.opencms.workplace.list.CmsListOrderEnum;
046
047import java.io.IOException;
048import java.util.ArrayList;
049import java.util.HashMap;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Map;
053
054import javax.servlet.ServletException;
055import javax.servlet.http.HttpServletRequest;
056import javax.servlet.http.HttpServletResponse;
057import javax.servlet.jsp.PageContext;
058
059/**
060 * Module dependencies view.<p>
061 *
062 * @since 6.0.0
063 */
064public class CmsModulesDependenciesList extends A_CmsListDialog {
065
066    /** List action delete. */
067    public static final String LIST_ACTION_DELETE = "ad";
068
069    /** list action id constant. */
070    public static final String LIST_ACTION_EDIT = "ae";
071
072    /** List column delete. */
073    public static final String LIST_COLUMN_DELETE = "cd";
074
075    /** List column edit. */
076    public static final String LIST_COLUMN_EDIT = "ce";
077
078    /** List column name. */
079    public static final String LIST_COLUMN_NAME = "cn";
080
081    /** List column version. */
082    public static final String LIST_COLUMN_VERSION = "cv";
083
084    /** list action id constant. */
085    public static final String LIST_DEFACTION_EDIT = "de";
086
087    /** list id constant. */
088    public static final String LIST_ID = "lmd";
089
090    /** List action multi delete. */
091    public static final String LIST_MACTION_DELETE = "md";
092
093    /** Dependency parameter. */
094    public static final String PARAM_DEPENDENCY = "dependency";
095
096    /** Module parameter. */
097    public static final String PARAM_MODULE = "module";
098
099    /** Path to the list buttons. */
100    public static final String PATH_BUTTONS = "tools/modules/buttons/";
101
102    /** Modulename. */
103    private String m_paramModule;
104
105    /**
106     * Public constructor.<p>
107     *
108     * @param jsp an initialized JSP action element
109     */
110    public CmsModulesDependenciesList(CmsJspActionElement jsp) {
111
112        super(
113            jsp,
114            LIST_ID,
115            Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_NAME_0),
116            LIST_COLUMN_NAME,
117            CmsListOrderEnum.ORDER_ASCENDING,
118            null);
119    }
120
121    /**
122     * Public constructor with JSP variables.<p>
123     *
124     * @param context the JSP page context
125     * @param req the JSP request
126     * @param res the JSP response
127     */
128    public CmsModulesDependenciesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
129
130        this(new CmsJspActionElement(context, req, res));
131    }
132
133    /**
134     * This method should handle every defined list multi action,
135     * by comparing <code>{@link #getParamListAction()}</code> with the id
136     * of the action to execute.<p>
137     */
138    @Override
139    public void executeListMultiActions() {
140
141        if (getParamListAction().equals(LIST_MACTION_DELETE)) {
142            String moduleName = getParamModule();
143            // execute the delete multiaction
144            Iterator itItems = getSelectedItems().iterator();
145
146            while (itItems.hasNext()) {
147                CmsModule module = (CmsModule)OpenCms.getModuleManager().getModule(moduleName).clone();
148                CmsListItem listItem = (CmsListItem)itItems.next();
149                String dependencyName = listItem.getId();
150                deleteDependency(module, dependencyName);
151            }
152        }
153        // refresh the list
154        Map objects = (Map)getSettings().getListObject();
155        if (objects != null) {
156            objects.remove(CmsModulesList.class.getName());
157        }
158        listSave();
159
160    }
161
162    /**
163     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
164     */
165    @Override
166    public void executeListSingleActions() throws IOException, ServletException {
167
168        String moduleName = getParamModule();
169        String dependencyName = getSelectedItem().getId();
170
171        Map params = new HashMap();
172        params.put(PARAM_MODULE, moduleName);
173        params.put(PARAM_DEPENDENCY, dependencyName);
174
175        if (getParamListAction().equals(LIST_ACTION_DELETE)) {
176            // delete a dependency
177            CmsModule module = (CmsModule)OpenCms.getModuleManager().getModule(moduleName).clone();
178            deleteDependency(module, dependencyName);
179        } else if (getParamListAction().equals(LIST_ACTION_EDIT) || getParamListAction().equals(LIST_DEFACTION_EDIT)) {
180            // edit a dependency from the list
181            params.put(PARAM_ACTION, DIALOG_INITIAL);
182            getToolManager().jspForwardTool(this, "/modules/edit/dependencies/edit", params);
183        }
184        // refresh the list
185        Map objects = (Map)getSettings().getListObject();
186        if (objects != null) {
187            objects.remove(CmsModulesList.class.getName());
188        }
189        listSave();
190    }
191
192    /**
193     * Gets the module parameter.<p>
194     *
195     * @return the module parameter
196     */
197    public String getParamModule() {
198
199        return m_paramModule;
200    }
201
202    /**
203     * Sets the module parameter.<p>
204     * @param paramModule the module parameter
205     */
206    public void setParamModule(String paramModule) {
207
208        m_paramModule = paramModule;
209    }
210
211    /**
212     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
213     */
214    @Override
215    protected void fillDetails(String detailId) {
216
217        // noop
218    }
219
220    /**
221     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
222     */
223    @Override
224    protected List getListItems() {
225
226        List ret = new ArrayList();
227
228        String moduleName = getParamModule();
229        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
230        // get dependencies
231        List dependencies = module.getDependencies();
232        Iterator i = dependencies.iterator();
233        while (i.hasNext()) {
234            CmsModuleDependency dependency = (CmsModuleDependency)i.next();
235            CmsListItem item = getList().newItem(dependency.getName());
236            // name
237            item.set(LIST_COLUMN_NAME, dependency.getName());
238            // version
239            item.set(LIST_COLUMN_VERSION, dependency.getVersion());
240
241            ret.add(item);
242        }
243        return ret;
244    }
245
246    /**
247     * @see org.opencms.workplace.CmsWorkplace#initMessages()
248     */
249    @Override
250    protected void initMessages() {
251
252        // add specific dialog resource bundle
253        addMessages(Messages.get().getBundleName());
254        // add default resource bundles
255        super.initMessages();
256    }
257
258    /**
259     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
260     */
261    @Override
262    protected void setColumns(CmsListMetadata metadata) {
263
264        //add column for edit action
265        CmsListColumnDefinition editCol = new CmsListColumnDefinition(LIST_COLUMN_EDIT);
266        editCol.setName(Messages.get().container(Messages.GUI_MODULES_LIST_COLS_EDIT_0));
267        editCol.setWidth("20");
268        editCol.setSorteable(false);
269        editCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
270        // add the edit action
271        CmsListDirectAction editColAction = new CmsListDirectAction(LIST_ACTION_EDIT);
272        editColAction.setName(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_EDIT_NAME_0));
273        editColAction.setHelpText(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_EDIT_HELP_0));
274        editColAction.setIconPath(PATH_BUTTONS + "module_dependencies.png");
275        editColAction.setEnabled(true);
276        editColAction.setConfirmationMessage(null);
277        editCol.addDirectAction(editColAction);
278        metadata.addColumn(editCol);
279
280        // add column for delete action
281        CmsListColumnDefinition delCol = new CmsListColumnDefinition(LIST_COLUMN_DELETE);
282        delCol.setName(Messages.get().container(Messages.GUI_MODULES_LIST_COLS_DELETE_0));
283        delCol.setWidth("20");
284        delCol.setSorteable(false);
285        delCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
286        // direct action: delete module
287        CmsListDirectAction delDependency = new CmsListDirectAction(LIST_ACTION_DELETE);
288        delDependency.setName(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_DELETE_NAME_0));
289        delDependency.setConfirmationMessage(
290            Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_DELETE_CONF_0));
291        delDependency.setIconPath(ICON_DELETE);
292        delDependency.setEnabled(true);
293        delDependency.setHelpText(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_DELETE_HELP_0));
294        delCol.addDirectAction(delDependency);
295        metadata.addColumn(delCol);
296
297        // add column for name
298        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
299        nameCol.setName(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_COLS_NAME_0));
300        nameCol.setWidth("80%");
301        nameCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
302        // create default edit action for name column: edit dependency
303        CmsListDefaultAction nameColAction = new CmsListDefaultAction(LIST_DEFACTION_EDIT);
304        nameColAction.setName(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_OVERVIEW_NAME_0));
305        nameColAction.setIconPath(null);
306        nameColAction.setHelpText(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_OVERVIEW_HELP_0));
307        nameColAction.setEnabled(true);
308        nameColAction.setConfirmationMessage(null);
309        // set action for the name column
310        nameCol.addDefaultAction(nameColAction);
311        metadata.addColumn(nameCol);
312
313        // add column for version
314        CmsListColumnDefinition versionCol = new CmsListColumnDefinition(LIST_COLUMN_VERSION);
315        versionCol.setName(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_COLS_VERSION_0));
316        versionCol.setWidth("20%");
317        versionCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
318        metadata.addColumn(versionCol);
319    }
320
321    /**
322     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
323     */
324    @Override
325    protected void setIndependentActions(CmsListMetadata metadata) {
326
327        // noop
328    }
329
330    /**
331     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
332     */
333    @Override
334    protected void setMultiActions(CmsListMetadata metadata) {
335
336        // add the delete dependencies multi action
337        CmsListMultiAction deleteDependencies = new CmsListMultiAction(LIST_MACTION_DELETE);
338        deleteDependencies.setName(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_MDELETE_NAME_0));
339        deleteDependencies.setConfirmationMessage(
340            Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_MDELETE_CONF_0));
341        deleteDependencies.setIconPath(ICON_MULTI_DELETE);
342        deleteDependencies.setEnabled(true);
343        deleteDependencies.setHelpText(Messages.get().container(Messages.GUI_DEPENDENCIES_LIST_ACTION_MDELETE_HELP_0));
344        metadata.addMultiAction(deleteDependencies);
345    }
346
347    /**
348     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
349     */
350    @Override
351    protected void validateParamaters() throws Exception {
352
353        if (OpenCms.getModuleManager().getModule(getParamModule()) == null) {
354            // just throw a dummy exception here since getModule does not produce an exception when a
355            // module is not found
356            throw new Exception();
357        }
358    }
359
360    /**
361     * Deletes a module dependency from a module.<p>
362     *
363     * @param module the module to delete the dependency from
364     * @param dependencyName the name of the dependcency to delete
365     */
366    private void deleteDependency(CmsModule module, String dependencyName) {
367
368        List oldDependencies = module.getDependencies();
369        List newDependencies = new ArrayList();
370        Iterator i = oldDependencies.iterator();
371        while (i.hasNext()) {
372            CmsModuleDependency dep = (CmsModuleDependency)i.next();
373            if (!dep.getName().equals(dependencyName)) {
374                newDependencies.add(dep);
375            }
376        }
377        module.setDependencies(newDependencies);
378        // update the module information
379        try {
380            OpenCms.getModuleManager().updateModule(getCms(), module);
381        } catch (CmsConfigurationException ce) {
382            // should never happen
383            throw new CmsRuntimeException(
384                Messages.get().container(Messages.ERR_ACTION_DEPENDENCIES_DELETE_2, dependencyName, module.getName()),
385                ce);
386
387        } catch (CmsRoleViolationException re) {
388            throw new CmsRuntimeException(
389                Messages.get().container(Messages.ERR_ACTION_DEPENDENCIES_DELETE_2, dependencyName, module.getName()),
390                re);
391        }
392    }
393}