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