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.workflow;
029
030import org.opencms.ade.publish.shared.CmsPublishResourceInfo;
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.util.CmsUUID;
037
038import java.util.Arrays;
039import java.util.List;
040import java.util.Set;
041
042import org.apache.commons.logging.Log;
043
044import com.google.common.base.Function;
045import com.google.common.base.Predicate;
046import com.google.common.cache.CacheBuilder;
047import com.google.common.cache.CacheLoader;
048import com.google.common.cache.LoadingCache;
049import com.google.common.collect.Sets;
050
051/**
052 * Publish resource formatter for the extended workflow manager.<p>
053 *
054 * This class handles warnings differently from the default implementation, since we don't need publish permissions for the release
055 * workflow, but need to check whether resources are already in a workflow.<p>
056 */
057public class CmsExtendedPublishResourceFormatter extends CmsDefaultPublishResourceFormatter {
058
059    /**
060     * Rule which checks whether a resource is already in a workflow.<p>
061     */
062    class ExcludeAlreadyInWorkflow implements I_PublishResourceValidator {
063
064        /**
065         * @see org.opencms.workflow.CmsDefaultPublishResourceFormatter.I_PublishResourceValidator#findInvalidResources(java.util.Set)
066         */
067        public Set<CmsResource> findInvalidResources(Set<CmsResource> input) {
068
069            Set<CmsResource> result = Sets.newHashSet();
070            CmsUUID optionsProject = m_options.getProjectId();
071            for (CmsResource resource : input) {
072                CmsUUID projectId = resource.getProjectLastModified();
073                if (isWorkflowProject(projectId) && ((optionsProject == null) || !projectId.equals(optionsProject))) {
074                    result.add(resource);
075                }
076            }
077            return result;
078        }
079
080        /**
081         * @see org.opencms.workflow.CmsDefaultPublishResourceFormatter.I_PublishResourceValidator#getInfoForResource(org.opencms.file.CmsResource)
082         */
083        public CmsPublishResourceInfo getInfoForResource(CmsResource resource) {
084
085            return new CmsPublishResourceInfo(
086                getMessage(Messages.GUI_ALREADY_IN_WORKFLOW_0),
087                CmsPublishResourceInfo.Type.WORKFLOW);
088        }
089    }
090
091    /** The logger instance for this class. */
092    private static final Log LOG = CmsLog.getLog(CmsExtendedPublishResourceFormatter.class);
093
094    /** Computing map which keeps track of which projects are workflow projects. */
095    protected LoadingCache<CmsUUID, Boolean> m_workflowProjectStatus = CacheBuilder.newBuilder().build(
096        CacheLoader.from(new Function<CmsUUID, Boolean>() {
097
098            public Boolean apply(CmsUUID projectId) {
099
100                try {
101                    CmsProject project = m_cms.readProject(projectId);
102                    return Boolean.valueOf(project.isWorkflowProject());
103                } catch (CmsException e) {
104                    getLog().warn(e.getLocalizedMessage(), e);
105                    return Boolean.FALSE;
106                }
107            }
108
109        }));
110
111    /** Flag which determines whether the resources should be formatted for the 'release' workflow. */
112    private boolean m_isRelease;
113
114    /**
115     * Constructor.<p>
116     *
117     * @param cms the CMS context to use
118     */
119    public CmsExtendedPublishResourceFormatter(CmsObject cms) {
120
121        super(cms);
122
123    }
124
125    /**
126     * Gets the logger for this class.<p>
127     *
128     * @return the logger for this class
129     */
130    public static Log getLog() {
131
132        return LOG;
133    }
134
135    /**
136     * Sets the 'release' mode.<p>
137     *
138     * @param release true if the resources should be formatted for the 'release' workflow
139     */
140    public void setRelease(boolean release) {
141
142        m_isRelease = release;
143    }
144
145    /**
146     * Gets a message from the message bundle.<p>
147     *
148     * @param key the message key
149     * @param args the message parameters
150     *
151     * @return the message from the message bundle
152     */
153    protected String getMessage(String key, String... args) {
154
155        return Messages.get().getBundle(m_cms.getRequestContext().getLocale()).key(key, args);
156    }
157
158    /**
159     * @see org.opencms.workflow.CmsDefaultPublishResourceFormatter#getResourceMapFilter()
160     */
161    @Override
162    protected Predicate<CmsResource> getResourceMapFilter() {
163
164        // in 'release' mode, we don't want to filter, otherwise we use the filter from the parent class
165        if (m_isRelease) {
166            return null;
167        } else {
168            return super.getResourceMapFilter();
169        }
170    }
171
172    /**
173     * @see org.opencms.workflow.CmsDefaultPublishResourceFormatter#getValidators()
174     */
175    @Override
176    protected List<I_PublishResourceValidator> getValidators() {
177
178        if (m_isRelease) {
179            return Arrays.asList(
180                new CmsDefaultPublishResourceFormatter.AlreadyPublishedValidator(),
181                new CmsDefaultPublishResourceFormatter.BlockingLockedValidator(),
182                new ExcludeAlreadyInWorkflow());
183        } else {
184            return Arrays.asList(
185                new CmsDefaultPublishResourceFormatter.AlreadyPublishedValidator(),
186                new CmsDefaultPublishResourceFormatter.NoPermissionsValidator(),
187                new CmsDefaultPublishResourceFormatter.BlockingLockedValidator(),
188                new ExcludeAlreadyInWorkflow());
189        }
190    }
191
192    /**
193     * Checks whether the project with the given id is a workflow project.<p>
194     *
195     * @param projectId the project id
196     *
197     * @return true if the project with the given id is a workflow project
198     */
199    protected boolean isWorkflowProject(CmsUUID projectId) {
200
201        try {
202            return m_workflowProjectStatus.get(projectId).booleanValue();
203        } catch (Exception e) {
204            LOG.error(e.getLocalizedMessage(), e);
205            return false;
206        }
207    }
208
209}