View Javadoc

1   /*Copyright (C) 2004-... Stephane Gauchet for Hyphonem
2   
3    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
4   
5    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6   
7    You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
8    
9    Written by St�phane Gauchet
10   mail me at : sgauchet@free.fr
11   */
12  package fr.hyphonem.conges;
13  
14  import java.math.BigInteger;
15  import java.text.ParseException;
16  import java.text.SimpleDateFormat;
17  import java.util.Calendar;
18  import java.util.Date;
19  import java.util.Enumeration;
20  import java.util.Hashtable;
21  
22  /**
23   * @author Stephane Gauchet pour Hyphonem
24   * 
25   * class for handling fery days by country
26   */
27  public class FeryDays {
28  
29  	/**
30  	 * Constructor for FeryDays.
31  	 */
32  	public FeryDays() {
33  		super();
34  	}
35  
36  	/**
37  	 * nouvelle methode pour charger les jours feries depuis les fichiers xml
38  	 * des pays <br>
39  	 * new method to load national days from country xml files
40  	 * 
41  	 * @param datedeb
42  	 * @param datefin
43  	 * @param paysISO
44  	 * @param dir
45  	 * @return Hashtable
46  	 * @throws Exception
47  	 * @throws ParseException
48  	 */
49  	public Hashtable getNationalDays(String datedeb, String datefin,
50  			String paysISO, String dir) throws Exception {
51  		Hashtable hjf = new Hashtable();
52  
53  		String anneedeb = datedeb.substring(datedeb.lastIndexOf("/") + 1);
54  		String anneefin = datefin.substring(datefin.lastIndexOf("/") + 1);
55  
56  		AccessData ad = new AccessDataMng().getInstance(dir);
57  
58  		if (anneedeb.equals(anneefin)) {
59  			hjf = ad.readNationalHolidays(paysISO, anneedeb);
60  		} else {
61  			Hashtable hJFdeb = ad.readNationalHolidays(paysISO, anneedeb);
62  			Hashtable hJFfin = ad.readNationalHolidays(paysISO, anneefin);
63  			hJFdeb.putAll(hJFfin);
64  			hjf = hJFdeb;
65  		}
66  		return hjf;
67  	}
68  
69  	/**
70  	 * @deprecated use getNationalDays instead return list of fery days from a
71  	 *             given period <br/> to be completed because only code for fery
72  	 *             days in FRANCE are set <br/> retourne les jours feries dans
73  	 *             une periode donn�e et pour le pays indiqu� A Compl�ter!!
74  	 *             seule la france est prise en compte pour l'instant cette
75  	 *             methode doit etre entierement revue un de ces 4
76  	 * @param datedeb
77  	 * @param datefin
78  	 * @param paysISO
79  	 * @return Hashtable
80  	 * @throws ParseException
81  	 */
82  	public Hashtable getJoursFeries(String datedeb, String datefin,
83  			String paysISO) throws ParseException {
84  		// Hashtable vJF = new Hashtable();
85  		// NationalDayData jf = new NationalDayData();
86  		Hashtable hJF = new Hashtable();
87  		Hashtable hJFFlottant = new Hashtable();
88  		Hashtable hJFFixesFlottants = new Hashtable();
89  		Hashtable hJFFixe = new Hashtable();
90  
91  		Date datdeb = new SimpleDateFormat("dd/MM/yyyy").parse(datedeb);
92  		Date datfin = new SimpleDateFormat("dd/MM/yyyy").parse(datefin);
93  
94  		// jours feries pour la france
95  		if (paysISO.equals("FR")) {
96  			// hashtable des jours feries fixes key=mois & value=Hashtable des
97  			// jours
98  			Hashtable vj = new Hashtable();
99  			// 1er janvier
100 			vj.put("01", "Jour de l'an");
101 			hJF.put("01", vj);
102 			// 1er & 8 mai
103 			vj = new Hashtable();
104 			vj.put("01", "Fete du Travail");
105 			vj.put("08", "Victoire 1945");
106 			hJF.put("05", vj);
107 			// 14 juillet
108 			vj = new Hashtable();
109 			vj.put("14", "Fete Nationale");
110 			hJF.put("07", vj);
111 			// 15 Aout
112 			vj = new Hashtable();
113 			vj.put("15", "Assomption");
114 			hJF.put("08", vj);
115 			// 1er et 11 Novembre
116 			vj = new Hashtable();
117 			vj.put("01", "Toussaint");
118 			vj.put("11", "Armistice 1918");
119 			hJF.put("11", vj);
120 			// 25 D�cembre
121 			vj = new Hashtable();
122 			vj.put("25", "Noel");
123 			hJF.put("12", vj);
124 
125 			Calendar cal = Calendar.getInstance();
126 			cal.setTime(datdeb);
127 			int year_deb = cal.get(Calendar.YEAR);
128 			// System.out.println("deb="+year_deb);
129 			cal.setTime(datfin);
130 			int year_fin = cal.get(Calendar.YEAR);
131 			// System.out.println("fin="+year_fin);
132 			// System.out.println("000");
133 			int[] years;
134 			// System.out.println("00");
135 			if (year_deb == year_fin) {
136 				years = new int[1];
137 				years[0] = year_deb;
138 			} else {
139 				int diff1 = year_fin - year_deb;
140 				// System.out.println("0");
141 				if (diff1 == 1) {
142 					// System.out.println("1");
143 					years = new int[2];
144 					// System.out.println("2");
145 					years[0] = year_deb;
146 					// System.out.println("3");
147 					years[1] = year_fin;
148 				} else {
149 					// year_deb & year_fin > 1
150 					int i = 0;
151 					int diff = year_fin - year_deb + 1;
152 					years = new int[diff];
153 					for (int a = year_deb; a <= year_fin; a++) {
154 						years[i] = a;
155 						// System.out.println("annee"+a);
156 						i++;
157 					}
158 				}
159 			}
160 
161 			for (int a = 0; a < years.length; a++) {
162 				int annee = years[a];
163 				// System.out.println(">>>>>>"+annee);
164 				for (Enumeration enumr = hJF.keys(); enumr.hasMoreElements();) {
165 					String mois = (String) enumr.nextElement();
166 					// System.out.println(mois);
167 					Hashtable hjours = (Hashtable) hJF.get(mois);
168 					for (Enumeration enumj = hjours.keys(); enumj
169 							.hasMoreElements();) {
170 						String jour = (String) enumj.nextElement();
171 						// System.out.println(jour);
172 						String label = (String) hjours.get(jour);
173 						// System.out.println(label);
174 						String dateJF = jour + "/" + mois + "/" + annee;
175 						// System.out.println(dateJF);
176 						Date djf = new SimpleDateFormat("dd/MM/yyyy")
177 								.parse(dateJF);
178 						if (djf.compareTo(datdeb) >= 0
179 								&& djf.compareTo(datfin) <= 0) {
180 							hJFFixe.put(dateJF, label);
181 						}
182 					}
183 				}
184 
185 				// jours feries flottants dependant du jour de paques
186 				String paques = getJourPaques("" + annee);
187 				Date datePaques = new SimpleDateFormat("dd/MM/yyyy")
188 						.parse(paques);
189 
190 				cal.setTime(datePaques);
191 				// lundi de paques = Paques + 1 jour
192 				cal.add(Calendar.DATE, 1);
193 				Date lundiPaq = cal.getTime();
194 				String datLundiPaq = new SimpleDateFormat("dd/MM/yyyy")
195 						.format(lundiPaq);
196 				if (lundiPaq.compareTo(datdeb) >= 0
197 						&& lundiPaq.compareTo(datfin) <= 0) {
198 					hJFFlottant.put(datLundiPaq, "Lundi de Paques");
199 				}
200 
201 				// ascension = Paques + 39 jours
202 				cal.add(Calendar.DATE, 38);
203 				Date ascension = cal.getTime();
204 				String datAscension = new SimpleDateFormat("dd/MM/yyyy")
205 						.format(ascension);
206 				if (ascension.compareTo(datdeb) >= 0
207 						&& ascension.compareTo(datfin) <= 0) {
208 					hJFFlottant.put(datAscension, "Ascension");
209 				}
210 
211 				// Lundi de Pentecote = Paques + 50 jours;
212 				cal.add(Calendar.DATE, 11);
213 				Date lundiPentecote = cal.getTime();
214 				String datlundiPentecote = new SimpleDateFormat("dd/MM/yyyy")
215 						.format(lundiPentecote);
216 				if (lundiPentecote.compareTo(datdeb) >= 0
217 						&& lundiPentecote.compareTo(datfin) <= 0) {
218 					hJFFlottant.put(datlundiPentecote, "Lundi de Pentecote");
219 				}
220 			}
221 			// mettre le code pour d'autres pays ici.
222 			// ...
223 			/**
224 			 * @todo list of fery days for others country
225 			 */
226 			// on ajoute hJFFlottant � hJFFixe
227 			hJFFixe.putAll(hJFFlottant);
228 			// et on met hJFFixe dans hJFFixesFlottants
229 			hJFFixesFlottants = hJFFixe;
230 		}
231 		// System.out.println(hJFFixesFlottants.toString());
232 		return hJFFixesFlottants;
233 	}
234 
235 	/**
236 	 * return Paques day in this year <br/> Method Paques : retourne la date du
237 	 * jour de Paques en fonction de l'annee cet algo est valable pour les dates
238 	 * comprises entre 1583 et 4099
239 	 * 
240 	 * @param annee
241 	 * @return String
242 	 */
243 	private String getJourPaques(String annee) {
244 		BigInteger year = new BigInteger(annee);
245 		BigInteger G0 = year.mod(new BigInteger("19"));
246 		BigInteger G = G0.add(BigInteger.ONE);
247 		BigInteger C = year.divide(new BigInteger("100")).add(BigInteger.ONE);
248 		BigInteger X = new BigInteger("3").multiply(C).divide(
249 				new BigInteger("4")).subtract(new BigInteger("12"));
250 		BigInteger Z = ((new BigInteger("8").multiply(C)).add(new BigInteger(
251 				"5"))).divide(new BigInteger("25")).subtract(
252 				new BigInteger("5"));
253 		BigInteger D = ((new BigInteger("5").multiply(year))
254 				.divide(new BigInteger("4"))).subtract(X).subtract(
255 				new BigInteger("10"));
256 		BigInteger E = ((new BigInteger("11").multiply(G)).add(
257 				new BigInteger("20")).add(Z).subtract(X)).mod(new BigInteger(
258 				"30"));
259 
260 		if ((E.equals(new BigInteger("25")) && G
261 				.compareTo(new BigInteger("11")) > 0)
262 				|| E.equals(new BigInteger("24"))) {
263 			E = E.add(BigInteger.ONE);
264 		}
265 
266 		BigInteger N = new BigInteger("44").subtract(E);
267 
268 		if (N.compareTo(new BigInteger("21")) < 0) {
269 			N = N.add(new BigInteger("30"));
270 		}
271 
272 		BigInteger P = N.add(new BigInteger("7")).subtract(
273 				(D.add(N)).mod(new BigInteger("7")));
274 		BigInteger J;
275 		if (P.compareTo(new BigInteger("31")) > 0) {
276 			J = P.subtract(new BigInteger("31"));
277 		} else {
278 			J = new BigInteger(P.toString());
279 		}
280 		BigInteger M;
281 		if (J.equals(P)) {
282 			M = new BigInteger("3");
283 		} else {
284 			M = new BigInteger("4");
285 		}
286 
287 		return J.toString() + "/" + M.toString() + "/" + year.toString();
288 	}
289 }