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.configuration;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsPermissionSet;
038import org.opencms.security.CmsRole;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.util.CmsUUID;
041import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
042import org.opencms.xml.content.CmsXmlContent;
043import org.opencms.xml.content.CmsXmlContentFactory;
044import org.opencms.xml.types.CmsXmlVarLinkValue;
045import org.opencms.xml.types.I_CmsXmlContentValue;
046
047import java.util.Comparator;
048import java.util.Locale;
049
050import org.apache.commons.logging.Log;
051
052import com.google.common.collect.ComparisonChain;
053import com.google.common.collect.Ordering;
054
055/**
056 * Represents a element view for the container page editor.<p>
057 */
058public class CmsElementView {
059
060    /**
061     * The element view comparator.<p>
062     */
063    public static class ElementViewComparator implements Comparator<CmsElementView> {
064
065        /**
066         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
067         */
068        public int compare(CmsElementView o1, CmsElementView o2) {
069
070            return ComparisonChain.start().compare(o1.getOrder(), o2.getOrder()).compare(
071                o1.m_title,
072                o2.m_title,
073                Ordering.natural().nullsLast()).result();
074        }
075    }
076
077    /** The default element view. */
078    public static final CmsElementView DEFAULT_ELEMENT_VIEW = new CmsElementView();
079
080    /** Default order if not configured. */
081    public static final int DEFAULT_ORDER = 1060;
082
083    /** The default element view title key. */
084    public static final String GUI_ELEMENT_VIEW_DEFAULT_TITLE_0 = "GUI_ELEMENT_VIEW_DEFAULT_TITLE_0";
085
086    /** The order node. */
087    public static final String N_ORDER = "Order";
088
089    /** The title node. */
090    public static final String N_TITLE = "Title";
091
092    /** The title key node. */
093    public static final String N_TITLE_KEY = "TitleKey";
094
095    /** Special value indicating that no view is selected (used for parent view selection). */
096    public static final String PARENT_NONE = "view://null";
097
098    /** The logger instance for this class. */
099    private static final Log LOG = CmsLog.getLog(CmsElementView.class);
100
101    /** The view title. */
102    String m_title;
103
104    /** The explorer type. */
105    private CmsExplorerTypeSettings m_explorerType;
106
107    /** Synthetic id for non-resource views. */
108    private CmsUUID m_id;
109
110    /** The order. */
111    private int m_order;
112
113    /** The parent view id. */
114    private CmsUUID m_parentViewId;
115
116    /** The view resource. */
117    private CmsResource m_resource;
118
119    /** The title localization key. */
120    private String m_titleKey;
121
122    /**
123     * Creates a new element view based on the given explorer type.<p>
124     *
125     * @param explorerType the explorer type
126     */
127    public CmsElementView(CmsExplorerTypeSettings explorerType) {
128
129        m_explorerType = explorerType;
130        m_id = getExplorerTypeViewId(explorerType.getName());
131        m_titleKey = explorerType.getKey();
132        m_order = Integer.valueOf(explorerType.getNewResourceOrder()).intValue();
133    }
134
135    /**
136     * Constructor.<p>
137     *
138     * @param cms the cms context
139     * @param resource the group resource
140     *
141     * @throws Exception  if parsing the resource fails
142     */
143    public CmsElementView(CmsObject cms, CmsResource resource)
144    throws Exception {
145
146        m_resource = resource;
147        init(cms);
148    }
149
150    /**
151     * Creates a new view with the given id, but initializes no other fields.<p>
152     *
153     * @param id the id
154     */
155    public CmsElementView(CmsUUID id) {
156
157        m_id = id;
158    }
159
160    /**
161     * Constructor for the default element view.<p>
162     */
163    private CmsElementView() {
164
165        // the default view
166        m_title = "Content elements";
167        m_titleKey = GUI_ELEMENT_VIEW_DEFAULT_TITLE_0;
168        m_order = 1050;
169    }
170
171    /**
172     * Helper method to compute the uuid for views based on explorer types.<p>
173     *
174     * @param typeName the explorer type name
175     * @return the element view id computed from the type name
176     */
177    public static CmsUUID getExplorerTypeViewId(String typeName) {
178
179        return CmsUUID.getConstantUUID("elementview-" + typeName);
180
181    }
182
183    /**
184     * Gets the explorer type settings.<p>
185     *
186     * @return the explorer type
187     */
188    public CmsExplorerTypeSettings getExplorerType() {
189
190        return m_explorerType;
191
192    }
193
194    /**
195     * Returns the element view id.<p>
196     *
197     * @return the group id
198     */
199    public CmsUUID getId() {
200
201        if (m_id != null) {
202            return m_id;
203        } else if (m_resource != null) {
204            return m_resource.getStructureId();
205        } else {
206            // only in case of the default element view
207            return CmsUUID.getNullUUID();
208        }
209    }
210
211    /**
212     * The order.<p>
213     *
214     * @return the order
215     */
216    public int getOrder() {
217
218        return m_order;
219    }
220
221    /**
222     * Gets the parent view id (null if there is no parent view).<p>
223     *
224     * @return the parent view id
225     */
226    public CmsUUID getParentViewId() {
227
228        return m_parentViewId;
229    }
230
231    /**
232     * Returns the element view resource.<p>
233     *
234     * @return the element view resource
235     */
236    public CmsResource getResource() {
237
238        return m_resource;
239    }
240
241    /**
242     * Returns the element view title.<p>
243     *
244     * @param cms the cms context
245     * @param locale the locale
246     *
247     * @return the title
248     */
249    public String getTitle(CmsObject cms, Locale locale) {
250
251        if (m_titleKey == null) {
252            return m_title;
253        } else {
254            return OpenCms.getWorkplaceManager().getMessages(locale).key(m_titleKey);
255        }
256    }
257
258    /**
259     * Checks whether the current user has permissions to use the element view.<p>
260     *
261     * @param cms the cms context
262     * @param folder used for permission checks for explorertype based views
263     *
264     * @return <code>true</code> if the current user has permissions to use the element view
265     **/
266    public boolean hasPermission(CmsObject cms, CmsResource folder) {
267
268        if ((m_explorerType != null) && (folder != null)) {
269            CmsPermissionSet permSet = m_explorerType.getAccess().getPermissions(cms, folder);
270            boolean result = permSet.requiresViewPermission();
271            return result;
272        }
273
274        try {
275            if (m_resource != null) {
276                return cms.hasPermissions(
277                    m_resource,
278                    CmsPermissionSet.ACCESS_VIEW,
279                    false,
280                    CmsResourceFilter.IGNORE_EXPIRATION.addRequireVisible());
281            } else {
282                return OpenCms.getRoleManager().hasRole(cms, CmsRole.ELEMENT_AUTHOR);
283            }
284
285        } catch (CmsException e) {
286            LOG.error(e.getLocalizedMessage(), e);
287        }
288        return false;
289    }
290
291    /**
292     * 'Other types' view, for everything that isn't assigned to any other view.<p>
293     *
294     * @return true if this is the 'other types' view
295     */
296    public boolean isOther() {
297
298        return (m_explorerType != null) && m_explorerType.getName().equals("view_other");
299    }
300
301    /**
302     * Parses the edit view resource.<p>
303     *
304     * @param cms the cms context
305     * @throws Exception if parsing the resource fails
306     */
307    @SuppressWarnings("null")
308    private void init(CmsObject cms) throws Exception {
309
310        CmsFile configFile = cms.readFile(m_resource);
311        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, configFile);
312        m_title = content.getValue(N_TITLE, CmsConfigurationReader.DEFAULT_LOCALE).getStringValue(cms);
313        I_CmsXmlContentValue titleKey = content.getValue(N_TITLE_KEY, CmsConfigurationReader.DEFAULT_LOCALE);
314        if ((titleKey != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(titleKey.getStringValue(cms))) {
315            m_titleKey = titleKey.getStringValue(cms);
316        }
317        I_CmsXmlContentValue orderVal = content.getValue(N_ORDER, CmsConfigurationReader.DEFAULT_LOCALE);
318        if (orderVal != null) {
319            try {
320                m_order = Integer.parseInt(orderVal.getStringValue(cms));
321            } catch (Exception e) {
322                LOG.error(e.getLocalizedMessage(), e);
323                m_order = DEFAULT_ORDER;
324            }
325        } else {
326            m_order = DEFAULT_ORDER;
327        }
328
329        I_CmsXmlContentValue parentView = content.getValue("Parent", CmsConfigurationReader.DEFAULT_LOCALE);
330        if (parentView != null) {
331            CmsUUID parentViewId = null;
332            try {
333                CmsXmlVarLinkValue elementViewValue = (CmsXmlVarLinkValue)parentView;
334                String stringValue = elementViewValue.getStringValue(cms);
335                if (CmsStringUtil.isEmpty(stringValue)) {
336                    parentViewId = CmsUUID.getNullUUID();
337                } else if (stringValue.equals(PARENT_NONE)) {
338                    parentViewId = null;
339                } else if (stringValue.startsWith(CmsConfigurationReader.VIEW_SCHEME)) {
340                    parentViewId = new CmsUUID(stringValue.substring(CmsConfigurationReader.VIEW_SCHEME.length()));
341                } else {
342                    parentViewId = elementViewValue.getLink(cms).getStructureId();
343                }
344            } catch (Exception e) {
345                LOG.error(e.getLocalizedMessage(), e);
346            }
347            m_parentViewId = parentViewId;
348        }
349
350    }
351}