001/*
002 *  Copyright 2016 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.publication;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.quartz.JobDataMap;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.ObservationManager;
028import org.ametys.core.schedule.Schedulable;
029import org.ametys.core.schedule.progression.ContainerProgressionTracker;
030import org.ametys.core.user.CurrentUserProvider;
031import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
032import org.ametys.plugins.core.schedule.Scheduler;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.web.ObservationConstants;
035import org.ametys.web.repository.page.ModifiableSitemapElement;
036
037/**
038 * A {@link Schedulable} job which publishes/unpublishes a page.
039 */
040public class PublishOrUnpublishPageSchedulable extends AbstractStaticSchedulable
041{
042    /** The key for the id of the page to publish */
043    public static final String PAGE_ID_KEY = "pageId";
044    
045    private static final String _JOBDATAMAP_PAGE_ID_KEY = Scheduler.PARAM_VALUES_PREFIX + PAGE_ID_KEY;
046    
047    /** The observation manager */
048    protected ObservationManager _observationManager;
049    /** The Ametys object resolver */
050    protected AmetysObjectResolver _resolver;
051    /** The current user provider */
052    protected CurrentUserProvider _currentUserProvider;
053    
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        super.service(manager);
058        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
059        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
060        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
061    }
062    
063    @Override
064    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
065    {
066        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
067        String pageId = (String) jobDataMap.get(_JOBDATAMAP_PAGE_ID_KEY);
068        ModifiableSitemapElement sitemapElement = _resolver.resolveById(pageId);
069        
070        getLogger().info("Trying to publish/unpublish the page '{}'", pageId);
071        Map<String, Object> eventParams = new HashMap<>();
072        eventParams.put(ObservationConstants.ARGS_PAGE, sitemapElement);
073        _observationManager.notify(new Event(ObservationConstants.EVENT_PAGE_CHANGED, _currentUserProvider.getUser(), eventParams));
074    }
075}