1 package com.td.service;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Calendar;
\r
5 import java.util.Collections;
\r
6 import java.util.Comparator;
\r
7 import java.util.Date;
\r
8 import java.util.List;
\r
10 import com.td.db.Keyword;
\r
11 import com.td.db.KeywordDao;
\r
12 import com.td.db.Monitor;
\r
13 import com.td.db.MonitorDao;
\r
14 import com.td.db.Train;
\r
15 import com.td.db.TrainDao;
\r
16 import com.td.utility.DateUtil;
\r
17 import com.td.utility.Debug;
\r
19 public class Scheduler {
\r
20 private static Calendar cal = Calendar.getInstance();
\r
21 private List<Term>[] list = null;
\r
22 private List<Keyword> keywords = null;
\r
23 private List<Train> trains = null;
\r
24 private Comparator<Term> comp = new Comparator<Term>(){
\r
25 public int compare(Term t1, Term t2) {
\r
26 return t1.getFrom() - t2.getFrom();
\r
35 Debug.d(this, "start init");
\r
39 Debug.d(this, "end init");
\r
42 public long calcSchedule(long now){
\r
43 Debug.d(this, "start");
\r
44 long t = nextSchedule(now);
\r
45 Debug.d(this, "end");
\r
49 public long nextSchedule(long now){
\r
51 //long next = now + 60 * DateUtil.LEN * 1000;
\r
52 long next = now + 60 * 1 * 1000;
\r
54 cal.setTimeInMillis(next);
\r
55 Date date = cal.getTime();
\r
56 int h = date.getHours();
\r
57 int m = date.getMinutes();
\r
58 int hm = DateUtil.getHm(h, m);
\r
59 int w = date.getDay();
\r
61 for (int i = 0; i < DateUtil.WEEK + 1; i++){
\r
62 int n = (w + i) % DateUtil.WEEK;
\r
63 List<Term> l = list[n];
\r
64 int j = isContain(l, hm);
\r
69 Term t = l.get(j - 1);
\r
70 //
\8d¡
\93ú(next)
\82©
\82ç
\81Ai
\93ú
\8cã
\82Ì
\81Afrom
\8e\9e\r
71 return DateUtil.getTime(date, i, t.getFrom());
\r
74 //
\91¼
\82Ì
\97j
\93ú
\82Ì
\8fê
\8d\87\r
77 //
\8d¡
\93ú(next)
\82©
\82ç
\81Ai
\93ú
\8cã
\82Ì
\81Afrom
\8e\9e\r
78 return DateUtil.getTime(date, i, t.getFrom());
\r
86 public int isContain(List<Term> l, int hm) {
\r
90 for (int i = 0; i < l.size(); i++){
\r
94 if (from <= hm && hm <= to){
\r
104 public void makeSchedule(){
\r
108 Debug.d(this, "update schedule");
\r
110 MonitorDao dao = MonitorDao.getInstance();
\r
111 List<Monitor> mList = dao.search();
\r
112 makeSchedule(mList);
\r
115 public void makeTrains(){
\r
116 if (trains != null){
\r
119 Debug.d(this, "update trains");
\r
121 TrainDao dao = TrainDao.getInstance();
\r
122 trains = dao.search(true);
\r
125 public void makeKeywords(){
\r
126 if (keywords != null){
\r
129 Debug.d(this, "update keywords");
\r
131 KeywordDao dao = KeywordDao.getInstance();
\r
132 keywords = dao.search();
\r
135 public List<Term>[] getList() {
\r
139 public List<Train> getTrains() {
\r
143 public List<Keyword> getKeywords() {
\r
147 public void makeSchedule(List<Monitor> mList){
\r
150 for (Monitor monitor : mList){
\r
151 add(list, monitor);
\r
155 public void clearSchedule(){
\r
156 list = new List[DateUtil.WEEK];
\r
157 for (int i = 0; i < list.length; i++){
\r
158 list[i] = new ArrayList<Term>();
\r
162 public void add(List<Term>[] ll, Monitor monitor){
\r
163 Term term = new Term(monitor.getFrom(), monitor.getTo());
\r
164 for (int i = 0; i < DateUtil.WEEK; i++){
\r
165 if (monitor.getWeek(i)){
\r
171 public void add(List<Term> l, Term term){
\r
173 while (i < l.size()){
\r
175 if (isContain(t, term)){
\r
176 term = add(t, term);
\r
183 Collections.sort(l, comp);
\r
186 public boolean isContain(Term t1, Term t2){
\r
187 int from1 = t1.getFrom();
\r
188 int to1 = t1.getTo();
\r
189 int from2 = t2.getFrom();
\r
190 int to2 = t2.getTo();
\r
192 if (from1 <= from2 && from2 <= to1){
\r
195 if (from1 <= to2 && to2 <= to1){
\r
198 if (from2 <= from1 && from1 <= to2){
\r
201 if (from2 <= to1 && to1 <= to2){
\r
207 public Term add(Term t1, Term t2){
\r
208 int from1 = t1.getFrom();
\r
209 int to1 = t1.getTo();
\r
210 int from2 = t2.getFrom();
\r
211 int to2 = t2.getTo();
\r
213 int from = (from1 < from2)? from1 : from2;
\r
214 int to = (to1 > to2)? to1 : to2;
\r
215 return new Term(from, to);
\r