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.ade.publish.shared.CmsProjectBean;
031import org.opencms.ade.publish.shared.CmsPublishOptions;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsResource;
034import org.opencms.file.CmsResourceFilter;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.main.OpenCms;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.util.CmsUUID;
040
041import java.util.ArrayList;
042import java.util.List;
043import java.util.Locale;
044import java.util.Map;
045import java.util.Set;
046
047import org.apache.commons.logging.Log;
048
049import com.google.common.collect.Sets;
050
051/**
052 *  Virtual project for 'direct publishing' of resources.<p>
053 *
054 *  This virtual project gets the names of the resources to publish from the publish parameter map.
055 *  If the 'add contents' mode is enabled (which is also determined from the publish parameters), the
056 *  contents of folders are added to the list of publish resources. This virtual project is only available
057 *  if any file names are passed via the
058 */
059public class CmsDirectPublishProject implements I_CmsVirtualProject {
060
061    /** The ID of this virtual project. */
062    public static final CmsUUID ID = CmsUUID.getConstantUUID("" + CmsDirectPublishProject.class);
063
064    /** The logger instance for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsDirectPublishProject.class);
066
067    /**
068     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectBean(org.opencms.file.CmsObject, java.util.Map)
069     */
070    public CmsProjectBean getProjectBean(CmsObject cms, Map<String, String> params) {
071
072        if (CmsStringUtil.isEmptyOrWhitespaceOnly(params.get(CmsPublishOptions.PARAM_FILES))) {
073            return null;
074        }
075
076        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
077        String name = Messages.get().getBundle(locale).key(Messages.GUI_PROJECT_DIRECT_PUBLISH_0);
078        CmsProjectBean bean = new CmsProjectBean(ID, 0, name, name);
079
080        bean.setRank(150);
081        bean.setDefaultGroupName(name);
082        return bean;
083    }
084
085    /**
086     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectId()
087     */
088    public CmsUUID getProjectId() {
089
090        return ID;
091    }
092
093    /**
094     * @see org.opencms.ade.publish.I_CmsVirtualProject#getRelatedResourceProvider(org.opencms.file.CmsObject, org.opencms.ade.publish.shared.CmsPublishOptions)
095     */
096    public I_CmsPublishRelatedResourceProvider getRelatedResourceProvider(
097        CmsObject cmsObject,
098        CmsPublishOptions options) {
099
100        return CmsDummyRelatedResourceProvider.INSTANCE;
101    }
102
103    /**
104     * @see org.opencms.ade.publish.I_CmsVirtualProject#getResources(org.opencms.file.CmsObject, java.util.Map, java.lang.String)
105     */
106    public List<CmsResource> getResources(CmsObject cms, Map<String, String> params, String workflowId)
107    throws CmsException {
108
109        Set<String> paths = getPaths(params);
110        boolean includeContents = shouldIncludeContents(params);
111        Set<CmsResource> result = Sets.newHashSet();
112        for (String path : paths) {
113            try {
114                result.add(cms.readResource(path, CmsResourceFilter.ALL));
115            } catch (CmsException e) {
116                LOG.error(e.getLocalizedMessage(), e);
117            }
118        }
119        if (includeContents) {
120            addSubResources(cms, result);
121        }
122        List<CmsResource> resultList = new ArrayList<CmsResource>();
123        for (CmsResource res : result) {
124            if (!res.getState().isUnchanged()) {
125                resultList.add(res);
126            }
127        }
128        return resultList;
129    }
130
131    /**
132     * @see org.opencms.ade.publish.I_CmsVirtualProject#isAutoSelectable()
133     */
134    public boolean isAutoSelectable() {
135
136        return true;
137    }
138
139    /**
140     * Adds contents of folders to a list of resources.<p>
141     *
142     * @param cms the CMS context to use
143     * @param resources the resource list to which to add the folder contents
144     * @throws CmsException if something goes wrong
145     */
146    protected void addSubResources(CmsObject cms, Set<CmsResource> resources) throws CmsException {
147
148        List<CmsResource> subResources = new ArrayList<CmsResource>();
149        CmsObject rootCms = OpenCms.initCmsObject(cms);
150        rootCms.getRequestContext().setSiteRoot("");
151        for (CmsResource res : resources) {
152            if (res.isFolder()) {
153                try {
154                    List<CmsResource> childrenOfCurrentResource = rootCms.readResources(
155                        res.getRootPath(),
156                        CmsResourceFilter.ALL,
157                        true);
158                    subResources.addAll(childrenOfCurrentResource);
159                } catch (CmsException e) {
160                    LOG.error(e.getLocalizedMessage(), e);
161                }
162            }
163        }
164        resources.addAll(subResources);
165    }
166
167    /**
168     * Returns true if the folder contents should be included.<p>
169     *
170     * @param params the publish parameters
171     * @return true if the folder contents should be included
172     */
173    protected boolean shouldIncludeContents(Map<String, String> params) {
174
175        String includeContentsStr = params.get(CmsPublishOptions.PARAM_INCLUDE_CONTENTS);
176        boolean includeContents = false;
177        try {
178            includeContents = Boolean.parseBoolean(includeContentsStr);
179        } catch (Exception e) {
180            // ignore; includeContents remains the default value
181        }
182        return includeContents;
183    }
184
185    /**
186     * Gets the set of site paths from the publish parameters.<p>
187     *
188     * @param params the publish parameters
189     *
190     * @return the set of site paths
191     */
192    private Set<String> getPaths(Map<String, String> params) {
193
194        Set<String> result = Sets.newHashSet();
195        String paths = params.get(CmsPublishOptions.PARAM_FILES);
196        if (paths != null) {
197            result.addAll(CmsStringUtil.splitAsList(paths, "|"));
198        }
199        return result;
200    }
201
202}