001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     <a href="mailto:nulrich@nuxeo.com">Nicolas Ulrich</a>
018 *
019 */
020
021package org.nuxeo.business.days.management.checker;
022
023import java.io.BufferedReader;
024import java.io.FileNotFoundException;
025import java.io.FileReader;
026import java.io.IOException;
027import java.io.InputStream;
028import java.io.InputStreamReader;
029import java.io.Reader;
030import java.text.ParseException;
031import java.text.SimpleDateFormat;
032import java.util.Date;
033import java.util.HashSet;
034import java.util.Iterator;
035import java.util.Set;
036
037import org.apache.commons.logging.Log;
038import org.apache.commons.logging.LogFactory;
039import org.nuxeo.business.days.management.checker.HolidaysChecker;
040import org.nuxeo.runtime.model.ComponentInstance;
041import org.nuxeo.runtime.model.DefaultComponent;
042
043/**
044 * Holidays checker implementation which loads the holidays dates in a CVS file. The extension point "configuration" of
045 * the component "org.nuxeo.business.days.management.checker.csv" allows to specify where is located the CSV file.
046 *
047 * @author Nicolas Ulrich
048 */
049public class CSVHolidaysChecker extends DefaultComponent implements HolidaysChecker {
050
051    private static final Log log = LogFactory.getLog(CSVHolidaysChecker.class);
052
053    private static final SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
054
055    private static Set<Date> dates = new HashSet<Date>();
056
057    public boolean isHoliday(Date date) {
058        return dates.contains(date);
059    }
060
061    /**
062     * Read the CVS file in order to load hollidays.cvs
063     */
064    private void initDates(String fileName, boolean isEmbedded) {
065
066        if (fileName == null) {
067            return;
068        }
069
070        try {
071
072            Reader reader = null;
073
074            if (isEmbedded) {
075                InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
076                reader = new InputStreamReader(is);
077            } else {
078                reader = new FileReader(fileName);
079            }
080
081            if (reader == null) {
082                log.error("Unable to read the CSV file");
083                return;
084            }
085
086            BufferedReader buffer = new BufferedReader(reader);
087
088            String line;
089            while ((line = buffer.readLine()) != null) {
090                dates.add(formater.parse(line));
091            }
092
093        } catch (FileNotFoundException e) {
094            log.error("Unable to find the CSV file", e);
095        } catch (IOException e) {
096            log.error("Unable to read the CSV file", e);
097        } catch (ParseException e) {
098            log.error("The CSV file is not formatted", e);
099        }
100
101    }
102
103    @Override
104    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
105
106        if (extensionPoint.equals("configuration")) {
107
108            CSVConfigurationDescriptor conf = ((CSVConfigurationDescriptor) contribution);
109            initDates(conf.csvFilePath, conf.embedded);
110
111        }
112    }
113
114}