001/*
002 *  Copyright 2010 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.newsletter.category;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.SourceResolver;
028
029import org.ametys.core.observation.AbstractNotifierAction;
030
031/**
032 * Apply a template to a given newsletter category
033 */
034public class ApplyTemplatesAction extends AbstractNotifierAction
035{
036    private CategoryProviderExtensionPoint _categoryProviderEP;
037    
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042        _categoryProviderEP = (CategoryProviderExtensionPoint) smanager.lookup(CategoryProviderExtensionPoint.ROLE);
043    }
044    
045    @Override
046    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
047    {
048        @SuppressWarnings("unchecked")
049        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
050        
051        @SuppressWarnings("unchecked")
052        List<String> ids = (List<String>) jsParameters.get("id");
053        String template = (String) jsParameters.get("template");
054
055        Set<String> providerIDs = _categoryProviderEP.getExtensionsIds();
056        
057        for (String id : ids)
058        {
059            for (String providerID : providerIDs)
060            {
061                CategoryProvider provider = _categoryProviderEP.getExtension(providerID);
062                if (provider.hasCategory(id))
063                {
064                    Category category = provider.getCategory(id);
065                    provider.setTemplate(category, template);
066                }
067                
068            }
069        }
070        
071        return EMPTY_MAP;
072    }
073   
074}