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.nio.file.Files;
019import java.nio.file.Path;
020import java.util.ArrayList;
021import java.util.List;
022import java.util.stream.Collectors;
023
024import org.apache.commons.lang3.StringUtils;
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                _absolutePaths.add(StringUtils.substringAfter(file.toUri().toString(), "file:"));
051            });
052        
053        _timeStamp = _getTimeStamp(getFile());
054    }
055
056    @Override
057    public Path getFile()
058    {
059        if (_files == null)
060        {
061            _files = _absolutePaths.stream()
062                    .map(PathTimeStampValidity::_getFile)
063                    .collect(Collectors.toList());
064        }
065        
066        for (Path file : _files)
067        {
068            if (Files.exists(file))
069            {
070                return file;
071            }
072        }
073        
074        return _files.get(0);
075    }
076}