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.broker;
017
018import java.util.Set;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.core.user.CurrentUserProvider;
028
029/**
030 * Log informations (message, receivers and sender)
031 */
032public class LoggerBroker extends AbstractLogEnabled implements Broker, Serviceable
033{
034    /** The user provider */
035    protected CurrentUserProvider _currentUserProvider;
036
037    /** Avalon component resolving ametys objects */
038    protected AmetysObjectResolver _ametysObjectResolver;
039    
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
043        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
044    }
045    
046    public void send(Set<String> phoneNumbers, String message, String listId) throws Exception
047    {
048        if (getLogger().isInfoEnabled())
049        {
050            getLogger().info("The user '" + _currentUserProvider.getUser() + "' send a sms message to the category :" + getCategory(listId) + ". The recipients phone numbers are : " + phoneNumbers.toString() + ". The message is: " + message);
051        }
052    }
053
054    public String getPhoneNumberFromStopRequest() throws Exception
055    {
056        throw new UnsupportedOperationException("The LoggerBroker does not support STOP request");
057    }
058    
059    /**
060     * Computes the name of the subscribers' list node corresponding to the list id passed as a parameter
061     * @param listId the id of the jcr node of the subscribers list
062     * @return the name of the category
063     */
064    protected String getCategory (String listId)
065    {
066        AmetysObject subscribers = _ametysObjectResolver.resolveById(listId);
067        return subscribers.getName();
068    }
069
070}