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