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.time.Instant;
019import java.time.LocalDateTime;
020import java.time.ZoneId;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Date;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.ametys.core.schedule.Runnable;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * A {@link Runnable} which schedules a {@link PublishOrUnpublishPageSchedulable} task for unpublishing a page.
033 */
034public class UnpublishPageRunnable implements Runnable
035{
036    /** The id of the page to unpublish */
037    protected String _pageId;
038    /** The name of the page to unpublish */
039    protected String _pageName;
040    /** The date when to unpublish the page */
041    protected Date _date;
042
043    /**
044     * Constructor
045     * @param pageId The id of the page to unpublish
046     * @param pageName The name of the page to unpublish
047     * @param date The date when to unpublish the page
048     */
049    public UnpublishPageRunnable(String pageId, String pageName, Date date)
050    {
051        _pageId = pageId;
052        _date = date;
053        _pageName = pageName;
054    }
055    
056    @Override
057    public String getId()
058    {
059        return this.getClass().getName() + "." + _pageId;
060    }
061    
062    @Override
063    public I18nizableText getLabel()
064    {
065        return new I18nizableText("plugin.web", "PLUGINS_WEB_RUNNABLE_UNPUBLISH_PAGE_LABEL", Collections.singletonList(_pageName));
066    }
067
068    @Override
069    public I18nizableText getDescription()
070    {
071        List<String> parameters = new ArrayList<>();
072        parameters.add(_pageId);
073        parameters.add(_pageName);
074        return new I18nizableText("plugin.web", "PLUGINS_WEB_RUNNABLE_UNPUBLISH_PAGE_DESCRIPTION", parameters);
075    }
076
077    @Override
078    public FireProcess getFireProcess()
079    {
080        return FireProcess.CRON;
081    }
082
083    @Override
084    public String getCronExpression()
085    {
086        // Convert java.util.Date to java.time.LocalDateTime
087        Instant instant = Instant.ofEpochMilli(_date.getTime());
088        LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());        
089        // Then tranform it to a Cron Expression
090        return ldt.getSecond() + " " + ldt.getMinute() + " " + ldt.getHour() + " " + ldt.getDayOfMonth() + " " + ldt.getMonthValue() + " ? " + ldt.getYear();
091    }
092
093    @Override
094    public String getSchedulableId()
095    {
096        return "org.ametys.web.publication.schedule.PublishOrUnpublish";
097    }
098
099    @Override
100    public boolean isRemovable()
101    {
102        return false;
103    }
104
105    @Override
106    public boolean isModifiable()
107    {
108        return false;
109    }
110
111    @Override
112    public boolean isDeactivatable()
113    {
114        return false;
115    }
116    
117    @Override
118    public MisfirePolicy getMisfirePolicy()
119    {
120        return MisfirePolicy.FIRE_ONCE;
121    }
122
123    @Override
124    public boolean isVolatile()
125    {
126        return false;
127    }
128
129    @Override
130    public Map<String, Object> getParameterValues()
131    {
132        Map<String, Object> values = new HashMap<>();
133        values.put(PublishOrUnpublishPageSchedulable.PAGE_ID_KEY, _pageId);
134        return values;
135    }
136}