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