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 */ 016 017package org.ametys.plugins.ugc.page; 018 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.plugins.repository.AmetysObjectFactory; 028import org.ametys.plugins.repository.AmetysObjectResolver; 029import org.ametys.plugins.repository.AmetysRepositoryException; 030import org.ametys.plugins.repository.UnknownAmetysObjectException; 031import org.ametys.web.data.type.ModelItemTypeExtensionPoint; 032import org.ametys.web.repository.page.Page; 033import org.ametys.web.service.ServiceExtensionPoint; 034import org.ametys.web.skin.SkinsManager; 035 036/** 037 * {@link AmetysObjectFactory} handling {@link UGCPage}. 038 */ 039public class UGCPageFactory implements AmetysObjectFactory<UGCPage>, Serviceable 040{ 041 private AmetysObjectResolver _resolver; 042 private UGCPageHandler _ugcPageHandler; 043 private SkinsManager _skinManager; 044 private ModelItemTypeExtensionPoint _pageDataTypeExtensionPoint; 045 private ServiceExtensionPoint _serviceExtensionPoint; 046 047 @Override 048 public void service(ServiceManager manager) throws ServiceException 049 { 050 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 051 _ugcPageHandler = (UGCPageHandler) manager.lookup(UGCPageHandler.ROLE); 052 _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 053 _pageDataTypeExtensionPoint = (ModelItemTypeExtensionPoint) manager.lookup(ModelItemTypeExtensionPoint.ROLE_PAGE_DATA); 054 _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE); 055 } 056 057 public UGCPage getAmetysObjectById(String id) throws AmetysRepositoryException 058 { 059 // E.g: ugccontent://path?rootId=...&contentId=... 060 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugccontent://"), "?rootId="); 061 String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId="); 062 Page root = _resolver.resolveById(rootId); 063 064 String contentId = StringUtils.substringAfter(id, "&contentId="); 065 Content ugcContent = _resolver.resolveById(contentId); 066 067 if ("_root".equals(path) && _ugcPageHandler.hasContentForRootPage(root, ugcContent)) 068 { 069 return new UGCPage(root, ugcContent, path, _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint); 070 } 071 else 072 { 073 Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(root); 074 if (transitionalPageName.containsKey(path)) 075 { 076 Map<String, String> attributes = transitionalPageName.get(path); 077 String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE); 078 if (_ugcPageHandler.hasContentForTransitionalPage(root, metadataValue, ugcContent)) 079 { 080 return new UGCPage(root, ugcContent, path, _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint); 081 } 082 } 083 } 084 085 throw new UnknownAmetysObjectException("The UGC page with id '" + id + "' does not match a existing UGC page for content with id " + contentId); 086 } 087 088 public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException 089 { 090 // E.g: ugccontent://path?rootId=...&contentId=... 091 String contentId = StringUtils.substringAfter(id, "&contentId="); 092 093 if (!_resolver.hasAmetysObjectForId(contentId)) 094 { 095 return false; 096 } 097 else 098 { 099 // Related content exists, check its page is the same 100 UGCPage ugcPage = _ugcPageHandler.getUgcPage(contentId, null); 101 return ugcPage != null ? id.equals(ugcPage.getId()) : false; 102 } 103 } 104 105 public String getScheme() 106 { 107 return "ugccontent"; 108 } 109}