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 org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.repository.AmetysObjectFactory;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.web.repository.page.Page;
029import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
030import org.ametys.web.service.ServiceExtensionPoint;
031import org.ametys.web.skin.SkinsManager;
032
033/**
034 * {@link AmetysObjectFactory} handling {@link UGCPage}.
035 */
036public class UGCPageFactory implements AmetysObjectFactory<UGCPage>, Serviceable
037{
038    private AmetysObjectResolver _resolver;
039    private UGCPageHandler _ugcPageHandler;
040    private SkinsManager _skinManager;
041    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
042    private ServiceExtensionPoint _serviceExtensionPoint;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048        _ugcPageHandler = (UGCPageHandler) manager.lookup(UGCPageHandler.ROLE);
049        _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
050        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
051        _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
052    }
053
054    public UGCPage getAmetysObjectById(String id) throws AmetysRepositoryException
055    {
056        // E.g: ugccontent://path?rootId=...&contentId=...
057        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugccontent://"), "?rootId=");
058        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
059        Page root = _resolver.resolveById(rootId);
060        
061        String contentId = StringUtils.substringAfter(id, "&contentId=");
062        Content ugcContent = _resolver.resolveById(contentId);
063        return new UGCPage(root, ugcContent, path, _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
064    }
065
066    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
067    {
068        // E.g: ugccontent://path?rootId=...&contentId=...
069        String contentId = StringUtils.substringAfter(id, "&contentId=");
070        return _resolver.hasAmetysObjectForId(contentId);
071    }
072
073    public String getScheme()
074    {
075        return "ugccontent";
076    }
077}