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