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.containerpage.CmsDetailOnlyContainerUtil;
031import org.opencms.ade.publish.shared.CmsProjectBean;
032import org.opencms.ade.publish.shared.CmsPublishOptions;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsProperty;
035import org.opencms.file.CmsPropertyDefinition;
036import org.opencms.file.CmsResource;
037import org.opencms.file.CmsResourceFilter;
038import org.opencms.file.collectors.I_CmsCollectorPublishListProvider;
039import org.opencms.gwt.shared.I_CmsCollectorInfoFactory;
040import org.opencms.gwt.shared.I_CmsContentLoadCollectorInfo;
041import org.opencms.main.CmsException;
042import org.opencms.main.CmsLog;
043import org.opencms.main.OpenCms;
044import org.opencms.util.CmsUUID;
045
046import java.util.HashSet;
047import java.util.List;
048import java.util.Locale;
049import java.util.Map;
050import java.util.Set;
051
052import org.apache.commons.logging.Log;
053
054import com.google.common.collect.Lists;
055import com.google.common.collect.Sets;
056import com.google.web.bindery.autobean.shared.AutoBean;
057import com.google.web.bindery.autobean.shared.AutoBeanCodex;
058import com.google.web.bindery.autobean.vm.AutoBeanFactorySource;
059
060/**
061 * Virtual project which includes the currently edited resource and all its related resources.
062 */
063public class CmsCurrentPageProject implements I_CmsVirtualProject {
064
065    /** The uuid of this virtual project. */
066    public static final CmsUUID ID = CmsUUID.getConstantUUID("currentpage");
067
068    /** The logger for this class. */
069    static final Log LOG = CmsLog.getLog(CmsCurrentPageProject.class);
070
071    /**
072     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectBean(org.opencms.file.CmsObject, java.util.Map)
073     */
074    public CmsProjectBean getProjectBean(CmsObject cms, Map<String, String> params) {
075
076        String pageId = params.get(CmsPublishOptions.PARAM_CONTAINERPAGE);
077        String elementId = params.get(CmsPublishOptions.PARAM_CONTENT);
078        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
079        String title = Messages.get().getBundle(locale).key(Messages.GUI_CURRENTPAGE_PROJECT_0);
080        CmsUUID structureIdForTitle;
081        if ((pageId == null) && (elementId == null)) {
082            return null;
083        } else {
084            structureIdForTitle = pageId != null ? new CmsUUID(pageId) : new CmsUUID(elementId);
085        }
086
087        CmsProjectBean bean = new CmsProjectBean(ID, 0, title, title);
088        bean.setRank(100);
089        bean.setDefaultGroupName("");
090        try {
091            CmsResource titleResource = cms.readResource(structureIdForTitle, CmsResourceFilter.IGNORE_EXPIRATION);
092            CmsProperty titleProp = cms.readPropertyObject(titleResource, CmsPropertyDefinition.PROPERTY_TITLE, true);
093            String rawName;
094            if (titleProp.isNullProperty()) {
095                rawName = cms.getSitePath(titleResource);
096            } else {
097                rawName = titleProp.getValue();
098            }
099            bean.setDefaultGroupName(Messages.get().getBundle(locale).key(Messages.GUI_PAGE_1, rawName));
100        } catch (Exception e) {
101            LOG.error(e.getLocalizedMessage(), e);
102        }
103        return bean;
104    }
105
106    /**
107     * @see org.opencms.ade.publish.I_CmsVirtualProject#getProjectId()
108     */
109    public CmsUUID getProjectId() {
110
111        return ID;
112    }
113
114    /**
115     * @see org.opencms.ade.publish.I_CmsVirtualProject#getRelatedResourceProvider(org.opencms.file.CmsObject, org.opencms.ade.publish.shared.CmsPublishOptions)
116     */
117    public I_CmsPublishRelatedResourceProvider getRelatedResourceProvider(
118        final CmsObject cmsObject,
119        final CmsPublishOptions options) {
120
121        return new I_CmsPublishRelatedResourceProvider() {
122
123            public Set<CmsResource> getAdditionalRelatedResources(CmsObject cms, CmsResource res) {
124
125                Map<String, String> params = options.getParameters();
126
127                String pageId = options.getParameters().get(CmsPublishOptions.PARAM_CONTAINERPAGE);
128                String detailId = options.getParameters().get(CmsPublishOptions.PARAM_DETAIL);
129
130                Set<CmsResource> result = Sets.newHashSet();
131
132                if (res.getStructureId().toString().equals(detailId)) {
133                    result.addAll(CmsDetailOnlyContainerUtil.getDetailOnlyResources(cms, res));
134                }
135                if (res.getStructureId().toString().equals(pageId)) {
136
137                    I_CmsCollectorInfoFactory collectorInfoFactory = AutoBeanFactorySource.create(
138                        I_CmsCollectorInfoFactory.class);
139                    for (Map.Entry<String, String> entry : params.entrySet()) {
140                        if (entry.getKey().startsWith(CmsPublishOptions.PARAM_COLLECTOR_INFO)) {
141                            try {
142                                AutoBean<I_CmsContentLoadCollectorInfo> autoBean = AutoBeanCodex.decode(
143                                    collectorInfoFactory,
144                                    I_CmsContentLoadCollectorInfo.class,
145                                    entry.getValue());
146                                String collectorName = autoBean.as().getCollectorName();
147                                I_CmsCollectorPublishListProvider publishListProvider = null;
148                                if (null != collectorName) { // registered collector
149                                    publishListProvider = OpenCms.getResourceManager().getContentCollector(
150                                        collectorName);
151                                }
152                                if (null == publishListProvider) { // unregistered collector
153                                    String collectorClassName = autoBean.as().getCollectorClass();
154                                    Class<?> collectorClass = Class.forName(collectorClassName);
155                                    publishListProvider = (I_CmsCollectorPublishListProvider)collectorClass.newInstance();
156                                }
157                                if (publishListProvider == null) {
158                                    continue;
159                                }
160                                result.addAll(publishListProvider.getPublishResources(cmsObject, autoBean.as()));
161                            } catch (Exception e) {
162                                LOG.error(e.getLocalizedMessage(), e);
163                            }
164                        }
165                    }
166
167                    String collectorItemsStr = options.getParameters().get(CmsPublishOptions.PARAM_COLLECTOR_ITEMS);
168                    if (collectorItemsStr != null) {
169                        for (String token : collectorItemsStr.split(",")) {
170                            try {
171                                if (CmsUUID.isValidUUID(token)) {
172                                    CmsResource collectorRes = cms.readResource(
173                                        new CmsUUID(token),
174                                        CmsResourceFilter.ALL);
175                                    if (!collectorRes.getState().isUnchanged()) {
176                                        result.add(collectorRes);
177                                    }
178                                }
179                            } catch (Exception e) {
180                                LOG.error(
181                                    "Error processing collector item " + token + ": " + e.getLocalizedMessage(),
182                                    e);
183                            }
184                        }
185                    }
186                }
187                return result;
188
189            }
190
191        };
192    }
193
194    /**
195     * @see org.opencms.ade.publish.I_CmsVirtualProject#getResources(org.opencms.file.CmsObject, java.util.Map, java.lang.String)
196     */
197    public List<CmsResource> getResources(CmsObject cms, Map<String, String> params, String workflowId) {
198
199        String containerpageId = params.get(CmsPublishOptions.PARAM_CONTAINERPAGE);
200        String elementId = params.get(CmsPublishOptions.PARAM_CONTENT);
201        String detailId = params.get(CmsPublishOptions.PARAM_DETAIL);
202        Set<CmsResource> resources = new HashSet<CmsResource>();
203        for (String id : new String[] {containerpageId, elementId, detailId}) {
204            if (CmsUUID.isValidUUID(id)) {
205                try {
206                    CmsResource resource = cms.readResource(new CmsUUID(id), CmsResourceFilter.ALL);
207                    resources.add(resource);
208                    CmsResource parent = cms.readParentFolder(resource.getStructureId());
209                    if (!parent.getState().isUnchanged()) {
210                        resources.add(parent);
211                    }
212                } catch (CmsException e) {
213                    LOG.error(e.getLocalizedMessage(), e);
214                }
215
216            }
217        }
218        return Lists.newArrayList(resources);
219    }
220
221    /**
222     * @see org.opencms.ade.publish.I_CmsVirtualProject#isAutoSelectable()
223     */
224    public boolean isAutoSelectable() {
225
226        return true;
227    }
228
229}