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.web.tags.observers;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.Request;
029
030import org.ametys.core.observation.Event;
031import org.ametys.core.observation.Observer;
032import org.ametys.web.ObservationConstants;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.site.SiteConfigurationExtensionPoint;
035
036/**
037 * Memorizes previous value of the autoposting site parameter to be able to compare it later.
038 */
039public class TagAutopostingSiteParameterObserverPart1 extends AbstractLogEnabled implements Observer, Contextualizable, Serviceable
040{
041    /** Site autoposting request attribute name */
042    public static final String SITE_TAGS_AUTOPOSTING_REQUEST_ATTR = "site.updating.autoposting.old.value";
043    
044    /** The avalon context */
045    protected Context _context;
046    
047    /** The site configuration EP */
048    protected SiteConfigurationExtensionPoint _scep;
049
050    @Override
051    public void contextualize(Context context) throws ContextException
052    {
053        _context = context;
054    }
055    
056    @Override
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _scep = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE);
060    }
061    
062    @Override
063    public boolean supports(Event event)
064    {
065        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATING);
066    }
067    
068    @Override
069    public int getPriority(Event event)
070    {
071        return MAX_PRIORITY + 3000;
072    }
073    
074    public void observe(Event event, Map<String, Object> transientVars) throws Exception
075    {
076        Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
077        Boolean autoposting = _scep.getValueAsBoolean(site.getName(), "tags-autoposting");
078        
079        // Saving previous "autoposting" parameter value as a request attribute.
080        Request request = ContextHelper.getRequest(_context);
081        request.setAttribute(SITE_TAGS_AUTOPOSTING_REQUEST_ATTR, autoposting);
082        
083    }
084}
085