001/*
002 *  Copyright 2021 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.plugins.workspaces.project.notification;
017
018import java.time.LocalTime;
019import java.time.OffsetTime;
020import java.time.ZoneOffset;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.configuration.DefaultConfiguration;
025
026import org.ametys.plugins.core.impl.schedule.StaticRunnable;
027import org.ametys.runtime.config.Config;
028
029/**
030 * Runnable for notification summary
031 *
032 */
033public class SendNotificationSummaryRunnable extends StaticRunnable
034{
035    @Override
036    public void configure(Configuration configuration) throws ConfigurationException
037    {
038        DefaultConfiguration modifiedConfiguration = new DefaultConfiguration(configuration);
039        
040        String frequency = configuration.getChild("frequency").getValue("daily");
041        
042        // Set our custom cron
043        String hourParamName = configuration.getChild("param-hour").getValue("").trim();
044        String hourCfg = Config.getInstance().getValue(hourParamName);
045        if (hourCfg != null)
046        {
047            LocalTime lt = LocalTime.parse(hourCfg);
048            OffsetTime utcTime = lt.atOffset(OffsetTime.now().getOffset()).withOffsetSameInstant(ZoneOffset.UTC);
049            
050            String cronExpression = "0 " /*seconds*/ + utcTime.getMinute() /*minutes*/ + " " + utcTime.getHour() /*hour*/;
051            
052            if ("daily".equals(frequency))
053            {
054                cronExpression += " * * ? *" /* every day, on every month, without restriction on day-of-week, on every year*/;
055            }
056            else if ("weekly".equals(frequency))
057            {
058                cronExpression += " ? * MON *"; /* every monday */
059            }
060            else if ("monthly".equals(frequency))
061            {
062                cronExpression += " 1 * ? *" /* 1st day of every month */;
063            }
064            
065            DefaultConfiguration cronConfig = new DefaultConfiguration("cron");
066            cronConfig.setValue(cronExpression);
067            modifiedConfiguration.addChild(cronConfig);
068        }
069        else
070        {
071            // FireProcess = never
072            DefaultConfiguration fireProcessConfig = new DefaultConfiguration("fire-process");
073            fireProcessConfig.setValue("never");
074            modifiedConfiguration.addChild(fireProcessConfig);
075        }
076        
077        // Call configure of StaticRunnable
078        super.configure(modifiedConfiguration);
079    }
080    
081}