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.util.Date; 032import java.util.HashSet; 033import java.util.Set; 034 035import org.apache.commons.lang3.time.FastDateFormat; 036import org.apache.commons.logging.Log; 037import org.apache.commons.logging.LogFactory; 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 FastDateFormat formater = FastDateFormat.getInstance("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 BufferedReader buffer = new BufferedReader(reader); 080 081 String line; 082 while ((line = buffer.readLine()) != null) { 083 dates.add(formater.parse(line)); 084 } 085 086 } catch (FileNotFoundException e) { 087 log.error("Unable to find the CSV file", e); 088 } catch (IOException e) { 089 log.error("Unable to read the CSV file", e); 090 } catch (ParseException e) { 091 log.error("The CSV file is not formatted", e); 092 } 093 094 } 095 096 @Override 097 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 098 099 if (extensionPoint.equals("configuration")) { 100 101 CSVConfigurationDescriptor conf = ((CSVConfigurationDescriptor) contribution); 102 initDates(conf.csvFilePath, conf.embedded); 103 104 } 105 } 106 107}