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.ZonedDateTime;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.ametys.core.schedule.Runnable;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.runtime.i18n.I18nizableText;
029
030/**
031 * A {@link Runnable} which schedules a {@link UnlockSchedulable} for unlocking an object.
032 */
033public class UnlockRunnable implements Runnable
034{
035    /** The id of the object to unlock */
036    protected String _objectId;
037    /** The name of the object to unlock */
038    protected String _objectName;
039    /** The date when to unlock the content */
040    protected ZonedDateTime _date;
041    /** The {@link UserIdentity} to launch the runnable task */
042    protected UserIdentity _userIdentity;
043
044    /**
045     * Constructor
046     * @param objectId The id of the object to unlock
047     * @param objectName The name of the object to unlock
048     * @param date The date when to unlock the content
049     * @param userIdentity The {@link UserIdentity} to launch the runnable task
050     */
051    public UnlockRunnable(String objectId, String objectName, ZonedDateTime date, UserIdentity userIdentity)
052    {
053        _objectId = objectId;
054        _objectName = objectName;
055        _date = date.withZoneSameInstant(ZoneId.systemDefault());
056        _userIdentity = userIdentity;
057    }
058    
059    @Override
060    public String getId()
061    {
062        return this.getClass().getName() + "." + _objectId;
063    }
064
065    @Override
066    public I18nizableText getLabel()
067    {
068        return new I18nizableText("plugin.repository", "PLUGINS_REPOSITORY_UNLOCK_RUNNABLE_LABEL", Collections.singletonList(_objectName));
069    }
070
071    @Override
072    public I18nizableText getDescription()
073    {
074        List<String> parameters = new ArrayList<>();
075        parameters.add(_objectId);
076        parameters.add(_objectName);
077        return new I18nizableText("plugin.repository", "PLUGINS_REPOSITORY_UNLOCK_RUNNABLE_DESCRIPTION", parameters);
078    }
079    
080    @Override
081    public FireProcess getFireProcess()
082    {
083        return FireProcess.CRON;
084    }
085
086    @Override
087    public String getCronExpression()
088    {
089        return _date.getSecond() + " " + _date.getMinute() + " " + _date.getHour() + " " + _date.getDayOfMonth() + " " + _date.getMonthValue() + " ? " + _date.getYear();
090    }
091
092    @Override
093    public String getSchedulableId()
094    {
095        return "org.ametys.plugins.repository.lock.UnlockSchedulable";
096    }
097
098    @Override
099    public boolean isRemovable()
100    {
101        return false;
102    }
103
104    @Override
105    public boolean isModifiable()
106    {
107        return false;
108    }
109
110    @Override
111    public boolean isDeactivatable()
112    {
113        return false;
114    }
115
116    @Override
117    public MisfirePolicy getMisfirePolicy()
118    {
119        return MisfirePolicy.FIRE_ONCE;
120    }
121
122    @Override
123    public boolean isVolatile()
124    {
125        return false;
126    }
127
128    @Override
129    public Map<String, Object> getParameterValues()
130    {
131        Map<String, Object> values = new HashMap<>();
132        values.put(UnlockSchedulable.OBJECT_ID_KEY, _objectId);
133        return values;
134    }
135
136    @Override
137    public UserIdentity getUserIdentity()
138    {
139        return _userIdentity;
140    }
141}