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.io.IOException;
019import java.util.Collection;
020import java.util.List;
021import java.util.Map;
022import java.util.Objects;
023import java.util.Optional;
024import java.util.stream.Collectors;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.quartz.SchedulerException;
029
030import org.ametys.core.ui.Callable;
031import org.ametys.core.ui.ClientSideElement;
032import org.ametys.core.ui.StaticClientSideElement;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.core.schedule.Scheduler;
035
036import com.google.common.base.Predicates;
037
038/**
039 * {@link ClientSideElement} for import archive button.
040 */
041public class ImportArchiveClientSideElement extends StaticClientSideElement
042{
043    private ArchiveHandler _archiveHandler;
044    private Scheduler _scheduler;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _archiveHandler = (ArchiveHandler) smanager.lookup(ArchiveHandler.ROLE);
051        _scheduler = (Scheduler) smanager.lookup(Scheduler.ROLE);
052    }
053    
054    /**
055     * Gets as JSON the {@link MergePolicy MergePolicies}
056     * @return the merge policies
057     */
058    @Callable
059    public List<Map<String, Object>> getMergePolicies()
060    {
061        return MergePolicy._ids
062                .values()
063                .stream()
064                .map(MergePolicy::toJson)
065                .collect(Collectors.toList());
066    }
067    
068    /**
069     * Enumerates the archive files managed by the archive mechanism
070     * @return the archive files
071     */
072    @Callable
073    public List<Map<String, String>> getArchiveFiles()
074    {
075        return _archiveHandler.getArchiveFiles()
076                .map(label -> Map.of("archiveName", label))
077                .collect(Collectors.toList());
078    }
079    
080    /**
081     * Enumerates the available partial imports of the given archive
082     * @param archiveName The archive name
083     * @return the available partial imports
084     * @throws IOException if an IO exception occured
085     */
086    @Callable
087    public List<Map<String, Object>> getAvailablePartialImports(String archiveName) throws IOException
088    {
089        return _archiveHandler.getPartialImports(archiveName)
090                .map(PartialImport::toJson)
091                .collect(Collectors.toList());
092    }
093    
094    /**
095     * Schedules the import
096     * @param values The client side values
097     * @return A result map
098     */
099    @Callable
100    public Map<String, Object> scheduleImport(Map<String, Object> values)
101    {
102        UserIdentity user = _currentUserProvider.getUser();
103        String archiveName = Objects.requireNonNull((String) values.get(ImportArchiveSchedulable.ARCHIVE_KEY));
104        Optional<Collection<String>> clientSideElements = Optional.of(ImportArchiveSchedulable.ELEMENTS_KEY)
105                .map(values::get)
106                .filter(Collection.class::isInstance)
107                .map(Collection.class::cast);
108        Optional<Collection<String>> elements = clientSideElements
109                .filter(Predicates.not(Collection::isEmpty));
110        MergePolicy mergePolicy = MergePolicy.forId((String) values.get(ImportArchiveSchedulable.MERGE_POLICY_KEY));
111        
112        try
113        {
114            _scheduler.scheduleJob(new ImportArchiveRunnable(archiveName, elements, mergePolicy, user));
115        }
116        catch (SchedulerException e)
117        {
118            getLogger().error("An exception occured when trying to schedule archive import", e);
119            return Map.of("ok", false);
120        }
121        return Map.of("ok", true);
122    }
123}
124