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;
017
018import java.io.IOException;
019import java.util.regex.Pattern;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.core.user.User;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.core.user.UserManager;
035import org.ametys.core.util.mail.SendMailHelper;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.web.repository.page.Page;
038
039/**
040 * Generate user info about a page
041 */
042public class InfoSubscriberGenerator extends ServiceableGenerator
043{
044    /** The pattern to check emails */
045    protected static final Pattern EMAIL_VALIDATOR = SendMailHelper.EMAIL_VALIDATION;
046    
047    /** The pattern to check text input */
048    protected static final Pattern TEXT_VALIDATOR = Pattern.compile("^\\s*$");
049    
050    /** Repository content */
051    protected AmetysObjectResolver _resolver;
052    
053    /** FO user manager */
054    protected UserManager _foUserManager;
055    
056    /** The current user provider */
057    protected CurrentUserProvider _currentUserProvider;
058    
059    /** The subscription dao */
060    protected PageSubscriptionDAO _subscriptionDAO;
061    
062    @Override
063    public void service(ServiceManager smanager) throws ServiceException
064    {
065        super.service(smanager);
066        _subscriptionDAO = (PageSubscriptionDAO) smanager.lookup(PageSubscriptionDAO.ROLE);
067        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
068        _foUserManager = (UserManager) smanager.lookup(UserManager.ROLE);
069        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
070    }
071
072    public void generate() throws IOException, SAXException, ProcessingException
073    {
074        Request request = ObjectModelHelper.getRequest(objectModel);
075        
076        UserIdentity userIdentity = _currentUserProvider.getUser();
077        
078        contentHandler.startDocument();
079        AttributesImpl attr = new AttributesImpl();
080        if (userIdentity == null)
081        {
082            attr.addCDATAAttribute("isLogged", "false");
083            XMLUtils.createElement(contentHandler, "user", attr);
084        }
085        else
086        {
087            User user = _foUserManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin());
088            
089            attr.addCDATAAttribute("isLogged", "true");
090            String email = user.getEmail();
091          
092            String pageId = request.getParameter("page-id");
093            if (pageId == null) 
094            {
095                throw new IllegalArgumentException("Unable to subscribe or unsubscribe to the page: cannot determine the current page");
096            }
097            else
098            {
099                Page page = _resolver.resolveById(pageId);
100                boolean isSubscriber = _subscriptionDAO.isSubscriber(page, email);
101                attr.addCDATAAttribute("isRegistered", Boolean.toString(isSubscriber));
102            }
103            
104            XMLUtils.createElement(contentHandler, "user", attr, email);    
105        }
106        contentHandler.endDocument();
107    }
108}