BrightSide Workbench Full Report + Source Code
constraints/WeekDayConstraint.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.scheduler.constraints;
19 
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.EnumSet;
23 import java.util.HashSet;
24 import java.util.List;
25 import org.jdom.Element;
26 
31 @Deprecated
32 public class WeekDayConstraint extends HashSet<WeekDay> {
33 
34  private WeekDay lastCheck;
35  private boolean refresh;
36 
37  public void readXML(Element root) {
38  Element parent = root.getChild("week-day-constraint");
39  for(Element el : (List<Element>) parent.getChildren("week-day")) {
40  add(WeekDay.valueOf(el.getAttributeValue("name")));
41  }
42  }
43 
44  public void writeXML(Element root) {
45  Element el = new Element("week-day-constraint");
46  root.addContent(el);
47  for(WeekDay wd : this) {
48  Element e = new Element("week-day");
49  e.setAttribute("name", wd.toString());
50  el.addContent(e);
51  }
52  }
53 
55  addAll(EnumSet.allOf(WeekDay.class));
56  return this;
57  }
58 
59  public boolean isValid(Date date) {
60  refresh = false;
61  WeekDay wd = getWeekDay(date);
62  if(wd != null && contains(wd)) {
63  refresh = !wd.equals(lastCheck);
64  lastCheck = wd;
65  return true;
66  }
67  lastCheck = wd;
68  return isEmpty();
69  }
70 
71  public boolean needsRefresh() {
72  return refresh;
73  }
74 
75  public WeekDay getWeekDay(Date date) {
76  Calendar c = Calendar.getInstance();
77  c.setTime(date);
78  switch(c.get(Calendar.DAY_OF_WEEK)) {
79  case Calendar.MONDAY:
80  return WeekDay.MONDAY;
81  case Calendar.TUESDAY:
82  return WeekDay.TUESDAY;
83  case Calendar.WEDNESDAY:
84  return WeekDay.WEDNESDAY;
85  case Calendar.THURSDAY:
86  return WeekDay.THURSDAY;
87  case Calendar.FRIDAY:
88  return WeekDay.FRIDAY;
89  case Calendar.SATURDAY:
90  return WeekDay.SATURDAY;
91  case Calendar.SUNDAY:
92  return WeekDay.SUNDAY;
93  }
94  return null;
95  }
96 
97 }