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.AmetysObjectIterable; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.plugins.repository.AmetysRepositoryException; 031import org.ametys.plugins.repository.UnknownAmetysObjectException; 032import org.ametys.web.data.type.ModelItemTypeExtensionPoint; 033import org.ametys.web.repository.page.Page; 034import org.ametys.web.service.ServiceExtensionPoint; 035import org.ametys.web.skin.SkinsManager; 036 037/** 038 * {@link AmetysObjectFactory} handling {@link UGCTransitionalPage}. 039 */ 040public class UGCTransitionalPageFactory implements AmetysObjectFactory<UGCTransitionalPage>, Serviceable 041{ 042 private AmetysObjectResolver _resolver; 043 private UGCPageHandler _ugcPageHandler; 044 private SkinsManager _skinManager; 045 private ModelItemTypeExtensionPoint _pageDataTypeExtensionPoint; 046 private ServiceExtensionPoint _serviceExtensionPoint; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 052 _ugcPageHandler = (UGCPageHandler) manager.lookup(UGCPageHandler.ROLE); 053 _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 054 _pageDataTypeExtensionPoint = (ModelItemTypeExtensionPoint) manager.lookup(ModelItemTypeExtensionPoint.ROLE_PAGE_DATA); 055 _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE); 056 } 057 058 public UGCTransitionalPage getAmetysObjectById(String id) throws AmetysRepositoryException 059 { 060 // E.g: ugctransitional://path?rootId=... 061 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugctransitional://"), "?rootId="); 062 063 String rootId = StringUtils.substringAfter(id, "?rootId="); 064 Page root = _resolver.resolveById(rootId); 065 066 Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(root); 067 if (transitionalPageName.containsKey(path)) 068 { 069 Map<String, String> attributes = transitionalPageName.get(path); 070 String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE); 071 String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE); 072 return new UGCTransitionalPage(root, title, metadataValue, path, _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint); 073 } 074 else 075 { 076 throw new UnknownAmetysObjectException("No transitional page named " + path); 077 } 078 } 079 080 public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException 081 { 082 try 083 { 084 // E.g: ugctransitional://path?rootId=... 085 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugctransitional://"), "?rootId="); 086 087 String rootId = StringUtils.substringAfter(id, "?rootId="); 088 Page root = _resolver.resolveById(rootId); 089 090 Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(root); 091 092 if (transitionalPageName.containsKey(path)) 093 { 094 Map<String, String> attributes = transitionalPageName.get(path); 095 String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE); 096 097 AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(root, metadataValue); 098 return contentsForTransitionalPage.getSize() != 0; 099 } 100 101 return false; 102 } 103 catch (UnknownAmetysObjectException e) 104 { 105 return false; 106 } 107 108 } 109 110 public String getScheme() 111 { 112 return "ugctransitional"; 113 } 114 115}