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.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.web.ObservationConstants;
030import org.ametys.web.repository.site.Site;
031
032/**
033 * Memorizes previous value of the autoposting site parameter to be able to compare it later.
034 */
035public class TagAutopostingSiteParameterObserverPart1 extends AbstractLogEnabled implements Observer, Contextualizable
036{
037    /** Site autoposting request attribute name */
038    public static final String SITE_TAGS_AUTOPOSTING_REQUEST_ATTR = "site.updating.autoposting.old.value";
039    
040    /** The avalon context */
041    protected Context _context;
042    
043    @Override
044    public void contextualize(Context context) throws ContextException
045    {
046        _context = context;
047    }
048    
049    @Override
050    public boolean supports(Event event)
051    {
052        return event.getId().equals(ObservationConstants.EVENT_SITE_UPDATING);
053    }
054    
055    @Override
056    public int getPriority(Event event)
057    {
058        return MAX_PRIORITY + 3000;
059    }
060    
061    public void observe(Event event, Map<String, Object> transientVars) throws Exception
062    {
063        Site site = (Site) event.getArguments().get(ObservationConstants.ARGS_SITE);
064        Boolean autoposting = site.getValue("tags-autoposting");
065        
066        // Saving previous "autoposting" parameter value as a request attribute.
067        Request request = ContextHelper.getRequest(_context);
068        request.setAttribute(SITE_TAGS_AUTOPOSTING_REQUEST_ATTR, autoposting);
069        
070    }
071}
072