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.plugins.repository.lock;
017
018import java.time.ZoneId;
019import java.time.ZoneOffset;
020import java.time.ZonedDateTime;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.ametys.core.schedule.Runnable;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * A {@link Runnable} which schedules a {@link UnlockSchedulable} for unlocking an object.
033 */
034public class UnlockRunnable implements Runnable
035{
036    /** The id of the object to unlock */
037    protected String _objectId;
038    /** The name of the object to unlock */
039    protected String _objectName;
040    /** The date when to unlock the content */
041    protected ZonedDateTime _date;
042    /** The {@link UserIdentity} to launch the runnable task */
043    protected UserIdentity _userIdentity;
044
045    /**
046     * Constructor
047     * @param objectId The id of the object to unlock
048     * @param objectName The name of the object to unlock
049     * @param date The date when to unlock the content
050     * @param userIdentity The {@link UserIdentity} to launch the runnable task
051     */
052    public UnlockRunnable(String objectId, String objectName, ZonedDateTime date, UserIdentity userIdentity)
053    {
054        _objectId = objectId;
055        _objectName = objectName;
056        _date = date.withZoneSameInstant(ZoneId.systemDefault());
057        _userIdentity = userIdentity;
058    }
059    
060    @Override
061    public String getId()
062    {
063        return this.getClass().getName() + "." + _objectId;
064    }
065
066    @Override
067    public I18nizableText getLabel()
068    {
069        return new I18nizableText("plugin.repository", "PLUGINS_REPOSITORY_UNLOCK_RUNNABLE_LABEL", Collections.singletonList(_objectName));
070    }
071
072    @Override
073    public I18nizableText getDescription()
074    {
075        List<String> parameters = new ArrayList<>();
076        parameters.add(_objectId);
077        parameters.add(_objectName);
078        return new I18nizableText("plugin.repository", "PLUGINS_REPOSITORY_UNLOCK_RUNNABLE_DESCRIPTION", parameters);
079    }
080    
081    @Override
082    public FireProcess getFireProcess()
083    {
084        return FireProcess.CRON;
085    }
086
087    @Override
088    public String getCronExpression()
089    {
090        return _date.getSecond() + " " + _date.getMinute() + " " + _date.getHour() + " " + _date.getDayOfMonth() + " " + _date.getMonthValue() + " ? " + _date.getYear();
091    }
092
093    @Override
094    public String getSchedulableId()
095    {
096        return "org.ametys.plugins.repository.lock.UnlockSchedulable";
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(UnlockSchedulable.OBJECT_ID_KEY, _objectId);
134        return values;
135    }
136
137    @Override
138    public UserIdentity getUserIdentity()
139    {
140        return _userIdentity;
141    }
142}