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.searchindex.sourcesearch;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsUUID;
036import org.opencms.workplace.explorer.CmsResourceUtil;
037import org.opencms.workplace.list.A_CmsListResourceCollector;
038import org.opencms.workplace.list.CmsListItem;
039
040import java.util.ArrayList;
041import java.util.Iterator;
042import java.util.List;
043import java.util.Map;
044
045import org.apache.commons.logging.Log;
046
047/**
048 * Collector for {@link org.opencms.file.CmsResource} resources to do source search in.<p>
049 *
050 * @since 7.5.3
051 */
052public class CmsSourceSearchCollector extends A_CmsListResourceCollector {
053
054    /** Parameter of the default collector name. */
055    public static final String COLLECTOR_NAME = "sourcesearchresources";
056
057    /** The log object for this class. */
058    private static final Log LOG = CmsLog.getLog(CmsSourceSearchCollector.class);
059
060    /** The source search files dialog. */
061    private CmsSourceSearchFilesDialog m_wp;
062
063    /**
064     * Constructor, creates a new instance.<p>
065     *
066     * @param wp the workplace object
067     */
068    public CmsSourceSearchCollector(CmsSourceSearchFilesDialog wp) {
069
070        super(wp);
071        m_wp = wp;
072    }
073
074    /**
075     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
076     */
077    public List<String> getCollectorNames() {
078
079        List<String> names = new ArrayList<String>();
080        names.add(COLLECTOR_NAME);
081        return names;
082    }
083
084    /**
085     * Returns the resource for the given item.<p>
086     *
087     * @param cms the cms object
088     * @param item the item
089     *
090     * @return the resource
091     */
092    @Override
093    public CmsResource getResource(CmsObject cms, CmsListItem item) {
094
095        CmsResource res = null;
096
097        CmsUUID id = new CmsUUID(item.getId());
098        if (!id.isNullUUID()) {
099            try {
100                res = cms.readResource(id, CmsResourceFilter.ALL);
101            } catch (CmsException e) {
102                // should never happen
103                if (LOG.isErrorEnabled()) {
104                    LOG.error(e.getLocalizedMessage(), e);
105                }
106            }
107        }
108
109        return res;
110    }
111
112    /**
113     * @see org.opencms.workplace.list.A_CmsListResourceCollector#getResources(org.opencms.file.CmsObject, java.util.Map)
114     */
115    @SuppressWarnings("rawtypes")
116    @Override
117    public List<CmsResource> getResources(CmsObject cms, Map params) throws CmsException {
118
119        ArrayList<CmsResource> files = new ArrayList<CmsResource>();
120        // read the files again, because they can be changed
121        if (m_wp.getFiles() != null) {
122            Iterator<CmsResource> iter = m_wp.getFiles().iterator();
123            while (iter.hasNext()) {
124                CmsResource oldResource = iter.next();
125                CmsResource newResource = cms.readResource(cms.getSitePath(oldResource));
126                files.add(newResource);
127            }
128        }
129        return files;
130    }
131
132    /**
133     * @see org.opencms.workplace.list.A_CmsListResourceCollector#setAdditionalColumns(org.opencms.workplace.list.CmsListItem, org.opencms.workplace.explorer.CmsResourceUtil)
134     */
135    @Override
136    protected void setAdditionalColumns(CmsListItem item, CmsResourceUtil resUtil) {
137
138        // no-op
139    }
140}