001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.ugc.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024import java.util.stream.Collectors; 025 026import javax.jcr.Node; 027import javax.jcr.RepositoryException; 028import javax.jcr.Value; 029 030import org.apache.avalon.framework.service.ServiceException; 031import org.apache.avalon.framework.service.ServiceManager; 032import org.apache.commons.lang3.StringUtils; 033import org.apache.jackrabbit.value.StringValue; 034 035import org.ametys.cms.contenttype.ContentType; 036import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 037import org.ametys.cms.contenttype.MetadataDefinition; 038import org.ametys.core.observation.Event; 039import org.ametys.core.observation.ObservationManager; 040import org.ametys.core.ui.Callable; 041import org.ametys.core.util.LambdaUtils; 042import org.ametys.plugins.repository.AmetysObjectResolver; 043import org.ametys.plugins.repository.jcr.JCRAmetysObject; 044import org.ametys.plugins.ugc.observation.ObservationConstants; 045import org.ametys.plugins.ugc.page.UGCPageHandler; 046import org.ametys.plugins.ugc.page.VirtualUGCPageFactory; 047import org.ametys.runtime.i18n.I18nizableText; 048import org.ametys.web.clientsideelement.AbstractPageClientSideElement; 049import org.ametys.web.repository.page.ModifiablePage; 050import org.ametys.web.repository.page.Page; 051 052/** 053 * Client side element for a controller wich set/remove the root page of a ugc. 054 */ 055public class SetUGCRootClientSideElement extends AbstractPageClientSideElement 056{ 057 /** Observer manager. */ 058 protected ObservationManager _observationManager; 059 /** The extension point for content types */ 060 protected ContentTypeExtensionPoint _contentTypeEP; 061 /** The UGC page handler */ 062 protected UGCPageHandler _ugcPageHandler; 063 064 @Override 065 public void service(ServiceManager smanager) throws ServiceException 066 { 067 super.service(smanager); 068 _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE); 069 _contentTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 070 _ugcPageHandler = (UGCPageHandler) smanager.lookup(UGCPageHandler.ROLE); 071 072 } 073 074 /** 075 * Gets the status of the given page 076 * @param pageId The page id 077 * @return the status of the given page 078 */ 079 @Callable 080 public Map<String, Object> getStatus(String pageId) 081 { 082 Map<String, Object> result = new HashMap<>(); 083 084 Map<String, Object> parameters = this._script.getParameters(); 085 086 Page page = _resolver.resolveById(pageId); 087 088 if (page instanceof JCRAmetysObject) 089 { 090 if (_isUGCRootPage((JCRAmetysObject) page)) 091 { 092 List<String> i18nParameters = new ArrayList<>(); 093 i18nParameters.add(page.getTitle()); 094 095 I18nizableText ed = (I18nizableText) parameters.get("ugc-page-description"); 096 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 097 result.put("ugc-page-title", msg); 098 099 ed = (I18nizableText) parameters.get("remove-ugc-page-description"); 100 msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 101 result.put("remove-ugc-page-title", msg); 102 103 String contentTypeId = page.getValue(UGCPageHandler.CONTENT_TYPE_METADATA_NAME, StringUtils.EMPTY); 104 if (StringUtils.isNotEmpty(contentTypeId)) 105 { 106 I18nizableText contentTypeText = _contentTypeEP.hasExtension(contentTypeId) ? _contentTypeEP.getExtension(contentTypeId).getLabel() : new I18nizableText(contentTypeId); 107 108 Map<String, I18nizableText> contentTypeI18nParameters = new HashMap<>(); 109 contentTypeI18nParameters.put("0", contentTypeText); 110 111 ed = (I18nizableText) parameters.get("contenttype-ugc-page-description"); 112 msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), contentTypeI18nParameters); 113 result.put("contenttype-ugc-page-description", msg); 114 } 115 116 result.put("ugc-page-id", new I18nizableText(page.getId())); 117 } 118 else if (!_ugcPageHandler.getUGCRootPages(page.getSiteName(), page.getSitemapName()).isEmpty()) 119 { 120 I18nizableText ed = (I18nizableText) parameters.get("ugc-page-already-exist"); 121 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey()); 122 result.put("ugc-page-already-exist", msg); 123 } 124 else 125 { 126 List<String> i18nParameters = new ArrayList<>(); 127 i18nParameters.add(page.getTitle()); 128 129 I18nizableText ed = (I18nizableText) parameters.get("add-ugc-page-description"); 130 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 131 132 result.put("add-ugc-page-id", new I18nizableText(page.getId())); 133 result.put("add-ugc-page-title", msg); 134 } 135 } 136 else 137 { 138 List<String> noJcrI18nParameters = new ArrayList<>(); 139 noJcrI18nParameters.add(page.getTitle()); 140 141 I18nizableText ed = (I18nizableText) parameters.get("no-jcr-page-description"); 142 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), noJcrI18nParameters); 143 144 result.put("no-jcr-page-id", new I18nizableText(page.getId())); 145 result.put("no-jcr-page-title", msg); 146 } 147 148 return result; 149 } 150 151 /** 152 * Sets the given page as the root of a ugc 153 * @param pageId The id of the page 154 * @param contentTypeId The id of the content type 155 * @param metadataPath The classification metadata path 156 * @return A result map 157 * @throws RepositoryException if a repository error occured 158 */ 159 @Callable 160 public Map<String, Object> setUGCRoot(String pageId, String contentTypeId, String metadataPath) throws RepositoryException 161 { 162 Map<String, Object> result = new HashMap<>(); 163 164 ContentType contentType = _contentTypeEP.getExtension(contentTypeId); 165 if (contentType == null) 166 { 167 result.put("error", "wrong-content-type"); 168 return result; 169 } 170 else if (StringUtils.isNotBlank(metadataPath)) 171 { 172 MetadataDefinition metadataDefinitionByPath = contentType.getMetadataDefinitionByPath(metadataPath); 173 if (metadataDefinitionByPath == null) 174 { 175 result.put("error", "wrong-metadata"); 176 return result; 177 } 178 } 179 180 Page page = _resolver.resolveById(pageId); 181 Page currentUGCPage = _ugcPageHandler.getUGCRootPage(page.getSiteName(), page.getSitemapName(), contentTypeId); 182 183 Map<String, Object> eventParams = new HashMap<>(); 184 eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page); 185 186 if (currentUGCPage != null && currentUGCPage.getId().equals(pageId)) 187 { 188 // Unindex pages for all workspaces before the properties changed 189 _observationManager.notify(new Event(ObservationConstants.EVENT_UGC_ROOT_UPDATING, _currentUserProvider.getUser(), eventParams)); 190 191 _updateUGCRootProperty(page, contentTypeId, metadataPath); 192 } 193 else 194 { 195 _addUGCRootProperty(page, contentTypeId, metadataPath); 196 } 197 198 // Live synchronization 199 _notifyPageUpdated(page); 200 201 // Indexation 202 _observationManager.notify(new Event(ObservationConstants.EVENT_UGC_ROOT_UPDATED, _currentUserProvider.getUser(), eventParams)); 203 return result; 204 } 205 206 /** 207 * Remove the ugc root status to the given page 208 * @param pageId The id of the page 209 * @return A result map 210 * @throws RepositoryException if a repository error occured 211 */ 212 @Callable 213 public Map<String, Object> removeUGCRoot(String pageId) throws RepositoryException 214 { 215 Map<String, Object> result = new HashMap<>(); 216 217 Page page = _resolver.resolveById(pageId); 218 219 if (page instanceof JCRAmetysObject) 220 { 221 if (!_isUGCRootPage((JCRAmetysObject) page)) 222 { 223 result.put("error", "no-root"); 224 return result; 225 } 226 227 Map<String, Object> eventParams = new HashMap<>(); 228 eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page); 229 230 // Unindex pages for all workspaces before the properties were removed 231 _observationManager.notify(new Event(ObservationConstants.EVENT_UGC_ROOT_DELETING, _currentUserProvider.getUser(), eventParams)); 232 233 _removeUGCRootProperty(page); 234 235 _notifyPageUpdated(page); 236 237 // After live synchronization 238 _observationManager.notify(new Event(ObservationConstants.EVENT_UGC_ROOT_DELETED, _currentUserProvider.getUser(), eventParams)); 239 } 240 else 241 { 242 result.put("error", "no-root"); 243 } 244 return result; 245 } 246 247 /** 248 * Gets information about ugc root status on the given. 249 * @param pageId The id of the page 250 * @return information about ugc root status on the given. 251 */ 252 @Callable 253 public Map<String, Object> getRootPageInfo(String pageId) 254 { 255 Map<String, Object> result = new HashMap<>(); 256 257 Page page = _resolver.resolveById(pageId); 258 Set<Page> currentUGCPages = _ugcPageHandler.getUGCRootPages(page.getSiteName(), page.getSitemapName()); 259 260 if (currentUGCPages.contains(page)) 261 { 262 result.put("isRoot", true); 263 result.put("contentType", page.getValue(UGCPageHandler.CONTENT_TYPE_METADATA_NAME)); 264 result.put("metadata", page.getValue(UGCPageHandler.CLASSIFICATION_METADATA_METADATA_NAME)); 265 } 266 else 267 { 268 result.put("isRoot", false); 269 } 270 271 return result; 272 } 273 274 private void _addUGCRootProperty(Page page, String contentType, String metadata) throws RepositoryException 275 { 276 if (page instanceof JCRAmetysObject) 277 { 278 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 279 Node node = jcrPage.getNode(); 280 281 List<Value> values = new ArrayList<>(); 282 if (node.hasProperty(AmetysObjectResolver.VIRTUAL_PROPERTY)) 283 { 284 values.addAll(Arrays.asList(node.getProperty(AmetysObjectResolver.VIRTUAL_PROPERTY).getValues())); 285 } 286 287 StringValue virtualUGCPageFactoryClassName = new StringValue(VirtualUGCPageFactory.class.getName()); 288 if (!values.contains(virtualUGCPageFactoryClassName)) 289 { 290 values.add(virtualUGCPageFactoryClassName); 291 } 292 293 node.setProperty(AmetysObjectResolver.VIRTUAL_PROPERTY, values.toArray(new Value[values.size()])); 294 295 // Set the ugc root property 296 if (page instanceof ModifiablePage) 297 { 298 ((ModifiablePage) page).setValue(UGCPageHandler.CONTENT_TYPE_METADATA_NAME, contentType); 299 ((ModifiablePage) page).setValue(UGCPageHandler.CLASSIFICATION_METADATA_METADATA_NAME, metadata); 300 } 301 302 jcrPage.saveChanges(); 303 } 304 } 305 306 private void _updateUGCRootProperty(Page page, String contentType, String metadata) 307 { 308 if (page instanceof ModifiablePage) 309 { 310 ModifiablePage modifiablePage = (ModifiablePage) page; 311 312 // Set the ugc root property 313 modifiablePage.setValue(UGCPageHandler.CONTENT_TYPE_METADATA_NAME, contentType); 314 modifiablePage.setValue(UGCPageHandler.CLASSIFICATION_METADATA_METADATA_NAME, metadata); 315 316 modifiablePage.saveChanges(); 317 } 318 } 319 320 private void _removeUGCRootProperty(Page page) throws RepositoryException 321 { 322 if (page instanceof JCRAmetysObject) 323 { 324 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 325 Node node = jcrPage.getNode(); 326 327 if (node.hasProperty(AmetysObjectResolver.VIRTUAL_PROPERTY)) 328 { 329 List<Value> values = new ArrayList<>(Arrays.asList(node.getProperty(AmetysObjectResolver.VIRTUAL_PROPERTY).getValues())); 330 int index = values.stream() 331 .map(LambdaUtils.wrap(Value::getString)) 332 .collect(Collectors.toList()) 333 .indexOf(VirtualUGCPageFactory.class.getName()); 334 if (index != -1) 335 { 336 values.remove(index); 337 node.setProperty(AmetysObjectResolver.VIRTUAL_PROPERTY, values.toArray(new Value[values.size()])); 338 } 339 340 // Remove the ugc root property 341 if (page instanceof ModifiablePage) 342 { 343 ModifiablePage modifiablePage = (ModifiablePage) page; 344 if (modifiablePage.hasValue(UGCPageHandler.CONTENT_TYPE_METADATA_NAME)) 345 { 346 modifiablePage.removeValue(UGCPageHandler.CONTENT_TYPE_METADATA_NAME); 347 } 348 if (modifiablePage.hasValue(UGCPageHandler.CLASSIFICATION_METADATA_METADATA_NAME)) 349 { 350 modifiablePage.removeValue(UGCPageHandler.CLASSIFICATION_METADATA_METADATA_NAME); 351 } 352 } 353 354 jcrPage.saveChanges(); 355 } 356 } 357 } 358 359 private boolean _isUGCRootPage (JCRAmetysObject jcrPage) 360 { 361 try 362 { 363 Node node = jcrPage.getNode(); 364 365 if (node.hasProperty(AmetysObjectResolver.VIRTUAL_PROPERTY)) 366 { 367 List<Value> values = Arrays.asList(node.getProperty(AmetysObjectResolver.VIRTUAL_PROPERTY).getValues()); 368 369 return values.stream() 370 .map(LambdaUtils.wrap(Value::getString)) 371 .anyMatch(v -> VirtualUGCPageFactory.class.getName().equals(v)); 372 } 373 else 374 { 375 return false; 376 } 377 } 378 catch (RepositoryException e) 379 { 380 return false; 381 } 382 } 383 384 private void _notifyPageUpdated(Page page) 385 { 386 Map<String, Object> eventParams = new HashMap<>(); 387 eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page); 388 _observationManager.notify(new Event(org.ametys.web.ObservationConstants.EVENT_PAGE_UPDATED, _currentUserProvider.getUser(), eventParams)); 389 } 390}