001/*
002 *  Copyright 2020 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.contentio.archive;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Optional;
022
023import org.ametys.core.schedule.Runnable;
024import org.ametys.core.user.UserIdentity;
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * A {@link Runnable} which schedules a {@link ImportArchiveSchedulable} for importing data from an archive.
029 */
030public class ImportArchiveRunnable implements Runnable
031{
032    private static final String __ID_PREFIX = ImportArchiveRunnable.class.getName();
033    
034    private String _id;
035    private String _archiveName;
036    private Optional<Collection<String>> _elements;
037    private MergePolicy _mergePolicy;
038    private UserIdentity _userIdentity;
039    
040    /**
041     * Constructor
042     * @param archiveName The name of the archive to import
043     * @param elements The elements to import
044     * @param mergePolicy The {@link MergePolicy}
045     * @param user The user identity
046     */
047    public ImportArchiveRunnable(String archiveName, Optional<Collection<String>> elements, MergePolicy mergePolicy, UserIdentity user)
048    {
049        _setId();
050        _archiveName = archiveName;
051        _elements = elements;
052        _mergePolicy = mergePolicy;
053        _userIdentity = user;
054    }
055    
056    private void _setId()
057    {
058        _id = __ID_PREFIX + "$" + System.currentTimeMillis();
059    }
060    
061    @Override
062    public String getId()
063    {
064        return _id;
065    }
066
067    @Override
068    public I18nizableText getLabel()
069    {
070        return new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_IMPORT_RUNNABLE_LABEL");
071    }
072
073    @Override
074    public I18nizableText getDescription()
075    {
076        return new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_IMPORT_RUNNABLE_DESC");
077    }
078
079    @Override
080    public FireProcess getFireProcess()
081    {
082        return FireProcess.NOW;
083    }
084
085    @Override
086    public String getCronExpression()
087    {
088        return null;
089    }
090
091    @Override
092    public String getSchedulableId()
093    {
094        return ImportArchiveSchedulable.ID;
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 null;
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(ImportArchiveSchedulable.ARCHIVE_KEY, _archiveName);
132        values.put(ImportArchiveSchedulable.MERGE_POLICY_KEY, _mergePolicy);
133        _elements.ifPresent(els -> values.put(ImportArchiveSchedulable.ELEMENTS_KEY, els));
134        return values;
135    }
136
137    @Override
138    public UserIdentity getUserIdentity()
139    {
140        return _userIdentity;
141    }
142
143}