001/*
002 *  Copyright 2017 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.odfpilotage.tool;
017
018import java.io.File;
019import java.io.FileFilter;
020import java.time.ZonedDateTime;
021
022import org.ametys.core.util.DateUtils;
023
024/**
025 * File filter for pilotage files and search of these files.
026 */
027public class PilotageFileFilter implements FileFilter
028{
029    private ZonedDateTime _lastModifiedAfter;
030    private ZonedDateTime _lastModifiedBefore;
031    private String _fileName;
032    
033    /**
034     * Constructor
035     * @param fileName Filter on the filename
036     * @param lastModifiedAfter Filter on the date after
037     * @param lastModifiedBefore Filter on the date before
038     */
039    public PilotageFileFilter(String fileName, ZonedDateTime lastModifiedAfter, ZonedDateTime lastModifiedBefore)
040    {
041        _fileName = fileName;
042        _lastModifiedAfter = lastModifiedAfter;
043        _lastModifiedBefore = lastModifiedBefore;
044    }
045    
046    @Override
047    public boolean accept(File pathname)
048    {
049        /* On accepte que les fichiers */
050        if (pathname.isDirectory())
051        {
052            return false;
053        }
054        
055        /* Tests sur le nom de fichier */
056        String fileName = pathname.getName().toLowerCase();
057        
058        if (!fileName.matches(".*\\.zip"))
059        {
060            return false;
061        }
062        if (_fileName != null && !fileName.contains(_fileName.toLowerCase()))
063        {
064            return false;
065        }
066
067        /* Tests sur la date de dernière modification */
068        ZonedDateTime lastModified = DateUtils.asZonedDateTime(pathname.lastModified());
069        if (_lastModifiedAfter != null && lastModified.isBefore(_lastModifiedAfter))
070        {
071            return false;
072        }
073        if (_lastModifiedBefore != null && lastModified.isAfter(_lastModifiedBefore))
074        {
075            return false;
076        }
077        
078        return true;
079    }
080}