001/*
002 *  Copyright 2011 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.blog.repository;
018
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
028import org.ametys.web.repository.page.PagesContainer;
029import org.ametys.web.skin.SkinsManager;
030
031/**
032 * {@link AmetysObjectFactory} handling {@link VirtualPostPage}.
033 */
034public class VirtualPostPageFactory implements AmetysObjectFactory<VirtualPostPage>, Serviceable
035{
036    
037    private AmetysObjectResolver _resolver;
038    private SkinsManager _skinsManager;
039    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
045        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
046        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
047    }
048    
049    @Override
050    public String getScheme()
051    {
052        return "post";
053    }
054    
055    @Override
056    public VirtualPostPage getAmetysObjectById(String id) throws AmetysRepositoryException
057    {
058        int i = id.indexOf('?');
059        int i2 = id.indexOf('&', i);
060            
061        String path = id.substring(getScheme().length() + 3, i);
062        String rootId = id.substring(i + "?rootId=".length(), i2);
063        String postId = id.substring(i2 + "&postId=".length());
064        
065        PagesContainer root = _resolver.resolveById(rootId);
066        Content postContent = _resolver.resolveById(postId);
067        
068        return new VirtualPostPage(_resolver, _skinsManager, root, postContent, path, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint);
069    }
070    
071    @Override
072    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
073    {
074        int i = id.indexOf('?');
075        int i2 = id.indexOf('&', i);
076            
077        String rootId = id.substring(i + "?rootId=".length(), i2);
078        String postId = id.substring(i2 + "&postId=".length());
079        
080        return _resolver.hasAmetysObjectForId(rootId) && _resolver.hasAmetysObjectForId(postId);
081    }
082    
083}