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