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.sms;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.core.right.RightManager;
031import org.ametys.core.right.RightManager.RightResult;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.sms.broker.Broker;
035import org.ametys.plugins.sms.dao.SubscriberDAO;
036
037
038/**
039 * Send the SMS
040 */
041public class SendSMSAction extends ServiceableAction
042{
043    /** The rights manager */
044    protected RightManager _rightManager;
045    /** The user provider */
046    protected CurrentUserProvider _userProvider;
047    /** The sms broker */
048    protected Broker _broker;
049    /** The subscriber DAO */
050    protected SubscriberDAO _subscriberDAO;
051    
052    @Override
053    public void service(ServiceManager serviceManager) throws ServiceException
054    {
055        super.service(serviceManager);
056        _userProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
057        _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE);
058        _broker = (Broker) serviceManager.lookup(Broker.ROLE);
059        _subscriberDAO = (SubscriberDAO) serviceManager.lookup(SubscriberDAO.ROLE);
060    }
061    
062    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
063    {
064        Request request = ObjectModelHelper.getRequest(objectModel);
065
066        UserIdentity user = _userProvider.getUser();
067        String message = request.getParameter("message");
068        String phoneList = request.getParameter("phoneList");
069        
070        if (_rightManager.hasRight(user, "Plugins_SMS_Right_Send", "/cms") != RightResult.RIGHT_ALLOW)
071        {
072            throw new IllegalAccessException("The user '" + user.toString() + "' tried to send a message to the list '" + phoneList + "' but right did not have the right to: " + message);
073        }
074        
075        Set<String> phoneNumberList = _subscriberDAO.getPhoneNumbersFromList(phoneList).keySet();
076        if (phoneNumberList.size() == 0)
077        {
078            throw new IllegalArgumentException("The user '" + user.toString() + "' tried to send a message to the empty list '" + phoneList + "'");
079        }
080        
081        _broker.send(phoneNumberList, message, phoneList);
082
083        return EMPTY_MAP;
084    }
085
086}