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