001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.ui.favorites; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProject; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsResourceFilter; 034import org.opencms.json.JSONException; 035import org.opencms.json.JSONObject; 036import org.opencms.main.CmsException; 037import org.opencms.main.OpenCms; 038import org.opencms.ui.A_CmsUI; 039import org.opencms.ui.CmsVaadinUtils; 040import org.opencms.ui.apps.A_CmsWorkplaceApp; 041import org.opencms.ui.apps.CmsFileExplorerConfiguration; 042import org.opencms.util.CmsStringUtil; 043import org.opencms.util.CmsUUID; 044 045/** 046 * Represents an entry in the favorite location list. 047 */ 048public class CmsFavoriteEntry { 049 050 /** 051 * Represents the type of the favorite. 052 */ 053 public enum Type { 054 055 /** Container page editor favorite. */ 056 explorerFolder("f"), 057 058 /** Page favorite. */ 059 page("p"); 060 061 /** String representing this type in the JSON format. */ 062 private String m_jsonId; 063 064 /** 065 * Creates new value. 066 * 067 * @param jsonId the JSON id. 068 */ 069 private Type(String jsonId) { 070 071 m_jsonId = jsonId; 072 073 } 074 075 /** 076 * Converts JSON id to correct type. 077 * 078 * @param id the JSON id 079 * @return the corresponding type 080 */ 081 public static Type fromJsonId(String id) { 082 083 for (Type type : Type.values()) { 084 if (type.getJsonId().equals(id)) { 085 return type; 086 } 087 } 088 return null; 089 } 090 091 /** 092 * Gets the JSON id for the type. 093 * 094 * @return the JSON id 095 */ 096 public String getJsonId() { 097 098 return m_jsonId; 099 } 100 } 101 102 /** JSON key. */ 103 public static final String JSON_DETAIL = "d"; 104 105 /** JSON key. */ 106 public static final String JSON_PROJECT = "p"; 107 108 /** JSON key. */ 109 public static final String JSON_SITEROOT = "s"; 110 111 /** JSON key. */ 112 public static final String JSON_STRUCTUREID = "i"; 113 114 /** JSON key. */ 115 public static final String JSON_TITLE = "ti"; 116 117 /** JSON key. */ 118 public static final String JSON_TYPE = "t"; 119 120 /** The custom title. */ 121 private String m_customTitle; 122 123 /** The detail id. */ 124 private CmsUUID m_detailId; 125 126 /** The project id. */ 127 private CmsUUID m_projectId; 128 129 /** The site root. */ 130 private String m_siteRoot; 131 132 /** The structure id. */ 133 private CmsUUID m_structureId; 134 135 /** The type. */ 136 private Type m_type; 137 138 /** 139 * Creates a new entry. 140 */ 141 public CmsFavoriteEntry() {} 142 143 /** 144 * Creates a new entry from a JSON object. 145 * 146 * @param obj the JSON object 147 */ 148 public CmsFavoriteEntry(JSONObject obj) { 149 150 m_detailId = readId(obj, JSON_DETAIL); 151 m_projectId = readId(obj, JSON_PROJECT); 152 setSiteRoot(obj.optString(JSON_SITEROOT)); 153 m_structureId = readId(obj, JSON_STRUCTUREID); 154 m_type = Type.fromJsonId(obj.optString(JSON_TYPE)); 155 m_customTitle = obj.optString(JSON_TITLE); 156 } 157 158 /** 159 * Reads a UUID from a JSON object. 160 * 161 * Returns null if the JSON value for the given key is not present or not a valid UUID 162 * 163 * @param obj the JSON object 164 * @param key the JSON key 165 * 166 * @return the UUID 167 */ 168 public static CmsUUID readId(JSONObject obj, String key) { 169 170 String strValue = obj.optString(key); 171 if (!CmsUUID.isValidUUID(strValue)) { 172 return null; 173 } 174 return new CmsUUID(strValue); 175 } 176 177 /** 178 * Gets the custom title. 179 * 180 * @return the custom title 181 */ 182 public String getCustomTitle() { 183 184 return m_customTitle; 185 } 186 187 /** 188 * Gets the detail id. 189 * 190 * @return the detail id 191 */ 192 public CmsUUID getDetailId() { 193 194 return m_detailId; 195 } 196 197 /** 198 * Gets the project id. 199 * 200 * @return the project id 201 */ 202 public CmsUUID getProjectId() { 203 204 return m_projectId; 205 } 206 207 /** 208 * Gets the site root. 209 * 210 * @return the site root 211 */ 212 public String getSiteRoot() { 213 214 return m_siteRoot; 215 } 216 217 /** 218 * Gets the structure id 219 * 220 * @return the structure id 221 */ 222 public CmsUUID getStructureId() { 223 224 return m_structureId; 225 } 226 227 /** 228 * Gets the type. 229 * 230 * @return the type 231 */ 232 public Type getType() { 233 234 return m_type; 235 } 236 237 /** 238 * Sets the custom title. 239 * 240 * @param title the custom title 241 */ 242 public void setCustomTitle(String title) { 243 244 if (CmsStringUtil.isEmpty(title)) { 245 m_customTitle = null; 246 } else { 247 m_customTitle = title; 248 } 249 } 250 251 /** 252 * Sets the detail id. 253 * 254 * @param detailId the detail id 255 */ 256 public void setDetailId(CmsUUID detailId) { 257 258 m_detailId = detailId; 259 } 260 261 /** 262 * Sets the project id. 263 * 264 * @param projectId the project id 265 */ 266 public void setProjectId(CmsUUID projectId) { 267 268 m_projectId = projectId; 269 } 270 271 /** 272 * Sets the site root. 273 * 274 * @param siteRoot the site root 275 */ 276 public void setSiteRoot(String siteRoot) { 277 278 if (siteRoot != null) { 279 siteRoot = siteRoot.replaceFirst("/$", ""); 280 } 281 m_siteRoot = siteRoot; 282 } 283 284 /** 285 * Sets the structure id. 286 * @param structureId the structure id 287 */ 288 public void setStructureId(CmsUUID structureId) { 289 290 m_structureId = structureId; 291 } 292 293 /** 294 * Sets the type. 295 * 296 * @param type the type 297 */ 298 public void setType(Type type) { 299 300 m_type = type; 301 } 302 303 /** 304 * Converts this object to JSON. 305 * 306 * @return the JSON representation 307 * 308 * @throws JSONException if JSON operations fail 309 */ 310 public JSONObject toJson() throws JSONException { 311 312 JSONObject result = new JSONObject(); 313 if (m_detailId != null) { 314 result.put(JSON_DETAIL, "" + m_detailId); 315 } 316 if (m_siteRoot != null) { 317 result.put(JSON_SITEROOT, m_siteRoot); 318 } 319 if (m_structureId != null) { 320 result.put(JSON_STRUCTUREID, "" + m_structureId); 321 } 322 if (m_projectId != null) { 323 result.put(JSON_PROJECT, "" + m_projectId); 324 } 325 if (m_type != null) { 326 result.put(JSON_TYPE, "" + m_type.getJsonId()); 327 } 328 329 if (!CmsStringUtil.isEmptyOrWhitespaceOnly(m_customTitle)) { 330 result.put(JSON_TITLE, m_customTitle); 331 } 332 return result; 333 } 334 335 /** 336 * Prepares the CmsObject for jumping to this favorite location, and returns the appropriate URL. 337 * 338 * @param cms the CmsObject to initialize for jumping to the favorite 339 * @return the link for the favorite location 340 * 341 * @throws CmsException if something goes wrong 342 */ 343 public String updateContextAndGetFavoriteUrl(CmsObject cms) throws CmsException { 344 345 CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION; 346 CmsProject project = null; 347 switch (getType()) { 348 case explorerFolder: 349 CmsResource folder = cms.readResource(getStructureId(), filter); 350 project = cms.readProject(getProjectId()); 351 cms.getRequestContext().setSiteRoot(getSiteRoot()); 352 A_CmsUI.get().getWorkplaceSettings().setSite(getSiteRoot()); 353 cms.getRequestContext().setCurrentProject(project); 354 String explorerLink = CmsVaadinUtils.getWorkplaceLink( 355 CmsFileExplorerConfiguration.APP_ID, 356 getProjectId() 357 + A_CmsWorkplaceApp.PARAM_SEPARATOR 358 + getSiteRoot() 359 + A_CmsWorkplaceApp.PARAM_SEPARATOR 360 + cms.getSitePath(folder)); 361 return explorerLink; 362 case page: 363 project = cms.readProject(getProjectId()); 364 CmsResource target = cms.readResource(getStructureId(), filter); 365 CmsResource detailContent = null; 366 String link = null; 367 cms.getRequestContext().setCurrentProject(project); 368 cms.getRequestContext().setSiteRoot(getSiteRoot()); 369 A_CmsUI.get().getWorkplaceSettings().setSite(getSiteRoot()); 370 if (getDetailId() != null) { 371 detailContent = cms.readResource(getDetailId()); 372 link = OpenCms.getLinkManager().substituteLinkForUnknownTarget( 373 cms, 374 cms.getSitePath(detailContent), 375 cms.getSitePath(target), 376 false); 377 } else { 378 link = OpenCms.getLinkManager().substituteLink(cms, target); 379 } 380 return link; 381 default: 382 return null; 383 } 384 } 385 386}