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, 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.ade.publish;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.collectors.I_CmsResourceCollector;
034import org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038
039import java.util.List;
040import java.util.Set;
041
042import org.apache.commons.lang3.RandomStringUtils;
043import org.apache.commons.logging.Log;
044
045import com.google.common.collect.Sets;
046
047/**
048 * Helper class used to determine which resources from a collector list should be included in a publish list.<p>
049 */
050public class CmsCollectorPublishListHelper {
051
052    /** The CMS context being used by this class. */
053    private CmsObject m_cms;
054
055    /** The collector information. */
056    private I_CmsContentLoadCollectorInfo m_info;
057
058    /** Boolean constant. */
059    public static final boolean OFFLINE = false;
060
061    /** Boolean constant. */
062    public static final boolean ONLINE = true;
063
064    /** Logger instance for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsCollectorPublishListHelper.class);
066
067    /** The number of resources which should be fetched via the collector. */
068    private int m_collectorLimit;
069
070    /**
071     * Creates a new instance.<p>
072     *
073     * @param cms the CMS context to use
074     * @param collectorInfo the collector information
075     * @param collectorLimit the number of resources which should be fetched via the collector
076     */
077    public CmsCollectorPublishListHelper(
078        CmsObject cms,
079        I_CmsContentLoadCollectorInfo collectorInfo,
080        int collectorLimit) {
081
082        if (cms.getRequestContext().getCurrentProject().isOnlineProject()) {
083            throw new IllegalArgumentException("CmsObject must not be set to the Online project!");
084        }
085
086        m_cms = cms;
087        m_info = collectorInfo;
088        m_collectorLimit = collectorLimit;
089    }
090
091    /**
092     * Initializes a CmsObject.<p>
093     *
094     * @param online true if a CmsObject for the Online project should be returned
095     * @return the initialized CmsObject
096     *
097     * @throws CmsException if something goes wrong
098     */
099    public CmsObject getCmsObject(boolean online) throws CmsException {
100
101        return CmsPublishListHelper.adjustCmsObject(m_cms, online);
102    }
103
104    /**
105     * Gets the collector to use.<p>
106     *
107     * @return the collector to use
108     */
109    public I_CmsResourceCollector getCollector() {
110
111        return OpenCms.getResourceManager().getContentCollector(m_info.getCollectorName());
112    }
113
114    /**
115     * Gets the list to add to the publish list for the collector list.<p>
116     *
117     * @return the resources to add to the publish list
118     * @throws CmsException if something goes wrong
119     */
120    public Set<CmsResource> getPublishListFiles() throws CmsException {
121
122        String context = "[" + RandomStringUtils.randomAlphabetic(8) + "] ";
123
124        List<CmsResource> offlineResults = computeCollectorResults(OFFLINE);
125        if (LOG.isDebugEnabled()) {
126            LOG.debug(context + "Offline collector results for " + m_info + ": " + resourcesToString(offlineResults));
127        }
128        List<CmsResource> onlineResults = computeCollectorResults(ONLINE);
129        if (LOG.isDebugEnabled()) {
130            LOG.debug(context + "Online collector results for " + m_info + ": " + resourcesToString(onlineResults));
131        }
132
133        Set<CmsResource> result = Sets.newHashSet();
134        for (CmsResource offlineRes : offlineResults) {
135
136            if (!(offlineRes.getState().isUnchanged())) {
137                result.add(offlineRes);
138            }
139        }
140        Set<CmsResource> onlineAndNotOffline = Sets.newHashSet(onlineResults);
141        onlineAndNotOffline.removeAll(offlineResults);
142        for (CmsResource res : onlineAndNotOffline) {
143            try {
144                // Because the resources have state 'unchanged' in the Online project, we need to read them again in the Offline project
145                res = getCmsObject(OFFLINE).readResource(res.getStructureId(), CmsResourceFilter.ALL);
146                result.add(res);
147            } catch (CmsException e) {
148                LOG.error(e.getLocalizedMessage(), e);
149            }
150        }
151        result.addAll(onlineAndNotOffline);
152        if (LOG.isDebugEnabled()) {
153            LOG.debug(context + "Publish list contributions for " + m_info + ": " + resourcesToString(result));
154        }
155        return result;
156    }
157
158    /**
159     * Computes the collector results.<p>
160     *
161     * @param online true if the collector results for the Online project should be returned
162     * @return the collector results
163     *
164     * @throws CmsException if something goes wrong
165     */
166    protected List<CmsResource> computeCollectorResults(boolean online) throws CmsException {
167
168        CmsObject cms = getCmsObject(online);
169        I_CmsResourceCollector collector = getCollector();
170        List<CmsResource> collectorResult = collector.getResults(
171            cms,
172            m_info.getCollectorName(),
173            m_info.getCollectorParams(),
174            m_collectorLimit);
175        return collectorResult;
176    }
177
178    /**
179     * Helper method to generate a string representation for a collection of resources for debug purposes.<p>
180     *
181     * @param resources the resources
182     * @return a string representing the list of resources
183     */
184    private String resourcesToString(Iterable<CmsResource> resources) {
185
186        StringBuffer buffer = new StringBuffer();
187        boolean first = true;
188        buffer.append("[");
189        for (CmsResource res : resources) {
190            if (!first) {
191                buffer.append(", ");
192            }
193            first = false;
194            buffer.append(res.getRootPath());
195            buffer.append("!");
196            buffer.append(res.getState().getAbbreviation());
197        }
198        buffer.append("]");
199        return buffer.toString();
200    }
201}