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.plugins.blog.BlogConstants;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.web.repository.page.Page;
028import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.service.ServiceExtensionPoint;
031
032/**
033 * {@link AmetysObjectFactory} handling {@link PostListZone}.
034 */
035public class PostListZoneFactory implements AmetysObjectFactory<PostListZone>, Serviceable
036{
037    private AmetysObjectResolver _resolver;
038    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
039    private ServiceExtensionPoint _serviceExtensionPoint;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
045        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
046        _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
047    }
048    
049    @Override
050    public String getScheme()
051    {
052        return "postListZone";
053    }
054    
055    @Override
056    public PostListZone getAmetysObjectById(String id) throws AmetysRepositoryException
057    {
058        int i = id.indexOf('?');
059        String pageId = id.substring(i + "?pageId=".length());
060        
061        Page page = _resolver.resolveById(pageId);
062        
063        Site site = page.getSite();
064        Long maxCount = site.getValue("posts-service-max-count", false, Long.valueOf(BlogConstants.DEFAULT_POST_COUNT_PER_PAGE));
065        
066        return new PostListZone(page, maxCount.intValue(), _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
067    }
068
069    @Override
070    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
071    {
072        int i = id.indexOf('?');
073        String pageId = id.substring(i + "?pageId=".length());
074        
075        return _resolver.hasAmetysObjectForId(pageId);
076    }
077}