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.service;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import javax.jcr.RepositoryException;
023
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030import org.apache.cocoon.ProcessingException;
031import org.apache.cocoon.components.ContextHelper;
032import org.apache.cocoon.environment.Request;
033
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.sms.dao.JCRSubscribersList;
036import org.ametys.plugins.sms.dao.SmsListDAO;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.model.Enumerator;
039import org.ametys.web.WebConstants;
040import org.ametys.web.repository.page.Page;
041
042/**
043 * Enumerate all the SubscribersList
044 */
045public class SubscribersListEnumerator implements Enumerator<String>, Serviceable, Contextualizable, org.ametys.runtime.parameter.Enumerator
046{
047    /** Subscriber list DAO */
048    private SmsListDAO _subscriberListDAO;
049    
050    /** The Ametys object resolver */
051    private AmetysObjectResolver _ametysObjectResolver;
052    
053    /** The Avalon context */
054    private Context _context;
055    
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _subscriberListDAO = (SmsListDAO) manager.lookup(SmsListDAO.ROLE);
059        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
060    }
061    
062    public void contextualize(Context context) throws ContextException
063    {
064        _context = context;
065    }
066
067    public I18nizableText getEntry(String value) throws Exception
068    {
069        return getEntries().get(value);
070    }
071
072    public Map<Object, I18nizableText> getEntries() throws Exception
073    {
074        return (Map<Object, I18nizableText>) (Object) getTypedEntries();
075    }
076    
077    public Map<String, I18nizableText> getTypedEntries() throws Exception
078    {
079        Request request = ContextHelper.getRequest(_context); 
080       
081        String siteName = (String) request.getAttribute("siteName");  
082        String pageId = (String) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE_ID);
083        
084        Page page = _ametysObjectResolver.resolveById(pageId);
085        String language = page.getSitemapName();
086        
087        Map<String, I18nizableText> entries = new HashMap<>();
088        
089        // We get all the subscribers list for the request's site and language
090        try
091        {
092            I18nizableText label = new I18nizableText("plugin.sms", "PLUGINS_SMS_SUBSCRIPTION_SERVICE_REGISTER_TYPE_USER_CHOICE");
093            entries.put("-", label);
094            
095            for (JCRSubscribersList subList : _subscriberListDAO.getSubscribersLists(siteName, language))
096            {    
097                label = new I18nizableText(subList.getTitle());
098                entries.put(subList.getId(), label);
099            }
100        }
101        catch (RepositoryException e)
102        {
103            throw new ProcessingException("An error occurred while getting the subscribers' lists for site: '" + siteName + "' and language '" + language + "'.", e);
104        }
105        
106        return entries;
107    }
108    
109    @Override
110    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
111    public Map<String, Object> getConfiguration()
112    {
113        return Collections.EMPTY_MAP;
114    }
115}