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 052/** 053 * Represents a element view for the container page editor.<p> 054 */ 055public class CmsElementView { 056 057 /** 058 * The element view comparator.<p> 059 */ 060 public static class ElementViewComparator implements Comparator<CmsElementView> { 061 062 /** 063 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 064 */ 065 public int compare(CmsElementView o1, CmsElementView o2) { 066 067 int result; 068 if (o1.getOrder() == o2.getOrder()) { 069 result = o1.m_title.compareTo(o2.m_title); 070 } else { 071 result = o1.getOrder() > o2.getOrder() ? 1 : -1; 072 } 073 return 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 m_explorerType = explorerType; 129 m_id = getExplorerTypeViewId(explorerType.getName()); 130 m_titleKey = explorerType.getKey(); 131 m_order = Integer.valueOf(explorerType.getNewResourceOrder()).intValue(); 132 } 133 134 /** 135 * Constructor.<p> 136 * 137 * @param cms the cms context 138 * @param resource the group resource 139 * 140 * @throws Exception if parsing the resource fails 141 */ 142 public CmsElementView(CmsObject cms, CmsResource resource) 143 throws Exception { 144 145 m_resource = resource; 146 init(cms); 147 } 148 149 /** 150 * Creates a new view with the given id, but initializes no other fields.<p> 151 * 152 * @param id the id 153 */ 154 public CmsElementView(CmsUUID id) { 155 m_id = id; 156 } 157 158 /** 159 * Constructor for the default element view.<p> 160 */ 161 private CmsElementView() { 162 163 // the default view 164 m_title = "Content elements"; 165 m_titleKey = GUI_ELEMENT_VIEW_DEFAULT_TITLE_0; 166 m_order = 1050; 167 } 168 169 /** 170 * Helper method to compute the uuid for views based on explorer types.<p> 171 * 172 * @param typeName the explorer type name 173 * @return the element view id computed from the type name 174 */ 175 public static CmsUUID getExplorerTypeViewId(String typeName) { 176 177 return CmsUUID.getConstantUUID("elementview-" + typeName); 178 179 } 180 181 /** 182 * Gets the explorer type settings.<p> 183 * 184 * @return the explorer type 185 */ 186 public CmsExplorerTypeSettings getExplorerType() { 187 188 return m_explorerType; 189 190 } 191 192 /** 193 * Returns the element view id.<p> 194 * 195 * @return the group id 196 */ 197 public CmsUUID getId() { 198 199 if (m_id != null) { 200 return m_id; 201 } else if (m_resource != null) { 202 return m_resource.getStructureId(); 203 } else { 204 // only in case of the default element view 205 return CmsUUID.getNullUUID(); 206 } 207 } 208 209 /** 210 * The order.<p> 211 * 212 * @return the order 213 */ 214 public int getOrder() { 215 216 return m_order; 217 } 218 219 /** 220 * Gets the parent view id (null if there is no parent view).<p> 221 * 222 * @return the parent view id 223 */ 224 public CmsUUID getParentViewId() { 225 226 return m_parentViewId; 227 } 228 229 /** 230 * Returns the element view resource.<p> 231 * 232 * @return the element view resource 233 */ 234 public CmsResource getResource() { 235 236 return m_resource; 237 } 238 239 /** 240 * Returns the element view title.<p> 241 * 242 * @param cms the cms context 243 * @param locale the locale 244 * 245 * @return the title 246 */ 247 public String getTitle(CmsObject cms, Locale locale) { 248 249 if (m_titleKey == null) { 250 return m_title; 251 } else { 252 return OpenCms.getWorkplaceManager().getMessages(locale).key(m_titleKey); 253 } 254 } 255 256 /** 257 * Checks whether the current user has permissions to use the element view.<p> 258 * 259 * @param cms the cms context 260 * @param folder used for permission checks for explorertype based views 261 * 262 * @return <code>true</code> if the current user has permissions to use the element view 263 **/ 264 public boolean hasPermission(CmsObject cms, CmsResource folder) { 265 266 if ((m_explorerType != null) && (folder != null)) { 267 CmsPermissionSet permSet = m_explorerType.getAccess().getPermissions(cms, folder); 268 boolean result = permSet.requiresViewPermission(); 269 return result; 270 } 271 272 try { 273 if (m_resource != null) { 274 return cms.hasPermissions( 275 m_resource, 276 CmsPermissionSet.ACCESS_VIEW, 277 false, 278 CmsResourceFilter.IGNORE_EXPIRATION.addRequireVisible()); 279 } else { 280 return OpenCms.getRoleManager().hasRole(cms, CmsRole.ELEMENT_AUTHOR); 281 } 282 283 } catch (CmsException e) { 284 LOG.error(e.getLocalizedMessage(), e); 285 } 286 return false; 287 } 288 289 /** 290 * 'Other types' view, for everything that isn't assigned to any other view.<p> 291 * 292 * @return true if this is the 'other types' view 293 */ 294 public boolean isOther() { 295 296 return (m_explorerType != null) && m_explorerType.getName().equals("view_other"); 297 } 298 299 /** 300 * Parses the edit view resource.<p> 301 * 302 * @param cms the cms context 303 * @throws Exception if parsing the resource fails 304 */ 305 @SuppressWarnings("null") 306 private void init(CmsObject cms) throws Exception { 307 308 CmsFile configFile = cms.readFile(m_resource); 309 CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, configFile); 310 m_title = content.getValue(N_TITLE, CmsConfigurationReader.DEFAULT_LOCALE).getStringValue(cms); 311 I_CmsXmlContentValue titleKey = content.getValue(N_TITLE_KEY, CmsConfigurationReader.DEFAULT_LOCALE); 312 if ((titleKey != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(titleKey.getStringValue(cms))) { 313 m_titleKey = titleKey.getStringValue(cms); 314 } 315 I_CmsXmlContentValue orderVal = content.getValue(N_ORDER, CmsConfigurationReader.DEFAULT_LOCALE); 316 if (orderVal != null) { 317 try { 318 m_order = Integer.parseInt(orderVal.getStringValue(cms)); 319 } catch (Exception e) { 320 LOG.error(e.getLocalizedMessage(), e); 321 m_order = DEFAULT_ORDER; 322 } 323 } else { 324 m_order = DEFAULT_ORDER; 325 } 326 327 I_CmsXmlContentValue parentView = content.getValue("Parent", CmsConfigurationReader.DEFAULT_LOCALE); 328 if (parentView != null) { 329 CmsUUID parentViewId = null; 330 try { 331 CmsXmlVarLinkValue elementViewValue = (CmsXmlVarLinkValue)parentView; 332 String stringValue = elementViewValue.getStringValue(cms); 333 if (CmsStringUtil.isEmpty(stringValue)) { 334 parentViewId = CmsUUID.getNullUUID(); 335 } else if (stringValue.equals(PARENT_NONE)) { 336 parentViewId = null; 337 } else if (stringValue.startsWith(CmsConfigurationReader.VIEW_SCHEME)) { 338 parentViewId = new CmsUUID(stringValue.substring(CmsConfigurationReader.VIEW_SCHEME.length())); 339 } else { 340 parentViewId = elementViewValue.getLink(cms).getStructureId(); 341 } 342 } catch (Exception e) { 343 LOG.error(e.getLocalizedMessage(), e); 344 } 345 m_parentViewId = parentViewId; 346 } 347 348 } 349}