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.web.source;
017
018import java.net.URI;
019import java.net.URISyntaxException;
020import java.nio.file.Files;
021import java.nio.file.Path;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.stream.Collectors;
025
026import org.ametys.core.util.path.PathTimeStampValidity;
027
028/**
029 * A validation object for time-stamps.
030 */
031public class SkinTimeStampValidity extends PathTimeStampValidity
032{
033    private transient List<Path> _files;
034    private List<String> _absolutePaths;
035
036    /**
037     * Creates from a list of files
038     * @param files files wrapped
039     */
040    public SkinTimeStampValidity(final List<Path> files)
041    {
042        super();
043        
044        _files = files;
045        _absolutePaths = new ArrayList<>();
046        
047        _files.stream()
048            .forEach(file -> 
049            {
050                try
051                {
052                    _absolutePaths.add((new URI(file.toUri().getRawSchemeSpecificPart())).getPath());
053                }
054                catch (URISyntaxException e)
055                {
056                    throw new RuntimeException("Cannot convert " + file.toString() + " to URI", e);
057                }
058            });
059        
060        _timeStamp = _getTimeStamp(getFile());
061    }
062
063    @Override
064    public Path getFile()
065    {
066        if (_files == null)
067        {
068            _files = _absolutePaths.stream()
069                    .map(PathTimeStampValidity::_getFile)
070                    .collect(Collectors.toList());
071        }
072        
073        for (Path file : _files)
074        {
075            if (Files.exists(file))
076            {
077                return file;
078            }
079        }
080        
081        return _files.get(0);
082    }
083}