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.projects;
029
030import org.opencms.db.CmsResourceState;
031import org.opencms.file.CmsProject;
032import org.opencms.file.CmsResource;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.main.CmsException;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsUUID;
037import org.opencms.widgets.A_CmsWidget;
038import org.opencms.workplace.explorer.CmsExplorer;
039import org.opencms.workplace.list.A_CmsListExplorerDialog;
040import org.opencms.workplace.list.CmsHtmlList;
041import org.opencms.workplace.list.CmsListDropdownAction;
042import org.opencms.workplace.list.CmsListMetadata;
043import org.opencms.workplace.list.I_CmsListResourceCollector;
044import org.opencms.workplace.tools.CmsToolDialog;
045
046import java.util.HashMap;
047import java.util.Iterator;
048import java.util.Map;
049
050import javax.servlet.http.HttpServletRequest;
051import javax.servlet.http.HttpServletResponse;
052import javax.servlet.jsp.PageContext;
053
054/**
055 * Explorer dialog for the project files view.<p>
056 *
057 * @since 6.0.0
058 */
059public class CmsProjectFilesDialog extends A_CmsListExplorerDialog {
060
061    /** list independent action constant. */
062    public static final String LIST_IACTION_FILTER = "iaf";
063
064    /** list id constant. */
065    public static final String LIST_ID = "lpr";
066
067    /** Session attribute key for the stored project. */
068    public static final String SESSION_STORED_PROJECT = "CmsProjectFilesDialog_storedProject";
069
070    /** The internal collector instance. */
071    private I_CmsListResourceCollector m_collector;
072
073    /** Stores the value of the request parameter for the resource filter. */
074    private String m_filter;
075
076    /** Stores the value of the request parameter for the project id. */
077    private String m_paramProjectid;
078
079    /**
080     * Public constructor with JSP action element.<p>
081     *
082     * @param jsp an initialized JSP action element
083     */
084    public CmsProjectFilesDialog(CmsJspActionElement jsp) {
085
086        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_PROJECT_FILES_LIST_NAME_0));
087    }
088
089    /**
090     * Public constructor with JSP variables.<p>
091     *
092     * @param context the JSP page context
093     * @param req the JSP request
094     * @param res the JSP response
095     */
096    public CmsProjectFilesDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
097
098        this(new CmsJspActionElement(context, req, res));
099    }
100
101    /**
102     * @see org.opencms.workplace.list.A_CmsListDialog#executeListIndepActions()
103     */
104    @Override
105    public void executeListIndepActions() {
106
107        if (getParamListAction().equals(LIST_IACTION_FILTER)) {
108            // forward to the editor
109            getList().setCurrentPage(1);
110            m_collector = null;
111            refreshList();
112        } else {
113            super.executeListIndepActions();
114        }
115    }
116
117    /**
118     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
119     */
120    @Override
121    public void executeListMultiActions() {
122
123        throwListUnsupportedActionException();
124    }
125
126    /**
127     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
128     */
129    @Override
130    public void executeListSingleActions() {
131
132        throwListUnsupportedActionException();
133    }
134
135    /**
136     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getCollector()
137     */
138    @Override
139    public I_CmsListResourceCollector getCollector() {
140
141        if (m_collector == null) {
142            CmsUUID projectId = getProject().getUuid();
143            CmsResourceState state = CmsResource.STATE_KEEP;
144            CmsHtmlList list = getList();
145            if (list != null) {
146                if (getSettings().getCollector() != null) {
147                    getSettings().setCollector(null);
148                }
149            }
150            if (m_filter.equals("new")) {
151                state = CmsResource.STATE_NEW;
152            } else if (m_filter.equals("changed")) {
153                state = CmsResource.STATE_CHANGED;
154            } else if (m_filter.equals("deleted")) {
155                state = CmsResource.STATE_DELETED;
156            }
157            m_collector = new CmsProjectFilesCollector(this, projectId, state);
158        }
159        return m_collector;
160    }
161
162    /**
163     * @see org.opencms.workplace.list.A_CmsListDialog#getList()
164     */
165    @Override
166    public CmsHtmlList getList() {
167
168        CmsHtmlList list = super.getList();
169        // get parameter
170        m_filter = getJsp().getRequest().getParameter(LIST_IACTION_FILTER + CmsListDropdownAction.SUFFIX_PARAM);
171        CmsListDropdownAction listAction = null;
172        if (list != null) {
173            listAction = ((CmsListDropdownAction)list.getMetadata().getIndependentAction(LIST_IACTION_FILTER));
174            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_filter)) {
175                // if no param, get old value
176                m_filter = listAction.getSelection();
177            }
178        }
179        if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_filter)) {
180            m_filter = CmsProjectResourcesDisplayMode.ALL_CHANGES.getMode();
181        }
182        if (listAction != null) {
183            listAction.setSelection(m_filter);
184        }
185        return list;
186    }
187
188    /**
189     * Returns the project id parameter value.<p>
190     *
191     * @return the project id parameter value
192     */
193    public String getParamProjectid() {
194
195        return m_paramProjectid;
196    }
197
198    /**
199     * @see org.opencms.workplace.list.A_CmsListDialog#refreshList()
200     */
201    @Override
202    public synchronized void refreshList() {
203
204        if (LIST_IACTION_FILTER.equals(getParamListAction())) {
205            if (m_collector != null) {
206                // refresh only if really necessary
207                return;
208            }
209        }
210        super.refreshList();
211    }
212
213    /**
214     * Sets the project id parameter value.<p>
215     *
216     * @param projectId the project id parameter value
217     */
218    public void setParamProjectid(String projectId) {
219
220        m_paramProjectid = projectId;
221        getJsp().getRequest().getSession().setAttribute("LASTPRJ", projectId);
222    }
223
224    /**
225     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
226     */
227    @Override
228    protected void fillDetails(String detailId) {
229
230        // no-details
231    }
232
233    /**
234     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getAdditionalParametersForExplorerForward()
235     */
236    @Override
237    protected java.util.Map<String, String[]> getAdditionalParametersForExplorerForward() {
238
239        Map<String, String[]> result = new HashMap<String, String[]>();
240        result.put(
241            CmsExplorer.PARAMETER_CONTEXTMENUPARAMS,
242            new String[] {CmsToolDialog.PARAM_ADMIN_PROJECT + "=" + m_paramProjectid});
243        return result;
244    }
245
246    /**
247     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getProject()
248     */
249    @Override
250    protected CmsProject getProject() {
251
252        CmsUUID projectId = new CmsUUID(getParamProjectid());
253        try {
254            return getCms().readProject(projectId);
255        } catch (CmsException e) {
256            return super.getProject();
257        }
258    }
259
260    /**
261     * @see org.opencms.workplace.CmsWorkplace#initMessages()
262     */
263    @Override
264    protected void initMessages() {
265
266        // add specific dialog resource bundle
267        addMessages(Messages.get().getBundleName());
268        // add default resource bundles
269        super.initMessages();
270    }
271
272    /**
273     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
274     */
275    @Override
276    protected void setIndependentActions(CmsListMetadata metadata) {
277
278        CmsListDropdownAction filterAction = new CmsListDropdownAction(LIST_IACTION_FILTER);
279        filterAction.setName(Messages.get().container(Messages.GUI_PROJECT_FILES_FILTER_ACTION_NAME_0));
280        filterAction.setHelpText(Messages.get().container(Messages.GUI_PROJECT_FILES_FILTER_ACTION_HELP_0));
281        Iterator<?> it = CmsProjectResourcesDisplayMode.VALUES.iterator();
282        while (it.hasNext()) {
283            CmsProjectResourcesDisplayMode mode = (CmsProjectResourcesDisplayMode)it.next();
284            filterAction.addItem(mode.getMode(), Messages.get().container(A_CmsWidget.LABEL_PREFIX + mode.getMode()));
285        }
286        metadata.addIndependentAction(filterAction);
287        super.setIndependentActions(metadata);
288    }
289
290    /**
291     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
292     */
293    @Override
294    protected void setMultiActions(CmsListMetadata metadata) {
295
296        // no LMAs
297    }
298
299    /**
300     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
301     */
302    @Override
303    protected void validateParamaters() throws Exception {
304
305        try {
306            getCms().readProject(new CmsUUID(getParamProjectid()));
307            setStoredProject(getParamProjectid()); // doing this after the readProject call because now we know the id is valid
308        } catch (Exception e) {
309            Exception exceptionToRethrow = e;
310            String storedProject = getStoredProject();
311            boolean usingStoredProject = false;
312            if (storedProject != null) {
313                try {
314                    getCms().readProject(new CmsUUID(storedProject));
315                    m_paramProjectid = storedProject;
316                    usingStoredProject = true;
317                } catch (Exception e2) {
318                    exceptionToRethrow = e2;
319                }
320            }
321            if (!usingStoredProject) {
322                if (!getCms().getRequestContext().getCurrentProject().isOnlineProject()) {
323                    m_paramProjectid = getCms().getRequestContext().getCurrentProject().getUuid().toString();
324                } else {
325                    throw exceptionToRethrow;
326                }
327            }
328        }
329    }
330
331    /**
332     * Gets the stored project id from the session.<p>
333     *
334     * @return the stored project id
335     */
336    private String getStoredProject() {
337
338        return (String)getSession().getAttribute(SESSION_STORED_PROJECT);
339    }
340
341    /**
342     * Sets the stored project id.<p>
343     *
344     * @param project the project id to be stored
345     */
346    private void setStoredProject(String project) {
347
348        getSession().setAttribute(SESSION_STORED_PROJECT, project);
349    }
350}