001/*
002 *  Copyright 2015 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.stop;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.acting.ServiceableAction;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.SourceResolver;
026
027import org.ametys.plugins.sms.SMSHelper;
028import org.ametys.plugins.sms.broker.Broker;
029import org.ametys.plugins.sms.dao.SubscriberDAO;
030
031/**
032 * Unsubscribe the sender of the a STOP request
033 */
034public class UnsubscribeAction extends ServiceableAction
035{
036    /** The subscriber DAO */
037    private SubscriberDAO _subscriberDAO;
038
039    /** The broker */
040    private Broker _broker;
041    
042    @Override
043    public void service(ServiceManager serviceManager) throws ServiceException
044    {
045        super.service(serviceManager);
046        _broker = (Broker) serviceManager.lookup(Broker.ROLE);
047        _subscriberDAO = (SubscriberDAO) serviceManager.lookup(SubscriberDAO.ROLE);
048    }
049    
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        String phoneNumber = _broker.getPhoneNumberFromStopRequest();
053        if (phoneNumber != null)
054        {
055            if (!SMSHelper.PHONE_NUMBER_INTERNATIONAL_VALIDATOR.matcher(phoneNumber).matches())
056            {   
057                throw new IllegalArgumentException("The phone number [" + phoneNumber + "] is invalid.");
058            }
059            
060            _subscriberDAO.deleteNumber(phoneNumber, null);
061        }
062        
063        return EMPTY_MAP;
064    }
065}