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