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.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.regex.Pattern;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031import org.apache.commons.lang.StringUtils;
032
033import org.ametys.core.cocoon.ActionResultGenerator;
034import org.ametys.core.observation.AbstractNotifierAction;
035import org.ametys.core.util.mail.SendMailHelper;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.RepositoryConstants;
038import org.ametys.plugins.repository.UnknownAmetysObjectException;
039import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
040import org.ametys.runtime.i18n.I18nizableText;
041import org.ametys.web.repository.page.Page;
042
043/**
044 * Action to subscribe to a page
045 */
046public class PageSubscribeAction extends AbstractNotifierAction
047{
048    /** The pattern to check emails */
049    protected static final Pattern EMAIL_VALIDATOR = SendMailHelper.EMAIL_VALIDATION;
050    
051    /** The pattern to check text input */
052    protected static final Pattern TEXT_VALIDATOR = Pattern.compile("^\\s*$");
053    
054    /** The ametys resolver */
055    protected AmetysObjectResolver _resolver;
056    /** The subscription dao */
057    protected PageSubscriptionDAO _subscriptionDAO;
058    
059    @Override
060    public void service(ServiceManager smanager) throws ServiceException
061    {
062        super.service(smanager);
063        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
064        _subscriptionDAO = (PageSubscriptionDAO) smanager.lookup(PageSubscriptionDAO.ROLE);
065    }
066    
067    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
068    {
069        Map<String, Object> result = new HashMap<>();
070        List<I18nizableText> errors = new ArrayList<>();
071        
072        Request request = ObjectModelHelper.getRequest(objectModel);
073        String action = request.getParameter("page-subscribe-action");
074        result.put("action", action);
075        
076        String email = request.getParameter("email");
077        
078        if (email == null || !EMAIL_VALIDATOR.matcher(StringUtils.trimToEmpty(email.toLowerCase())).matches() || TEXT_VALIDATOR.matcher(StringUtils.trimToEmpty(email)).matches())
079        {
080            errors.add(new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_FORM_MAIL_ERROR_MAILBY"));
081        }
082        else
083        {
084            result.put("email", email);
085            
086            String pageId = request.getParameter("page-id");
087            if (pageId == null) 
088            {
089                throw new IllegalArgumentException("Unable to subscribe or unsubscribe to the page: cannot determine the current page");
090            }
091            else
092            {
093                // Force default workspace
094                String currentWorspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
095                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
096                
097                try
098                {
099                    Page page = _resolver.resolveById(pageId);
100                    
101                    
102                    if (action.equals("subscribe"))
103                    {
104                        _subscriptionDAO.addSubscriber(page, email);
105                    }
106                    else if (action.equals("unsubscribe"))
107                    {
108                        _subscriptionDAO.removeSubscriber(page, email);
109                    }
110                }
111                catch (UnknownAmetysObjectException e)
112                {
113                    throw new IllegalArgumentException("Unable to subscribe or unsubscribe to the unknown page with id '" + pageId + "'");
114                }
115                finally
116                {
117                    RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWorspace);
118                }
119            }
120        }
121         
122        if (!errors.isEmpty())
123        {
124            result.put("error", errors);
125        }
126        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
127        
128        return EMPTY_MAP;
129    }
130}