001/*
002 *  Copyright 2025 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 */
016
017package org.ametys.web.cache.scheduler;
018
019import java.time.ZonedDateTime;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.ametys.core.schedule.Runnable;
025import org.ametys.core.user.UserIdentity;
026import org.ametys.core.user.population.UserPopulationDAO;
027import org.ametys.runtime.i18n.I18nizableText;
028
029/**
030 * A {@link Runnable} which schedules a {@link InvalidateRemoteFOCacheSchedulable} for invalidating a site cache.
031 */
032public class InvalidateRemoteFOCacheRunnable implements Runnable
033{
034    private String _siteName;
035    private ZonedDateTime _invalidationDate;
036    
037    /**
038     * The constructor.
039     * @param siteName The site name
040     */
041    public InvalidateRemoteFOCacheRunnable(String siteName)
042    {
043        _siteName = siteName;
044    }
045    
046    /**
047     * Set the date to execute the scheduable.
048     * @param invalidationDate The date
049     */
050    public void setInvalidationDate(ZonedDateTime invalidationDate)
051    {
052        _invalidationDate = invalidationDate;
053    }
054    
055    public String getId()
056    {
057        return this.getClass().getName() + "." + _siteName;
058    }
059    
060    public I18nizableText getLabel()
061    {
062        return new I18nizableText("plugin.web", "PLUGINS_WEB_RUNNABLE_INVALIDATE_REMOTE_FO_CACHE_LABEL", List.of(_siteName));
063    }
064    
065    public I18nizableText getDescription()
066    {
067        return new I18nizableText("plugin.web", "PLUGINS_WEB_RUNNABLE_INVALIDATE_REMOTE_FO_CACHE_DESCRIPTION", List.of(_siteName));
068    }
069    
070    public String getSchedulableId()
071    {
072        return InvalidateRemoteFOCacheSchedulable.class.getName();
073    }
074    
075    public FireProcess getFireProcess()
076    {
077        if (_invalidationDate.isAfter(ZonedDateTime.now()))
078        {
079            return FireProcess.CRON;
080        }
081        return FireProcess.NOW;
082    }
083    
084    public String getCronExpression()
085    {
086        return _invalidationDate.getSecond() + " " + _invalidationDate.getMinute() + " " + _invalidationDate.getHour() + " " + _invalidationDate.getDayOfMonth() + " " + _invalidationDate.getMonthValue() + " ? " + _invalidationDate.getYear();
087    }
088    
089    public boolean isRemovable()
090    {
091        return true;
092    }
093
094    public boolean isModifiable()
095    {
096        return false;
097    }
098
099    public boolean isDeactivatable()
100    {
101        return false;
102    }
103
104    public MisfirePolicy getMisfirePolicy()
105    {
106        return MisfirePolicy.FIRE_ONCE;
107    }
108
109    public boolean isVolatile()
110    {
111        return true;
112    }
113
114    @Override
115    public Map<String, Object> getParameterValues()
116    {
117        Map<String, Object> values = new HashMap<>();
118        values.put(InvalidateRemoteFOCacheSchedulable.SITENAME_KEY, _siteName);
119        return values;
120    }
121
122    @Override
123    public UserIdentity getUserIdentity()
124    {
125        return UserPopulationDAO.SYSTEM_USER_IDENTITY;
126    }
127}