001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * <a href="mailto:nulrich@nuxeo.com">Nicolas Ulrich</a> 016 * 017 */ 018 019package org.nuxeo.business.days.management.checker; 020 021import java.io.BufferedReader; 022import java.io.FileNotFoundException; 023import java.io.FileReader; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.InputStreamReader; 027import java.io.Reader; 028import java.text.ParseException; 029import java.text.SimpleDateFormat; 030import java.util.Date; 031import java.util.HashSet; 032import java.util.Iterator; 033import java.util.Set; 034 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.nuxeo.business.days.management.checker.HolidaysChecker; 038import org.nuxeo.runtime.model.ComponentInstance; 039import org.nuxeo.runtime.model.DefaultComponent; 040 041/** 042 * Holidays checker implementation which loads the holidays dates in a CVS file. The extension point "configuration" of 043 * the component "org.nuxeo.business.days.management.checker.csv" allows to specify where is located the CSV file. 044 * 045 * @author Nicolas Ulrich 046 */ 047public class CSVHolidaysChecker extends DefaultComponent implements HolidaysChecker { 048 049 private static final Log log = LogFactory.getLog(CSVHolidaysChecker.class); 050 051 private static final SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy"); 052 053 private static Set<Date> dates = new HashSet<Date>(); 054 055 public boolean isHoliday(Date date) { 056 return dates.contains(date); 057 } 058 059 /** 060 * Read the CVS file in order to load hollidays.cvs 061 */ 062 private void initDates(String fileName, boolean isEmbedded) { 063 064 if (fileName == null) { 065 return; 066 } 067 068 try { 069 070 Reader reader = null; 071 072 if (isEmbedded) { 073 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); 074 reader = new InputStreamReader(is); 075 } else { 076 reader = new FileReader(fileName); 077 } 078 079 if (reader == null) { 080 log.error("Unable to read the CSV file"); 081 return; 082 } 083 084 BufferedReader buffer = new BufferedReader(reader); 085 086 String line; 087 while ((line = buffer.readLine()) != null) { 088 dates.add(formater.parse(line)); 089 } 090 091 } catch (FileNotFoundException e) { 092 log.error("Unable to find the CSV file", e); 093 } catch (IOException e) { 094 log.error("Unable to read the CSV file", e); 095 } catch (ParseException e) { 096 log.error("The CSV file is not formatted", e); 097 } 098 099 } 100 101 @Override 102 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 103 104 if (extensionPoint.equals("configuration")) { 105 106 CSVConfigurationDescriptor conf = ((CSVConfigurationDescriptor) contribution); 107 initDates(conf.csvFilePath, conf.embedded); 108 109 } 110 } 111 112}