001/*
002 *  Copyright 2017 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.time.ZonedDateTime;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.ametys.core.schedule.Runnable;
023import org.ametys.core.user.UserIdentity;
024
025/**
026 * A {@link Runnable} which schedules a {@link PublishOrUnpublishPageSchedulable} task for (un)publishing a page.
027 */
028public abstract class AbstractPublishOrUnpublishPageRunnable implements Runnable
029{
030    /** The id of the page to (un)publish */
031    protected String _pageId;
032    /** The name of the page to (un)publish */
033    protected String _pageName;
034    /** The {@link UserIdentity} to launch the runnable task */
035    protected UserIdentity _userIdentity;
036    /** The date when to (un)publish the page */
037    protected ZonedDateTime _date;
038
039    /**
040     * Constructor
041     * @param pageId The id of the page to (un)publish
042     * @param pageName The name of the page to (un)publish
043     * @param userIdentity The {@link UserIdentity} to launch the runnable task
044     * @param date The date when to (un)publish the page
045     */
046    public AbstractPublishOrUnpublishPageRunnable(String pageId, String pageName, UserIdentity userIdentity, ZonedDateTime date)
047    {
048        _pageId = pageId;
049        _date = date;
050        _pageName = pageName;
051        _userIdentity = userIdentity;
052    }
053    
054    @Override
055    public String getId()
056    {
057        return this.getClass().getName() + "." + _pageId;
058    }
059        
060    @Override
061    public FireProcess getFireProcess()
062    {
063        return FireProcess.CRON;
064    }
065
066    @Override
067    public String getCronExpression()
068    {
069        return _date.getSecond() + " " + _date.getMinute() + " " + _date.getHour() + " " + _date.getDayOfMonth() + " " + _date.getMonthValue() + " ? " + _date.getYear();
070    }
071
072    @Override
073    public String getSchedulableId()
074    {
075        return "org.ametys.web.publication.schedule.PublishOrUnpublish";
076    }
077
078    @Override
079    public boolean isRemovable()
080    {
081        return false;
082    }
083
084    @Override
085    public boolean isModifiable()
086    {
087        return false;
088    }
089
090    @Override
091    public boolean isDeactivatable()
092    {
093        return false;
094    }
095    
096    @Override
097    public MisfirePolicy getMisfirePolicy()
098    {
099        return MisfirePolicy.FIRE_ONCE;
100    }
101    
102    @Override
103    public boolean isVolatile()
104    {
105        return false;
106    }
107
108    @Override
109    public Map<String, Object> getParameterValues()
110    {
111        Map<String, Object> values = new HashMap<>();
112        values.put(PublishOrUnpublishPageSchedulable.PAGE_ID_KEY, _pageId);
113        return values;
114    }
115
116    @Override
117    public UserIdentity getUserIdentity()
118    {
119        return _userIdentity;
120    }
121}