001/*
002 *  Copyright 2014 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 */
016package org.ametys.plugins.pagesubscription.helpers;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027
028import org.ametys.plugins.pagesubscription.PageSubscriptionDAO;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.web.repository.page.Page;
031
032/**
033 *  Helper component to be used from XSL stylesheets.
034 */
035public class PageSubscriptionXSLTHelper implements Component, Contextualizable, Serviceable
036{
037    private static Context _context;
038    private static PageSubscriptionDAO _subscriptionDAO;
039    private static AmetysObjectResolver _resolver;
040    
041    @Override
042    public void contextualize(Context context) throws ContextException
043    {
044        _context = context;
045    }
046    
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        _subscriptionDAO = (PageSubscriptionDAO) smanager.lookup(PageSubscriptionDAO.ROLE);
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    /**
054     * Determines is the given email is registered as subscriber on current page
055     * @param email The email
056     * @return true if the given email is registered as subscriber on current page
057     * @throws IllegalArgumentException if the current page does not support subscription
058     */
059    public static boolean isPageSubscriber(String email) throws IllegalArgumentException
060    {
061        Request request = ContextHelper.getRequest(_context);
062        Page page = (Page) request.getAttribute(Page.class.getName());
063        
064        return _subscriptionDAO.isSubscriber(page, email);
065    }
066    
067    /**
068     * Determines is the given email is registered as subscriber on the given page
069     * @param pageId the id of page
070     * @param email The email
071     * @return true if the given email is registered as subscriber on current page
072     * @throws IllegalArgumentException if the current page does not support subscription
073     */
074    public static boolean isPageSubscriber(String pageId, String email) throws IllegalArgumentException
075    {
076        Page page = _resolver.resolveById(pageId);
077        return _subscriptionDAO.isSubscriber(page, email);
078    }
079}