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