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