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 GmbH & Co. KG, 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.workplace.commons;
029
030import org.opencms.db.CmsResourceState;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.file.types.CmsResourceTypePlain;
035import org.opencms.main.CmsException;
036import org.opencms.main.CmsLog;
037import org.opencms.relations.CmsRelation;
038import org.opencms.relations.CmsRelationFilter;
039import org.opencms.relations.CmsRelationType;
040import org.opencms.util.CmsUUID;
041import org.opencms.workplace.explorer.CmsResourceUtil;
042import org.opencms.workplace.list.A_CmsListExplorerDialog;
043import org.opencms.workplace.list.A_CmsListResourceCollector;
044import org.opencms.workplace.list.CmsListItem;
045
046import java.util.ArrayList;
047import java.util.HashMap;
048import java.util.Iterator;
049import java.util.List;
050import java.util.Map;
051
052import org.apache.commons.logging.Log;
053
054/**
055 * Collector for resources with relations to a given resource.<p>
056 *
057 * @since 6.9.1
058 */
059public class CmsListResourceLinkRelationCollector extends A_CmsListResourceCollector {
060
061    /** The log object for this class. */
062    protected static final Log LOG = CmsLog.getLog(CmsListResourceLinkRelationCollector.class);
063
064    /** Parameter of the default collector name. */
065    private static final String COLLECTOR_NAME = "linkRelations";
066
067    /** Indicates if the current request shows the source resources for the relations are shown. */
068    private boolean m_isSource;
069
070    /** The current resource to get link relations for. */
071    private String m_resource;
072
073    /**
074     * Public constructor.<p>
075     *
076     * @param wp the current list explorer dialog
077     * @param resource the current resource to get link relations for
078     * @param isSource indicates if the current request shows the source resources for the relations are shown
079     */
080    public CmsListResourceLinkRelationCollector(A_CmsListExplorerDialog wp, String resource, boolean isSource) {
081
082        super(wp);
083        m_isSource = isSource;
084        m_resource = resource;
085    }
086
087    /**
088     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
089     */
090    public List<String> getCollectorNames() {
091
092        List<String> names = new ArrayList<String>();
093        names.add(COLLECTOR_NAME);
094        return names;
095    }
096
097    /**
098     * Returns the resource.<p>
099     *
100     * @return the resource
101     */
102    public String getResource() {
103
104        return m_resource;
105    }
106
107    /**
108     * @see org.opencms.workplace.list.A_CmsListResourceCollector#getResource(org.opencms.file.CmsObject, org.opencms.workplace.list.CmsListItem)
109     */
110    @Override
111    public CmsResource getResource(CmsObject cms, CmsListItem item) {
112
113        String itemId;
114        if (item.getId().startsWith(item.get(CmsResourceLinkRelationList.LIST_COLUMN_RELATION_TYPE) + "_")) {
115            itemId = item.getId().substring(item.getId().lastIndexOf("_") + 1);
116        } else {
117            itemId = item.getId();
118        }
119        CmsResource res = m_resCache.get(itemId);
120        if (res == null) {
121            CmsUUID id = new CmsUUID(item.getId());
122            if (!id.isNullUUID()) {
123                try {
124                    res = cms.readResource(id, CmsResourceFilter.ALL);
125                    m_resCache.put(itemId, res);
126                } catch (CmsException e) {
127                    // should never happen
128                    if (LOG.isErrorEnabled()) {
129                        LOG.error(e.getLocalizedMessage(), e);
130                    }
131                }
132            }
133        }
134        return res;
135    }
136
137    /**
138     * @see org.opencms.workplace.list.A_CmsListResourceCollector#getResources(org.opencms.file.CmsObject, java.util.Map)
139     */
140    @Override
141    public List<CmsResource> getResources(CmsObject cms, Map<String, String> params) {
142
143        List<CmsResource> allResources = new ArrayList<CmsResource>();
144        CmsRelationFilter filter = CmsRelationFilter.TARGETS;
145        if (isSource()) {
146            filter = CmsRelationFilter.SOURCES;
147        }
148        List<CmsRelation> relations = new ArrayList<CmsRelation>();
149        try {
150            relations = cms.getRelationsForResource(getResource(), filter);
151        } catch (CmsException e) {
152            if (LOG.isErrorEnabled()) {
153                LOG.error(e.getLocalizedMessage(getWp().getLocale()), e);
154            }
155        }
156        Map<CmsResource, List<CmsRelationType>> relationTypes = new HashMap<CmsResource, List<CmsRelationType>>();
157        List<String> brokenLinks = new ArrayList<String>();
158        Iterator<CmsRelation> itRelations = relations.iterator();
159        while (itRelations.hasNext()) {
160            CmsRelation relation = itRelations.next();
161            CmsResource resource = null;
162            try {
163                if (isSource()) {
164                    resource = relation.getSource(cms, CmsResourceFilter.ALL);
165                } else {
166                    resource = relation.getTarget(cms, CmsResourceFilter.ALL);
167                }
168            } catch (CmsException e) {
169                if (LOG.isWarnEnabled()) {
170                    LOG.warn(e.getLocalizedMessage(getWp().getLocale()), e);
171                }
172                resource = new CmsResource(
173                    new CmsUUID(),
174                    new CmsUUID(),
175                    relation.getTargetPath(),
176                    CmsResourceTypePlain.getStaticTypeId(),
177                    false,
178                    0,
179                    getWp().getJsp().getRequestContext().getCurrentProject().getUuid(),
180                    CmsResourceState.STATE_DELETED,
181                    0,
182                    getWp().getJsp().getRequestContext().getCurrentUser().getId(),
183                    0,
184                    getWp().getJsp().getRequestContext().getCurrentUser().getId(),
185                    0,
186                    0,
187                    0,
188                    0,
189                    0,
190                    0);
191                if (!brokenLinks.contains(resource)) {
192                    brokenLinks.add(
193                        relation.getType().getLocalizedName(getWp().getJsp().getRequestContext().getLocale())
194                            + "_"
195                            + resource.getStructureId());
196                }
197            }
198            allResources.add(resource);
199            if (relationTypes.containsKey(resource)) {
200                relationTypes.get(resource).add(relation.getType());
201            } else {
202                List<CmsRelationType> types = new ArrayList<CmsRelationType>();
203                types.add(relation.getType());
204                relationTypes.put(resource, types);
205            }
206        }
207        if (getWp() instanceof CmsResourceLinkRelationList) {
208            CmsResourceLinkRelationList wp = (CmsResourceLinkRelationList)getWp();
209            wp.setRelationTypes(relationTypes);
210            wp.setBrokenLinks(brokenLinks);
211        }
212        return allResources;
213    }
214
215    /**
216     * Returns the isSource.<p>
217     *
218     * @return the isSource
219     */
220    public boolean isSource() {
221
222        return m_isSource;
223    }
224
225    /**
226     * Sets the resource.<p>
227     *
228     * @param resource the resource to set
229     */
230    public void setResource(String resource) {
231
232        m_resource = resource;
233    }
234
235    /**
236     * Sets the isSource.<p>
237     *
238     * @param isSource the isSource to set
239     */
240    public void setSource(boolean isSource) {
241
242        m_isSource = isSource;
243    }
244
245    /**
246     * @see org.opencms.workplace.list.A_CmsListResourceCollector#setAdditionalColumns(org.opencms.workplace.list.CmsListItem, org.opencms.workplace.explorer.CmsResourceUtil)
247     */
248    @Override
249    protected void setAdditionalColumns(CmsListItem item, CmsResourceUtil resUtil) {
250
251        // noop
252    }
253}