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.CmsObject;
032import org.opencms.file.CmsProject;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsUUID;
038import org.opencms.workplace.CmsWorkplace;
039import org.opencms.workplace.explorer.CmsResourceUtil;
040import org.opencms.workplace.list.A_CmsListExplorerDialog;
041import org.opencms.workplace.list.A_CmsListResourceCollector;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.I_CmsListResourceCollector;
044
045import java.util.ArrayList;
046import java.util.Iterator;
047import java.util.List;
048import java.util.Map;
049
050import org.apache.commons.logging.Log;
051
052/**
053 * Collector for {@link org.opencms.file.CmsResource} objects from a project.<p>
054 *
055 * @since 6.1.0
056 */
057public class CmsProjectFilesCollector extends A_CmsListResourceCollector {
058
059    /** Parameter of the default collector name. */
060    public static final String COLLECTOR_NAME = "projectresources";
061
062    /** Project Parameter name constant. */
063    public static final String PARAM_PROJECT = "project";
064
065    /** Resource state Parameter name constant. */
066    public static final String PARAM_STATE = "state";
067
068    /** The log object for this class. */
069    private static final Log LOG = CmsLog.getLog(CmsProjectFilesCollector.class);
070
071    /**
072     * Constructor, creates a new instance.<p>
073     *
074     * @param wp the workplace object
075     * @param projectId the id of the project
076     * @param state the state of the resources to filter
077     */
078    public CmsProjectFilesCollector(A_CmsListExplorerDialog wp, CmsUUID projectId, CmsResourceState state) {
079
080        super(wp);
081        m_collectorParameter += I_CmsListResourceCollector.SEP_PARAM
082            + PARAM_STATE
083            + I_CmsListResourceCollector.SEP_KEYVAL
084            + state;
085        m_collectorParameter += I_CmsListResourceCollector.SEP_PARAM
086            + PARAM_PROJECT
087            + I_CmsListResourceCollector.SEP_KEYVAL
088            + projectId;
089    }
090
091    /**
092     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
093     */
094    public List getCollectorNames() {
095
096        List names = new ArrayList();
097        names.add(COLLECTOR_NAME);
098        return names;
099    }
100
101    /**
102     * @see org.opencms.workplace.list.A_CmsListResourceCollector#getResources(org.opencms.file.CmsObject, java.util.Map)
103     */
104    @Override
105    public List getResources(CmsObject cms, Map params) throws CmsException {
106
107        CmsUUID projectId = CmsProject.ONLINE_PROJECT_ID;
108        try {
109            projectId = new CmsUUID((String)params.get(PARAM_PROJECT));
110        } catch (Throwable e) {
111            if (LOG.isDebugEnabled()) {
112                LOG.debug(e.getLocalizedMessage(), e);
113            }
114        }
115        CmsResourceState state = CmsResource.STATE_KEEP;
116        try {
117            state = CmsResourceState.valueOf(Integer.parseInt((String)params.get(PARAM_STATE)));
118        } catch (Throwable e) {
119            if (LOG.isDebugEnabled()) {
120                LOG.debug(e.getLocalizedMessage(), e);
121            }
122        }
123
124        // show files in the selected project with the selected status
125        List resources = cms.readProjectView(projectId, state);
126
127        // remove not visible files
128        Iterator itRes = resources.iterator();
129        // dont's show resources that  are in a different site root
130        String siteRoot = cms.getRequestContext().getSiteRoot();
131        // this is not sufficient (startsWith) if one siteRoot is prefix of another as siteRoot ends without slash!
132        siteRoot += "/";
133        while (itRes.hasNext()) {
134            CmsResource resource = (CmsResource)itRes.next();
135            String rootPath = resource.getRootPath();
136            if (!rootPath.startsWith(siteRoot)
137                && !rootPath.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)
138                && !OpenCms.getSiteManager().startsWithShared(rootPath)) {
139                itRes.remove();
140            }
141        }
142        return resources;
143    }
144
145    /**
146     * @see org.opencms.workplace.list.A_CmsListResourceCollector#setAdditionalColumns(org.opencms.workplace.list.CmsListItem, org.opencms.workplace.explorer.CmsResourceUtil)
147     */
148    @Override
149    protected void setAdditionalColumns(CmsListItem item, CmsResourceUtil resUtil) {
150
151        // no-op
152    }
153}